Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot create a disk_snapshot_policy. #468

Open
bulowaty opened this issue Jan 14, 2025 · 0 comments
Open

Cannot create a disk_snapshot_policy. #468

bulowaty opened this issue Jan 14, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@bulowaty
Copy link

TL;DR

There is missing optional parameter in submodule compute_disk_snapshot variables.
snapshot_schedule originally accepts only one of: daily_schedule, hourly_schedule or weekly_schedule but in module it requests to define them all.

Expected behavior

module "disk_snapshots" {
  count  = var.enable_disks_snapshot ? 1 : 0
  source = "git@github.com:terraform-google-modules/terraform-google-vm.git?ref=v13.1.1"

  disks = [
    "${local.boot_disk_id}",
    "${google_compute_disk.data_disk.id}"
  ]
  module_depends_on = [
    "${google_compute_instance.instance.name}",
    "${google_compute_disk.data_disk.name}"
  ]
  name    = "${var.name}-snapshot-policy"
  project = var.project_id
  region  = var.region
  snapshot_properties = {
    labels          = var.labels
    guest_flush = false
    storage_locations = [
      var.region
    ]
  }
  snapshot_retention_policy = {
    max_retention_days    = var.snapshot_retention_days
    on_source_disk_delete = var.keep_snapshots_after_delete ? "KEEP_AUTO_SNAPSHOTS" : "APPLY_RETENTION_POLICY"
  }
  snapshot_schedule = {
    daily_schedule = {
      days_in_cycle = 1
      start_time    = var.snapshot_start_time
    }
  }
}

Then after plan I expect to create resources related to module
so in this particular example I have a one VM with two disks: boot and data
and it should create snapshot policy with attachment to both.

Observed behavior

When I'll set only one schedule (MARK: I kept others in errors commented for visibility)

  snapshot_schedule = {
    daily_schedule = {
      days_in_cycle = 1
      start_time    = var.snapshot_start_time
    }
  }

Then I have an error:

│ Error: Invalid value for input variable
│ 
│   on module/main.tf line 76, in module "disk_snapshots":
│   76:   snapshot_schedule = {
│   77:     daily_schedule = {
│   78:       days_in_cycle = 1
│   79:       start_time    = var.snapshot_start_time
│   80:     }
│   81:     #hourly_schedule = {
│   82:     #  hours_in_cycle = 24
│   83:     #  start_time     = var.snapshot_start_time
│   84:     #}
│   85:     #weekly_schedule = {
│   86:     #  day_of_weeks = [
│   87:     #    {
│   88:     #      day        = "MONDAY"
│   89:     #      start_time = var.snapshot_start_time
│   90:     #    },
│   91:     #    {
│   92:     #      day        = "TUESDAY"
│   93:     #      start_time = var.snapshot_start_time
│   94:     #    },
│   95:     #  ]
│   96:     #}
│   97:   }
│ 
│ The given value is not suitable for
│ module.vm.module.disk_snapshots.var.snapshot_schedule declared at
│ .terraform/modules/vm.disk_snapshots/modules/compute_disk_snapshot/variables.tf:42,1-29:
│ attributes "hourly_schedule" and "weekly_schedule" are required.

To satisfy the requirements I set them all

  snapshot_schedule = {
    daily_schedule = {
      days_in_cycle = 1
      start_time    = var.snapshot_start_time
    }
    hourly_schedule = {
      hours_in_cycle = 24
      start_time     = var.snapshot_start_time
    }
    weekly_schedule = {
      day_of_weeks = [
        {
          day        = "MONDAY"
          start_time = var.snapshot_start_time
        },
        {
          day        = "TUESDAY"
          start_time = var.snapshot_start_time
        },
      ]
    }
  }

And I got following error

│ Error: Invalid combination of arguments
│ 
│   with module.vm["<instance_name>"].module.disk_snapshots[0].google_compute_resource_policy.policy,
│   on .terraform/modules/vm.disk_snapshots/modules/compute_disk_snapshot/main.tf line 34, in resource "google_compute_resource_policy" "policy":
│   34:     schedule {
│ 
│ "snapshot_schedule_policy.0.schedule.0.daily_schedule": only one of
│ `snapshot_schedule_policy.0.schedule.0.daily_schedule,snapshot_schedule_policy.0.schedule.0.hourly_schedule,snapshot_schedule_policy.0.schedule.0.weekly_schedule`
│ can be specified, but
│ `snapshot_schedule_policy.0.schedule.0.daily_schedule,snapshot_schedule_policy.0.schedule.0.hourly_schedule,snapshot_schedule_policy.0.schedule.0.weekly_schedule`
│ were specified.
╵
╷
│ Error: Invalid combination of arguments
│ 
│   with module.vm["<instance_name>"].module.disk_snapshots[0].google_compute_resource_policy.policy,
│   on .terraform/modules/vm.disk_snapshots/modules/compute_disk_snapshot/main.tf line 34, in resource "google_compute_resource_policy" "policy":
│   34:     schedule {
│ 
│ "snapshot_schedule_policy.0.schedule.0.hourly_schedule": only one of
│ `snapshot_schedule_policy.0.schedule.0.daily_schedule,snapshot_schedule_policy.0.schedule.0.hourly_schedule,snapshot_schedule_policy.0.schedule.0.weekly_schedule`
│ can be specified, but
│ `snapshot_schedule_policy.0.schedule.0.daily_schedule,snapshot_schedule_policy.0.schedule.0.hourly_schedule,snapshot_schedule_policy.0.schedule.0.weekly_schedule`
│ were specified.
╵
╷
│ Error: Invalid combination of arguments
│ 
│   with module.vm["<instance_name>"].module.disk_snapshots[0].google_compute_resource_policy.policy,
│   on .terraform/modules/vm.disk_snapshots/modules/compute_disk_snapshot/main.tf line 34, in resource "google_compute_resource_policy" "policy":
│   34:     schedule {
│ 
│ "snapshot_schedule_policy.0.schedule.0.weekly_schedule": only one of
│ `snapshot_schedule_policy.0.schedule.0.daily_schedule,snapshot_schedule_policy.0.schedule.0.hourly_schedule,snapshot_schedule_policy.0.schedule.0.weekly_schedule`
│ can be specified, but
│ `snapshot_schedule_policy.0.schedule.0.daily_schedule,snapshot_schedule_policy.0.schedule.0.hourly_schedule,snapshot_schedule_policy.0.schedule.0.weekly_schedule`
│ were specified.

So the error says that I have to use only one of those possibilities.

Terraform Configuration

- Installing hashicorp/google v4.85.0...
- Installed hashicorp/google v4.85.0 (signed by HashiCorp)
- Installing hashicorp/null v3.2.3...
- Installed hashicorp/null v3.2.3 (signed by HashiCorp)

Terraform Version

Terraform v1.6.3
on linux_amd64

terragrunt version v0.53.2

Additional information

I forked this locally to check what can be done.
It just require a small improvement by adding optional( ) attribute for them as below.

variable "snapshot_schedule" {
  description = "The scheduled to be used by the snapshot policy. For more details see https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_resource_policy#schedule"
  type = object(
    {
      daily_schedule = optional(object(
        {
          days_in_cycle = number
          start_time    = string
        }
      ))
      hourly_schedule = optional(object(
        {
          hours_in_cycle = number
          start_time     = string
        }
      ))
      weekly_schedule = optional(object(
        {
          day_of_weeks = set(object(
            {
              day        = string
              start_time = string
            }
          ))
        }
      ))
    }
  )
}
@bulowaty bulowaty added the bug Something isn't working label Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant