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