helper/schema: on destroy/create, reset state to be empty [GH-464]

This commit is contained in:
Mitchell Hashimoto 2014-10-21 00:28:41 -07:00
parent d1324678dd
commit d0ce67a5b7
4 changed files with 103 additions and 0 deletions

View File

@ -41,6 +41,7 @@ BUG FIXES:
* providers/aws: Fix crash case if launch config is manually deleted. [GH-421]
* providers/aws: Disassociate EIP before destroying.
* providers/aws: ELB treats subnets as a set.
* providers/aws: Fix case where in a destroy/create tags weren't reapplied. [GH-464]
* providers/consul: Fix regression where `key` param changed to `keys. [GH-475]
## 0.3.0 (October 14, 2014)

View File

@ -91,6 +91,12 @@ func (r *Resource) Apply(
if !d.RequiresNew() {
return nil, nil
}
// Reset the data to be stateless since we just destroyed
data, err = schemaMap(r.Schema).Data(nil, d)
if err != nil {
return nil, err
}
}
err = nil

View File

@ -912,6 +912,31 @@ func TestResourceDataHasChange(t *testing.T) {
Change: false,
},
{
Schema: map[string]*Schema{
"tags": &Schema{
Type: TypeMap,
Optional: true,
Computed: true,
},
},
State: nil,
Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"tags.Name": &terraform.ResourceAttrDiff{
Old: "foo",
New: "foo",
},
},
},
Key: "tags",
Change: true,
},
}
for i, tc := range cases {

View File

@ -95,6 +95,77 @@ func TestResourceApply_destroy(t *testing.T) {
}
}
func TestResourceApply_destroyCreate(t *testing.T) {
r := &Resource{
Schema: map[string]*Schema{
"foo": &Schema{
Type: TypeInt,
Optional: true,
},
"tags": &Schema{
Type: TypeMap,
Optional: true,
Computed: true,
},
},
}
change := false
r.Create = func(d *ResourceData, m interface{}) error {
change = d.HasChange("tags")
d.SetId("foo")
return nil
}
r.Delete = func(d *ResourceData, m interface{}) error {
return nil
}
var s *terraform.InstanceState = &terraform.InstanceState{
ID: "bar",
Attributes: map[string]string{
"foo": "bar",
"tags.Name": "foo",
},
}
d := &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"foo": &terraform.ResourceAttrDiff{
New: "42",
RequiresNew: true,
},
"tags.Name": &terraform.ResourceAttrDiff{
Old: "foo",
New: "foo",
RequiresNew: true,
},
},
}
actual, err := r.Apply(s, d, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if !change {
t.Fatal("should have change")
}
expected := &terraform.InstanceState{
ID: "foo",
Attributes: map[string]string{
"id": "foo",
"foo": "42",
"tags.Name": "foo",
},
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}
func TestResourceApply_destroyPartial(t *testing.T) {
r := &Resource{
Schema: map[string]*Schema{