From ebf4413e9568b0227b9a5ea93109a874e0562bdc Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 23 Jun 2017 10:13:03 -0400 Subject: [PATCH 1/2] add named named state delete+create-delete test This ensures that we don't leave any conflicting state artifacts preventing the recreation of a named state. --- backend/testing.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/backend/testing.go b/backend/testing.go index 936f1ddfa..f8546b309 100644 --- a/backend/testing.go +++ b/backend/testing.go @@ -187,6 +187,24 @@ func testBackendStates(t *testing.T, b Backend) { t.Fatal("expected error") } + // Create and delete the foo state again. + // Make sure that there are no leftover artifacts from a deleted state + // preventing re-creation. + foo, err = b.State("foo") + if err != nil { + t.Fatalf("error: %s", err) + } + if err := foo.RefreshState(); err != nil { + t.Fatalf("bad: %s", err) + } + if v := foo.State(); v.HasResources() { + t.Fatalf("should be empty: %s", v) + } + // and delete it again + if err := b.DeleteState("foo"); err != nil { + t.Fatalf("err: %s", err) + } + // Verify deletion { states, err := b.States() From 1fa7667ad4117a981418195e90d55b27290937aa Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 23 Jun 2017 10:19:50 -0400 Subject: [PATCH 2/2] s3 backend should use Client.Delete to DeleteState The s3.Backend was using it's own code for DeleteState, but the dynamo entries are only handled through the RemoteClient. Have DeleteState use a RemoteClient for delete. --- backend/remote-state/s3/backend_state.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/backend/remote-state/s3/backend_state.go b/backend/remote-state/s3/backend_state.go index 1835e7402..3d7a3c4e5 100644 --- a/backend/remote-state/s3/backend_state.go +++ b/backend/remote-state/s3/backend_state.go @@ -70,20 +70,16 @@ func (b *Backend) DeleteState(name string) error { return fmt.Errorf("can't delete default state") } - params := &s3.DeleteObjectInput{ - Bucket: &b.bucketName, - Key: aws.String(b.path(name)), - } - - _, err := b.s3Client.DeleteObject(params) + client, err := b.remoteClient(name) if err != nil { return err } - return nil + return client.Delete() } -func (b *Backend) State(name string) (state.State, error) { +// get a remote client configured for this state +func (b *Backend) remoteClient(name string) (*RemoteClient, error) { if name == "" { return nil, errors.New("missing state name") } @@ -99,8 +95,16 @@ func (b *Backend) State(name string) (state.State, error) { ddbTable: b.ddbTable, } - stateMgr := &remote.State{Client: client} + return client, nil +} +func (b *Backend) State(name string) (state.State, error) { + client, err := b.remoteClient(name) + if err != nil { + return nil, err + } + + stateMgr := &remote.State{Client: client} // Check to see if this state already exists. // If we're trying to force-unlock a state, we can't take the lock before // fetching the state. If the state doesn't exist, we have to assume this