Skip to content

Commit

Permalink
WIP Add top-level ELB Attachment resource
Browse files Browse the repository at this point in the history
Add an aws_elb_attachment resource so that the attment of instances to
an ELB can be managed separately from an aws_elb and prevent dependency
cycles.
  • Loading branch information
jbardin committed May 25, 2016
1 parent f891ab8 commit 7565125
Show file tree
Hide file tree
Showing 3 changed files with 304 additions and 0 deletions.
1 change: 1 addition & 0 deletions builtin/providers/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func Provider() terraform.ResourceProvider {
"aws_elastic_beanstalk_environment": resourceAwsElasticBeanstalkEnvironment(),
"aws_elasticsearch_domain": resourceAwsElasticSearchDomain(),
"aws_elb": resourceAwsElb(),
"aws_elb_attachment": resourceAwsElbAttachment(),
"aws_flow_log": resourceAwsFlowLog(),
"aws_glacier_vault": resourceAwsGlacierVault(),
"aws_iam_access_key": resourceAwsIamAccessKey(),
Expand Down
142 changes: 142 additions & 0 deletions builtin/providers/aws/resource_aws_elb_attachment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsElbAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceAwsElbAttachmentCreate,
Read: resourceAwsElbAttachmentRead,
Update: resourceAwsElbAttachmentUpdate,
Delete: resourceAwsElbAttachmentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"elb": &schema.Schema{
Type: schema.TypeString,
Required: true,
},

"instances": &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Required: true,
},
},
}
}

func resourceAwsElbAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn
elbName := d.Get("elb").(string)

instances := expandInstanceString(d.Get("instances").(*schema.Set).List())

registerInstancesOpts := elb.RegisterInstancesWithLoadBalancerInput{
LoadBalancerName: aws.String(elbName),
Instances: instances,
}

_, err := elbconn.RegisterInstancesWithLoadBalancer(&registerInstancesOpts)
if err != nil {
return fmt.Errorf("Failure registering instances with ELB: %s", err)
}

d.SetId(resource.PrefixedUniqueId(fmt.Sprintf("%s-", elbName)))

return nil
}

func resourceAwsElbAttachmentRead(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn
elbName := d.Get("elb").(string)

// Retrieve the ELB properties to get a list of attachments
describeElbOpts := &elb.DescribeLoadBalancersInput{
LoadBalancerNames: []*string{aws.String(elbName)},
}

describeResp, err := elbconn.DescribeLoadBalancers(describeElbOpts)
if err != nil {
return fmt.Errorf("Error retrieving ELB: %s", err)
}
if len(describeResp.LoadBalancerDescriptions) != 1 {
return fmt.Errorf("Unable to find ELB: %#v", describeResp.LoadBalancerDescriptions)
}

lb := describeResp.LoadBalancerDescriptions[0]

d.Set("instances", flattenInstances(lb.Instances))

return nil
}

func resourceAwsElbAttachmentUpdate(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn
elbName := d.Get("elb").(string)

if d.HasChange("instances") {
o, n := d.GetChange("instances")
os := o.(*schema.Set)
ns := n.(*schema.Set)
remove := expandInstanceString(os.Difference(ns).List())
add := expandInstanceString(ns.Difference(os).List())

if len(add) > 0 {
registerInstancesOpts := elb.RegisterInstancesWithLoadBalancerInput{
LoadBalancerName: aws.String(elbName),
Instances: add,
}

_, err := elbconn.RegisterInstancesWithLoadBalancer(&registerInstancesOpts)
if err != nil {
return fmt.Errorf("Failure registering instances with ELB: %s", err)
}
}
if len(remove) > 0 {
deRegisterInstancesOpts := elb.DeregisterInstancesFromLoadBalancerInput{
LoadBalancerName: aws.String(elbName),
Instances: remove,
}

_, err := elbconn.DeregisterInstancesFromLoadBalancer(&deRegisterInstancesOpts)
if err != nil {
return fmt.Errorf("Failure deregistering instances from ELB: %s", err)
}
}
}

return resourceAwsElbAttachmentRead(d, meta)
}

func resourceAwsElbAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn
elbName := d.Get("elb").(string)

instanceSet := d.Get("instances").(*schema.Set)
instances := expandInstanceString(instanceSet.List())

log.Printf("[INFO] Deleting Attachments %s from: %s", instanceSet, elbName)

deRegisterInstancesOpts := elb.DeregisterInstancesFromLoadBalancerInput{
LoadBalancerName: aws.String(elbName),
Instances: instances,
}

_, err := elbconn.DeregisterInstancesFromLoadBalancer(&deRegisterInstancesOpts)
if err != nil {
return fmt.Errorf("Failure deregistering instances from ELB: %s", err)
}

return nil
}
161 changes: 161 additions & 0 deletions builtin/providers/aws/resource_aws_elb_attachment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package aws

import (
"fmt"
"reflect"
"sort"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSELBAttachment(t *testing.T) {
var conf elb.LoadBalancerDescription

testCheckInstanceAttached := func(count int) resource.TestCheckFunc {
return func(*terraform.State) error {
if len(conf.Instances) != count {
return fmt.Errorf("instance count does not match")
}
return nil
}
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_elb.bar",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSELBDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSELBAttachmentConfig1,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSELBExists("aws_elb.bar", &conf),
testCheckInstanceAttached(1),
),
},

resource.TestStep{
Config: testAccAWSELBAttachmentConfig2,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSELBExists("aws_elb.bar", &conf),
testCheckInstanceAttached(2),
),
},

resource.TestStep{
Config: testAccAWSELBAttachmentConfig3,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSELBExists("aws_elb.bar", &conf),
testCheckInstanceAttached(0),
),
},
},
})

}

func testAccCheckAWSELBAttachments(conf *elb.LoadBalancerDescription) resource.TestCheckFunc {
return func(s *terraform.State) error {
zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"}
azs := make([]string, 0, len(conf.AvailabilityZones))
for _, x := range conf.AvailabilityZones {
azs = append(azs, *x)
}
sort.StringSlice(azs).Sort()
if !reflect.DeepEqual(azs, zones) {
return fmt.Errorf("bad availability_zones")
}

l := elb.Listener{
InstancePort: aws.Int64(int64(8000)),
InstanceProtocol: aws.String("HTTP"),
LoadBalancerPort: aws.Int64(int64(80)),
Protocol: aws.String("HTTP"),
}

if !reflect.DeepEqual(conf.ListenerDescriptions[0].Listener, &l) {
return fmt.Errorf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
conf.ListenerDescriptions[0].Listener,
l)
}

if *conf.DNSName == "" {
return fmt.Errorf("empty dns_name")
}

return nil
}

}

const testAccAWSELBAttachmentConfig1 = `
resource "aws_elb" "bar" {
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
listener {
instance_port = 8000
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
}
resource "aws_instance" "foo" {
# us-west-2
ami = "ami-043a5034"
instance_type = "t1.micro"
}
resource "aws_elb_attachment" "app" {
elb = "${aws_elb.bar.id}"
instances = ["${aws_instance.foo.id}"]
}
`

const testAccAWSELBAttachmentConfig2 = `
resource "aws_elb" "bar" {
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
listener {
instance_port = 8000
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
}
resource "aws_instance" "foo" {
# us-west-2
ami = "ami-043a5034"
instance_type = "t1.micro"
}
resource "aws_instance" "foo2" {
# us-west-2
ami = "ami-043a5034"
instance_type = "t1.micro"
}
resource "aws_elb_attachment" "app" {
elb = "${aws_elb.bar.id}"
instances = ["${aws_instance.foo.id}", "${aws_instance.foo2.id}"]
}
`

const testAccAWSELBAttachmentConfig3 = `
resource "aws_elb" "bar" {
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
listener {
instance_port = 8000
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
}
`

0 comments on commit 7565125

Please sign in to comment.