diff --git a/modules/compute-vm/README.md b/modules/compute-vm/README.md index 4d6cb6622..f934066fb 100644 --- a/modules/compute-vm/README.md +++ b/modules/compute-vm/README.md @@ -735,13 +735,13 @@ module "instance" { }] boot_disk = { image = "projects/cos-cloud/global/images/family/cos-stable" - snapshot_schedule = "boot" + snapshot_schedule = ["boot"] } attached_disks = [ { name = "disk-1" size = 10 - snapshot_schedule = "generic-vm" + snapshot_schedule = ["generic-vm"] } ] snapshot_schedules = { @@ -855,8 +855,8 @@ module "sole-tenancy" { | [project_id](variables.tf#L293) | Project id. | string | ✓ | | | [zone](variables.tf#L391) | 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({…}) | | {…} | +| [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 | diff --git a/modules/compute-vm/resource-policies.tf b/modules/compute-vm/resource-policies.tf index e46ea5602..743e1e335 100644 --- a/modules/compute-vm/resource-policies.tf +++ b/modules/compute-vm/resource-policies.tf @@ -29,6 +29,29 @@ locals { # externally managed policy : [var.instance_schedule.resource_policy_id] ) + + disk_zonal_schedule_attachments = flatten([ + for disk_key, disk_data in try(local.attached_disks_zonal, []) : + disk_data.snapshot_schedule != null ? [ + for schedule in disk_data.snapshot_schedule : { + disk_key = disk_key + source_type = disk_data.source_type + source = disk_data.source + snapshot_schedule = schedule + } + ] : [] + ]) + disk_regional_schedule_attachments = flatten([ + for disk_key, disk_data in try(local.attached_disks_regional, []) : + disk_data.snapshot_schedule != null ? [ + for schedule in disk_data.snapshot_schedule : { + disk_key = disk_key + source_type = disk_data.source_type + source = disk_data.source + snapshot_schedule = schedule + } + ] : [] + ]) } resource "google_compute_resource_policy" "schedule" { @@ -118,12 +141,12 @@ resource "google_compute_resource_policy" "snapshot" { } resource "google_compute_disk_resource_policy_attachment" "boot" { - count = var.boot_disk.snapshot_schedule != null ? 1 : 0 - project = var.project_id - zone = var.zone + for_each = var.boot_disk.snapshot_schedule != null ? toset(var.boot_disk.snapshot_schedule) : [] + project = var.project_id + zone = var.zone name = try( - google_compute_resource_policy.snapshot[var.boot_disk.snapshot_schedule].name, - var.boot_disk.snapshot_schedule + google_compute_resource_policy.snapshot[each.value].name, + each.value ) disk = var.name depends_on = [google_compute_instance.default] @@ -131,9 +154,10 @@ resource "google_compute_disk_resource_policy_attachment" "boot" { resource "google_compute_disk_resource_policy_attachment" "attached" { for_each = { - for k, v in local.attached_disks_zonal : - k => v if v.snapshot_schedule != null + for attachment in local.disk_zonal_schedule_attachments : + "${attachment.disk_key}-${attachment.snapshot_schedule}" => attachment } + project = var.project_id zone = var.zone name = try( @@ -143,7 +167,7 @@ resource "google_compute_disk_resource_policy_attachment" "attached" { disk = ( each.value.source_type == "attach" ? each.value.source - : google_compute_disk.disks[each.key].name + : google_compute_disk.disks[each.value.disk_key].name ) depends_on = [ google_compute_instance.default, @@ -153,11 +177,11 @@ resource "google_compute_disk_resource_policy_attachment" "attached" { resource "google_compute_region_disk_resource_policy_attachment" "attached" { for_each = { - for k, v in local.attached_disks_regional : - k => v if v.snapshot_schedule != null + for attachment in local.disk_regional_schedule_attachments : + "${attachment.disk_key}-${attachment.snapshot_schedule}" => attachment } + project = var.project_id - region = substr(var.zone, 0, length(var.zone) - 2) name = try( google_compute_resource_policy.snapshot[each.value.snapshot_schedule].name, each.value.snapshot_schedule @@ -165,10 +189,10 @@ resource "google_compute_region_disk_resource_policy_attachment" "attached" { disk = ( each.value.source_type == "attach" ? each.value.source - : google_compute_region_disk.disks[each.key].name + : google_compute_disk.disks[each.value.disk_key].name ) depends_on = [ google_compute_instance.default, - google_compute_region_disk.disks + google_compute_disk.disks ] } diff --git a/modules/compute-vm/variables.tf b/modules/compute-vm/variables.tf index ff531263d..d11e00ec8 100644 --- a/modules/compute-vm/variables.tf +++ b/modules/compute-vm/variables.tf @@ -41,7 +41,7 @@ variable "attached_disks" { device_name = optional(string) # TODO: size can be null when source_type is attach size = string - snapshot_schedule = optional(string) + snapshot_schedule = optional(list(string)) source = optional(string) source_type = optional(string) options = optional( @@ -84,7 +84,7 @@ variable "boot_disk" { description = "Boot disk properties." type = object({ auto_delete = optional(bool, true) - snapshot_schedule = optional(string) + snapshot_schedule = optional(list(string)) source = optional(string) initialize_params = optional(object({ image = optional(string, "projects/debian-cloud/global/images/family/debian-11")