From 25e0cba10a56eaea14f8b73e3087085258523776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Taneli=20Lepp=C3=A4?= Date: Tue, 3 Jan 2023 16:38:46 +0100 Subject: [PATCH] Added device_name field to compute-vm attached_disks parameter (for stateful disks). --- modules/compute-vm/README.md | 54 ++++++++++++++++----------------- modules/compute-vm/main.tf | 8 ++--- modules/compute-vm/variables.tf | 1 + 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/modules/compute-vm/README.md b/modules/compute-vm/README.md index ee25ac10f3..2f715555a2 100644 --- a/modules/compute-vm/README.md +++ b/modules/compute-vm/README.md @@ -278,34 +278,34 @@ module "instance-group" { | name | description | type | required | default | |---|---|:---:|:---:|:---:| -| [name](variables.tf#L180) | Instance name. | string | ✓ | | -| [network_interfaces](variables.tf#L185) | Network interfaces configuration. Use self links for Shared VPC, set addresses to null if not needed. | list(object({…})) | ✓ | | -| [project_id](variables.tf#L222) | Project id. | string | ✓ | | -| [zone](variables.tf#L281) | Compute zone. | string | ✓ | | +| [name](variables.tf#L181) | Instance name. | string | ✓ | | +| [network_interfaces](variables.tf#L186) | Network interfaces configuration. Use self links for Shared VPC, set addresses to null if not needed. | list(object({…})) | ✓ | | +| [project_id](variables.tf#L223) | Project id. | string | ✓ | | +| [zone](variables.tf#L282) | Compute zone. | string | ✓ | | | [attached_disk_defaults](variables.tf#L17) | Defaults for attached disks options. | object({…}) | | {…} | -| [attached_disks](variables.tf#L38) | Additional disks, if options is null defaults will be used in its place. Source type is one of 'image' (zonal disks in vms and template), 'snapshot' (vm), 'existing', and null. | list(object({…})) | | [] | -| [boot_disk](variables.tf#L81) | Boot disk properties. | object({…}) | | {…} | -| [can_ip_forward](variables.tf#L97) | Enable IP forwarding. | bool | | false | -| [confidential_compute](variables.tf#L103) | Enable Confidential Compute for these instances. | bool | | false | -| [create_template](variables.tf#L109) | Create instance template instead of instances. | bool | | false | -| [description](variables.tf#L114) | Description of a Compute Instance. | string | | "Managed by the compute-vm Terraform module." | -| [enable_display](variables.tf#L120) | Enable virtual display on the instances. | bool | | false | -| [encryption](variables.tf#L126) | Encryption options. Only one of kms_key_self_link and disk_encryption_key_raw may be set. If needed, you can specify to encrypt or not the boot disk. | object({…}) | | null | -| [group](variables.tf#L136) | Define this variable to create an instance group for instances. Disabled for template use. | object({…}) | | null | -| [hostname](variables.tf#L144) | Instance FQDN name. | string | | null | -| [iam](variables.tf#L150) | IAM bindings in {ROLE => [MEMBERS]} format. | map(list(string)) | | {} | -| [instance_type](variables.tf#L156) | Instance type. | string | | "f1-micro" | -| [labels](variables.tf#L162) | Instance labels. | map(string) | | {} | -| [metadata](variables.tf#L168) | Instance metadata. | map(string) | | {} | -| [min_cpu_platform](variables.tf#L174) | Minimum CPU platform. | string | | null | -| [options](variables.tf#L200) | Instance options. | object({…}) | | {…} | -| [scratch_disks](variables.tf#L227) | Scratch disks configuration. | object({…}) | | {…} | -| [service_account](variables.tf#L239) | Service account email. Unused if service account is auto-created. | string | | null | -| [service_account_create](variables.tf#L245) | Auto-create service account. | bool | | false | -| [service_account_scopes](variables.tf#L253) | Scopes applied to service account. | list(string) | | [] | -| [shielded_config](variables.tf#L259) | Shielded VM configuration of the instances. | object({…}) | | null | -| [tag_bindings](variables.tf#L269) | Tag bindings for this instance, in key => tag value id format. | map(string) | | null | -| [tags](variables.tf#L275) | Instance network tags for firewall rule targets. | list(string) | | [] | +| [attached_disks](variables.tf#L38) | Additional disks, if options is null defaults will be used in its place. Source type is one of 'image' (zonal disks in vms and template), 'snapshot' (vm), 'existing', and null. | list(object({…})) | | [] | +| [boot_disk](variables.tf#L82) | Boot disk properties. | object({…}) | | {…} | +| [can_ip_forward](variables.tf#L98) | Enable IP forwarding. | bool | | false | +| [confidential_compute](variables.tf#L104) | Enable Confidential Compute for these instances. | bool | | false | +| [create_template](variables.tf#L110) | Create instance template instead of instances. | bool | | false | +| [description](variables.tf#L115) | Description of a Compute Instance. | string | | "Managed by the compute-vm Terraform module." | +| [enable_display](variables.tf#L121) | Enable virtual display on the instances. | bool | | false | +| [encryption](variables.tf#L127) | Encryption options. Only one of kms_key_self_link and disk_encryption_key_raw may be set. If needed, you can specify to encrypt or not the boot disk. | object({…}) | | null | +| [group](variables.tf#L137) | Define this variable to create an instance group for instances. Disabled for template use. | object({…}) | | null | +| [hostname](variables.tf#L145) | Instance FQDN name. | string | | null | +| [iam](variables.tf#L151) | IAM bindings in {ROLE => [MEMBERS]} format. | map(list(string)) | | {} | +| [instance_type](variables.tf#L157) | Instance type. | string | | "f1-micro" | +| [labels](variables.tf#L163) | Instance labels. | map(string) | | {} | +| [metadata](variables.tf#L169) | Instance metadata. | map(string) | | {} | +| [min_cpu_platform](variables.tf#L175) | Minimum CPU platform. | string | | null | +| [options](variables.tf#L201) | Instance options. | object({…}) | | {…} | +| [scratch_disks](variables.tf#L228) | Scratch disks configuration. | object({…}) | | {…} | +| [service_account](variables.tf#L240) | Service account email. Unused if service account is auto-created. | string | | null | +| [service_account_create](variables.tf#L246) | Auto-create service account. | bool | | false | +| [service_account_scopes](variables.tf#L254) | Scopes applied to service account. | list(string) | | [] | +| [shielded_config](variables.tf#L260) | Shielded VM configuration of the instances. | object({…}) | | null | +| [tag_bindings](variables.tf#L270) | Tag bindings for this instance, in key => tag value id format. | map(string) | | null | +| [tags](variables.tf#L276) | Instance network tags for firewall rule targets. | list(string) | | [] | ## Outputs diff --git a/modules/compute-vm/main.tf b/modules/compute-vm/main.tf index 32051555c5..31e3706727 100644 --- a/modules/compute-vm/main.tf +++ b/modules/compute-vm/main.tf @@ -17,7 +17,7 @@ locals { attached_disks = { for disk in var.attached_disks : - disk.name => merge(disk, { + (disk.name != null ? disk.name : disk.device_name) => merge(disk, { options = disk.options == null ? var.attached_disk_defaults : disk.options }) } @@ -138,7 +138,7 @@ resource "google_compute_instance" "default" { for_each = local.attached_disks_zonal iterator = config content { - device_name = config.value.name + device_name = config.value.device_name != null ? config.value.device_name : config.value.name mode = config.value.options.mode source = ( config.value.source_type == "attach" @@ -152,7 +152,7 @@ resource "google_compute_instance" "default" { for_each = local.attached_disks_regional iterator = config content { - device_name = config.value.name + device_name = config.value.device_name != null ? config.value.device_name : config.value.name mode = config.value.options.mode source = ( config.value.source_type == "attach" @@ -285,7 +285,7 @@ resource "google_compute_instance_template" "default" { iterator = config content { auto_delete = config.value.options.auto_delete - device_name = config.value.name + device_name = config.value.device_name != null ? config.value.device_name : config.value.name # Cannot use `source` with any of the fields in # [disk_size_gb disk_name disk_type source_image labels] disk_type = ( diff --git a/modules/compute-vm/variables.tf b/modules/compute-vm/variables.tf index 4287999fee..dc5651775d 100644 --- a/modules/compute-vm/variables.tf +++ b/modules/compute-vm/variables.tf @@ -39,6 +39,7 @@ variable "attached_disks" { description = "Additional disks, if options is null defaults will be used in its place. Source type is one of 'image' (zonal disks in vms and template), 'snapshot' (vm), 'existing', and null." type = list(object({ name = string + device_name = optional(string) size = string source = optional(string) source_type = optional(string)