bpg / terraform-provider-proxmox

Terraform Provider for Proxmox
https://registry.terraform.io/providers/bpg/proxmox
Mozilla Public License 2.0
731 stars 124 forks source link

proxmox_virtual_environment_datastores should group attributes in an array scoped to a single datastore #1354

Open vanillaSprinkles opened 2 months ago

vanillaSprinkles commented 2 months ago

Describe the bug use of proxmox_virtual_environment_datastores with multiple datastores currently utilizes each attribute as an array, resulting in a sorting tf-logic nightmare; the output should instead be a list of datastore objects (perhaps utilizing a filter on resource input?) the output seems to not follow the "normal" paradigm of output structures

To Reproduce Steps to reproduce the behavior:

  1. basic proxmox setup
  2. setup below minimal hcl
  3. tofu output

Please also provide a minimal Terraform configuration that reproduces the issue.

data "proxmox_virtual_environment_datastores" "first_node" {
  node_name = "first-node"
}

output datastores { value = data.proxmox_virtual_environment_datastores.first_node }
$ tofu output
datastores = {
  "active" = tolist([
    false,
    true,
  ])
  "content_types" = tolist([
    tolist([
      "backup",
      "iso",
      "vztmpl",
    ]),
    tolist([
      "backup",
      "images",
      "iso",
      "rootdir",
      "vztmpl",
    ]),
  ])
  "datastore_ids" = tolist([
    "local",
    "local-btrfs",
  ])
  "enabled" = tolist([
    false,
    true,
  ])
  "id" = "first_node_datastores"
  "node_name" = "first_node"
  "shared" = tolist([
    false,
    false,
  ])
  "space_available" = tolist([
    0,
    492695429120,
  ])
  "space_total" = tolist([
    0,
    499033051136,
  ])
  "space_used" = tolist([
    0,
    5329494016,
  ])
  "types" = tolist([
    "dir",
    "btrfs",
  ])
}

Expected behavior

data "proxmox_virtual_environment_datastores" "first_node" {
  node_name = "first-node"

  # filters = {
  #   enabled = true
  # }

}

output datastores { value = data.proxmox_virtual_environment_datastores.first_node }
$ tofo output
datastores = [
  {
    "active" = false
    "content_types" = tolist([
      "backup",
      "iso",
      "vztmpl",
    ])
    "datastore_ids" = "local"
    "enabled" = false
    "id" = "first_node_datastores"
    "node_name" = "first_node"
    "shared" = false
    "space_available" = 0
    "space_total" = 0
    "space_used" = 0
    "types" = "dir"
  },
  {
    "active" = true
    "content_types" = tolist([
      "backup",
      "images",
      "iso",
      "rootdir",
      "vztmpl",
    ])
    "datastore_ids" = "local-btrfs"
    "enabled" = true
    "id" = "first_node_datastores"
    "node_name" = "first_node"
    "shared" = false
    "space_available" = 492695429120
    "space_total" = 499033051136
    "space_used" = 5329494016
    "types" = "btrfs"
  },
]

Additional context (cant quite decide if this is a bug or feature; more like a QoL bug)

Workaround mangle the data object with a locals blob; though i am not proud of this, it does get the job done (i did not optimize it from zombie-brain coding):

locals {

  proxmox_virtual_environment_datastores__first_node = ({
    datastores = ([for index_num in range(0, length(data.proxmox_virtual_environment_datastores.first_node.enabled)) : merge(
      { id = data.proxmox_virtual_environment_datastores.first_node.id },
      { node_name = data.proxmox_virtual_environment_datastores.first_node.node_name },
      [
        for k, v in data.proxmox_virtual_environment_datastores.first_node : (
          {
            (k) = v[index_num]
          }
        ) if length(v) == length(data.proxmox_virtual_environment_datastores.first_node.enabled)
      ]...)
    ])
  }).datastores

  ## and to get a list of only enabled data stores:
  proxmox_virtual_environment_datastores__first_node__enabled = [for k, v in local.proxmox_virtual_environment_datastores__first_node : v if v.enabled]
}
LtdJorge commented 2 months ago

I'm having the same "problem". I believe this could be handled the same way proxmox_virtual_environment_hagroups and proxmox_virtual_environment_hagroup are, using a proxmox_virtual_environment_datastores data source that gets the entire list of ids and then using a proxmox_virtual_environment_datastore referencing an id to retrieve a datastore object.