github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/config.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  	"unicode"
     8  
     9  	"github.com/hashicorp/terraform/helper/multierror"
    10  	"github.com/mitchellh/goamz/autoscaling"
    11  	"github.com/mitchellh/goamz/aws"
    12  	"github.com/mitchellh/goamz/ec2"
    13  	"github.com/mitchellh/goamz/elb"
    14  	"github.com/mitchellh/goamz/rds"
    15  	"github.com/mitchellh/goamz/route53"
    16  	"github.com/mitchellh/goamz/s3"
    17  )
    18  
    19  type Config struct {
    20  	AccessKey string
    21  	SecretKey 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  	rdsconn         *rds.Rds
    31  	route53         *route53.Route53
    32  }
    33  
    34  // Client configures and returns a fully initailized AWSClient
    35  func (c *Config) Client() (interface{}, error) {
    36  	var client AWSClient
    37  
    38  	// Get the auth and region. This can fail if keys/regions were not
    39  	// specified and we're attempting to use the environment.
    40  	var errs []error
    41  	log.Println("[INFO] Building AWS auth structure")
    42  	auth, err := c.AWSAuth()
    43  	if err != nil {
    44  		errs = append(errs, err)
    45  	}
    46  
    47  	log.Println("[INFO] Building AWS region structure")
    48  	region, err := c.AWSRegion()
    49  	if err != nil {
    50  		errs = append(errs, err)
    51  	}
    52  
    53  	if len(errs) == 0 {
    54  		log.Println("[INFO] Initializing EC2 connection")
    55  		client.ec2conn = ec2.New(auth, region)
    56  		log.Println("[INFO] Initializing ELB connection")
    57  		client.elbconn = elb.New(auth, region)
    58  		log.Println("[INFO] Initializing AutoScaling connection")
    59  		client.autoscalingconn = autoscaling.New(auth, region)
    60  		log.Println("[INFO] Initializing S3 connection")
    61  		client.s3conn = s3.New(auth, region)
    62  		log.Println("[INFO] Initializing RDS connection")
    63  		client.rdsconn = rds.New(auth, region)
    64  		log.Println("[INFO] Initializing Route53 connection")
    65  		client.route53 = route53.New(auth, region)
    66  	}
    67  
    68  	if len(errs) > 0 {
    69  		return nil, &multierror.Error{Errors: errs}
    70  	}
    71  
    72  	return &client, nil
    73  }
    74  
    75  // AWSAuth returns a valid aws.Auth object for access to AWS services, or
    76  // an error if the authentication couldn't be resolved.
    77  //
    78  // TODO(mitchellh): Test in some way.
    79  func (c *Config) AWSAuth() (aws.Auth, error) {
    80  	auth, err := aws.GetAuth(c.AccessKey, c.SecretKey)
    81  	if err == nil {
    82  		// Store the accesskey and secret that we got...
    83  		c.AccessKey = auth.AccessKey
    84  		c.SecretKey = auth.SecretKey
    85  	}
    86  
    87  	return auth, err
    88  }
    89  
    90  // IsValidRegion returns true if the configured region is a valid AWS
    91  // region and false if it's not
    92  func (c *Config) IsValidRegion() bool {
    93  	var regions = [11]string{"us-east-1", "us-west-2", "us-west-1", "eu-west-1",
    94  		"eu-central-1", "ap-southeast-1", "ap-southeast-2", "ap-northeast-1",
    95  		"sa-east-1", "cn-north-1", "us-gov-west-1"}
    96  
    97  	for _, valid := range regions {
    98  		if c.Region == valid {
    99  			return true
   100  		}
   101  	}
   102  	return false
   103  }
   104  
   105  // AWSRegion returns the configured region.
   106  //
   107  // TODO(mitchellh): Test in some way.
   108  func (c *Config) AWSRegion() (aws.Region, error) {
   109  	if c.Region != "" {
   110  		if c.IsValidRegion() {
   111  			return aws.Regions[c.Region], nil
   112  		} else {
   113  			return aws.Region{}, fmt.Errorf("Not a valid region: %s", c.Region)
   114  		}
   115  	}
   116  
   117  	md, err := aws.GetMetaData("placement/availability-zone")
   118  	if err != nil {
   119  		return aws.Region{}, err
   120  	}
   121  
   122  	region := strings.TrimRightFunc(string(md), unicode.IsLetter)
   123  	return aws.Regions[region], nil
   124  }