搜尋
首頁後端開發Golang如何為地圖值的地圖建立自訂 terraform 資料來源提供者架構?

如何为地图值的地图创建自定义 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。如有侵權,請聯絡admin@php.cn刪除
與GO接口鍵入斷言和類型開關與GO接口鍵入斷言和類型開關May 02, 2025 am 12:20 AM

Gohandlesinterfacesandtypeassertionseffectively,enhancingcodeflexibilityandrobustness.1)Typeassertionsallowruntimetypechecking,asseenwiththeShapeinterfaceandCircletype.2)Typeswitcheshandlemultipletypesefficiently,usefulforvariousshapesimplementingthe

使用errors.is和錯誤。使用errors.is和錯誤。May 02, 2025 am 12:11 AM

Go語言的錯誤處理通過errors.Is和errors.As函數變得更加靈活和可讀。 1.errors.Is用於檢查錯誤是否與指定錯誤相同,適用於錯誤鏈的處理。 2.errors.As不僅能檢查錯誤類型,還能將錯誤轉換為具體類型,方便提取錯誤信息。使用這些函數可以簡化錯誤處理邏輯,但需注意錯誤鏈的正確傳遞和避免過度依賴以防代碼複雜化。

在GO中進行性能調整:優化您的應用程序在GO中進行性能調整:優化您的應用程序May 02, 2025 am 12:06 AM

tomakegoapplicationsRunfasterandMorefly,useProflingTools,leverageConCurrency,andManageMoryfectily.1)usepprofforcpuorforcpuandmemoryproflingtoidentifybottlenecks.2)upitizegorizegoroutizegoroutinesandchannelstoparalletaparelalyizetasksandimproverperformance.3)

GO的未來:趨勢和發展GO的未來:趨勢和發展May 02, 2025 am 12:01 AM

go'sfutureisbrightwithtrendslikeMprikeMprikeTooling,仿製藥,雲 - 納蒂維德象,performanceEnhancements,andwebassemblyIntegration,butchallengeSinclainSinClainSinClainSiNgeNingsImpliCityInsImplicityAndimimprovingingRornhandRornrorlling。

了解Goroutines:深入研究GO的並發了解Goroutines:深入研究GO的並發May 01, 2025 am 12:18 AM

goroutinesarefunctionsormethodsthatruncurranceingo,啟用效率和燈威量。 1)shememanagedbodo'sruntimemultimusingmultiplexing,允許千sstorunonfewerosthreads.2)goroutinessimproverentimensImproutinesImproutinesImproveranceThroutinesImproveranceThrountinesimproveranceThroundinesImproveranceThroughEasySytaskParallowalizationAndeff

了解GO中的初始功能:目的和用法了解GO中的初始功能:目的和用法May 01, 2025 am 12:16 AM

purposeoftheInitfunctionoIsistoInitializeVariables,setUpConfigurations,orperformneccesSetarySetupBeforEtheMainFunctionExeCutes.useInitby.UseInitby:1)placingitinyourcodetorunautoamenationally oneraty oneraty oneraty on inity in ofideShortAndAndAndAndForemain,2)keepitiTshortAntAndFocusedonSimImimpletasks,3)

了解GO界面:綜合指南了解GO界面:綜合指南May 01, 2025 am 12:13 AM

Gointerfacesaremethodsignaturesetsthattypesmustimplement,enablingpolymorphismwithoutinheritanceforcleaner,modularcode.Theyareimplicitlysatisfied,usefulforflexibleAPIsanddecoupling,butrequirecarefulusetoavoidruntimeerrorsandmaintaintypesafety.

從恐慌中恢復:何時以及如何使用recover()從恐慌中恢復:何時以及如何使用recover()May 01, 2025 am 12:04 AM

在Go中使用recover()函數可以從panic中恢復。具體方法是:1)在defer函數中使用recover()捕獲panic,避免程序崩潰;2)記錄詳細的錯誤信息以便調試;3)根據具體情況決定是否恢復程序執行;4)謹慎使用,以免影響性能。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。