简体   繁体   中英

Terraform list object in Golang

I am trying to represent Terraform list of objects in Go ie

variable "map_roles" {
  description = "Additional IAM roles to add to the aws-auth configmap."
  type = list(object({
    rolearn  = string
    username = string
    groups   = list(string)
  }))

Are rolearn and others basic types or composite ones eg a map? And so is map_roles just a struct of strings and slice of string (list), or is it a struct of maps?

Terratest converts variable values given in terraform.Options to -var command line arguments using its internal function toHclString .

From reading through the implementation of that function and the other functions it calls, it seems like it will convert a Go []interface{} value into Terraform tuple syntax and a Go map[string]interface{} into Terraform object syntax, so a valid value for the type constraint shown might look like this:

[]interface{}{
    map[string]interface{}{
        "rolearn":  "foo",
        "username": "bar",
        "groups":   []interface{"baz"},
    },
    map[string]interface{}{
        "rolearn":  "boop",
        "username": "beep",
        "groups":   []interface{"blurp"},
    },
}

Based on my read of the code (note: I didn't actually test it ) I would expect that to generate a -var argument value like this:

-var map_roles='[{"rolearn" = "foo", "username" = "bar", "groups" = ["baz"]},{"rolearn" = "boop", "username" = "beep", "groups" = ["blurp"]}]'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM