Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable workspace prefix for S3 Backend #15370

Merged
merged 3 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions backend/remote-state/s3/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ func New() backend.Backend {
Description: "The permissions applied when assuming a role.",
Default: "",
},

"workspace_key_prefix": {
Type: schema.TypeString,
Optional: true,
Description: "The prefix applied to the state path inside the bucket",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we mention that it's for non-default workspaces here? Maybe:

applied to the non-default state path

Default: "env:",
},
},
}

Expand All @@ -160,6 +167,7 @@ type Backend struct {
acl string
kmsKeyID string
ddbTable string
workspaceKeyPrefix string
}

func (b *Backend) configure(ctx context.Context) error {
Expand All @@ -175,6 +183,7 @@ func (b *Backend) configure(ctx context.Context) error {
b.serverSideEncryption = data.Get("encrypt").(bool)
b.acl = data.Get("acl").(string)
b.kmsKeyID = data.Get("kms_key_id").(string)
b.workspaceKeyPrefix = data.Get("workspace_key_prefix").(string)

b.ddbTable = data.Get("dynamodb_table").(string)
if b.ddbTable == "" {
Expand Down
12 changes: 3 additions & 9 deletions backend/remote-state/s3/backend_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@ import (
"github.com/hashicorp/terraform/terraform"
)

const (
// This will be used as directory name, the odd looking colon is simply to
// reduce the chance of name conflicts with existing objects.
keyEnvPrefix = "env:"
)

func (b *Backend) States() ([]string, error) {
params := &s3.ListObjectsInput{
Bucket: &b.bucketName,
Prefix: aws.String(keyEnvPrefix + "/"),
Prefix: aws.String(b.workspaceKeyPrefix + "/"),
}

resp, err := b.s3Client.ListObjects(params)
Expand Down Expand Up @@ -53,7 +47,7 @@ func (b *Backend) keyEnv(key string) string {
}

// shouldn't happen since we listed by prefix
if parts[0] != keyEnvPrefix {
if parts[0] != b.workspaceKeyPrefix {
return ""
}

Expand Down Expand Up @@ -179,7 +173,7 @@ func (b *Backend) path(name string) string {
return b.keyName
}

return strings.Join([]string{keyEnvPrefix, name, b.keyName}, "/")
return strings.Join([]string{b.workspaceKeyPrefix, name, b.keyName}, "/")
}

const errStateUnlock = `
Expand Down
6 changes: 3 additions & 3 deletions backend/remote-state/s3/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func TestBackendExtraPaths(t *testing.T) {
}

// put a state in an env directory name
client.path = keyEnvPrefix + "/error"
client.path = b.workspaceKeyPrefix + "/error"
stateMgr.WriteState(terraform.NewState())
if err := stateMgr.PersistState(); err != nil {
t.Fatal(err)
Expand All @@ -169,7 +169,7 @@ func TestBackendExtraPaths(t *testing.T) {
}

// add state with the wrong key for an existing env
client.path = keyEnvPrefix + "/s2/notTestState"
client.path = b.workspaceKeyPrefix + "/s2/notTestState"
stateMgr.WriteState(terraform.NewState())
if err := stateMgr.PersistState(); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -202,7 +202,7 @@ func TestBackendExtraPaths(t *testing.T) {
s2 = s2Mgr.State()

// add a state with a key that matches an existing environment dir name
client.path = keyEnvPrefix + "/s2/"
client.path = b.workspaceKeyPrefix + "/s2/"
stateMgr.WriteState(terraform.NewState())
if err := stateMgr.PersistState(); err != nil {
t.Fatal(err)
Expand Down
3 changes: 3 additions & 0 deletions website/docs/backends/types/s3.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ The following configuration options or environment variables are supported:
* `assume_role_policy` - (Optional) The permissions applied when assuming a role.
* `external_id` - (Optional) The external ID to use when assuming the role.
* `session_name` - (Optional) The session name to use when assuming the role.
* `workspace_key_prefix` - (Optional) The prefix applied to the state path
inside the bucket. This is only relevant when using a non-default workspace.
This defaults to "env:"