github.com/tarrant/terraform@v0.3.8-0.20150402012457-f68c9eee638e/builtin/providers/aws/config.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/multierror"
     8  
     9  	"github.com/hashicorp/aws-sdk-go/aws"
    10  	"github.com/hashicorp/aws-sdk-go/gen/autoscaling"
    11  	"github.com/hashicorp/aws-sdk-go/gen/ec2"
    12  	"github.com/hashicorp/aws-sdk-go/gen/elb"
    13  	"github.com/hashicorp/aws-sdk-go/gen/iam"
    14  	"github.com/hashicorp/aws-sdk-go/gen/rds"
    15  	"github.com/hashicorp/aws-sdk-go/gen/route53"
    16  	"github.com/hashicorp/aws-sdk-go/gen/s3"
    17  )
    18  
    19  type Config struct {
    20  	AccessKey string
    21  	SecretKey string
    22  	Token     string
    23  	Region    string
    24  }
    25  
    26  type AWSClient struct {
    27  	ec2conn         *ec2.EC2
    28  	elbconn         *elb.ELB
    29  	autoscalingconn *autoscaling.AutoScaling
    30  	s3conn          *s3.S3
    31  	r53conn         *route53.Route53
    32  	region          string
    33  	rdsconn         *rds.RDS
    34  	iamconn         *iam.IAM
    35  }
    36  
    37  // Client configures and returns a fully initailized AWSClient
    38  func (c *Config) Client() (interface{}, error) {
    39  	var client AWSClient
    40  
    41  	// Get the auth and region. This can fail if keys/regions were not
    42  	// specified and we're attempting to use the environment.
    43  	var errs []error
    44  
    45  	log.Println("[INFO] Building AWS region structure")
    46  	err := c.ValidateRegion()
    47  	if err != nil {
    48  		errs = append(errs, err)
    49  	}
    50  
    51  	if len(errs) == 0 {
    52  		// store AWS region in client struct, for region specific operations such as
    53  		// bucket storage in S3
    54  		client.region = c.Region
    55  
    56  		log.Println("[INFO] Building AWS auth structure")
    57  		creds := aws.Creds(c.AccessKey, c.SecretKey, c.Token)
    58  
    59  		log.Println("[INFO] Initializing ELB connection")
    60  		client.elbconn = elb.New(creds, c.Region, nil)
    61  		log.Println("[INFO] Initializing AutoScaling connection")
    62  		client.autoscalingconn = autoscaling.New(creds, c.Region, nil)
    63  		log.Println("[INFO] Initializing S3 connection")
    64  		client.s3conn = s3.New(creds, c.Region, nil)
    65  		log.Println("[INFO] Initializing RDS connection")
    66  		client.rdsconn = rds.New(creds, c.Region, nil)
    67  
    68  		// aws-sdk-go uses v4 for signing requests, which requires all global
    69  		// endpoints to use 'us-east-1'.
    70  		// See http://docs.aws.amazon.com/general/latest/gr/sigv4_changes.html
    71  		log.Println("[INFO] Initializing Route53 connection")
    72  		client.r53conn = route53.New(creds, "us-east-1", nil)
    73  		log.Println("[INFO] Initializing EC2 Connection")
    74  		client.ec2conn = ec2.New(creds, c.Region, nil)
    75  
    76  		client.iamconn = iam.New(creds, c.Region, nil)
    77  	}
    78  
    79  	if len(errs) > 0 {
    80  		return nil, &multierror.Error{Errors: errs}
    81  	}
    82  
    83  	return &client, nil
    84  }
    85  
    86  // IsValidRegion returns true if the configured region is a valid AWS
    87  // region and false if it's not
    88  func (c *Config) ValidateRegion() error {
    89  	var regions = [11]string{"us-east-1", "us-west-2", "us-west-1", "eu-west-1",
    90  		"eu-central-1", "ap-southeast-1", "ap-southeast-2", "ap-northeast-1",
    91  		"sa-east-1", "cn-north-1", "us-gov-west-1"}
    92  
    93  	for _, valid := range regions {
    94  		if c.Region == valid {
    95  			return nil
    96  		}
    97  	}
    98  	return fmt.Errorf("Not a valid region: %s", c.Region)
    99  }