helper/schema: ResourceData.Get works in most cases

This commit is contained in:
Mitchell Hashimoto 2014-08-15 17:39:08 -07:00
parent 31067ee8f6
commit 1d9528e595
2 changed files with 101 additions and 6 deletions

View File

@ -37,14 +37,31 @@ func (d *ResourceData) getObject(
k string, k string,
parts []string, parts []string,
schema map[string]*Schema) interface{} { schema map[string]*Schema) interface{} {
key := parts[0] if len(parts) > 0 {
parts = parts[1:] // We're requesting a specific key in an object
s, ok := schema[key] key := parts[0]
if !ok { parts = parts[1:]
return nil s, ok := schema[key]
if !ok {
return nil
}
if k != "" {
// If we're not at the root, then we need to append
// the key to get the full key path.
key = fmt.Sprintf("%s.%s", k, key)
}
return d.get(key, parts, s)
} }
return d.get(key, parts, s) // Get the entire object
result := make(map[string]interface{})
for field, _ := range schema {
result[field] = d.getObject(k, []string{field}, schema)
}
return result
} }
func (d *ResourceData) getList( func (d *ResourceData) getList(

View File

@ -172,6 +172,84 @@ func TestResourceDataGet(t *testing.T) {
Value: []interface{}{1, 2, 5}, Value: []interface{}{1, 2, 5},
}, },
{
Schema: map[string]*Schema{
"ingress": &Schema{
Type: TypeList,
Required: true,
Elem: &Resource{
Schema: map[string]*Schema{
"from": &Schema{
Type: TypeInt,
Required: true,
},
},
},
},
},
State: nil,
Diff: &terraform.ResourceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"ingress.#": &terraform.ResourceAttrDiff{
Old: "",
New: "1",
},
"ingress.0.from": &terraform.ResourceAttrDiff{
Old: "",
New: "8080",
},
},
},
Key: "ingress.0",
Value: map[string]interface{}{
"from": 8080,
},
},
{
Schema: map[string]*Schema{
"ingress": &Schema{
Type: TypeList,
Required: true,
Elem: &Resource{
Schema: map[string]*Schema{
"from": &Schema{
Type: TypeInt,
Required: true,
},
},
},
},
},
State: nil,
Diff: &terraform.ResourceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"ingress.#": &terraform.ResourceAttrDiff{
Old: "",
New: "1",
},
"ingress.0.from": &terraform.ResourceAttrDiff{
Old: "",
New: "8080",
},
},
},
Key: "ingress",
Value: []interface{}{
map[string]interface{}{
"from": 8080,
},
},
},
} }
for i, tc := range cases { for i, tc := range cases {