From 1d9528e595cc3cb21fe5dcc2eb332ec466115186 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 15 Aug 2014 17:39:08 -0700 Subject: [PATCH] helper/schema: ResourceData.Get works in most cases --- helper/schema/resource_data.go | 29 ++++++++--- helper/schema/resource_data_test.go | 78 +++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 6 deletions(-) diff --git a/helper/schema/resource_data.go b/helper/schema/resource_data.go index 61cfcebe7..d1057f813 100644 --- a/helper/schema/resource_data.go +++ b/helper/schema/resource_data.go @@ -37,14 +37,31 @@ func (d *ResourceData) getObject( k string, parts []string, schema map[string]*Schema) interface{} { - key := parts[0] - parts = parts[1:] - s, ok := schema[key] - if !ok { - return nil + if len(parts) > 0 { + // We're requesting a specific key in an object + key := parts[0] + parts = parts[1:] + 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( diff --git a/helper/schema/resource_data_test.go b/helper/schema/resource_data_test.go index 781e788c2..af3c52288 100644 --- a/helper/schema/resource_data_test.go +++ b/helper/schema/resource_data_test.go @@ -172,6 +172,84 @@ func TestResourceDataGet(t *testing.T) { 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 {