From bc9509d85f5c542932d5312c3838a37573f7c48c Mon Sep 17 00:00:00 2001 From: Andrey Kostov Date: Wed, 7 Jan 2015 12:18:42 +0200 Subject: [PATCH] Consolidate the s3 driver parameters with a struct --- storagedriver/s3/s3.go | 43 ++++++++++++++++++++++++++++--------- storagedriver/s3/s3_test.go | 17 +++++++++++++-- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/storagedriver/s3/s3.go b/storagedriver/s3/s3.go index 3ef55ce1..e7c6cb09 100644 --- a/storagedriver/s3/s3.go +++ b/storagedriver/s3/s3.go @@ -39,6 +39,18 @@ const chunkSize = 5 * 1024 * 1024 // listMax is the largest amount of objects you can request from S3 in a list call const listMax = 1000 +//DriverParameters A struct that encapsulates all of the driver parameters after all values have been set +type DriverParameters struct { + AccessKey string + SecretKey string + Bucket string + Region aws.Region + Encrypt bool + Secure bool + V4Auth bool + RootDirectory string +} + func init() { factory.Register(driverName, &s3DriverFactory{}) } @@ -119,28 +131,39 @@ func FromParameters(parameters map[string]interface{}) (*Driver, error) { rootDirectory = "" } - return New(fmt.Sprint(accessKey), fmt.Sprint(secretKey), fmt.Sprint(bucket), fmt.Sprint(rootDirectory), region, encryptBool, secureBool, v4AuthBool) + params := DriverParameters{ + fmt.Sprint(accessKey), + fmt.Sprint(secretKey), + fmt.Sprint(bucket), + region, + encryptBool, + secureBool, + v4AuthBool, + fmt.Sprint(rootDirectory), + } + + return New(params) } // New constructs a new Driver with the given AWS credentials, region, encryption flag, and // bucketName -func New(accessKey, secretKey, bucketName, rootDirectory string, region aws.Region, encrypt, secure, v4auth bool) (*Driver, error) { - auth, err := aws.GetAuth(accessKey, secretKey, "", time.Time{}) +func New(params DriverParameters) (*Driver, error) { + auth, err := aws.GetAuth(params.AccessKey, params.SecretKey, "", time.Time{}) if err != nil { return nil, err } - if !secure { - region.S3Endpoint = strings.Replace(region.S3Endpoint, "https", "http", 1) + if !params.Secure { + params.Region.S3Endpoint = strings.Replace(params.Region.S3Endpoint, "https", "http", 1) } - s3obj := s3.New(auth, region) - bucket := s3obj.Bucket(bucketName) + s3obj := s3.New(auth, params.Region) + bucket := s3obj.Bucket(params.Bucket) - if v4auth { + if params.V4Auth { s3obj.Signature = aws.V4Signature } else { - if region.Name == "eu-central-1" { + if params.Region.Name == "eu-central-1" { return nil, fmt.Errorf("The eu-central-1 region only works with v4 authentication") } } @@ -164,7 +187,7 @@ func New(accessKey, secretKey, bucketName, rootDirectory string, region aws.Regi // } // } - return &Driver{s3obj, bucket, encrypt, rootDirectory}, nil + return &Driver{s3obj, bucket, params.Encrypt, params.RootDirectory}, nil } // Implement the storagedriver.StorageDriver interface diff --git a/storagedriver/s3/s3_test.go b/storagedriver/s3/s3_test.go index e3c3e535..f70080f2 100644 --- a/storagedriver/s3/s3_test.go +++ b/storagedriver/s3/s3_test.go @@ -28,9 +28,10 @@ func init() { if err != nil { panic(err) } + defer os.Remove(root) s3DriverConstructor := func(region aws.Region) (storagedriver.StorageDriver, error) { - encryptBool := true + encryptBool := false if encrypt != "" { encryptBool, err = strconv.ParseBool(encrypt) if err != nil { @@ -53,7 +54,19 @@ func init() { return nil, err } } - return New(accessKey, secretKey, bucket, root, region, encryptBool, secureBool, v4AuthBool) + + parameters := DriverParameters{ + accessKey, + secretKey, + bucket, + region, + encryptBool, + secureBool, + v4AuthBool, + root, + } + + return New(parameters) } // Skip S3 storage driver tests if environment variable parameters are not provided