forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/aws: aws_autoscaling_attachment resource (hashicorp#9146)
* hashicorpGH-8755 - Adding in support to attach ASG to ELB as independent action * hashicorpGH-8755 - Adding in docs * hashicorpGH-8755 - Adjusting attribute name and responding to other PR feedback
- Loading branch information
Showing
6 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
builtin/providers/aws/resource_aws_autoscaling_attachment.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/autoscaling" | ||
"github.com/hashicorp/errwrap" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsAutoscalingAttachment() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsAutoscalingAttachmentCreate, | ||
Read: resourceAwsAutoscalingAttachmentRead, | ||
Delete: resourceAwsAutoscalingAttachmentDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"autoscaling_group_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
ForceNew: true, | ||
Required: true, | ||
}, | ||
|
||
"elb": &schema.Schema{ | ||
Type: schema.TypeString, | ||
ForceNew: true, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsAutoscalingAttachmentCreate(d *schema.ResourceData, meta interface{}) error { | ||
asgconn := meta.(*AWSClient).autoscalingconn | ||
asgName := d.Get("autoscaling_group_name").(string) | ||
elbName := d.Get("elb").(string) | ||
|
||
attachElbInput := &autoscaling.AttachLoadBalancersInput{ | ||
AutoScalingGroupName: aws.String(asgName), | ||
LoadBalancerNames: []*string{aws.String(elbName)}, | ||
} | ||
|
||
log.Printf("[INFO] registering asg %s with ELBs %s", asgName, elbName) | ||
|
||
if _, err := asgconn.AttachLoadBalancers(attachElbInput); err != nil { | ||
return errwrap.Wrapf(fmt.Sprintf("Failure attaching AutoScaling Group %s with Elastic Load Balancer: %s: {{err}}", asgName, elbName), err) | ||
} | ||
|
||
d.SetId(resource.PrefixedUniqueId(fmt.Sprintf("%s-", asgName))) | ||
|
||
return resourceAwsAutoscalingAttachmentRead(d, meta) | ||
} | ||
|
||
func resourceAwsAutoscalingAttachmentRead(d *schema.ResourceData, meta interface{}) error { | ||
asgconn := meta.(*AWSClient).autoscalingconn | ||
asgName := d.Get("autoscaling_group_name").(string) | ||
elbName := d.Get("elb").(string) | ||
|
||
// Retrieve the ASG properites to get list of associated ELBs | ||
asg, err := getAwsAutoscalingGroup(asgName, asgconn) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
if asg == nil { | ||
log.Printf("[INFO] Autoscaling Group %q not found", asgName) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
found := false | ||
for _, i := range asg.LoadBalancerNames { | ||
if elbName == *i { | ||
d.Set("elb", elbName) | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
log.Printf("[WARN] Association for %s was not found in ASG assocation", elbName) | ||
d.SetId("") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsAutoscalingAttachmentDelete(d *schema.ResourceData, meta interface{}) error { | ||
asgconn := meta.(*AWSClient).autoscalingconn | ||
asgName := d.Get("autoscaling_group_name").(string) | ||
elbName := d.Get("elb").(string) | ||
|
||
log.Printf("[INFO] Deleting ELB %s association from: %s", elbName, asgName) | ||
|
||
detachOpts := &autoscaling.DetachLoadBalancersInput{ | ||
AutoScalingGroupName: aws.String(asgName), | ||
LoadBalancerNames: []*string{aws.String(elbName)}, | ||
} | ||
|
||
if _, err := asgconn.DetachLoadBalancers(detachOpts); err != nil { | ||
return errwrap.Wrapf(fmt.Sprintf("Failure detaching AutoScaling Group %s with Elastic Load Balancer: %s: {{err}}", asgName, elbName), err) | ||
} | ||
|
||
return nil | ||
} |
144 changes: 144 additions & 0 deletions
144
builtin/providers/aws/resource_aws_autoscaling_attachment_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/autoscaling" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAwsAutoscalingAttachment_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccAWSAutoscalingAttachment_basic, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSAutocalingAttachmentExists("aws_autoscaling_group.asg", 0), | ||
), | ||
}, | ||
// Add in one association | ||
resource.TestStep{ | ||
Config: testAccAWSAutoscalingAttachment_associated, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSAutocalingAttachmentExists("aws_autoscaling_group.asg", 1), | ||
), | ||
}, | ||
// Test adding a 2nd | ||
resource.TestStep{ | ||
Config: testAccAWSAutoscalingAttachment_double_associated, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSAutocalingAttachmentExists("aws_autoscaling_group.asg", 2), | ||
), | ||
}, | ||
// Now remove that newest one | ||
resource.TestStep{ | ||
Config: testAccAWSAutoscalingAttachment_associated, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSAutocalingAttachmentExists("aws_autoscaling_group.asg", 1), | ||
), | ||
}, | ||
// Now remove them both | ||
resource.TestStep{ | ||
Config: testAccAWSAutoscalingAttachment_basic, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSAutocalingAttachmentExists("aws_autoscaling_group.asg", 0), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAWSAutocalingAttachmentExists(asgname string, loadBalancerCount int) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[asgname] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", asgname) | ||
} | ||
|
||
conn := testAccProvider.Meta().(*AWSClient).autoscalingconn | ||
asg := rs.Primary.ID | ||
|
||
actual, err := conn.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{ | ||
AutoScalingGroupNames: []*string{aws.String(asg)}, | ||
}) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Recieved an error when attempting to load %s: %s", asg, err) | ||
} | ||
|
||
if loadBalancerCount != len(actual.AutoScalingGroups[0].LoadBalancerNames) { | ||
return fmt.Errorf("Error: ASG has the wrong number of load balacners associated. Expected [%d] but got [%d]", loadBalancerCount, len(actual.AutoScalingGroups[0].LoadBalancerNames)) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
const testAccAWSAutoscalingAttachment_basic = ` | ||
resource "aws_elb" "foo" { | ||
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_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_launch_configuration" "as_conf" { | ||
name = "test_config" | ||
image_id = "ami-f34032c3" | ||
instance_type = "t1.micro" | ||
} | ||
resource "aws_autoscaling_group" "asg" { | ||
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] | ||
name = "asg-lb-assoc-terraform-test" | ||
max_size = 1 | ||
min_size = 0 | ||
desired_capacity = 0 | ||
health_check_grace_period = 300 | ||
force_delete = true | ||
launch_configuration = "${aws_launch_configuration.as_conf.name}" | ||
tag { | ||
key = "Name" | ||
value = "terraform-asg-lg-assoc-test" | ||
propagate_at_launch = true | ||
} | ||
} | ||
` | ||
|
||
const testAccAWSAutoscalingAttachment_associated = testAccAWSAutoscalingAttachment_basic + ` | ||
resource "aws_autoscaling_attachment" "asg_attachment_foo" { | ||
autoscaling_group_name = "${aws_autoscaling_group.asg.id}" | ||
elb = "${aws_elb.foo.id}" | ||
} | ||
` | ||
|
||
const testAccAWSAutoscalingAttachment_double_associated = testAccAWSAutoscalingAttachment_associated + ` | ||
resource "aws_autoscaling_attachment" "asg_attachment_bar" { | ||
autoscaling_group_name = "${aws_autoscaling_group.asg.id}" | ||
elb = "${aws_elb.bar.id}" | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
website/source/docs/providers/aws/r/autoscaling_attachment.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_autoscaling_attachment" | ||
sidebar_current: "docs-aws-resource-autoscaling-attachment" | ||
description: |- | ||
Provides an AutoScaling Group Attachment resource. | ||
--- | ||
|
||
# aws\_autoscaling\_attachment | ||
|
||
Provides an AutoScaling Attachment resource. | ||
|
||
~> **NOTE on AutoScaling Groups and ASG Attachments:** Terraform currently provides | ||
both a standalone ASG Attachment resource (describing an ASG attached to | ||
an ELB), and an [AutoScaling Group resource](autoscaling_group.html) with | ||
`load_balancers` defined in-line. At this time you cannot use an ASG with in-line | ||
load balancers in conjunction with an ASG Attachment resource. Doing so will cause a | ||
conflict and will overwrite attachments. | ||
## Example Usage | ||
|
||
``` | ||
# Create a new load balancer attachment | ||
resource "aws_autoscaling_attachment" "asg_attachment_bar" { | ||
autoscaling_group_name = "${aws_autoscaling_group.asg.id}" | ||
elb = "${aws_elb.bar.id}" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `autoscaling_group_name` - (Required) Name of ASG to associate with the ELB. | ||
* `elb` - (Required) The name of the ELB. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters