2014-06-24 04:01:57 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-07-30 00:22:37 +02:00
|
|
|
"fmt"
|
2014-11-21 17:58:34 +01:00
|
|
|
"log"
|
2014-06-24 04:01:57 +02:00
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/multierror"
|
2014-06-24 04:01:57 +02:00
|
|
|
"github.com/mitchellh/goamz/aws"
|
2014-11-21 17:58:34 +01:00
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
"github.com/mitchellh/goamz/elb"
|
|
|
|
"github.com/mitchellh/goamz/rds"
|
2015-02-12 17:48:48 +01:00
|
|
|
|
|
|
|
awsGo "github.com/awslabs/aws-sdk-go/aws"
|
2015-02-20 23:26:43 +01:00
|
|
|
"github.com/awslabs/aws-sdk-go/gen/autoscaling"
|
2015-02-23 22:22:52 +01:00
|
|
|
awsRDS "github.com/awslabs/aws-sdk-go/gen/rds"
|
2015-02-12 17:48:48 +01:00
|
|
|
"github.com/awslabs/aws-sdk-go/gen/route53"
|
2015-02-19 22:38:56 +01:00
|
|
|
"github.com/awslabs/aws-sdk-go/gen/s3"
|
2014-06-24 04:01:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2014-11-21 17:58:34 +01:00
|
|
|
AccessKey string
|
|
|
|
SecretKey string
|
|
|
|
Region string
|
|
|
|
}
|
|
|
|
|
|
|
|
type AWSClient struct {
|
2015-02-20 23:26:43 +01:00
|
|
|
ec2conn *ec2.EC2
|
|
|
|
elbconn *elb.ELB
|
|
|
|
autoscalingconn *autoscaling.AutoScaling
|
|
|
|
s3conn *s3.S3
|
|
|
|
rdsconn *rds.Rds
|
|
|
|
r53conn *route53.Route53
|
|
|
|
region string
|
2015-02-25 04:43:03 +01:00
|
|
|
awsRDSconn *awsRDS.RDS
|
2014-11-21 17:58:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client configures and returns a fully initailized AWSClient
|
|
|
|
func (c *Config) Client() (interface{}, error) {
|
|
|
|
var client AWSClient
|
|
|
|
|
|
|
|
// Get the auth and region. This can fail if keys/regions were not
|
|
|
|
// specified and we're attempting to use the environment.
|
|
|
|
var errs []error
|
|
|
|
log.Println("[INFO] Building AWS auth structure")
|
|
|
|
auth, err := c.AWSAuth()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("[INFO] Building AWS region structure")
|
|
|
|
region, err := c.AWSRegion()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) == 0 {
|
2015-02-19 22:38:56 +01:00
|
|
|
// store AWS region in client struct, for region specific operations such as
|
|
|
|
// bucket storage in S3
|
|
|
|
client.region = c.Region
|
2015-02-20 15:55:54 +01:00
|
|
|
|
2015-02-19 22:38:56 +01:00
|
|
|
creds := awsGo.Creds(c.AccessKey, c.SecretKey, "")
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
log.Println("[INFO] Initializing EC2 connection")
|
|
|
|
client.ec2conn = ec2.New(auth, region)
|
|
|
|
log.Println("[INFO] Initializing ELB connection")
|
|
|
|
client.elbconn = elb.New(auth, region)
|
2015-02-20 18:28:20 +01:00
|
|
|
log.Println("[INFO] Initializing AutoScaling connection")
|
2015-02-20 23:26:43 +01:00
|
|
|
client.autoscalingconn = autoscaling.New(creds, c.Region, nil)
|
2014-11-21 17:58:34 +01:00
|
|
|
log.Println("[INFO] Initializing S3 connection")
|
2015-02-20 15:55:54 +01:00
|
|
|
client.s3conn = s3.New(creds, c.Region, nil)
|
2014-11-21 17:58:34 +01:00
|
|
|
log.Println("[INFO] Initializing RDS connection")
|
|
|
|
client.rdsconn = rds.New(auth, region)
|
2015-02-20 15:55:54 +01:00
|
|
|
|
2015-02-12 17:48:48 +01:00
|
|
|
// aws-sdk-go uses v4 for signing requests, which requires all global
|
|
|
|
// endpoints to use 'us-east-1'.
|
|
|
|
// See http://docs.aws.amazon.com/general/latest/gr/sigv4_changes.html
|
2015-02-20 16:08:41 +01:00
|
|
|
log.Println("[INFO] Initializing Route53 connection")
|
2015-02-12 17:48:48 +01:00
|
|
|
client.r53conn = route53.New(creds, "us-east-1", nil)
|
2015-02-23 22:22:52 +01:00
|
|
|
log.Println("[INFO] Initializing AWS Go RDS connection")
|
|
|
|
client.awsRDSconn = awsRDS.New(creds, c.Region, nil)
|
2014-11-21 17:58:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return nil, &multierror.Error{Errors: errs}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &client, nil
|
2014-06-24 04:01:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2014-10-30 15:07:12 +01:00
|
|
|
var regions = [11]string{"us-east-1", "us-west-2", "us-west-1", "eu-west-1",
|
|
|
|
"eu-central-1", "ap-southeast-1", "ap-southeast-2", "ap-northeast-1",
|
|
|
|
"sa-east-1", "cn-north-1", "us-gov-west-1"}
|
2014-07-30 00:22:37 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|