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:
@@ -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({ auto_delete = optional(bool, false) mode = string replica_zone = string type = string })">object({…})</code> | | <code title="{ auto_delete = true mode = "READ_WRITE" replica_zone = null type = "pd-balanced" }">{…}</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(object({ name = string device_name = optional(string) size = string snapshot_schedule = optional(string) source = optional(string) source_type = optional(string) options = optional( object({ auto_delete = optional(bool, false) mode = optional(string, "READ_WRITE") replica_zone = optional(string) type = optional(string, "pd-balanced") }), { auto_delete = true mode = "READ_WRITE" replica_zone = null type = "pd-balanced" } ) }))">list(object({…}))</code> | | <code>[]</code> |
|
||||
| [boot_disk](variables.tf#L83) | Boot disk properties. | <code title="object({ auto_delete = optional(bool, true) snapshot_schedule = optional(string) source = optional(string) initialize_params = optional(object({ image = optional(string, "projects/debian-cloud/global/images/family/debian-11") size = optional(number, 10) type = optional(string, "pd-balanced") })) use_independent_disk = optional(bool, false) })">object({…})</code> | | <code title="{ initialize_params = {} }">{…}</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(object({ name = string device_name = optional(string) size = string snapshot_schedule = optional(list(string)) source = optional(string) source_type = optional(string) options = optional( object({ auto_delete = optional(bool, false) mode = optional(string, "READ_WRITE") replica_zone = optional(string) type = optional(string, "pd-balanced") }), { auto_delete = true mode = "READ_WRITE" replica_zone = null type = "pd-balanced" } ) }))">list(object({…}))</code> | | <code>[]</code> |
|
||||
| [boot_disk](variables.tf#L83) | Boot disk properties. | <code title="object({ auto_delete = optional(bool, true) snapshot_schedule = optional(list(string)) source = optional(string) initialize_params = optional(object({ image = optional(string, "projects/debian-cloud/global/images/family/debian-11") size = optional(number, 10) type = optional(string, "pd-balanced") })) use_independent_disk = optional(bool, false) })">object({…})</code> | | <code title="{ initialize_params = {} }">{…}</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> |
|
||||
|
||||
@@ -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
|
||||
]
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user