map[string]*schema.Schema{"create_strategy": {Type: schema.TypeBool,Optional: true,Default: "foo",Description: "Available `foo`, `bar`.",},}
create_strategy 仅作为创建时传入的只写参数,创建后无法获取,那么这个参数在导入的时候, Value 为空,会导致比对默认值 foo 时产生 +diff,所以在实现导入的时候,应该主动设置为它的默认值:&schema.Resource{Importer: &schema.ResourceImporter{// helper.ImportWithDefaultValue 在 provider 已封装,可以直接使用State: helper.ImportWithDefaultValue(map[string]interface{}{"create_strategy": "foo",}),},
参数名称 | 是否必填 | 数据类型 | 参数说明 |
DataDisks.N | 否 | Array of DataDisk | 实例数据盘配置信息。若不指定该参数,则默认不购买数据盘。支持购买的时候指定21块数据盘,其中最多包含1块LOCAL_BASIC数据盘或者LOCAL_SSD数据盘,最多包含20块CLOUD_BASIC数据盘、CLOUD_PREMIUM数据盘或者CLOUD_SSD数据盘。 |
map[string]*schema.Schema{"data_disks": {Type: schema.TypeList,Optional: true,Description: "数据盘配置",MaxItems: 21,},}
Description 中说明,因为异常输入会被云 API 拦截,Terraform 无需额外加这一层校验。resource "foo" "bar" {strs = ["a", "b", "c"]nums = [1, 2, 3]}
resource "foo" "bar" {list_item {name = "l1" # foo.bar.list_item.0.name}list_item {name = "l2" # foo.bar.list_item.1.namesub_item {name = "l-2-1" # foo.bar.list_item.1.sub_item.0.name}}}
var s = map[string]*schema.Schema{"list_item": {Type: schema.TypeList,Elem: &schema.Resource{Schema: map[string]*schema.Schema{"sub_item": {Type: schema.TypeList,},},},},}func create(d *schema.ResourceData) {listItems := d.Get("list_item").([]interface{})listItem1 := listItems[1].(map[string]interface{})listItem1Sub := listItem1["sub_item"].([]interface{})[0].(map[string]interface{})}
= 衔接。如下所示:resource "foo" "bar" {map_item = {key1: "1", # foo.bar.map_item.key1key2: "2" # foo.bar.map_item.key2}}
var s = map[string]*schema.Schema{"map_item": {Type: schema.TypeMap,Optional: true,// Elem: Map 不支持定义 Elem, sdk v1 中无效,v2 中会抛出异常},}func create(d *schema.ResourceData) {mapItem := d.Get("map_item").(map[string]interface{})mapVal1 := mapItem["key1"].(string)}
resource "foo" "bar" {set_list = ["a", "b", "c", "c"] # 实际为 ["a", "b", "c"]}resource "foo" "bar_update" {set_list = ["c", "b", "a"] # 等效于 ["a", "b", "c"] ,不会产生 Diff}
Get 函数,返回唯一索引,只要索引相同则视为元素相同。如下所示:var s = map[string]*schema.Schema{"set_list": {Type: schema.TypeList,Elem: &schema.Schema{Type: schema.TypeString},Get: func(v interface) { return getValueHash(v.(string)) },},}func create(d *schema.ResourceData) {setList := d.Get("set_list").(*schema.Set).List()setListItemHead := listItems[0].(string)}
文档反馈