diff --git a/modules/compute-vm/README.md b/modules/compute-vm/README.md index 97d304e86..09b682647 100644 --- a/modules/compute-vm/README.md +++ b/modules/compute-vm/README.md @@ -659,10 +659,7 @@ module "cos-test" { } } attached_disks = [ - { - name = "disk-1" - size = 10 - } + { size = 10 } ] service_account = { email = module.iam-service-account.email @@ -908,39 +905,39 @@ module "sole-tenancy" { | name | description | type | required | default | |---|---|:---:|:---:|:---:| -| [name](variables.tf#L271) | Instance name. | string | ✓ | | -| [network_interfaces](variables.tf#L283) | Network interfaces configuration. Use self links for Shared VPC, set addresses to null if not needed. | list(object({…})) | ✓ | | -| [project_id](variables.tf#L363) | Project id. | string | ✓ | | -| [zone](variables.tf#L476) | Compute zone. | string | ✓ | | +| [name](variables.tf#L270) | Instance name. | string | ✓ | | +| [network_interfaces](variables.tf#L282) | Network interfaces configuration. Use self links for Shared VPC, set addresses to null if not needed. | list(object({…})) | ✓ | | +| [project_id](variables.tf#L362) | Project id. | string | ✓ | | +| [zone](variables.tf#L475) | Compute zone. | string | ✓ | | | [attached_disk_defaults](variables.tf#L17) | Defaults for attached disks options. | object({…}) | | {…} | -| [attached_disks](variables.tf#L37) | 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#L83) | Boot disk properties. | object({…}) | | {…} | -| [can_ip_forward](variables.tf#L117) | Enable IP forwarding. | bool | | false | -| [confidential_compute](variables.tf#L123) | Enable Confidential Compute for these instances. | bool | | false | -| [create_template](variables.tf#L129) | Create instance template instead of instances. | bool | | false | -| [description](variables.tf#L135) | Description of a Compute Instance. | string | | "Managed by the compute-vm Terraform module." | -| [enable_display](variables.tf#L141) | Enable virtual display on the instances. | bool | | false | -| [encryption](variables.tf#L147) | 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 | -| [gpu](variables.tf#L157) | GPU information. Based on https://cloud.google.com/compute/docs/gpus. | object({…}) | | null | -| [group](variables.tf#L192) | Define this variable to create an instance group for instances. Disabled for template use. | object({…}) | | null | -| [hostname](variables.tf#L200) | Instance FQDN name. | string | | null | -| [iam](variables.tf#L206) | IAM bindings in {ROLE => [MEMBERS]} format. | map(list(string)) | | {} | -| [instance_schedule](variables.tf#L212) | Assign or create and assign an instance schedule policy. Either resource policy id or create_config must be specified if not null. Set active to null to dtach a policy from vm before destroying. | object({…}) | | null | -| [instance_type](variables.tf#L247) | Instance type. | string | | "f1-micro" | -| [labels](variables.tf#L253) | Instance labels. | map(string) | | {} | -| [metadata](variables.tf#L259) | Instance metadata. | map(string) | | {} | -| [min_cpu_platform](variables.tf#L265) | Minimum CPU platform. | string | | null | -| [network_attached_interfaces](variables.tf#L276) | Network interfaces using network attachments. | list(string) | | [] | -| [network_tag_bindings](variables.tf#L299) | Resource manager tag bindings in arbitrary key => tag key or value id format. Set on both the instance only for networking purposes, and modifiable without impacting the main resource lifecycle. | map(string) | | {} | -| [options](variables.tf#L306) | Instance options. | object({…}) | | {…} | -| [project_number](variables.tf#L368) | Project number. Used in tag bindings to avoid a permadiff. | string | | null | -| [scratch_disks](variables.tf#L374) | Scratch disks configuration. | object({…}) | | {…} | -| [service_account](variables.tf#L386) | Service account email and scopes. If email is null, the default Compute service account will be used unless auto_create is true, in which case a service account will be created. Set the variable to null to avoid attaching a service account. | object({…}) | | {} | -| [shielded_config](variables.tf#L396) | Shielded VM configuration of the instances. | object({…}) | | null | -| [snapshot_schedules](variables.tf#L406) | Snapshot schedule resource policies that can be attached to disks. | map(object({…})) | | {} | -| [tag_bindings](variables.tf#L449) | Resource manager tag bindings in arbitrary key => tag key or value id format. Set on both the instance and zonal disks, and modifiable without impacting the main resource lifecycle. | map(string) | | {} | -| [tag_bindings_immutable](variables.tf#L456) | Immutable resource manager tag bindings, in tagKeys/id => tagValues/id format. These are set on the instance or instance template at creation time, and trigger recreation if changed. | map(string) | | null | -| [tags](variables.tf#L470) | Instance network tags for firewall rule targets. | list(string) | | [] | +| [attached_disks](variables.tf#L37) | 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#L116) | Enable IP forwarding. | bool | | false | +| [confidential_compute](variables.tf#L122) | Enable Confidential Compute for these instances. | bool | | false | +| [create_template](variables.tf#L128) | Create instance template instead of instances. | bool | | false | +| [description](variables.tf#L134) | Description of a Compute Instance. | string | | "Managed by the compute-vm Terraform module." | +| [enable_display](variables.tf#L140) | Enable virtual display on the instances. | bool | | false | +| [encryption](variables.tf#L146) | 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 | +| [gpu](variables.tf#L156) | GPU information. Based on https://cloud.google.com/compute/docs/gpus. | object({…}) | | null | +| [group](variables.tf#L191) | Define this variable to create an instance group for instances. Disabled for template use. | object({…}) | | null | +| [hostname](variables.tf#L199) | Instance FQDN name. | string | | null | +| [iam](variables.tf#L205) | IAM bindings in {ROLE => [MEMBERS]} format. | map(list(string)) | | {} | +| [instance_schedule](variables.tf#L211) | Assign or create and assign an instance schedule policy. Either resource policy id or create_config must be specified if not null. Set active to null to dtach a policy from vm before destroying. | object({…}) | | null | +| [instance_type](variables.tf#L246) | Instance type. | string | | "f1-micro" | +| [labels](variables.tf#L252) | Instance labels. | map(string) | | {} | +| [metadata](variables.tf#L258) | Instance metadata. | map(string) | | {} | +| [min_cpu_platform](variables.tf#L264) | Minimum CPU platform. | string | | null | +| [network_attached_interfaces](variables.tf#L275) | Network interfaces using network attachments. | list(string) | | [] | +| [network_tag_bindings](variables.tf#L298) | Resource manager tag bindings in arbitrary key => tag key or value id format. Set on both the instance only for networking purposes, and modifiable without impacting the main resource lifecycle. | map(string) | | {} | +| [options](variables.tf#L305) | Instance options. | object({…}) | | {…} | +| [project_number](variables.tf#L367) | Project number. Used in tag bindings to avoid a permadiff. | string | | null | +| [scratch_disks](variables.tf#L373) | Scratch disks configuration. | object({…}) | | {…} | +| [service_account](variables.tf#L385) | Service account email and scopes. If email is null, the default Compute service account will be used unless auto_create is true, in which case a service account will be created. Set the variable to null to avoid attaching a service account. | object({…}) | | {} | +| [shielded_config](variables.tf#L395) | Shielded VM configuration of the instances. | object({…}) | | null | +| [snapshot_schedules](variables.tf#L405) | Snapshot schedule resource policies that can be attached to disks. | map(object({…})) | | {} | +| [tag_bindings](variables.tf#L448) | Resource manager tag bindings in arbitrary key => tag key or value id format. Set on both the instance and zonal disks, and modifiable without impacting the main resource lifecycle. | map(string) | | {} | +| [tag_bindings_immutable](variables.tf#L455) | Immutable resource manager tag bindings, in tagKeys/id => tagValues/id format. These are set on the instance or instance template at creation time, and trigger recreation if changed. | map(string) | | null | +| [tags](variables.tf#L469) | 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 0430db20c..ecfd68be6 100644 --- a/modules/compute-vm/main.tf +++ b/modules/compute-vm/main.tf @@ -17,8 +17,8 @@ locals { advanced_mf = var.options.advanced_machine_features attached_disks = { - for disk in var.attached_disks : - (disk.name != null ? disk.name : disk.device_name) => merge(disk, { + for i, disk in var.attached_disks : + coalesce(disk.name, disk.device_name, "disk-${i}") => merge(disk, { options = disk.options == null ? var.attached_disk_defaults : disk.options }) } @@ -196,10 +196,8 @@ resource "google_compute_instance" "default" { for_each = local.attached_disks_regional iterator = config content { - device_name = ( - config.value.device_name != null - ? config.value.device_name - : config.value.name + device_name = coalesce( + config.value.device_name, config.value.name, config.key ) mode = config.value.options.mode source = ( @@ -442,7 +440,9 @@ resource "google_compute_instance_template" "default" { iterator = config content { auto_delete = config.value.options.auto_delete - device_name = config.value.device_name != null ? config.value.device_name : config.value.name + device_name = coalesce( + config.value.device_name, config.value.name, config.key + ) # 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 9f96f1b8f..4297a0597 100644 --- a/modules/compute-vm/variables.tf +++ b/modules/compute-vm/variables.tf @@ -37,7 +37,7 @@ variable "attached_disk_defaults" { 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 + name = optional(string) device_name = optional(string) # TODO: size can be null when source_type is attach size = string @@ -70,7 +70,6 @@ variable "attached_disks" { ]) == length(var.attached_disks) error_message = "Source type must be one of 'image', 'snapshot', 'attach', null." } - validation { condition = length([ for d in var.attached_disks : d if d.options == null || diff --git a/tests/modules/compute_vm/examples/template.yaml b/tests/modules/compute_vm/examples/template.yaml index be45c9299..ea7c4389f 100644 --- a/tests/modules/compute_vm/examples/template.yaml +++ b/tests/modules/compute_vm/examples/template.yaml @@ -29,9 +29,8 @@ values: source_snapshot: null source_snapshot_encryption_key: [] - auto_delete: true - device_name: disk-1 + device_name: disk-0 disk_encryption_key: [] - disk_name: disk-1 disk_size_gb: 10 disk_type: pd-balanced labels: null