helper/schema: ResourceData.Get works in most cases
This commit is contained in:
parent
31067ee8f6
commit
1d9528e595
|
@ -37,6 +37,8 @@ func (d *ResourceData) getObject(
|
|||
k string,
|
||||
parts []string,
|
||||
schema map[string]*Schema) interface{} {
|
||||
if len(parts) > 0 {
|
||||
// We're requesting a specific key in an object
|
||||
key := parts[0]
|
||||
parts = parts[1:]
|
||||
s, ok := schema[key]
|
||||
|
@ -44,9 +46,24 @@ func (d *ResourceData) getObject(
|
|||
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)
|
||||
}
|
||||
|
||||
// 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(
|
||||
k string,
|
||||
parts []string,
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue