2014-06-24 04:01:57 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-07-30 00:22:37 +02:00
|
|
|
"fmt"
|
2014-07-10 20:31:16 +02:00
|
|
|
"os"
|
2014-06-24 04:01:57 +02:00
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
|
|
|
|
"github.com/mitchellh/goamz/aws"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
AccessKey string `mapstructure:"access_key"`
|
|
|
|
SecretKey string `mapstructure:"secret_key"`
|
|
|
|
Region string `mapstructure:"region"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// AWSAuth returns a valid aws.Auth object for access to AWS services, or
|
|
|
|
// an error if the authentication couldn't be resolved.
|
|
|
|
//
|
|
|
|
// TODO(mitchellh): Test in some way.
|
|
|
|
func (c *Config) AWSAuth() (aws.Auth, error) {
|
|
|
|
auth, err := aws.GetAuth(c.AccessKey, c.SecretKey)
|
|
|
|
if err == nil {
|
|
|
|
// Store the accesskey and secret that we got...
|
|
|
|
c.AccessKey = auth.AccessKey
|
|
|
|
c.SecretKey = auth.SecretKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return auth, err
|
|
|
|
}
|
|
|
|
|
2014-07-30 00:22:37 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2014-06-24 04:01:57 +02:00
|
|
|
// AWSRegion returns the configured region.
|
|
|
|
//
|
|
|
|
// TODO(mitchellh): Test in some way.
|
|
|
|
func (c *Config) AWSRegion() (aws.Region, error) {
|
|
|
|
if c.Region != "" {
|
2014-07-30 00:22:37 +02:00
|
|
|
if c.IsValidRegion() {
|
|
|
|
return aws.Regions[c.Region], nil
|
|
|
|
} else {
|
|
|
|
return aws.Region{}, fmt.Errorf("Not a valid region: %s", c.Region)
|
|
|
|
}
|
2014-06-24 04:01:57 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 20:31:16 +02:00
|
|
|
if v := os.Getenv("AWS_REGION"); v != "" {
|
|
|
|
return aws.Regions[v], nil
|
|
|
|
}
|
|
|
|
|
2014-06-24 04:01:57 +02:00
|
|
|
md, err := aws.GetMetaData("placement/availability-zone")
|
|
|
|
if err != nil {
|
|
|
|
return aws.Region{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
region := strings.TrimRightFunc(string(md), unicode.IsLetter)
|
|
|
|
return aws.Regions[region], nil
|
|
|
|
}
|