github.com/maheshbr/terraform@v0.3.1-0.20141020033300-deec7194a3ea/builtin/providers/aws/config.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 "unicode" 8 9 "github.com/mitchellh/goamz/aws" 10 ) 11 12 type Config struct { 13 AccessKey string `mapstructure:"access_key"` 14 SecretKey string `mapstructure:"secret_key"` 15 Region string `mapstructure:"region"` 16 } 17 18 // AWSAuth returns a valid aws.Auth object for access to AWS services, or 19 // an error if the authentication couldn't be resolved. 20 // 21 // TODO(mitchellh): Test in some way. 22 func (c *Config) AWSAuth() (aws.Auth, error) { 23 auth, err := aws.GetAuth(c.AccessKey, c.SecretKey) 24 if err == nil { 25 // Store the accesskey and secret that we got... 26 c.AccessKey = auth.AccessKey 27 c.SecretKey = auth.SecretKey 28 } 29 30 return auth, err 31 } 32 33 // IsValidRegion returns true if the configured region is a valid AWS 34 // region and false if it's not 35 func (c *Config) IsValidRegion() bool { 36 var regions = [8]string{"us-east-1", "us-west-2", "us-west-1", "eu-west-1", 37 "ap-southeast-1", "ap-southeast-2", "ap-northeast-1", "sa-east-1"} 38 39 for _, valid := range regions { 40 if c.Region == valid { 41 return true 42 } 43 } 44 return false 45 } 46 47 // AWSRegion returns the configured region. 48 // 49 // TODO(mitchellh): Test in some way. 50 func (c *Config) AWSRegion() (aws.Region, error) { 51 if c.Region != "" { 52 if c.IsValidRegion() { 53 return aws.Regions[c.Region], nil 54 } else { 55 return aws.Region{}, fmt.Errorf("Not a valid region: %s", c.Region) 56 } 57 } 58 59 if v := os.Getenv("AWS_REGION"); v != "" { 60 return aws.Regions[v], nil 61 } 62 63 md, err := aws.GetMetaData("placement/availability-zone") 64 if err != nil { 65 return aws.Region{}, err 66 } 67 68 region := strings.TrimRightFunc(string(md), unicode.IsLetter) 69 return aws.Regions[region], nil 70 }