首頁 > 後端開發 > Golang > 主體

如何為地圖值的地圖建立自訂 terraform 資料來源提供者架構?

PHPz
發布: 2024-02-10 11:03:09
轉載
796 人瀏覽過

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

php小編香蕉今天將為您介紹如何為地圖值的地圖建立自訂terraform資料來源提供者架構。在使用Terraform進行基礎架構編排時,我們經常需要取得外部系統或服務的資料來進行動態配置。而自訂資料來源提供者可以幫助我們實現這一目標。透過建立自訂資料來源提供者架構,我們可以輕鬆地從地圖值的地圖上取得所需的數據,並將其應用於我們的Terraform配置中。接下來,讓我們一起來了解如何實現吧!

問題內容

我有一個 golang 函數,它傳回 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]]
登入後複製

我為它創建了一個架構,如下所示...

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,
            },
        },
    }
}
登入後複製

以及將角色值傳遞給架構的建立方法

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
}
登入後複製

但是 terraform 輸出是一個空地圖,我該如何修復它,請幫忙:)

outputs:

output = {
  "roles" = tomap(null) /* of map of string */
  "id" = tostring(null)
}
登入後複製

期望輸出如下

Outputs:

output = {
  "roles" = { foo    = {name = "abc" env = "dev" id= 465 project = "e-1"}
              boo    = {name = "efg" env = "prd" id= 82 project = "e-2"}       
            },
  "id" = "-"
}
登入後複製

解決方法

您正在使用的舊版 terraform sdk 無法實現您在此處嘗試執行的操作。映射只能是基本型別:typestringtypeinttypebool

要建立此結構,您需要遷移到新框架,它是為現代terraform 的類型系統構建的,而不是(如sdkv2 的情況)經典terraform v0.11 及更早版本的類型系統。

在terraform 外掛程式框架中,與您嘗試在此處描述的結構等效的結構是mapnestedattribute,以下內容描述了您在問題中顯示的架構結構:

schema.mapnestedattribute{
    nestedobject: schema.nestedattributeobject{
        attributes: map[string]schema.attribute{
            "name": schema.stringattribute{
                // ...
            },
            "env": schema.stringattribute{
                // ...
            },
            "id": schema.numberattribute{
                // ...
            },
            "project": schema.stringattribute{
                // ...
            },
        },
    },
}
登入後複製

這表示具有給定屬性的物件的映射,因此上述模式類型相當於以下類型約束,可以使用 terraform 語言的類型約束語法

map(
  object({
    name    = string
    env     = string
    id      = number
    project = string
  })
)
登入後複製

以上是如何為地圖值的地圖建立自訂 terraform 資料來源提供者架構?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!