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

时间:2019-05-22 11:50:40

标签: go terraform

注意:@JimB发表评论后编辑

我正在尝试在Go中构建一个新的Terraform提供程序。我需要的资源有点复杂。它包括结构,结构内的阵列,阵列和阵列内的结构。当我运行Terraform时,它给了我错误,例如: panic: Error reading level config: '' expected type 'string', got unconvertible type 'map[string]interface {}'。我不知道自己在做什么错。

当我使结构足够简单时,它们确实可以工作,但是我需要此资源,并且我确定有办法做到这一点,而我只是缺少了一些琐碎的东西。

-这是Terraform结构:

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"
        }
    ]
}

-这是进行中的Schema定义,如我所能简化的那样:

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,
            },
        },
    },
},

-----最后,这是我要使用的代码(但是,我认为问题出在代码本身开始之前):

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)
}

好吧,我希望create函数创建一个文件,该文件的名称取自name属性,而数据则为其他Terraform属性值的JSON表示形式。相反,我收到了上面指定的错误。

0 个答案:

没有答案
相关问题