Add option to attach multiple snapshot schedule to disks (#2639)

* Add changes to link mutiple Snapshot Schedule to a disk/boot disk

* Update Readme and fix for boot snapshot schedule

* Updating for_each expression, fix linting error

---------

Co-authored-by: usamashujaat <155731944+usamashujaat@users.noreply.github.com>
This commit is contained in:
Usama Shujaat
2024-10-28 21:53:43 +04:00
committed by GitHub
parent 601f137c9b
commit 6db881da34
3 changed files with 43 additions and 19 deletions

View File

@@ -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. | <code>string</code> | ✓ | |
| [zone](variables.tf#L391) | Compute zone. | <code>string</code> | ✓ | |
| [attached_disk_defaults](variables.tf#L17) | Defaults for attached disks options. | <code title="object&#40;&#123;&#10; auto_delete &#61; optional&#40;bool, false&#41;&#10; mode &#61; string&#10; replica_zone &#61; string&#10; type &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; auto_delete &#61; true&#10; mode &#61; &#34;READ_WRITE&#34;&#10; replica_zone &#61; null&#10; type &#61; &#34;pd-balanced&#34;&#10;&#125;">&#123;&#8230;&#125;</code> |
| [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. | <code title="list&#40;object&#40;&#123;&#10; name &#61; string&#10; device_name &#61; optional&#40;string&#41;&#10; size &#61; string&#10; snapshot_schedule &#61; optional&#40;string&#41;&#10; source &#61; optional&#40;string&#41;&#10; source_type &#61; optional&#40;string&#41;&#10; options &#61; optional&#40;&#10; object&#40;&#123;&#10; auto_delete &#61; optional&#40;bool, false&#41;&#10; mode &#61; optional&#40;string, &#34;READ_WRITE&#34;&#41;&#10; replica_zone &#61; optional&#40;string&#41;&#10; type &#61; optional&#40;string, &#34;pd-balanced&#34;&#41;&#10; &#125;&#41;,&#10; &#123;&#10; auto_delete &#61; true&#10; mode &#61; &#34;READ_WRITE&#34;&#10; replica_zone &#61; null&#10; type &#61; &#34;pd-balanced&#34;&#10; &#125;&#10; &#41;&#10;&#125;&#41;&#41;">list&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#91;&#93;</code> |
| [boot_disk](variables.tf#L83) | Boot disk properties. | <code title="object&#40;&#123;&#10; auto_delete &#61; optional&#40;bool, true&#41;&#10; snapshot_schedule &#61; optional&#40;string&#41;&#10; source &#61; optional&#40;string&#41;&#10; initialize_params &#61; optional&#40;object&#40;&#123;&#10; image &#61; optional&#40;string, &#34;projects&#47;debian-cloud&#47;global&#47;images&#47;family&#47;debian-11&#34;&#41;&#10; size &#61; optional&#40;number, 10&#41;&#10; type &#61; optional&#40;string, &#34;pd-balanced&#34;&#41;&#10; &#125;&#41;&#41;&#10; use_independent_disk &#61; optional&#40;bool, false&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; initialize_params &#61; &#123;&#125;&#10;&#125;">&#123;&#8230;&#125;</code> |
| [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. | <code title="list&#40;object&#40;&#123;&#10; name &#61; string&#10; device_name &#61; optional&#40;string&#41;&#10; size &#61; string&#10; snapshot_schedule &#61; optional&#40;list&#40;string&#41;&#41;&#10; source &#61; optional&#40;string&#41;&#10; source_type &#61; optional&#40;string&#41;&#10; options &#61; optional&#40;&#10; object&#40;&#123;&#10; auto_delete &#61; optional&#40;bool, false&#41;&#10; mode &#61; optional&#40;string, &#34;READ_WRITE&#34;&#41;&#10; replica_zone &#61; optional&#40;string&#41;&#10; type &#61; optional&#40;string, &#34;pd-balanced&#34;&#41;&#10; &#125;&#41;,&#10; &#123;&#10; auto_delete &#61; true&#10; mode &#61; &#34;READ_WRITE&#34;&#10; replica_zone &#61; null&#10; type &#61; &#34;pd-balanced&#34;&#10; &#125;&#10; &#41;&#10;&#125;&#41;&#41;">list&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#91;&#93;</code> |
| [boot_disk](variables.tf#L83) | Boot disk properties. | <code title="object&#40;&#123;&#10; auto_delete &#61; optional&#40;bool, true&#41;&#10; snapshot_schedule &#61; optional&#40;list&#40;string&#41;&#41;&#10; source &#61; optional&#40;string&#41;&#10; initialize_params &#61; optional&#40;object&#40;&#123;&#10; image &#61; optional&#40;string, &#34;projects&#47;debian-cloud&#47;global&#47;images&#47;family&#47;debian-11&#34;&#41;&#10; size &#61; optional&#40;number, 10&#41;&#10; type &#61; optional&#40;string, &#34;pd-balanced&#34;&#41;&#10; &#125;&#41;&#41;&#10; use_independent_disk &#61; optional&#40;bool, false&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; initialize_params &#61; &#123;&#125;&#10;&#125;">&#123;&#8230;&#125;</code> |
| [can_ip_forward](variables.tf#L117) | Enable IP forwarding. | <code>bool</code> | | <code>false</code> |
| [confidential_compute](variables.tf#L123) | Enable Confidential Compute for these instances. | <code>bool</code> | | <code>false</code> |
| [create_template](variables.tf#L129) | Create instance template instead of instances. | <code>bool</code> | | <code>false</code> |

View File

@@ -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
]
}

View File

@@ -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")