terraform/builtin/providers/aws/resource_provider.go

103 lines
2.2 KiB
Go
Raw Normal View History

2014-05-30 02:28:38 +02:00
package aws
import (
2014-06-18 18:33:13 +02:00
"fmt"
2014-06-24 04:01:57 +02:00
"log"
2014-06-18 18:33:13 +02:00
2014-06-24 04:01:57 +02:00
"github.com/hashicorp/terraform/helper/config"
"github.com/hashicorp/terraform/terraform"
2014-06-24 04:01:57 +02:00
"github.com/mitchellh/goamz/ec2"
)
2014-05-30 02:28:38 +02:00
type ResourceProvider struct {
2014-06-24 04:01:57 +02:00
Config Config
ec2conn *ec2.EC2
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-06-24 04:01:57 +02:00
return nil, nil
2014-06-13 07:39:29 +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
log.Println("Building AWS auth structure")
auth, err := p.Config.AWSAuth()
if err != nil {
errs = append(errs, err)
}
log.Println("Building AWS region structure")
region, err := p.Config.AWSRegion()
if err != nil {
errs = append(errs, err)
}
if len(errs) == 0 {
log.Println("Initializing EC2 connection")
p.ec2conn = ec2.New(auth, region)
}
if len(errs) > 0 {
return &terraform.MultiError{Errors: errs}
}
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) {
result := &terraform.ResourceState{
ID: "foo",
}
2014-06-19 23:08:48 +02:00
result = result.MergeDiff(d)
result.Attributes["public_dns"] = "foo"
result.Attributes["public_ip"] = "foo"
result.Attributes["private_dns"] = "foo"
result.Attributes["private_ip"] = "foo"
2014-06-19 01:52:21 +02:00
return result, nil
}
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-06-18 18:33:13 +02:00
b := diffMap.Get(s.Type)
if b == nil {
return nil, fmt.Errorf("Unknown type: %s", s.Type)
}
return b.Diff(s, c)
2014-05-30 02:28:38 +02:00
}
2014-06-20 20:52:21 +02:00
func (p *ResourceProvider) Refresh(
s *terraform.ResourceState) (*terraform.ResourceState, error) {
2014-06-24 04:01:57 +02:00
// If there isn't an ID previously, then the thing didn't exist,
// so there is nothing to refresh.
if s.ID == "" {
return s, nil
}
f, ok := refreshMap[s.Type]
if !ok {
return s, fmt.Errorf("Unknown resource type: %s", s.Type)
}
return f(p, s)
2014-06-20 20:52:21 +02:00
}
func (p *ResourceProvider) Resources() []terraform.ResourceType {
2014-06-10 20:34:14 +02:00
return []terraform.ResourceType{
terraform.ResourceType{
Name: "aws_instance",
},
}
}