-
Notifications
You must be signed in to change notification settings - Fork 9.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
provider/aws: aws_autoscaling_attachment resource #9146
Merged
stack72
merged 3 commits into
hashicorp:master
from
jrnt30:GH-8755-Autoscaling-elb-attachment
Nov 3, 2016
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would break here, so that you don't check rest of them, as there will be no need at this point, I believe.