diff --git a/backend/remote-state/s3/backend_state.go b/backend/remote-state/s3/backend_state.go index a920648a2..e26db42f7 100644 --- a/backend/remote-state/s3/backend_state.go +++ b/backend/remote-state/s3/backend_state.go @@ -15,9 +15,15 @@ import ( ) func (b *Backend) States() ([]string, error) { + prefix := b.workspaceKeyPrefix + "/" + + // List bucket root if there is no workspaceKeyPrefix + if b.workspaceKeyPrefix == "" { + prefix = "" + } params := &s3.ListObjectsInput{ Bucket: &b.bucketName, - Prefix: aws.String(b.workspaceKeyPrefix + "/"), + Prefix: aws.String(prefix), } resp, err := b.s3Client.ListObjects(params) @@ -27,7 +33,7 @@ func (b *Backend) States() ([]string, error) { wss := []string{backend.DefaultStateName} for _, obj := range resp.Contents { - ws := getWorkspaceForKey(*obj.Key, b) + ws := b.keyEnv(*obj.Key) if ws != "" { wss = append(wss, ws) } @@ -37,7 +43,7 @@ func (b *Backend) States() ([]string, error) { return wss, nil } -func getWorkspaceForKey(key string, b *Backend) string { +func (b *Backend) keyEnv(key string) string { if b.workspaceKeyPrefix == "" { parts := strings.SplitN(key, "/", 2) if len(parts) > 1 && parts[1] == b.keyName { @@ -190,7 +196,12 @@ func (b *Backend) path(name string) string { return b.keyName } - return strings.Join([]string{b.workspaceKeyPrefix, name, b.keyName}, "/") + if b.workspaceKeyPrefix != "" { + return strings.Join([]string{b.workspaceKeyPrefix, name, b.keyName}, "/") + } else { + // Trim the leading / for no workspace prefix + return strings.Join([]string{b.workspaceKeyPrefix, name, b.keyName}, "/")[1:] + } } const errStateUnlock = ` diff --git a/backend/remote-state/s3/backend_test.go b/backend/remote-state/s3/backend_test.go index 481653ae7..a86b144b5 100644 --- a/backend/remote-state/s3/backend_test.go +++ b/backend/remote-state/s3/backend_test.go @@ -305,13 +305,13 @@ func TestKeyEnv(t *testing.T) { t.Fatal(err) } - //backend.TestBackend(t, b0, nil) + backend.TestBackend(t, b0, nil) backend.TestBackend(t, b1, nil) backend.TestBackend(t, b2, nil) } func testGetWorkspaceForKey(b *Backend, key string, expected string) error { - if actual := getWorkspaceForKey(key, b); actual != expected { + if actual := b.keyEnv(key); actual != expected { return fmt.Errorf("incorrect workspace for key[%q]. Expected[%q]: Actual[%q]", key, expected, actual) } return nil