使用嵌套地图编写Terraform提供程序

时间:2019-01-04 04:38:56

标签: go terraform

我正在编写一个具有2级深度嵌套地图的自定义提供程序。我可以在调用create函数时扩展架构。但是,创建此资源后,尝试从读取函数设置此值时遇到问题。我尝试遵循Terraform文档步骤“复杂读取”部分,但出现错误Invalid address to set: []string{"docker_info", "0", "port_mapping"}

模式如下:

 "docker_info": {
            Type:     schema.TypeList,
            Required: true,
            MaxItems: 1,
            Elem: &schema.Resource{
                Schema: map[string]*schema.Schema{
                    "image": &schema.Schema{
                        Type:     schema.TypeString,
                        Required: true,
                    },
                    "force_pull_image": &schema.Schema{
                        Type:     schema.TypeBool,
                        Optional: true,
                        Default:  "false",
                    },
                    "network": &schema.Schema{
                        Type:         schema.TypeString,
                        Optional:     true,
                        Default:      "BRIDGE",
                        ValidateFunc: validateDockerNetwork,
                    },
                    // We use typeSet because this parameter can be unordered list and must be unique.
                    "port_mapping": &schema.Schema{
                        Type:     schema.TypeSet,
                        Optional: true,
                        Elem: &schema.Resource{
                            Schema: map[string]*schema.Schema{
                                "host_port": &schema.Schema{
                                    Type:     schema.TypeInt,
                                    Required: true,
                                },
                                "container_port": &schema.Schema{
                                    Type:     schema.TypeInt,
                                    Required: true,
                                },
                                "container_port_type": &schema.Schema{
                                    Type:         schema.TypeString,
                                    Optional:     true,
                                    ValidateFunc: validateSingularityPortMappingType,
                                },
                                "host_port_type": &schema.Schema{
                                    Type:         schema.TypeString,
                                    Optional:     true,
                                    ValidateFunc: validateSingularityPortMappingType,
                                },
                                "protocol": &schema.Schema{
                                    Type:         schema.TypeString,
                                    Optional:     true,
                                    ValidateFunc: validateSingularityPortProtocol,
                                    Default:      "tcp",
                                },
                            },
                        },
                    },
                },
            },
        },

在我的阅读功能中,我有:

    d.Set("docker_info", flattenDockerInfo(ContainerInfo.DockerInfo))

    func flattenDockerInfo(in singularity.DockerInfo) []interface{} {
    var out = make([]interface{}, 0, 0)
    m := make(map[string]interface{})
    m["network"] = in.Network
    m["image"] = in.Image
    m["force_pull_image"] = in.ForcePullImage
    if len(in.PortMappings) > 0 {
        m["port_mapping"] = flattenDockerPortMappings(in.PortMappings)
    }
    out = append(out, m)
    return out
}

func flattenDockerPortMappings(in []singularity.DockerPortMapping []map[string]interface{} {

    var out = make([]map[string]interface{}, len(in), len(in))
    for i := range in {
    m := make(map[string]interface{})
    m["container_port"] = v.ContainerPort
    m["container_port_type"] = v.ContainerPortType
    m["host_port"] = v.HostPort
    m["host_port_type"] = v.HostPortType
    m["protocol"] = v.Protocol
    out[i] = m
}
return out
}

单一性Docker结构:

    type DockerInfo struct {
    Parameters              map[string]string           
    `json:"parameters,omitempty"`
        ForcePullImage              bool                         
    `json:"forcePullImage,omitempty"`
    SingularityDockerParameters []SingularityDockerParameter 
    `json:"dockerParameters,omitEmpty"`
    Privileged                  bool                         
    `json:"privileged,omitEmpty"`
    Network                     string                       
    `json:"network,omitEmpty"` //Value can be BRIDGE, HOST, or NONE
    Image                       string                       
    `json:"image"`
    PortMappings                []DockerPortMapping          
    `json:"portMappings,omitempty"`
}

type DockerPortMapping struct {
    ContainerPort     int64  `json:"containerPort"`
    ContainerPortType string `json:"containerPortType,omitempty"` 
    HostPort          int64  `json:"hostPort"`
    HostPortType      string `json:"hostPortType,omitempty"`
    Protocol          string `json:"protocol,omitempty"`
 }

我希望看到类似

的内容
"docker_info.0.port_mapping.3218452487.container_port": "8888",                        
"docker_info.0.port_mapping.3218452487.container_port_type": "LITERAL",
"docker_info.0.port_mapping.3218452487.host_port": "0",
"docker_info.0.port_mapping.3218452487.host_port_type": "FROM_OFFER",
"docker_info.0.port_mapping.3218452487.protocol": "tcp",

我发现通过添加以下代码来创建typeSet

m["port_mapping"] = schema.NewSet(portMappingHash, []interface{}{flattenDockerPortMappings(in.PortMappings)})


func portMappingHash(v interface{}) int {
    var buf bytes.Buffer
    x := v.([]map[string]interface{})
    for i := range x {
        buf.WriteString(fmt.Sprintf("%s-%d", "test", i))
    }
    return hashcode.String(buf.String())
}

我现在得到docker_info.0.port_mapping.2384314926: '' expected a map, got 'slice'

0 个答案:

没有答案