How to create a custom terraform data source provider schema for a map of map values?

PHPz
Release: 2024-02-10 11:03:09
forward
797 people have browsed it

如何为地图值的地图创建自定义 terraform 数据源提供程序架构?

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!

Question content

I have a golang function that returns a role of type map[string]map[string]string

For example:

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]]
Copy after login

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,
            },
        },
    }
}
Copy after login

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
}
Copy after login

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)
}
Copy after login

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" = "-"
}
Copy after login

Workaround

The 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.

To create this structure you need to

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 is

mapnestedattribute, 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{
                // ...
            },
        },
    },
}
Copy after login

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
  })
)
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!