Home >Backend Development >Golang >How to create a custom terraform data source provider schema for a map of map values?
php editor Banana today will introduce you how to create a custom terraform data source provider architecture for a map of map values. When using Terraform for infrastructure orchestration, we often need to obtain data from external systems or services for dynamic configuration. And a custom data source provider can help us achieve this goal. By creating a custom data source provider schema, we can easily get the data we need from the map of map values and apply it in our Terraform configuration. Next, let’s learn how to achieve it!
I have a golang function that returns a role of type map[string]map[string]string
map[foo:map[name:abc env:dev id:465 project:e-1] boo:map[name:def env:prd id:82 project:e-1] :doo[name:ght env:stg id:353 project:e-3]]I created a schema for it as shown below...
func datasourceaccounthelper() *schema.resource { return &schema.resource{ read: accounthelperread, schema: map[string]*schema.schema{ "roles": { type: schema.typemap, elem: &schema.schema{ type: schema.typemap, computed: true, elem: &schema.schema{ type: schema.typestring, }, }, computed: true, }, "id": &schema.schema{ computed: true, type: schema.typestring, }, }, } }And the creation method that passes the role value to the schema
func rolesread(d *schema.resourcedata, m interface{}) error { filteredroles := filteraccounts("john") // returns `map[string]map[string]string` if err := d.set("account_map", filteredroles); err != nil { return err } //accountmaps := make(map[string]interface{}) d.setid("-") return nil }But the terraform output is an empty map, how can I fix it, please help :)
outputs: output = { "roles" = tomap(null) /* of map of string */ "id" = tostring(null) }The expected output is as follows
Outputs: output = { "roles" = { foo = {name = "abc" env = "dev" id= 465 project = "e-1"} boo = {name = "efg" env = "prd" id= 82 project = "e-2"} }, "id" = "-" }WorkaroundThe older version of terraform sdk you are using does not enable what you are trying to do here. Mappings can only be basic types:
typestring,
typeint,
typebool.
migrate to the new framework , which is built for the type system of modern terraform, rather than (as is the case with sdkv2) that of classic terraform v0.11 and earlier Type system.
In the terraform plugin framework, the equivalent structure to what you are trying to describe here ismapnestedattribute, and the following describes the schema structure you show in your question:
schema.mapnestedattribute{ nestedobject: schema.nestedattributeobject{ attributes: map[string]schema.attribute{ "name": schema.stringattribute{ // ... }, "env": schema.stringattribute{ // ... }, "id": schema.numberattribute{ // ... }, "project": schema.stringattribute{ // ... }, }, }, }This represents a mapping of objects with the given properties, so the above schema type is equivalent to the following type constraint, which can be used
The type constraint syntax of the terraform language :
map( object({ name = string env = string id = number project = string }) )
The above is the detailed content of How to create a custom terraform data source provider schema for a map of map values?. For more information, please follow other related articles on the PHP Chinese website!