Skip to content
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: Allows aws_alb security_groups to be updated #9804

Merged
merged 1 commit into from
Nov 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion builtin/providers/aws/resource_aws_alb.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func resourceAwsAlb() *schema.Resource {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
ForceNew: true,
Optional: true,
Set: schema.HashString,
},
Expand Down Expand Up @@ -331,6 +330,20 @@ func resourceAwsAlbUpdate(d *schema.ResourceData, meta interface{}) error {
}
}

if d.HasChange("security_groups") {
sgs := expandStringList(d.Get("security_groups").(*schema.Set).List())

params := &elbv2.SetSecurityGroupsInput{
LoadBalancerArn: aws.String(d.Id()),
SecurityGroups: sgs,
}
_, err := elbconn.SetSecurityGroups(params)
if err != nil {
return fmt.Errorf("Failure Setting ALB Security Groups: %s", err)
}

}

return resourceAwsAlbRead(d, meta)
}

Expand Down
123 changes: 123 additions & 0 deletions builtin/providers/aws/resource_aws_alb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,35 @@ func TestAccAWSALB_tags(t *testing.T) {
})
}

func TestAccAWSALB_updatedSecurityGroups(t *testing.T) {
var pre, post elbv2.LoadBalancer
albName := fmt.Sprintf("testaccawsalb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_alb.alb_test",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSALBDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSALBConfig_basic(albName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSALBExists("aws_alb.alb_test", &pre),
resource.TestCheckResourceAttr("aws_alb.alb_test", "security_groups.#", "1"),
),
},
{
Config: testAccAWSALBConfig_updateSecurityGroups(albName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSALBExists("aws_alb.alb_test", &post),
resource.TestCheckResourceAttr("aws_alb.alb_test", "security_groups.#", "2"),
testAccCheckAWSAlbARNs(&pre, &post),
),
},
},
})
}

// TestAccAWSALB_noSecurityGroup regression tests the issue in #8264,
// where if an ALB is created without a security group, a default one
// is assigned.
Expand Down Expand Up @@ -259,6 +288,16 @@ func TestAccAWSALB_accesslogs(t *testing.T) {
})
}

func testAccCheckAWSAlbARNs(pre, post *elbv2.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
if *pre.LoadBalancerArn != *post.LoadBalancerArn {
return errors.New("ALB has been recreated. ARNs are different")
}

return nil
}
}

func testAccCheckAWSALBExists(n string, res *elbv2.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -741,3 +780,87 @@ resource "aws_subnet" "alb_test" {
}
}`, albName)
}

func testAccAWSALBConfig_updateSecurityGroups(albName string) string {
return fmt.Sprintf(`resource "aws_alb" "alb_test" {
name = "%s"
internal = false
security_groups = ["${aws_security_group.alb_test.id}", "${aws_security_group.alb_test_2.id}"]
subnets = ["${aws_subnet.alb_test.*.id}"]

idle_timeout = 30
enable_deletion_protection = false

tags {
TestName = "TestAccAWSALB_basic"
}
}

variable "subnets" {
default = ["10.0.1.0/24", "10.0.2.0/24"]
type = "list"
}

data "aws_availability_zones" "available" {}

resource "aws_vpc" "alb_test" {
cidr_block = "10.0.0.0/16"

tags {
TestName = "TestAccAWSALB_basic"
}
}

resource "aws_subnet" "alb_test" {
count = 2
vpc_id = "${aws_vpc.alb_test.id}"
cidr_block = "${element(var.subnets, count.index)}"
map_public_ip_on_launch = true
availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}"

tags {
TestName = "TestAccAWSALB_basic"
}
}

resource "aws_security_group" "alb_test_2" {
name = "allow_all_alb_test_2"
description = "Used for ALB Testing"
vpc_id = "${aws_vpc.alb_test.id}"

ingress {
from_port = 80
to_port = 80
protocol = "TCP"
cidr_blocks = ["0.0.0.0/0"]
}

tags {
TestName = "TestAccAWSALB_basic_2"
}
}

resource "aws_security_group" "alb_test" {
name = "allow_all_alb_test"
description = "Used for ALB Testing"
vpc_id = "${aws_vpc.alb_test.id}"

ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags {
TestName = "TestAccAWSALB_basic"
}
}`, albName)
}