dsl2014 2019-05-22 11:50
浏览 57

在Terraform中表达复杂的结构并在Go中阅读

Note: edited after a comment from @JimB

I am trying to build a new Terraform provider in Go. The resource that I need is a bit complex. It includes structures, arrays within structures, arrays and structures within arrays. When I run Terraform, it gives me errors, for example: panic: Error reading level config: '' expected type 'string', got unconvertible type 'map[string]interface {}'. I can't figure out what I am doing wrong.

When I make the structures simple enough, they do work, but I need this resource and I'm sure there's a way to do it, and I'm just missing something perhaps trivial.

-- Here's the Terraform structure:

resource "struct" "my-struct-1" {
    name = "MyFile"
    complexstruct = [{
        onebool = true
        onearray = [{
            name = "name-1"
            value = "value-1"
        }, {
            name = "name-2"
            value = "value-2"
        }]
        internalstruct = [{
            attr1 = false
            attr2 = "attribute"
        }]
    }]
    array = [
        {
            attrib1 = "attrib1.1"
            attrib2 = false
            attrib3 = "attrib1.3"
        },
        {
            attrib1 = "attrib2.1"
            attrib2 = true
            attrib3 = "attrib2.3"
        }
    ]
}

-- Here is the Schema definition in go, as simplified as I could make it:

Schema: map[string]*schema.Schema{
    "name": {
        Type:     schema.TypeString,
        Required: true,
    },
    "complexstruct": {
        Type:   schema.TypeList,
        MaxItems: 1,
        Optional: true,
        Elem: &schema.Resource{
            Schema: map[string]*schema.Schema{
                "onebool": {
                    Type:     schema.TypeBool,
                    Optional: true,
                },
                "onearray": {
                    Type:     schema.TypeList,
                    Optional: true,
                    Elem: &schema.Resource{
                        Schema: map[string]*schema.Schema{
                            "name": {
                                Type:     schema.TypeString,
                                Optional: true,
                            },
                            "value": {
                                Type:     schema.TypeString,
                                Optional: true,
                            },
                        },
                    },
                },
                "internalstruct": {
                    Type:     schema.TypeList,
                    MaxItems: 1,
                    Optional: true,
                    Elem: &schema.Resource{
                        Schema: map[string]*schema.Schema{
                            "attr1": {
                                Type:     schema.TypeBool,
                                Optional: true,
                            },
                            "attr2": {
                                Type:     schema.TypeString,
                                Optional: true,
                            },
                        },
                    },
                },
            },
        },
    },
    "array": {
        Type:     schema.TypeList,
        Optional: true,
        Elem: map[string]*schema.Schema{
            "attrib1": {
                Type: schema.TypeString,
                Optional: true,
            },
            "attrib2": {
                Type: schema.TypeBool,
                Optional: true,
            },
            "attrib3": {
                Type: schema.TypeString,
                Optional: true,
            },
        },
    },
},

----- And lastly, here's the code that I am trying to use (however, I think the problem is before it starts with the code itself):

fname := d.Get("name").(string)
d.SetId(fname)
if _, ok := d.GetOk("complexstruct"); ok {
    fc := d.Get("complexstruct").([]map[string]interface{})
    myBool := fc[0]["onebool"].(bool)
    myArray := fc[0]["onearray"].([]map[string]interface{})
    type fcS struct {
        Name    string `json:"name"`
        Value   string `json:"value"`
    }
    fcs := make([]fcS, len(myArray))
    for ifc := range myArray {
        fcs[ifc].Name = myArray[ifc]["name"].(string)
        fcs[ifc].Value = myArray[ifc]["value"].(string)
    }

    myInternalStruct := fc[0]["internalstruct"].([]map[string]interface{})
    type misS struct {
        Attr1   bool    `json:"attr1"`
        Attr2   string  `json:"attr2"'`
    }
    var mis misS
    mis.Attr1 = myInternalStruct[0]["attr1"].(bool)
    mis.Attr2 = myInternalStruct[0]["attr2"].(string)

    type myWholeStruct struct {
        MyBool      bool        `json:"onebool"`
        MyArray     []fcS       `json:"onearray"`
        MyInter     misS        `json:"internalstruct"`
    }
    outp := myWholeStruct{
        myBool,
        fcs,
        mis,
    }
    o, _ := json.Marshal(outp)
    writeStringToFile(string(o), fname, false)
}

Well, I expect the create function to create a file with the name taken from the name attribute, and the data a JSON representation of the values of the other Terraform attributes. Instead I am getting errors as specified above.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 Revit2020下载问题
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大