From afa13a3d8e214520b73af51d0375abb50f2e0f6d Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Thu, 5 Oct 2017 09:54:41 +0200 Subject: [PATCH] backend/remote-state/gcs: Move toBucketName to the tests. --- backend/remote-state/gcs/backend.go | 38 +---------------------- backend/remote-state/gcs/backend_test.go | 39 +++++++++++++++++++++++- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/backend/remote-state/gcs/backend.go b/backend/remote-state/gcs/backend.go index 3dc6fc9e4..3ec322e15 100644 --- a/backend/remote-state/gcs/backend.go +++ b/backend/remote-state/gcs/backend.go @@ -94,7 +94,7 @@ func (b *gcsBackend) configure(ctx context.Context) error { data := schema.FromContextBackendConfig(b.storageContext) - b.bucketName = toBucketName(data.Get("bucket").(string)) + b.bucketName = data.Get("bucket").(string) b.prefix = strings.TrimLeft(data.Get("prefix").(string), "/") b.defaultStateFile = strings.TrimLeft(data.Get("path").(string), "/") @@ -144,39 +144,3 @@ func (b *gcsBackend) ensureBucketExists() error { return b.storageClient.Bucket(b.bucketName).Create(b.storageContext, b.projectID, attrs) } - -// toBucketName returns a copy of in that is suitable for use as a bucket name. -// All upper case characters are converted to lower case, other invalid -// characters are replaced by '_'. -func toBucketName(in string) string { - // Bucket names must contain only lowercase letters, numbers, dashes - // (-), and underscores (_). - isValid := func(r rune) bool { - switch { - case r >= 'a' && r <= 'z': - return true - case r >= '0' && r <= '9': - return true - case r == '-' || r == '_': - return true - default: - return false - - } - } - - out := make([]rune, 0, len(in)) - for _, r := range strings.ToLower(in) { - if !isValid(r) { - r = '_' - } - out = append(out, r) - } - - // Bucket names must contain 3 to 63 characters. - if len(out) > 63 { - out = out[:63] - } - - return string(out) -} diff --git a/backend/remote-state/gcs/backend_test.go b/backend/remote-state/gcs/backend_test.go index 93f46dab4..a5cd21f55 100644 --- a/backend/remote-state/gcs/backend_test.go +++ b/backend/remote-state/gcs/backend_test.go @@ -3,6 +3,7 @@ package gcs import ( "fmt" "os" + "strings" "testing" "github.com/hashicorp/terraform/backend" @@ -117,7 +118,7 @@ func setupBackend(t *testing.T) backend.Backend { config := map[string]interface{}{ "project": projectID, - "bucket": projectID + "-" + t.Name(), + "bucket": toBucketName(projectID + "-" + t.Name()), "prefix": "", } @@ -166,3 +167,39 @@ func teardownBackend(t *testing.T, be backend.Backend) { t.Fatalf("deleting bucket failed: %v; manual cleanup may be required, though later test runs will happily reuse an existing bucket", err) } } + +// toBucketName returns a copy of in that is suitable for use as a bucket name. +// All upper case characters are converted to lower case, other invalid +// characters are replaced by '_'. +func toBucketName(in string) string { + // Bucket names must contain only lowercase letters, numbers, dashes + // (-), and underscores (_). + isValid := func(r rune) bool { + switch { + case r >= 'a' && r <= 'z': + return true + case r >= '0' && r <= '9': + return true + case r == '-' || r == '_': + return true + default: + return false + + } + } + + out := make([]rune, 0, len(in)) + for _, r := range strings.ToLower(in) { + if !isValid(r) { + r = '_' + } + out = append(out, r) + } + + // Bucket names must contain 3 to 63 characters. + if len(out) > 63 { + out = out[:63] + } + + return string(out) +}