helper/schema: can set sets

This commit is contained in:
Mitchell Hashimoto 2014-08-20 18:11:14 -07:00
parent 9fe21f0423
commit ca18e971d1
2 changed files with 123 additions and 1 deletions

View File

@ -480,8 +480,16 @@ func (d *ResourceData) set(
return d.setList(k, parts, schema, value)
case TypeMap:
return d.setMapValue(k, parts, schema, value)
default:
case TypeSet:
return d.setSet(k, parts, schema, value)
case TypeBool:
fallthrough
case TypeInt:
fallthrough
case TypeString:
return d.setPrimitive(k, schema, value)
default:
panic(fmt.Sprintf("%s: unknown type %s", k, schema.Type))
}
}
@ -661,6 +669,22 @@ func (d *ResourceData) setPrimitive(
return nil
}
func (d *ResourceData) setSet(
k string,
parts []string,
schema *Schema,
value interface{}) error {
if len(parts) > 0 {
return fmt.Errorf("%s: can only set the full set, not elements", k)
}
if s, ok := value.(*Set); ok {
value = s.List()
}
return d.setList(k, nil, schema, value)
}
func (d *ResourceData) stateList(
prefix string,
schema *Schema) map[string]string {

View File

@ -1055,6 +1055,101 @@ func TestResourceDataSet(t *testing.T) {
},
},
},
// Set, with list
{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeSet,
Optional: true,
Computed: true,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
},
State: &terraform.ResourceState{
Attributes: map[string]string{
"ports.#": "3",
"ports.0": "100",
"ports.1": "80",
"ports.2": "80",
},
},
Key: "ports",
Value: []interface{}{100, 125, 125},
GetKey: "ports",
GetValue: []interface{}{100, 125},
},
// Set, with Set
{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeSet,
Optional: true,
Computed: true,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
},
State: &terraform.ResourceState{
Attributes: map[string]string{
"ports.#": "3",
"ports.0": "100",
"ports.1": "80",
"ports.2": "80",
},
},
Key: "ports",
Value: &Set{
m: map[int]interface{}{
1: 1,
2: 2,
},
},
GetKey: "ports",
GetValue: []interface{}{1, 2},
},
// Set single item
{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeSet,
Optional: true,
Computed: true,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
},
State: &terraform.ResourceState{
Attributes: map[string]string{
"ports.#": "2",
"ports.0": "100",
"ports.1": "80",
},
},
Key: "ports.0",
Value: 256,
Err: true,
GetKey: "ports",
GetValue: []interface{}{80, 100},
},
}
for i, tc := range cases {
@ -1069,6 +1164,9 @@ func TestResourceDataSet(t *testing.T) {
}
v := d.Get(tc.GetKey)
if s, ok := v.(*Set); ok {
v = s.List()
}
if !reflect.DeepEqual(v, tc.GetValue) {
t.Fatalf("Get Bad: %d\n\n%#v", i, v)
}