github.com/bengesoff/terraform@v0.3.1-0.20141018223233-b25a53629922/builtin/providers/aws/resource_provider.go (about)

     1  package aws
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/hashicorp/terraform/helper/config"
     7  	"github.com/hashicorp/terraform/helper/multierror"
     8  	"github.com/hashicorp/terraform/helper/schema"
     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  	// This is the schema.Provider. Eventually this will replace much
    29  	// of this structure. For now it is an element of it for compatiblity.
    30  	p *schema.Provider
    31  }
    32  
    33  func (p *ResourceProvider) Input(
    34  	input terraform.UIInput,
    35  	c *terraform.ResourceConfig) (*terraform.ResourceConfig, error) {
    36  	return Provider().Input(input, c)
    37  }
    38  
    39  func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) {
    40  	return Provider().Validate(c)
    41  }
    42  
    43  func (p *ResourceProvider) ValidateResource(
    44  	t string, c *terraform.ResourceConfig) ([]string, []error) {
    45  	prov := Provider()
    46  	if _, ok := prov.ResourcesMap[t]; ok {
    47  		return prov.ValidateResource(t, c)
    48  	}
    49  
    50  	return resourceMap.Validate(t, c)
    51  }
    52  
    53  func (p *ResourceProvider) Configure(c *terraform.ResourceConfig) error {
    54  	if _, err := config.Decode(&p.Config, c.Config); err != nil {
    55  		return err
    56  	}
    57  
    58  	// Get the auth and region. This can fail if keys/regions were not
    59  	// specified and we're attempting to use the environment.
    60  	var errs []error
    61  	log.Println("[INFO] Building AWS auth structure")
    62  	auth, err := p.Config.AWSAuth()
    63  	if err != nil {
    64  		errs = append(errs, err)
    65  	}
    66  
    67  	log.Println("[INFO] Building AWS region structure")
    68  	region, err := p.Config.AWSRegion()
    69  	if err != nil {
    70  		errs = append(errs, err)
    71  	}
    72  
    73  	if len(errs) == 0 {
    74  		log.Println("[INFO] Initializing EC2 connection")
    75  		p.ec2conn = ec2.New(auth, region)
    76  		log.Println("[INFO] Initializing ELB connection")
    77  		p.elbconn = elb.New(auth, region)
    78  		log.Println("[INFO] Initializing AutoScaling connection")
    79  		p.autoscalingconn = autoscaling.New(auth, region)
    80  		log.Println("[INFO] Initializing S3 connection")
    81  		p.s3conn = s3.New(auth, region)
    82  		log.Println("[INFO] Initializing RDS connection")
    83  		p.rdsconn = rds.New(auth, region)
    84  		log.Println("[INFO] Initializing Route53 connection")
    85  		p.route53 = route53.New(auth, region)
    86  	}
    87  
    88  	if len(errs) > 0 {
    89  		return &multierror.Error{Errors: errs}
    90  	}
    91  
    92  	// Create the provider, set the meta
    93  	p.p = Provider()
    94  	p.p.SetMeta(p)
    95  
    96  	return nil
    97  }
    98  
    99  func (p *ResourceProvider) Apply(
   100  	info *terraform.InstanceInfo,
   101  	s *terraform.InstanceState,
   102  	d *terraform.InstanceDiff) (*terraform.InstanceState, error) {
   103  	if _, ok := p.p.ResourcesMap[info.Type]; ok {
   104  		return p.p.Apply(info, s, d)
   105  	}
   106  
   107  	return resourceMap.Apply(info, s, d, p)
   108  }
   109  
   110  func (p *ResourceProvider) Diff(
   111  	info *terraform.InstanceInfo,
   112  	s *terraform.InstanceState,
   113  	c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
   114  	if _, ok := p.p.ResourcesMap[info.Type]; ok {
   115  		return p.p.Diff(info, s, c)
   116  	}
   117  
   118  	return resourceMap.Diff(info, s, c, p)
   119  }
   120  
   121  func (p *ResourceProvider) Refresh(
   122  	info *terraform.InstanceInfo,
   123  	s *terraform.InstanceState) (*terraform.InstanceState, error) {
   124  	if _, ok := p.p.ResourcesMap[info.Type]; ok {
   125  		return p.p.Refresh(info, s)
   126  	}
   127  
   128  	return resourceMap.Refresh(info, s, p)
   129  }
   130  
   131  func (p *ResourceProvider) Resources() []terraform.ResourceType {
   132  	result := resourceMap.Resources()
   133  	result = append(result, Provider().Resources()...)
   134  	return result
   135  }