From d0ce67a5b7962cd54b1a454b5a864e19f48e082d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 21 Oct 2014 00:28:41 -0700 Subject: [PATCH] helper/schema: on destroy/create, reset state to be empty [GH-464] --- CHANGELOG.md | 1 + helper/schema/resource.go | 6 +++ helper/schema/resource_data_test.go | 25 ++++++++++ helper/schema/resource_test.go | 71 +++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49e1fbf21..f492b2ef2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/helper/schema/resource.go b/helper/schema/resource.go index 16c49f230..7f5736bc6 100644 --- a/helper/schema/resource.go +++ b/helper/schema/resource.go @@ -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 diff --git a/helper/schema/resource_data_test.go b/helper/schema/resource_data_test.go index b71d441e8..98ab79ba8 100644 --- a/helper/schema/resource_data_test.go +++ b/helper/schema/resource_data_test.go @@ -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 { diff --git a/helper/schema/resource_test.go b/helper/schema/resource_test.go index 32ad34454..845bd264d 100644 --- a/helper/schema/resource_test.go +++ b/helper/schema/resource_test.go @@ -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{