GoogleCloudPlatform / terraform-google-netapp-volumes

Deploy NetApp Storage Volumes
https://registry.terraform.io/modules/GoogleCloudPlatform/netapp-volumes/google
Apache License 2.0
2 stars 3 forks source link

snapshot_policy - Attempt to get attribute from null value #8

Closed jdafoe1 closed 8 months ago

jdafoe1 commented 8 months ago

Hello,

This module defaults an empty snapshot_policy to null.

From variables.tf:

    snapshot_policy = optional(object({
      enabled = optional(bool, false)
      hourly_schedule = optional(object({
        snapshots_to_keep = optional(number)
        minute            = optional(number)
      }), null)

      daily_schedule = optional(object({
        snapshots_to_keep = optional(number)
        minute            = optional(number)
        hour              = optional(number)
      }), null)

      weekly_schedule = optional(object({
        snapshots_to_keep = optional(number)
        minute            = optional(number)
        hour              = optional(number)
        day               = optional(string)
      }), null)
    }))

However, there is check on fields of the snapshot_policy, resulting in errors if snapshot_policy input isn't passed to the module.

In main.tf:

  dynamic "snapshot_policy" {
    for_each = each.value.snapshot_policy.enabled ? ["volume_snapshot_policy"] : []
   ...

      dynamic "daily_schedule" {
        for_each = each.value.snapshot_policy.daily_schedule == null ? [] : ["daily_schedule"]
       ...

      dynamic "weekly_schedule" {
        for_each = each.value.snapshot_policy.weekly_schedule == null ? [] : ["weekly_schedule"]
        ...
  }

When the module tries to access each.value.snapshot_policy.<field>, it fails if snapshot_policy isn't passed as input to the module:

module "volumes_only" {
   source  = "GoogleCloudPlatform/netapp-volumes/google"  
   version = "~> 0.1"

  project_id = "my-project-id"
  location   = "my-location"

  storage_pool = {
    create_pool = false
    name        = "pool-name"
  }

  storage_volumes = [
    {
      name       = "test-volume-1"
      share_name = "test-volume-1"
      size       = "100"
      protocols  = ["NFSV3"]
      export_policy_rules = {
        test = {
          allowed_clients = "10.0.0.0/24,10.100.0.0/24"
          access_type     = "READ_WRITE"
          nfsv3           = true
          has_root_access = true
        }
      }
    },
  ]
}
│ Error: Attempt to get attribute from null value
│ 
│   on .terraform/modules/volume.netapp_volumes/main.tf line 57, in resource "google_netapp_volume" "storage_volumes":
│   57:     for_each = each.value.snapshot_policy.enabled ? ["volume_snapshot_policy"] : []
│     ├────────────────
│     │ each.value.snapshot_policy is null
│ 
│ This value is null, so it does not have any attributes.
╵