2014-05-30 02:28:38 +02:00
|
|
|
package aws
|
|
|
|
|
2014-06-03 23:26:31 +02:00
|
|
|
import (
|
2014-06-24 04:01:57 +02:00
|
|
|
"log"
|
2014-07-23 21:13:36 +02:00
|
|
|
"os"
|
2014-06-18 18:33:13 +02:00
|
|
|
|
2014-06-24 04:01:57 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/config"
|
2014-07-03 22:06:24 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/multierror"
|
2014-08-20 00:40:48 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-06-03 23:26:31 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-07-10 01:00:11 +02:00
|
|
|
"github.com/mitchellh/goamz/autoscaling"
|
2014-06-24 04:01:57 +02:00
|
|
|
"github.com/mitchellh/goamz/ec2"
|
2014-06-27 18:55:24 +02:00
|
|
|
"github.com/mitchellh/goamz/elb"
|
2014-07-22 22:26:48 +02:00
|
|
|
"github.com/mitchellh/goamz/rds"
|
2014-07-22 23:55:19 +02:00
|
|
|
"github.com/mitchellh/goamz/route53"
|
2014-07-21 20:34:47 +02:00
|
|
|
"github.com/mitchellh/goamz/s3"
|
2014-06-03 23:26:31 +02:00
|
|
|
)
|
|
|
|
|
2014-05-30 02:28:38 +02:00
|
|
|
type ResourceProvider struct {
|
2014-06-24 04:01:57 +02:00
|
|
|
Config Config
|
|
|
|
|
2014-07-10 01:00:11 +02:00
|
|
|
ec2conn *ec2.EC2
|
|
|
|
elbconn *elb.ELB
|
|
|
|
autoscalingconn *autoscaling.AutoScaling
|
2014-07-21 20:34:47 +02:00
|
|
|
s3conn *s3.S3
|
2014-07-22 22:26:48 +02:00
|
|
|
rdsconn *rds.Rds
|
2014-07-22 23:55:19 +02:00
|
|
|
route53 *route53.Route53
|
2014-08-20 00:40:48 +02:00
|
|
|
|
|
|
|
// This is the schema.Provider. Eventually this will replace much
|
|
|
|
// of this structure. For now it is an element of it for compatiblity.
|
|
|
|
p *schema.Provider
|
2014-05-30 02:28:38 +02:00
|
|
|
}
|
|
|
|
|
2014-06-13 08:08:47 +02:00
|
|
|
func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) {
|
2014-07-23 21:13:36 +02:00
|
|
|
type param struct {
|
|
|
|
env string
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
params := []param{
|
|
|
|
{"AWS_REGION", "region"},
|
|
|
|
{"AWS_ACCESS_KEY", "access_key"},
|
|
|
|
{"AWS_SECRET_KEY", "secret_key"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var optional []string
|
|
|
|
var required []string
|
|
|
|
for _, p := range params {
|
|
|
|
if v := os.Getenv(p.env); v != "" {
|
|
|
|
optional = append(optional, p.key)
|
|
|
|
} else {
|
|
|
|
required = append(required, p.key)
|
|
|
|
}
|
2014-07-10 20:31:16 +02:00
|
|
|
}
|
|
|
|
|
2014-07-23 21:13:36 +02:00
|
|
|
v := &config.Validator{
|
|
|
|
Required: required,
|
|
|
|
Optional: optional,
|
|
|
|
}
|
2014-07-10 20:31:16 +02:00
|
|
|
return v.Validate(c)
|
2014-06-13 07:39:29 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 22:06:24 +02:00
|
|
|
func (p *ResourceProvider) ValidateResource(
|
|
|
|
t string, c *terraform.ResourceConfig) ([]string, []error) {
|
2014-08-20 01:56:23 +02:00
|
|
|
prov := Provider()
|
|
|
|
if _, ok := prov.ResourcesMap[t]; ok {
|
|
|
|
return prov.ValidateResource(t, c)
|
|
|
|
}
|
|
|
|
|
2014-07-08 19:18:39 +02:00
|
|
|
return resourceMap.Validate(t, c)
|
2014-07-03 22:06:24 +02:00
|
|
|
}
|
|
|
|
|
2014-06-24 04:01:57 +02:00
|
|
|
func (p *ResourceProvider) Configure(c *terraform.ResourceConfig) error {
|
|
|
|
if _, err := config.Decode(&p.Config, c.Config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the auth and region. This can fail if keys/regions were not
|
|
|
|
// specified and we're attempting to use the environment.
|
|
|
|
var errs []error
|
2014-07-10 20:31:16 +02:00
|
|
|
log.Println("[INFO] Building AWS auth structure")
|
2014-06-24 04:01:57 +02:00
|
|
|
auth, err := p.Config.AWSAuth()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
2014-07-10 20:31:16 +02:00
|
|
|
log.Println("[INFO] Building AWS region structure")
|
2014-06-24 04:01:57 +02:00
|
|
|
region, err := p.Config.AWSRegion()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) == 0 {
|
2014-07-10 20:31:16 +02:00
|
|
|
log.Println("[INFO] Initializing EC2 connection")
|
2014-06-24 04:01:57 +02:00
|
|
|
p.ec2conn = ec2.New(auth, region)
|
2014-07-10 20:31:16 +02:00
|
|
|
log.Println("[INFO] Initializing ELB connection")
|
2014-06-27 18:47:19 +02:00
|
|
|
p.elbconn = elb.New(auth, region)
|
2014-07-10 20:31:16 +02:00
|
|
|
log.Println("[INFO] Initializing AutoScaling connection")
|
2014-07-10 01:00:11 +02:00
|
|
|
p.autoscalingconn = autoscaling.New(auth, region)
|
2014-07-21 20:34:47 +02:00
|
|
|
log.Println("[INFO] Initializing S3 connection")
|
|
|
|
p.s3conn = s3.New(auth, region)
|
2014-07-22 22:26:48 +02:00
|
|
|
log.Println("[INFO] Initializing RDS connection")
|
|
|
|
p.rdsconn = rds.New(auth, region)
|
2014-07-22 23:55:19 +02:00
|
|
|
log.Println("[INFO] Initializing Route53 connection")
|
|
|
|
p.route53 = route53.New(auth, region)
|
2014-06-24 04:01:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
2014-07-03 22:06:24 +02:00
|
|
|
return &multierror.Error{Errors: errs}
|
2014-06-24 04:01:57 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 00:40:48 +02:00
|
|
|
// Create the provider, set the meta
|
|
|
|
p.p = Provider()
|
|
|
|
p.p.SetMeta(p)
|
|
|
|
|
2014-06-07 05:17:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-19 01:52:21 +02:00
|
|
|
func (p *ResourceProvider) Apply(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
d *terraform.ResourceDiff) (*terraform.ResourceState, error) {
|
2014-08-20 00:40:48 +02:00
|
|
|
if _, ok := p.p.ResourcesMap[s.Type]; ok {
|
|
|
|
return p.p.Apply(s, d)
|
|
|
|
}
|
|
|
|
|
2014-06-24 04:32:49 +02:00
|
|
|
return resourceMap.Apply(s, d, p)
|
2014-06-19 01:52:21 +02:00
|
|
|
}
|
|
|
|
|
2014-06-07 05:17:38 +02:00
|
|
|
func (p *ResourceProvider) Diff(
|
|
|
|
s *terraform.ResourceState,
|
2014-06-13 07:15:36 +02:00
|
|
|
c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) {
|
2014-08-20 00:40:48 +02:00
|
|
|
if _, ok := p.p.ResourcesMap[s.Type]; ok {
|
|
|
|
return p.p.Diff(s, c)
|
|
|
|
}
|
|
|
|
|
2014-06-24 19:27:39 +02:00
|
|
|
return resourceMap.Diff(s, c, p)
|
2014-05-30 02:28:38 +02:00
|
|
|
}
|
2014-06-03 23:26:31 +02:00
|
|
|
|
2014-06-20 20:52:21 +02:00
|
|
|
func (p *ResourceProvider) Refresh(
|
|
|
|
s *terraform.ResourceState) (*terraform.ResourceState, error) {
|
2014-08-20 00:40:48 +02:00
|
|
|
if _, ok := p.p.ResourcesMap[s.Type]; ok {
|
|
|
|
return p.p.Refresh(s)
|
|
|
|
}
|
|
|
|
|
2014-06-24 04:32:49 +02:00
|
|
|
return resourceMap.Refresh(s, p)
|
2014-06-20 20:52:21 +02:00
|
|
|
}
|
|
|
|
|
2014-06-03 23:26:31 +02:00
|
|
|
func (p *ResourceProvider) Resources() []terraform.ResourceType {
|
2014-08-20 00:40:48 +02:00
|
|
|
result := resourceMap.Resources()
|
2014-08-20 01:56:23 +02:00
|
|
|
result = append(result, Provider().Resources()...)
|
2014-08-20 00:40:48 +02:00
|
|
|
return result
|
2014-06-03 23:26:31 +02:00
|
|
|
}
|