Changing prefix for empty workspace prefix

This commit is contained in:
rv-jmaggio 2017-12-19 13:14:31 -05:00
parent bef64cfe91
commit b313ce80c4
2 changed files with 17 additions and 6 deletions

View File

@ -15,9 +15,15 @@ import (
) )
func (b *Backend) States() ([]string, error) { func (b *Backend) States() ([]string, error) {
prefix := b.workspaceKeyPrefix + "/"
// List bucket root if there is no workspaceKeyPrefix
if b.workspaceKeyPrefix == "" {
prefix = ""
}
params := &s3.ListObjectsInput{ params := &s3.ListObjectsInput{
Bucket: &b.bucketName, Bucket: &b.bucketName,
Prefix: aws.String(b.workspaceKeyPrefix + "/"), Prefix: aws.String(prefix),
} }
resp, err := b.s3Client.ListObjects(params) resp, err := b.s3Client.ListObjects(params)
@ -27,7 +33,7 @@ func (b *Backend) States() ([]string, error) {
wss := []string{backend.DefaultStateName} wss := []string{backend.DefaultStateName}
for _, obj := range resp.Contents { for _, obj := range resp.Contents {
ws := getWorkspaceForKey(*obj.Key, b) ws := b.keyEnv(*obj.Key)
if ws != "" { if ws != "" {
wss = append(wss, ws) wss = append(wss, ws)
} }
@ -37,7 +43,7 @@ func (b *Backend) States() ([]string, error) {
return wss, nil return wss, nil
} }
func getWorkspaceForKey(key string, b *Backend) string { func (b *Backend) keyEnv(key string) string {
if b.workspaceKeyPrefix == "" { if b.workspaceKeyPrefix == "" {
parts := strings.SplitN(key, "/", 2) parts := strings.SplitN(key, "/", 2)
if len(parts) > 1 && parts[1] == b.keyName { if len(parts) > 1 && parts[1] == b.keyName {
@ -190,7 +196,12 @@ func (b *Backend) path(name string) string {
return b.keyName 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 = ` const errStateUnlock = `

View File

@ -305,13 +305,13 @@ func TestKeyEnv(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
//backend.TestBackend(t, b0, nil) backend.TestBackend(t, b0, nil)
backend.TestBackend(t, b1, nil) backend.TestBackend(t, b1, nil)
backend.TestBackend(t, b2, nil) backend.TestBackend(t, b2, nil)
} }
func testGetWorkspaceForKey(b *Backend, key string, expected string) error { 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 fmt.Errorf("incorrect workspace for key[%q]. Expected[%q]: Actual[%q]", key, expected, actual)
} }
return nil return nil