2014-06-27 18:47:19 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-10-10 08:58:48 +02:00
|
|
|
"bytes"
|
2014-06-27 18:47:19 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-06-27 18:55:24 +02:00
|
|
|
"github.com/mitchellh/goamz/elb"
|
2014-06-27 18:47:19 +02:00
|
|
|
)
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
func resourceAwsElb() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsElbCreate,
|
|
|
|
Read: resourceAwsElbRead,
|
|
|
|
Update: resourceAwsElbUpdate,
|
|
|
|
Delete: resourceAwsElbDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
"internal": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2014-10-10 09:17:35 +02:00
|
|
|
Computed: true,
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
"availability_zones": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2014-07-03 00:55:28 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
"instances": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Optional: true,
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
// TODO: could be not ForceNew
|
|
|
|
"security_groups": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2014-10-10 09:17:35 +02:00
|
|
|
Computed: true,
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
2014-07-28 03:20:03 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
// TODO: could be not ForceNew
|
|
|
|
"subnets": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2014-10-10 09:17:35 +02:00
|
|
|
Computed: true,
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
2014-09-17 23:56:27 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
// TODO: could be not ForceNew
|
|
|
|
"listener": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"instance_port": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"instance_protocol": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"lb_port": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"lb_protocol": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ssl_certificate_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceAwsElbListenerHash,
|
|
|
|
},
|
2014-08-08 02:14:48 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
// TODO: could be not ForceNew
|
|
|
|
"health_check": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2014-10-10 09:17:35 +02:00
|
|
|
Computed: true,
|
2014-10-10 08:58:48 +02:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"healthy_threshold": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"unhealthy_threshold": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"target": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"interval": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"timeout": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceAwsElbHealthCheckHash,
|
|
|
|
},
|
|
|
|
|
|
|
|
"dns_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
2014-08-08 02:14:48 +02:00
|
|
|
}
|
2014-10-10 08:58:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsElbHealthCheckHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["healthy_threshold"].(int)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["unhealthy_threshold"].(int)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["target"].(string)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["interval"].(int)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["timeout"].(int)))
|
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsElbListenerHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["instance_port"].(int)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["instance_protocol"].(string)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["lb_port"].(int)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["lb_protocol"].(string)))
|
2014-08-08 02:14:48 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
if v, ok := m["ssl_certificate_id"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
|
2014-07-01 20:56:04 +02:00
|
|
|
}
|
2014-07-02 21:03:58 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsElbCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
elbconn := p.elbconn
|
2014-07-01 20:56:04 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
// Expand the "listener" set to goamz compat []elb.Listener
|
|
|
|
listeners, err := expandListeners(d.Get("listener").(*schema.Set).List())
|
2014-07-01 20:56:04 +02:00
|
|
|
if err != nil {
|
2014-10-10 08:58:48 +02:00
|
|
|
return err
|
2014-07-01 20:56:04 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
// Provision the elb
|
|
|
|
elbOpts := &elb.CreateLoadBalancer{
|
|
|
|
LoadBalancerName: d.Get("name").(string),
|
|
|
|
Listeners: listeners,
|
|
|
|
Internal: d.Get("internal").(bool),
|
|
|
|
}
|
2014-07-01 20:56:04 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
if v, ok := d.GetOk("availability_zones"); ok {
|
|
|
|
elbOpts.AvailZone = expandStringList(v.([]interface{}))
|
|
|
|
}
|
2014-07-05 01:03:01 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
if v, ok := d.GetOk("security_groups"); ok {
|
|
|
|
elbOpts.SecurityGroups = expandStringList(v.([]interface{}))
|
|
|
|
}
|
2014-07-05 01:03:01 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
if v, ok := d.GetOk("subnets"); ok {
|
|
|
|
elbOpts.Subnets = expandStringList(v.([]interface{}))
|
|
|
|
}
|
2014-07-05 01:03:01 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
log.Printf("[DEBUG] ELB create configuration: %#v", elbOpts)
|
|
|
|
if _, err := elbconn.CreateLoadBalancer(elbOpts); err != nil {
|
|
|
|
return fmt.Errorf("Error creating ELB: %s", err)
|
2014-07-05 01:03:01 +02:00
|
|
|
}
|
2014-07-30 16:15:22 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
// Assign the elb's unique identifier for use later
|
|
|
|
d.SetId(d.Get("name").(string))
|
|
|
|
log.Printf("[INFO] ELB ID: %s", d.Id())
|
|
|
|
|
|
|
|
// Enable partial mode and record what we set
|
|
|
|
d.Partial(true)
|
|
|
|
d.SetPartial("name")
|
|
|
|
d.SetPartial("internal")
|
|
|
|
d.SetPartial("availability_zones")
|
2014-10-17 03:02:03 +02:00
|
|
|
d.SetPartial("listener")
|
2014-10-10 08:58:48 +02:00
|
|
|
d.SetPartial("security_groups")
|
|
|
|
d.SetPartial("subnets")
|
|
|
|
|
2014-10-10 09:07:08 +02:00
|
|
|
if d.HasChange("health_check") {
|
|
|
|
vs := d.Get("health_check").(*schema.Set).List()
|
|
|
|
if len(vs) > 0 {
|
|
|
|
check := vs[0].(map[string]interface{})
|
2014-07-30 13:46:51 +02:00
|
|
|
|
2014-10-10 09:07:08 +02:00
|
|
|
configureHealthCheckOpts := elb.ConfigureHealthCheck{
|
|
|
|
LoadBalancerName: d.Id(),
|
|
|
|
Check: elb.HealthCheck{
|
|
|
|
HealthyThreshold: int64(check["healthy_threshold"].(int)),
|
|
|
|
UnhealthyThreshold: int64(check["unhealthy_threshold"].(int)),
|
|
|
|
Interval: int64(check["interval"].(int)),
|
|
|
|
Target: check["target"].(string),
|
|
|
|
Timeout: int64(check["timeout"].(int)),
|
|
|
|
},
|
|
|
|
}
|
2014-07-30 13:46:51 +02:00
|
|
|
|
2014-10-10 09:07:08 +02:00
|
|
|
_, err = elbconn.ConfigureHealthCheck(&configureHealthCheckOpts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failure configuring health check: %s", err)
|
|
|
|
}
|
2014-07-30 13:46:51 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-05 01:03:01 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
return resourceAwsElbUpdate(d, meta)
|
2014-06-27 18:47:19 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error {
|
2014-07-16 23:02:47 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
elbconn := p.elbconn
|
2014-07-03 07:40:55 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
d.Partial(true)
|
2014-07-07 20:03:29 +02:00
|
|
|
|
2014-07-16 23:02:47 +02:00
|
|
|
// If we currently have instances, or did have instances,
|
|
|
|
// we want to figure out what to add and remove from the load
|
|
|
|
// balancer
|
2014-10-10 08:58:48 +02:00
|
|
|
if d.HasChange("instances") {
|
|
|
|
o, n := d.GetChange("instances")
|
|
|
|
os := o.(*schema.Set)
|
|
|
|
ns := n.(*schema.Set)
|
|
|
|
remove := expandStringList(os.Difference(ns).List())
|
|
|
|
add := expandStringList(ns.Difference(os).List())
|
|
|
|
|
|
|
|
if len(add) > 0 {
|
2014-07-16 23:02:47 +02:00
|
|
|
registerInstancesOpts := elb.RegisterInstancesWithLoadBalancer{
|
2014-10-10 08:58:48 +02:00
|
|
|
LoadBalancerName: d.Id(),
|
|
|
|
Instances: add,
|
2014-07-16 23:02:47 +02:00
|
|
|
}
|
2014-07-07 20:03:29 +02:00
|
|
|
|
2014-07-16 23:02:47 +02:00
|
|
|
_, err := elbconn.RegisterInstancesWithLoadBalancer(®isterInstancesOpts)
|
|
|
|
if err != nil {
|
2014-10-10 08:58:48 +02:00
|
|
|
return fmt.Errorf("Failure registering instances: %s", err)
|
2014-07-16 23:02:47 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-10 08:58:48 +02:00
|
|
|
if len(remove) > 0 {
|
2014-07-16 23:02:47 +02:00
|
|
|
deRegisterInstancesOpts := elb.DeregisterInstancesFromLoadBalancer{
|
2014-10-10 08:58:48 +02:00
|
|
|
LoadBalancerName: d.Id(),
|
|
|
|
Instances: remove,
|
2014-07-16 23:02:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := elbconn.DeregisterInstancesFromLoadBalancer(&deRegisterInstancesOpts)
|
|
|
|
if err != nil {
|
2014-10-10 08:58:48 +02:00
|
|
|
return fmt.Errorf("Failure deregistering instances: %s", err)
|
2014-07-16 23:02:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
d.SetPartial("instances")
|
2014-07-16 23:02:47 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
d.Partial(false)
|
2014-10-10 22:50:08 +02:00
|
|
|
return resourceAwsElbRead(d, meta)
|
2014-07-03 07:40:55 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
func resourceAwsElbDelete(d *schema.ResourceData, meta interface{}) error {
|
2014-07-07 20:03:29 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
elbconn := p.elbconn
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
log.Printf("[INFO] Deleting ELB: %s", d.Id())
|
2014-07-07 20:03:29 +02:00
|
|
|
|
|
|
|
// Destroy the load balancer
|
|
|
|
deleteElbOpts := elb.DeleteLoadBalancer{
|
2014-10-10 08:58:48 +02:00
|
|
|
LoadBalancerName: d.Id(),
|
2014-07-07 20:03:29 +02:00
|
|
|
}
|
2014-10-10 08:58:48 +02:00
|
|
|
if _, err := elbconn.DeleteLoadBalancer(&deleteElbOpts); err != nil {
|
2014-07-07 20:03:29 +02:00
|
|
|
return fmt.Errorf("Error deleting ELB: %s", err)
|
|
|
|
}
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-07-01 20:56:04 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error {
|
2014-07-07 23:31:27 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
elbconn := p.elbconn
|
2014-07-01 20:56:04 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
// Retrieve the ELB properties for updating the state
|
|
|
|
describeElbOpts := &elb.DescribeLoadBalancer{
|
|
|
|
Names: []string{d.Id()},
|
2014-07-01 20:56:04 +02:00
|
|
|
}
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
describeResp, err := elbconn.DescribeLoadBalancers(describeElbOpts)
|
|
|
|
if err != nil {
|
2014-10-10 19:35:34 +02:00
|
|
|
if ec2err, ok := err.(*elb.Error); ok && ec2err.Code == "LoadBalancerNotFound" {
|
|
|
|
// The ELB is gone now, so just remove it from the state
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
return fmt.Errorf("Error retrieving ELB: %s", err)
|
2014-09-30 22:37:04 +02:00
|
|
|
}
|
2014-10-10 08:58:48 +02:00
|
|
|
if len(describeResp.LoadBalancers) != 1 {
|
|
|
|
return fmt.Errorf("Unable to find ELB: %#v", describeResp.LoadBalancers)
|
2014-07-30 16:15:22 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
lb := describeResp.LoadBalancers[0]
|
2014-08-08 02:14:48 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
d.Set("name", lb.LoadBalancerName)
|
|
|
|
d.Set("dns_name", lb.DNSName)
|
|
|
|
d.Set("internal", lb.Scheme == "internal")
|
|
|
|
d.Set("instances", flattenInstances(lb.Instances))
|
2014-10-11 01:35:52 +02:00
|
|
|
d.Set("listener", flattenListeners(lb.Listeners))
|
2014-10-10 08:58:48 +02:00
|
|
|
d.Set("security_groups", lb.SecurityGroups)
|
|
|
|
d.Set("subnets", lb.Subnets)
|
2014-08-08 02:14:48 +02:00
|
|
|
|
2014-07-30 16:15:22 +02:00
|
|
|
// There's only one health check, so save that to state as we
|
|
|
|
// currently can
|
2014-10-10 09:07:08 +02:00
|
|
|
if lb.HealthCheck.Target != "" {
|
|
|
|
d.Set("health_check", flattenHealthCheck(lb.HealthCheck))
|
2014-07-30 16:15:22 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
return nil
|
2014-07-15 18:18:36 +02:00
|
|
|
}
|