Telmate / terraform-provider-proxmox

Terraform provider plugin for proxmox
MIT License
2.21k stars 533 forks source link

Allow for_each on proxmox_lxc mountpoints #1135

Open fmartingr opened 4 weeks ago

fmartingr commented 4 weeks ago

Hello,

I'm using this to handle my homelab and while refactoring it into modules I tried to use:

resource "proxmox_lxc" "container" {
  ...
  mountpoint {
    for_each  = var.lxc_mountpoints
    storage   = each.mountpoint_storage
    size      = each.mountpoint_data_size
    slot      = each.mountpoint_slot
    key       = each.mountpoint_key
    mp        = each.mountpoint_mountpoint
    backup    = each.mountpoint_backup
    replicate = each.mountpoint_replicate
    shared    = each.mountpoint_shared
  }
  ...
}

But apparently the mountpoint directive does not support for-each:

CleanShot 2024-10-25 at 12 41 09@2x

And trying to run it complains about trying to use each.value:

╷
│ Error: Invalid "each" attribute
│
│   on modules/docker-container/main.tf line 21, in resource "proxmox_lxc" "container":
│   21:     storage   = each.storage
│
│ The "each" object does not have an attribute named "storage". The
│ supported attributes are each.key and each.value, the current key and
│ value pair of the "for_each" attribute set.
╵
...
clevelandcs commented 18 hours ago

@fmartingr mountpoint is meant to have multiple blocks such that the terraform path is proxmox_lxc.container["key"].mountpoint["key"].storage. The correct way to do this when done in a module is with dynamic.

dynamic "mountpoint" {
    for_each = try(each.value.mountpoints, {})
    content {
      key     = mountpoint.key
      slot    = tonumber(mountpoint.key)
      storage = try(mountpoint.value.storage, "local-lvm")
      volume = try(mountpoint.value.volume, null)
      mp     = try(mountpoint.value.mp, "/mnt/container/mp")
      size   = try(mountpoint.value.size, "8G")
      acl    = try(mountpoint.value.acl, false)
      backup = try(mountpoint.value.backup, true)
      replicate = try(mountpoint.value.replicate, false)
      shared = try(mountpoint.value.shared, true)
    }
  } 

In my case my input object looks like this for reference

containers = {
  "mp-test" = {
      description = "lxc test"
      target = "pve1"
      pool = "test"
      template = "media:vztmpl/debian-12-standard_12.7-1_amd64.tar.zst"
      os_type = "debian"
      unprivileged = true
      onboot = false
      hastate = "stopped"
      hagroup = "ceph"
      sshkeys      = ""
      rootfs_storage = "vmdisks"
      tags = ["test"]
      mountpoints = {
        "0" = {
          storage = "vmdisks"
          mp = "/mnt/mp0"
        }
        "1" = {
          storage = "vmdisks"
          mp = "/mnt/mp1"
        }
      }
    } 
}