Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create instance_template and compute_instance_from_template submodules #10

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions modules/compute_instance_from_template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Compute Instance From Template

This module is used to create compute instances (and only compute instances) using
[google_compute_instance_from_template](https://www.terraform.io/docs/providers/google/r/compute_instance_from_template), with no instance groups.

## Usage

See the [simple](https://github.com/terraform-google-modules/terraform-google-vm/tree/master/examples/compute_instance/simple) for a usage example.

## Testing


<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
| <a name="requirement_google"></a> [google](#requirement\_google) | ~> 3.90.1 |
| <a name="requirement_google-beta"></a> [google-beta](#requirement\_google-beta) | ~> 3.90.1 |
| <a name="requirement_null"></a> [null](#requirement\_null) | ~> 2.1 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_google"></a> [google](#provider\_google) | ~> 3.90.1 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [google_compute_instance_from_template.compute_instance](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance_from_template) | resource |
| [google_compute_instance_template.tpl](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_instance_template) | data source |
| [google_compute_zones.available](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_zones) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_add_hostname_suffix"></a> [add\_hostname\_suffix](#input\_add\_hostname\_suffix) | Adds a suffix to the hostname | `bool` | `true` | no |
| <a name="input_deletion_protection"></a> [deletion\_protection](#input\_deletion\_protection) | Enable deletion protection on this instance. Note: you must disable deletion protection before removing the resource, or the instance cannot be deleted and the Terraform run will not complete successfully. | `bool` | `false` | no |
| <a name="input_hostname"></a> [hostname](#input\_hostname) | Hostname of instances | `string` | `""` | no |
| <a name="input_hostname_suffix_separator"></a> [hostname\_suffix\_separator](#input\_hostname\_suffix\_separator) | Separator character to compose hostname when add\_hostname\_suffix is set to true. | `string` | `"-"` | no |
| <a name="input_instance_template"></a> [instance\_template](#input\_instance\_template) | Instance template self\_link used to create compute instances | `string` | n/a | yes |
| <a name="input_labels"></a> [labels](#input\_labels) | (Optional) Labels to override those from the template, provided as a map | `map(string)` | `null` | no |
| <a name="input_network_interfaces"></a> [network\_interfaces](#input\_network\_interfaces) | network interface details for GCE, if any. | <pre>list(object({<br> network = string<br> subnetwork = string<br> subnetwork_project = string<br> network_ip = string<br><br> # Ignore next 3 objects until we upgrade TF and can use optional attributes<br> # access_config = list(object({<br> # nat_ip = string<br> # network_tier = string<br> # }))<br> # ipv6_access_config = list(object({<br> # network_tier = string<br> # }))<br> # alias_ip_range = list(object({<br> # ip_cidr_range = string<br> # subnetwork_range_name = string<br> # }))<br> }))</pre> | `[]` | no |
| <a name="input_region"></a> [region](#input\_region) | Region where the instances should be created. | `string` | `null` | no |
| <a name="input_resource_policies"></a> [resource\_policies](#input\_resource\_policies) | (Optional) A list of short names or self\_links of resource policies to attach to the instance. Modifying this list will cause the instance to recreate. Currently a max of 1 resource policy is supported. | `list(string)` | `[]` | no |
| <a name="input_zone"></a> [zone](#input\_zone) | Zone where the instances should be created. If not specified, instances will be spread across available zones in the region. | `string` | `null` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_available_zones"></a> [available\_zones](#output\_available\_zones) | List of available zones in region |
| <a name="output_instances_details"></a> [instances\_details](#output\_instances\_details) | List of all details for compute instances |
| <a name="output_instances_self_links"></a> [instances\_self\_links](#output\_instances\_self\_links) | List of self-links for compute instances |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
78 changes: 78 additions & 0 deletions modules/compute_instance_from_template/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
locals {
hostname = var.hostname == "" ? "default" : var.hostname
num_instances = 1

project_id = length(regexall("/projects/([^/]*)", var.instance_template)) > 0 ? flatten(regexall("/projects/([^/]*)", var.instance_template))[0] : null

# Read the network interface info from the variables and override the template
network_interfaces = var.network_interfaces
}

###############
# Data Sources
###############

data "google_compute_zones" "available" {
project = local.project_id
region = var.region
}

data "google_compute_instance_template" "tpl" {
name = var.instance_template
project = local.project_id
}

#############
# Instances
#############

resource "google_compute_instance_from_template" "compute_instance" {
provider = google
count = local.num_instances
name = var.add_hostname_suffix ? format("%s%s%s", local.hostname, var.hostname_suffix_separator, format("%03d", count.index + 1)) : local.hostname
project = local.project_id
zone = var.zone == null ? data.google_compute_zones.available.names[count.index % length(data.google_compute_zones.available.names)] : var.zone
deletion_protection = var.deletion_protection
resource_policies = var.resource_policies
labels = var.labels

dynamic "network_interface" {
for_each = local.network_interfaces

content {
network = network_interface.value.network
subnetwork = network_interface.value.subnetwork
subnetwork_project = network_interface.value.subnetwork_project
network_ip = network_interface.value.network_ip
# Ignore these dynamic blocks until we upgrade TF and can use optional attributes
# dynamic "access_config" {
# for_each = network_interface.value.access_config
# content {
# nat_ip = access_config.value.nat_ip
# network_tier = access_config.value.network_tier
# }
# }

# dynamic "ipv6_access_config" {
# for_each = network_interface.value.ipv6_access_config
# content {
# network_tier = ipv6_access_config.value.network_tier
# }
# }

# dynamic "alias_ip_range" {
# for_each = network_interface.value.alias_ip_range
# content {
# ip_cidr_range = alias_ip_range.value.ip_cidr_range
# subnetwork_range_name = alias_ip_range.value.subnetwork_range_name
# }
# }
}
}

source_instance_template = var.instance_template

depends_on = [
data.google_compute_instance_template.tpl
]
}
30 changes: 30 additions & 0 deletions modules/compute_instance_from_template/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

output "instances_self_links" {
description = "List of self-links for compute instances"
value = google_compute_instance_from_template.compute_instance.*.self_link
}

output "instances_details" {
description = "List of all details for compute instances"
value = google_compute_instance_from_template.compute_instance.*
}

output "available_zones" {
description = "List of available zones in region"
value = data.google_compute_zones.available.names
}
81 changes: 81 additions & 0 deletions modules/compute_instance_from_template/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
variable "hostname" {
description = "Hostname of instances"
type = string
default = ""
}

variable "add_hostname_suffix" {
description = "Adds a suffix to the hostname"
type = bool
default = true
}
variable "network_interfaces" {
description = "network interface details for GCE, if any."
default = []
type = list(object({
network = string
subnetwork = string
subnetwork_project = string
network_ip = string

# Ignore next 3 objects until we upgrade TF and can use optional attributes
# access_config = list(object({
# nat_ip = string
# network_tier = string
# }))
# ipv6_access_config = list(object({
# network_tier = string
# }))
# alias_ip_range = list(object({
# ip_cidr_range = string
# subnetwork_range_name = string
# }))
}))
}

# variable "num_instances" {
# description = "Number of instances to create. This value is ignored if static_ips is provided."
# type = number
# default = "1"
# }

variable "instance_template" {
description = "Instance template self_link used to create compute instances"
type = string
}

variable "region" {
type = string
description = "Region where the instances should be created."
default = null
}

variable "zone" {
type = string
description = "Zone where the instances should be created. If not specified, instances will be spread across available zones in the region."
default = null
}

variable "hostname_suffix_separator" {
type = string
description = "Separator character to compose hostname when add_hostname_suffix is set to true."
default = "-"
}

variable "deletion_protection" {
type = bool
description = "Enable deletion protection on this instance. Note: you must disable deletion protection before removing the resource, or the instance cannot be deleted and the Terraform run will not complete successfully."
default = false
}

variable "resource_policies" {
description = "(Optional) A list of short names or self_links of resource policies to attach to the instance. Modifying this list will cause the instance to recreate. Currently a max of 1 resource policy is supported."
type = list(string)
default = []
}

variable "labels" {
type = map(string)
description = "(Optional) Labels to override those from the template, provided as a map"
default = null
}
20 changes: 20 additions & 0 deletions modules/compute_instance_from_template/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 3.90.1"
}
google-beta = {
source = "hashicorp/google-beta"
version = "~> 3.90.1"
}
null = {
source = "hashicorp/null"
version = "~> 2.1"
}
random = {
source = "hashicorp/random"
}
}
required_version = ">= 0.13"
}
Loading
Loading