2014-06-27 18:47:19 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/diff"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-06-27 18:55:24 +02:00
|
|
|
"github.com/mitchellh/goamz/elb"
|
2014-06-27 18:47:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func resource_aws_elb_create(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
d *terraform.ResourceDiff,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
elbconn := p.elbconn
|
|
|
|
|
|
|
|
// Merge the diff into the state so that we have all the attributes
|
|
|
|
// properly.
|
|
|
|
rs := s.MergeDiff(d)
|
|
|
|
|
2014-07-01 20:56:04 +02:00
|
|
|
// The name specified for the ELB. This is also our unique ID
|
|
|
|
// we save to state if the creation is succesful (amazon verifies
|
|
|
|
// it is unique)
|
|
|
|
elbName := rs.Attributes["name"]
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-07-01 20:56:04 +02:00
|
|
|
// Provision the elb
|
|
|
|
elbOpts := &elb.CreateLoadBalancer{
|
|
|
|
LoadBalancerName: elbName,
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] ELB create configuration: %#v", elbOpts)
|
|
|
|
|
|
|
|
_, err := elbconn.CreateLoadBalancer(elbOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error creating ELB: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign the elb's unique identifer for use later
|
|
|
|
rs.ID = elbName
|
|
|
|
log.Printf("[INFO] ELB ID: %s", elbName)
|
|
|
|
|
|
|
|
// Filter by our name
|
|
|
|
describeElbOpts := &elb.DescribeLoadBalancer{
|
|
|
|
Names: []string{elbName},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the ELB properties for updating the state
|
|
|
|
describeResp, err := elbconn.DescribeLoadBalancers(describeElbOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error retrieving ELB: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify AWS returned our ELB
|
|
|
|
if len(describeResp.LoadBalancers) != 1 ||
|
|
|
|
describeResp.LoadBalancers[0].LoadBalancerName != elbName {
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to find ELB: %#v", describeResp.LoadBalancers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loadBalancer := describeResp.LoadBalancers[0]
|
|
|
|
|
|
|
|
return resource_aws_elb_update_state(rs, &loadBalancer)
|
2014-06-27 18:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_elb_destroy(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
meta interface{}) error {
|
|
|
|
|
2014-07-01 20:56:04 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_elb_refresh(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
|
|
|
|
|
|
|
loadBalancer := &elb.LoadBalancer{}
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-07-01 20:56:04 +02:00
|
|
|
return resource_aws_elb_update_state(s, loadBalancer)
|
2014-06-27 18:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_elb_diff(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
c *terraform.ResourceConfig,
|
|
|
|
meta interface{}) (*terraform.ResourceDiff, error) {
|
|
|
|
|
2014-07-01 20:56:04 +02:00
|
|
|
b := &diff.ResourceBuilder{
|
|
|
|
CreateComputedAttrs: []string{
|
|
|
|
"dns_name",
|
|
|
|
},
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-07-01 20:56:04 +02:00
|
|
|
RequiresNewAttrs: []string{
|
|
|
|
"name",
|
|
|
|
},
|
|
|
|
}
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-07-01 20:56:04 +02:00
|
|
|
return b.Diff(s, c)
|
|
|
|
}
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-07-01 20:56:04 +02:00
|
|
|
func resource_aws_elb_update_state(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
balancer *elb.LoadBalancer) (*terraform.ResourceState, error) {
|
|
|
|
s.Attributes["name"] = balancer.LoadBalancerName
|
|
|
|
s.Attributes["dns_name"] = balancer.DNSName
|
|
|
|
return s, nil
|
2014-06-27 18:47:19 +02:00
|
|
|
}
|