helper/schema: map counts in state

This commit is contained in:
Mitchell Hashimoto 2015-01-15 14:12:24 -08:00
parent 7c34ce307b
commit 448887f3c4
6 changed files with 69 additions and 2 deletions

View File

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

View File

@ -28,6 +28,7 @@ func TestMapFieldReader(t *testing.T) {
"listInt.0": "21",
"listInt.1": "42",
"map.#": "2",
"map.foo": "bar",
"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
}

View File

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

View File

@ -1641,8 +1641,10 @@ func TestResourceDataState(t *testing.T) {
State: &terraform.InstanceState{
Attributes: map[string]string{
"config_vars.#": "2",
"config_vars.0.#": "2",
"config_vars.0.foo": "bar",
"config_vars.0.bar": "bar",
"config_vars.1.#": "1",
"config_vars.1.bar": "baz",
},
},
@ -1669,7 +1671,9 @@ func TestResourceDataState(t *testing.T) {
Result: &terraform.InstanceState{
Attributes: map[string]string{
"config_vars.#": "2",
"config_vars.0.#": "1",
"config_vars.0.foo": "bar",
"config_vars.1.#": "1",
"config_vars.1.baz": "bang",
},
},
@ -2094,8 +2098,10 @@ func TestResourceDataState(t *testing.T) {
Attributes: map[string]string{
// TODO: broken, shouldn't bar be removed?
"config_vars.#": "2",
"config_vars.0.#": "2",
"config_vars.0.foo": "bar",
"config_vars.0.bar": "bar",
"config_vars.1.#": "1",
"config_vars.1.bar": "baz",
},
},
@ -2199,6 +2205,7 @@ func TestResourceDataState(t *testing.T) {
Result: &terraform.InstanceState{
Attributes: map[string]string{
"tags.#": "1",
"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 {

View File

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