Merge pull request #96 from jorgeng87/master
Add check for valid AWS region
This commit is contained in:
commit
5f2146999f
|
@ -1,6 +1,7 @@
|
||||||
package aws
|
package aws
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
@ -29,12 +30,30 @@ func (c *Config) AWSAuth() (aws.Auth, error) {
|
||||||
return auth, err
|
return auth, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsValidRegion returns true if the configured region is a valid AWS
|
||||||
|
// region and false if it's not
|
||||||
|
func (c *Config) IsValidRegion() bool {
|
||||||
|
var regions = [8]string{"us-east-1", "us-west-2", "us-west-1", "eu-west-1",
|
||||||
|
"ap-southeast-1", "ap-southeast-2", "ap-northeast-1", "sa-east-1"}
|
||||||
|
|
||||||
|
for _, valid := range regions {
|
||||||
|
if c.Region == valid {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// AWSRegion returns the configured region.
|
// AWSRegion returns the configured region.
|
||||||
//
|
//
|
||||||
// TODO(mitchellh): Test in some way.
|
// TODO(mitchellh): Test in some way.
|
||||||
func (c *Config) AWSRegion() (aws.Region, error) {
|
func (c *Config) AWSRegion() (aws.Region, error) {
|
||||||
if c.Region != "" {
|
if c.Region != "" {
|
||||||
return aws.Regions[c.Region], nil
|
if c.IsValidRegion() {
|
||||||
|
return aws.Regions[c.Region], nil
|
||||||
|
} else {
|
||||||
|
return aws.Region{}, fmt.Errorf("Not a valid region: %s", c.Region)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if v := os.Getenv("AWS_REGION"); v != "" {
|
if v := os.Getenv("AWS_REGION"); v != "" {
|
||||||
|
|
|
@ -30,7 +30,7 @@ func TestResourceProvider_Configure(t *testing.T) {
|
||||||
raw := map[string]interface{}{
|
raw := map[string]interface{}{
|
||||||
"access_key": "foo",
|
"access_key": "foo",
|
||||||
"secret_key": "bar",
|
"secret_key": "bar",
|
||||||
"region": "us-east",
|
"region": "us-east-1",
|
||||||
}
|
}
|
||||||
|
|
||||||
rawConfig, err := config.NewRawConfig(raw)
|
rawConfig, err := config.NewRawConfig(raw)
|
||||||
|
@ -46,7 +46,7 @@ func TestResourceProvider_Configure(t *testing.T) {
|
||||||
expected := Config{
|
expected := Config{
|
||||||
AccessKey: "foo",
|
AccessKey: "foo",
|
||||||
SecretKey: "bar",
|
SecretKey: "bar",
|
||||||
Region: "us-east",
|
Region: "us-east-1",
|
||||||
}
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(rp.Config, expected) {
|
if !reflect.DeepEqual(rp.Config, expected) {
|
||||||
|
|
Loading…
Reference in New Issue