Merge pull request #17086 from hashicorp/jbardin/GH-17083

only split the prefix string once from the s3 key
This commit is contained in:
James Bardin 2018-01-17 18:51:43 -05:00 committed by GitHub
commit a5ed7d0ae4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

@ -53,7 +53,7 @@ func (b *Backend) keyEnv(key string) string {
} }
} }
parts := strings.SplitAfter(key, b.workspaceKeyPrefix) parts := strings.SplitAfterN(key, b.workspaceKeyPrefix, 2)
if len(parts) < 2 { if len(parts) < 2 {
return "" return ""

View File

@ -249,6 +249,35 @@ func TestBackendExtraPaths(t *testing.T) {
} }
} }
// ensure we can separate the workspace prefix when it also matches the prefix
// of the workspace name itself.
func TestBackendPrefixInWorkspace(t *testing.T) {
testACC(t)
bucketName := fmt.Sprintf("terraform-remote-s3-test-%x", time.Now().Unix())
b := backend.TestBackendConfig(t, New(), map[string]interface{}{
"bucket": bucketName,
"key": "test-env.tfstate",
"workspace_key_prefix": "env",
}).(*Backend)
createS3Bucket(t, b.s3Client, bucketName)
defer deleteS3Bucket(t, b.s3Client, bucketName)
// get a state that contains the prefix as a substring
sMgr, err := b.State("env-1")
if err != nil {
t.Fatal(err)
}
if err := sMgr.RefreshState(); err != nil {
t.Fatal(err)
}
if err := checkStateList(b, []string{"default", "env-1"}); err != nil {
t.Fatal(err)
}
}
func TestKeyEnv(t *testing.T) { func TestKeyEnv(t *testing.T) {
testACC(t) testACC(t)
keyName := "some/paths/tfstate" keyName := "some/paths/tfstate"