Merge pull request #811 from hashicorp/b-computed-map-counts

helper/schema: map counts in state
This commit is contained in:
Mitchell Hashimoto 2015-01-15 14:28:11 -08:00
commit 89c3766b66
6 changed files with 69 additions and 2 deletions

View File

@ -56,9 +56,12 @@ func (r *MapFieldReader) readMap(k string) (FieldReadResult, error) {
prefix := k + "." prefix := k + "."
r.Map.Range(func(k, v string) bool { r.Map.Range(func(k, v string) bool {
if strings.HasPrefix(k, prefix) { if strings.HasPrefix(k, prefix) {
result[k[len(prefix):]] = v key := k[len(prefix):]
if key != "#" {
result[key] = v
resultSet = true resultSet = true
} }
}
return true return true
}) })

View File

@ -28,6 +28,7 @@ func TestMapFieldReader(t *testing.T) {
"listInt.0": "21", "listInt.0": "21",
"listInt.1": "42", "listInt.1": "42",
"map.#": "2",
"map.foo": "bar", "map.foo": "bar",
"map.bar": "baz", "map.bar": "baz",

View File

@ -170,6 +170,9 @@ func (w *MapFieldWriter) setMap(
} }
} }
// Set the count
w.result[k+".#"] = strconv.Itoa(len(vs))
return nil return nil
} }

View File

@ -107,6 +107,7 @@ func TestMapFieldWriter(t *testing.T) {
map[string]interface{}{"foo": "bar"}, map[string]interface{}{"foo": "bar"},
false, false,
map[string]string{ map[string]string{
"map.#": "1",
"map.foo": "bar", "map.foo": "bar",
}, },
}, },

View File

@ -1641,8 +1641,10 @@ func TestResourceDataState(t *testing.T) {
State: &terraform.InstanceState{ State: &terraform.InstanceState{
Attributes: map[string]string{ Attributes: map[string]string{
"config_vars.#": "2", "config_vars.#": "2",
"config_vars.0.#": "2",
"config_vars.0.foo": "bar", "config_vars.0.foo": "bar",
"config_vars.0.bar": "bar", "config_vars.0.bar": "bar",
"config_vars.1.#": "1",
"config_vars.1.bar": "baz", "config_vars.1.bar": "baz",
}, },
}, },
@ -1669,7 +1671,9 @@ func TestResourceDataState(t *testing.T) {
Result: &terraform.InstanceState{ Result: &terraform.InstanceState{
Attributes: map[string]string{ Attributes: map[string]string{
"config_vars.#": "2", "config_vars.#": "2",
"config_vars.0.#": "1",
"config_vars.0.foo": "bar", "config_vars.0.foo": "bar",
"config_vars.1.#": "1",
"config_vars.1.baz": "bang", "config_vars.1.baz": "bang",
}, },
}, },
@ -2094,8 +2098,10 @@ func TestResourceDataState(t *testing.T) {
Attributes: map[string]string{ Attributes: map[string]string{
// TODO: broken, shouldn't bar be removed? // TODO: broken, shouldn't bar be removed?
"config_vars.#": "2", "config_vars.#": "2",
"config_vars.0.#": "2",
"config_vars.0.foo": "bar", "config_vars.0.foo": "bar",
"config_vars.0.bar": "bar", "config_vars.0.bar": "bar",
"config_vars.1.#": "1",
"config_vars.1.bar": "baz", "config_vars.1.bar": "baz",
}, },
}, },
@ -2199,6 +2205,7 @@ func TestResourceDataState(t *testing.T) {
Result: &terraform.InstanceState{ Result: &terraform.InstanceState{
Attributes: map[string]string{ Attributes: map[string]string{
"tags.#": "1",
"tags.Name": "foo", "tags.Name": "foo",
}, },
}, },
@ -2289,6 +2296,57 @@ func TestResourceDataState(t *testing.T) {
}, },
}, },
}, },
// #23 Set of maps
{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeSet,
Optional: true,
Computed: true,
Elem: &Resource{
Schema: map[string]*Schema{
"index": &Schema{Type: TypeInt},
"uuids": &Schema{Type: TypeMap},
},
},
Set: func(a interface{}) int {
m := a.(map[string]interface{})
return m["index"].(int)
},
},
},
State: nil,
Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"ports.10.uuids.#": &terraform.ResourceAttrDiff{
NewComputed: true,
},
},
},
Set: map[string]interface{}{
"ports": []interface{}{
map[string]interface{}{
"index": 10,
"uuids": map[string]interface{}{
"80": "value",
},
},
},
},
Result: &terraform.InstanceState{
Attributes: map[string]string{
"ports.#": "1",
"ports.10.index": "10",
"ports.10.uuids.#": "1",
"ports.10.uuids.80": "value",
},
},
},
} }
for i, tc := range cases { for i, tc := range cases {

View File

@ -157,6 +157,7 @@ func TestResourceApply_destroyCreate(t *testing.T) {
Attributes: map[string]string{ Attributes: map[string]string{
"id": "foo", "id": "foo",
"foo": "42", "foo": "42",
"tags.#": "1",
"tags.Name": "foo", "tags.Name": "foo",
}, },
} }