hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.33k stars 1.73k forks source link

Altering Snapshot Schedule should edit, NOT destroy Snapshot schedule #17880

Open JakeCooper opened 6 months ago

JakeCooper commented 6 months ago

Community Note

Terraform Version

1.8

Affected Resource(s)

google_compute_resource_policy

Terraform Configuration

resource "google_compute_resource_policy" "stacker_volumes_disk_backup" {
  name    = "${var.environment}-stacker-volumes-disk-backup-${var.region}"
  project = var.project
  region  = var.region
  snapshot_schedule_policy {
    schedule {
      hourly_schedule {
        hours_in_cycle = 2
        start_time     = "05:00"
      }
    }

    retention_policy {
      max_retention_days    = 3
      on_source_disk_delete = "APPLY_RETENTION_POLICY"
    }

    snapshot_properties {
      storage_locations = [var.region]
    }
  }
}

Debug Output

  # module.stacker_us-west1-b.google_compute_resource_policy.stacker_volumes_disk_backup must be replaced
-/+ resource "google_compute_resource_policy" "stacker_volumes_disk_backup" {
      ~ id        = "projects/railway-infra/regions/us-west1/resourcePolicies/production-stacker-volumes-disk-backup-us-west1" -> (known after apply)
        name      = "production-stacker-volumes-disk-backup-us-west1"
      ~ region    = "https://www.googleapis.com/compute/v1/projects/railway-infra/regions/us-west1" -> "us-west1"
      ~ self_link = "https://www.googleapis.com/compute/v1/projects/railway-infra/regions/us-west1/resourcePolicies/production-stacker-volumes-disk-backup-us-west1" -> (known after apply)
        # (1 unchanged attribute hidden)

      ~ snapshot_schedule_policy {
          ~ schedule {
              - daily_schedule { # forces replacement
                  - days_in_cycle = 1 -> null # forces replacement
                  - start_time    = "05:00" -> null # forces replacement
                }
              + hourly_schedule { # forces replacement
                  + hours_in_cycle = 2 # forces replacement
                  + start_time     = "05:00" # forces replacement
                }
            }
          ~ snapshot_properties {
              - guest_flush       = false -> null
              - labels            = {} -> null
                # (1 unchanged attribute hidden)
            }

            # (1 unchanged block hidden)
        }
    }

Expected Behavior

Since the Google Cloud UI allows you to edit snapshot schedules and change the timeframe (days, weeks, etc), it SHOULD edit the policy not destroy it

Actual Behavior

The policy is destroyed and recreated

Steps to reproduce

  1. terraform apply

Important Factoids

Normally this wouldn't be an issue, but, if you recreate a snapshot policy, it'll orphan any old snapshots created by it (NOT remove them)

Which means Google will keep around a range of snapshots for every lifecycle change that forces a replacement. Example: You change the policy 2x, it'll keep 3 "generations" of snapshots around (and charge you 3x for it)

This is a 200k bug for us at @railwayapp :/.

References

No response

b/335507340

ggtisc commented 6 months ago

This scenario was detected as a possible enhancement not as a bug, so it was forwarded to take in consideration for the future

JakeCooper commented 6 months ago

Sounds good! Apologies if I added the wrong label

Enhancement makes sense given my understanding of the terraform state machine

JakeCooper commented 4 months ago

To clarify, this isn't an enhancement in relation to the provider itself.

Updating a snapshot policy in the console does not recreate it.

This is a bug in relation to Google's API and Product definition

JakeCooper commented 4 months ago

According to the Google Cloud Docs, this behavior does not match the stated behavior for Google Cloud and is thus a bug, not an enhancement

image

https://cloud.google.com/compute/docs/disks/manage-snapshot-schedules#change_snapshot_schedule

harshulsoni commented 4 months ago

This should be resolved with https://github.com/GoogleCloudPlatform/magic-modules/pull/11091