backend/remote-state/gcs: Move toBucketName to the tests.

This commit is contained in:
Florian Forster 2017-10-05 09:54:41 +02:00 committed by James Bardin
parent 3023ae2813
commit afa13a3d8e
2 changed files with 39 additions and 38 deletions

View File

@ -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)
}

View File

@ -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)
}