remote/s3: using address for bucket and path

This commit is contained in:
Jakub Janczak 2015-02-06 12:26:11 +01:00
parent 98d7bcefef
commit 84e6364bff
2 changed files with 20 additions and 14 deletions

View File

@ -8,6 +8,7 @@ import (
"io" "io"
"net/http" "net/http"
"os" "os"
"regexp"
"time" "time"
"github.com/goamz/goamz/aws" "github.com/goamz/goamz/aws"
@ -19,7 +20,7 @@ type S3RemoteClient struct {
Path string Path string
} }
func GetRegion(conf map[string]string) (aws.Region, error) { func getRegion(conf map[string]string) (aws.Region, error) {
regionName, ok := conf["region"] regionName, ok := conf["region"]
if !ok || regionName == "" { if !ok || regionName == "" {
regionName = os.Getenv("AWS_DEFAULT_REGION") regionName = os.Getenv("AWS_DEFAULT_REGION")
@ -35,6 +36,15 @@ func GetRegion(conf map[string]string) (aws.Region, error) {
return region, nil return region, nil
} }
func getBucketAndPath(address string) (string, string, error) {
re := regexp.MustCompile("^s3://([^/]+)/(.+)$")
matches := re.FindStringSubmatch(address)
if len(matches) < 3 {
return "", "", fmt.Errorf("Address for s3 should be of form: s3://<bucket_name>/<path>")
}
return matches[1], matches[2], nil
}
func NewS3RemoteClient(conf map[string]string) (*S3RemoteClient, error) { func NewS3RemoteClient(conf map[string]string) (*S3RemoteClient, error) {
client := &S3RemoteClient{} client := &S3RemoteClient{}
@ -43,22 +53,20 @@ func NewS3RemoteClient(conf map[string]string) (*S3RemoteClient, error) {
return nil, err return nil, err
} }
region, err := GetRegion(conf) region, err := getRegion(conf)
if err != nil { if err != nil {
return nil, err return nil, err
} }
bucketName, ok := conf["bucket"] address, ok := conf["address"]
if !ok { if !ok {
return nil, fmt.Errorf("Missing 'bucket_name' configuration") return nil, fmt.Errorf("'address' configuration not set for S3 remote state storage backend")
}
bucketName, path, err := getBucketAndPath(address)
if err != nil {
return nil, err
} }
client.Bucket = s3.New(auth, region).Bucket(bucketName) client.Bucket = s3.New(auth, region).Bucket(bucketName)
path, ok := conf["path"]
if !ok {
return nil, fmt.Errorf("Missing 'path' configuration")
}
client.Path = path client.Path = path
return client, nil return client, nil

View File

@ -17,8 +17,7 @@ func TestS3Remote_NewClient(t *testing.T) {
conf["access_token"] = "test" conf["access_token"] = "test"
conf["secret_token"] = "test" conf["secret_token"] = "test"
conf["path"] = "hashicorp/test-state" conf["address"] = "s3://plan3-test/hashicorp/test-state"
conf["bucket"] = "plan3-test"
conf["region"] = "eu-west-1" conf["region"] = "eu-west-1"
if _, err := NewS3RemoteClient(conf); err != nil { if _, err := NewS3RemoteClient(conf); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
@ -40,8 +39,7 @@ func TestS3Remote_Validate_envVar(t *testing.T) {
defer os.Setenv("AWS_DEFAULT_REGION", os.Getenv("AWS_DEFAULT_REGION")) defer os.Setenv("AWS_DEFAULT_REGION", os.Getenv("AWS_DEFAULT_REGION"))
os.Setenv("AWS_DEFAULT_REGION", "eu-west-1") os.Setenv("AWS_DEFAULT_REGION", "eu-west-1")
conf["path"] = "hashicorp/test-state" conf["address"] = "s3://terraform-state/hashicorp/test-state"
conf["bucket"] = "plan3-test"
if _, err := NewS3RemoteClient(conf); err != nil { if _, err := NewS3RemoteClient(conf); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }