github.com/ezbercih/terraform@v0.1.1-0.20140729011846-3c33865e0839/builtin/providers/aws/resource_provider.go (about) 1 package aws 2 3 import ( 4 "log" 5 "os" 6 7 "github.com/hashicorp/terraform/helper/config" 8 "github.com/hashicorp/terraform/helper/multierror" 9 "github.com/hashicorp/terraform/terraform" 10 "github.com/mitchellh/goamz/autoscaling" 11 "github.com/mitchellh/goamz/ec2" 12 "github.com/mitchellh/goamz/elb" 13 "github.com/mitchellh/goamz/rds" 14 "github.com/mitchellh/goamz/route53" 15 "github.com/mitchellh/goamz/s3" 16 ) 17 18 type ResourceProvider struct { 19 Config Config 20 21 ec2conn *ec2.EC2 22 elbconn *elb.ELB 23 autoscalingconn *autoscaling.AutoScaling 24 s3conn *s3.S3 25 rdsconn *rds.Rds 26 route53 *route53.Route53 27 } 28 29 func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) { 30 type param struct { 31 env string 32 key string 33 } 34 params := []param{ 35 {"AWS_REGION", "region"}, 36 {"AWS_ACCESS_KEY", "access_key"}, 37 {"AWS_SECRET_KEY", "secret_key"}, 38 } 39 40 var optional []string 41 var required []string 42 for _, p := range params { 43 if v := os.Getenv(p.env); v != "" { 44 optional = append(optional, p.key) 45 } else { 46 required = append(required, p.key) 47 } 48 } 49 50 v := &config.Validator{ 51 Required: required, 52 Optional: optional, 53 } 54 return v.Validate(c) 55 } 56 57 func (p *ResourceProvider) ValidateResource( 58 t string, c *terraform.ResourceConfig) ([]string, []error) { 59 return resourceMap.Validate(t, c) 60 } 61 62 func (p *ResourceProvider) Configure(c *terraform.ResourceConfig) error { 63 if _, err := config.Decode(&p.Config, c.Config); err != nil { 64 return err 65 } 66 67 // Get the auth and region. This can fail if keys/regions were not 68 // specified and we're attempting to use the environment. 69 var errs []error 70 log.Println("[INFO] Building AWS auth structure") 71 auth, err := p.Config.AWSAuth() 72 if err != nil { 73 errs = append(errs, err) 74 } 75 76 log.Println("[INFO] Building AWS region structure") 77 region, err := p.Config.AWSRegion() 78 if err != nil { 79 errs = append(errs, err) 80 } 81 82 if len(errs) == 0 { 83 log.Println("[INFO] Initializing EC2 connection") 84 p.ec2conn = ec2.New(auth, region) 85 log.Println("[INFO] Initializing ELB connection") 86 p.elbconn = elb.New(auth, region) 87 log.Println("[INFO] Initializing AutoScaling connection") 88 p.autoscalingconn = autoscaling.New(auth, region) 89 log.Println("[INFO] Initializing S3 connection") 90 p.s3conn = s3.New(auth, region) 91 log.Println("[INFO] Initializing RDS connection") 92 p.rdsconn = rds.New(auth, region) 93 log.Println("[INFO] Initializing Route53 connection") 94 p.route53 = route53.New(auth, region) 95 } 96 97 if len(errs) > 0 { 98 return &multierror.Error{Errors: errs} 99 } 100 101 return nil 102 } 103 104 func (p *ResourceProvider) Apply( 105 s *terraform.ResourceState, 106 d *terraform.ResourceDiff) (*terraform.ResourceState, error) { 107 return resourceMap.Apply(s, d, p) 108 } 109 110 func (p *ResourceProvider) Diff( 111 s *terraform.ResourceState, 112 c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) { 113 return resourceMap.Diff(s, c, p) 114 } 115 116 func (p *ResourceProvider) Refresh( 117 s *terraform.ResourceState) (*terraform.ResourceState, error) { 118 return resourceMap.Refresh(s, p) 119 } 120 121 func (p *ResourceProvider) Resources() []terraform.ResourceType { 122 return resourceMap.Resources() 123 }