-
Notifications
You must be signed in to change notification settings - Fork 23
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
Correctly aquire information on AWS_REGION #49
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
@@ -23,7 +24,7 @@ const HTTP = "http" | |
|
||
// TODO : unit tests | ||
// Given an S3 bucket name, attempt to determine its region | ||
func findBucketRegion(bucket string, config *aws.Config) (string, error) { | ||
var findBucketRegion = func(bucket string, config *aws.Config) (string, error) { | ||
input := s3.GetBucketLocationInput{ | ||
Bucket: aws.String(bucket), | ||
} | ||
|
@@ -47,13 +48,35 @@ func findBucketRegion(bucket string, config *aws.Config) (string, error) { | |
} | ||
|
||
// TODO : unit tests | ||
func getAWSRegion(s3Bucket string, config *aws.Config, settings map[string]string) (string, error) { | ||
func GetAWSRegion(s3Bucket string, config *aws.Config, settings map[string]string) (string, error) { | ||
if region, ok := settings[RegionSetting]; ok { | ||
return region, nil | ||
} | ||
|
||
if config.Endpoint == nil || | ||
*config.Endpoint == "" || | ||
strings.HasSuffix(*config.Endpoint, ".amazonaws.com") { | ||
*config.Endpoint == "" { | ||
|
||
if config.Endpoint != nil { | ||
hostAddr, parseErr := url.Parse(*config.Endpoint) | ||
|
||
if parseErr != nil { | ||
return "us-east-1", parseErr | ||
} | ||
|
||
host, _, err := net.SplitHostPort(hostAddr.Host) | ||
|
||
if err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this logic, if WAL-G failed to parse the endpoint host, it won't attempt to call |
||
return "us-east-1", err | ||
} | ||
|
||
if strings.HasSuffix(host, ".amazonaws.com") { | ||
region, err := findBucketRegion(s3Bucket, config) | ||
return region, errors.Wrapf(err, "%s is not set and s3:GetBucketLocation failed", RegionSetting) | ||
} | ||
|
||
return "us-east-1", nil | ||
} | ||
|
||
region, err := findBucketRegion(s3Bucket, config) | ||
return region, errors.Wrapf(err, "%s is not set and s3:GetBucketLocation failed", RegionSetting) | ||
} else { | ||
|
@@ -122,7 +145,7 @@ func createSession(bucket string, settings map[string]string) (*session.Session, | |
config.S3ForcePathStyle = aws.Bool(s3ForcePathStyle) | ||
} | ||
|
||
region, err := getAWSRegion(bucket, config, settings) | ||
region, err := GetAWSRegion(bucket, config, settings) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package s3 | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/aws/aws-sdk-go/aws/defaults" | ||
"testing" | ||
) | ||
|
||
var bucket = "s3://test-bucket/wal-g-test-folder/Sub0" | ||
var settings = map[string]string{ | ||
EndpointSetting: "http://s3.mdst.yandex.net/", | ||
} | ||
var config = defaults.Get().Config.WithRegion(settings[RegionSetting]) | ||
|
||
func TestGetAWSRegionWithEmptyEndpoint(t *testing.T) { | ||
findBucketRegion = func(bucket string, config *aws.Config) (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I missed the point, but I think that we just need to provide different
|
||
return "europe", nil | ||
} | ||
|
||
region, err := GetAWSRegion(bucket, config, settings) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, region, "europe") | ||
} | ||
|
||
func TestGetAWSRegionWithPort(t *testing.T) { | ||
bucket = "s3://test-bucket:8080/wal-g-test-folder/Sub0" | ||
findBucketRegion = func(bucket string, config *aws.Config) (string, error) { | ||
return "europe", nil | ||
} | ||
|
||
region, err := GetAWSRegion(bucket, config, settings) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, region, "europe") | ||
} | ||
|
||
func TestGetAWSRegionWithEndpoint(t *testing.T) { | ||
config.Endpoint = aws.String("s3://test-bucket:8080/wal-g-test-folder/Sub0") | ||
findBucketRegion = func(bucket string, config *aws.Config) (string, error) { | ||
return "europe", nil | ||
} | ||
|
||
region, err := GetAWSRegion(bucket, config, settings) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, region, "us-east-1") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new extracting logic deserves extracting into a separate function, just like the
findBucketRegion