helper/schema: set the field to empty if it is a list and computed

This commit is contained in:
Mitchell Hashimoto 2014-10-10 15:58:38 -07:00
parent e5d64318bf
commit 3a107d2e50
3 changed files with 116 additions and 4 deletions

View File

@ -874,12 +874,19 @@ func (d *ResourceData) stateList(
schema *Schema) map[string]string {
countRaw := d.get(prefix, []string{"#"}, schema, d.stateSource(prefix))
if !countRaw.Exists {
return nil
if schema.Computed {
// If it is computed, then it always _exists_ in the state,
// it is just empty.
countRaw.Exists = true
countRaw.Value = 0
} else {
return nil
}
}
count := countRaw.Value.(int)
result := make(map[string]string)
if count > 0 {
if count > 0 || schema.Computed {
result[prefix+".#"] = strconv.FormatInt(int64(count), 10)
}
for i := 0; i < count; i++ {
@ -974,7 +981,14 @@ func (d *ResourceData) stateSet(
schema *Schema) map[string]string {
raw := d.get(prefix, nil, schema, d.stateSource(prefix))
if !raw.Exists {
return nil
if schema.Computed {
// If it is computed, then it always _exists_ in the state,
// it is just empty.
raw.Exists = true
raw.Value = new(Set)
} else {
return nil
}
}
set := raw.Value.(*Set)

View File

@ -1685,7 +1685,9 @@ func TestResourceDataState(t *testing.T) {
},
Result: &terraform.InstanceState{
Attributes: map[string]string{},
Attributes: map[string]string{
"config_vars.#": "0",
},
},
},
@ -1834,6 +1836,40 @@ func TestResourceDataState(t *testing.T) {
},
},
{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeList,
Optional: true,
Computed: true,
Elem: &Schema{Type: TypeInt},
},
},
State: nil,
Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"ports.#": &terraform.ResourceAttrDiff{
Old: "",
NewComputed: true,
},
},
},
Partial: []string{},
Set: map[string]interface{}{
"ports": []interface{}{},
},
Result: &terraform.InstanceState{
Attributes: map[string]string{
"ports.#": "0",
},
},
},
// List of resources
{
Schema: map[string]*Schema{
@ -1974,6 +2010,39 @@ func TestResourceDataState(t *testing.T) {
},
},
},
{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeSet,
Optional: true,
Computed: true,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
},
State: nil,
Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"ports.#": &terraform.ResourceAttrDiff{
Old: "",
NewComputed: true,
},
},
},
Partial: []string{},
Result: &terraform.InstanceState{
Attributes: map[string]string{
"ports.#": "0",
},
},
},
}
for i, tc := range cases {

View File

@ -667,6 +667,35 @@ func TestSchemaMap_Diff(t *testing.T) {
Err: false,
},
{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeSet,
Optional: true,
Computed: true,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
},
State: nil,
Config: nil,
Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"ports.#": &terraform.ResourceAttrDiff{
Old: "",
NewComputed: true,
},
},
},
Err: false,
},
{
Schema: map[string]*Schema{
"ports": &Schema{