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