"transpose" a map in Terraform

58 Views Asked by At

I have this construct

  permissionScopes = {
    "All" = {
      description = "Access Al"
      type        = "Admin"
      value       = "All"
      pre_authorized_clients = [
        "demo.dev",
        "demo.sit"
      ]
    }
    "Test" = {
      description = "Access Al"
      type        = "Admin"
      value       = "All"
      pre_authorized_clients = [
        "demo.dev",
        "demo.sit"
      ]
    }
  }

And I would like to get something like this:

{
   "demo.dev" = [
     "All",
     "Test"
   ],
   "demo.sit" = [
      "All",
      "Test"
    ]
}

So far I tried this

  pre_authorized_clients = {
    for scope, values in var.permissionScopes : scope => {
      for app_name in values.pre_authorized_clients : app_name => [
        scope
      ]
    }

Which creates this

pre_authorized_clients = {
    "All" = {
        "demo.dev" = [
            "All",
        ]
        "demo.sit" = [
            "All",
        ]
    }
    "Test" = {
        "demo.dev" = [
            "Test",
        ]
        "demo.sit" = [
            "Test",
        ]
    }
}

No I am stuck, cause I don't see how I go from there, or if I could even achieve a better (intermediate) result to my end goal.

1

There are 1 best solutions below

0
papanito On

They keyword in the title gave me an indication, using the transpose function. The following construct does as what I need

  pre_authorized_clients = transpose({
    for scope, values in var.permissionScopes : scope => flatten([
      values.pre_authorized_clients
    ])
  })
}