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

Find LB listener by the LB ARN #2110

Closed
Closed
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
44 changes: 41 additions & 3 deletions aws/data_source_aws_lb_listener.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package aws

import "github.com/hashicorp/terraform/helper/schema"
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
"log"
)

func dataSourceAwsLbListener() *schema.Resource {
return &schema.Resource{
Expand All @@ -9,11 +16,13 @@ func dataSourceAwsLbListener() *schema.Resource {
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Required: true,
Optional: true,
Computed: true,
},

"load_balancer_arn": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"port": {
Expand Down Expand Up @@ -57,6 +66,35 @@ func dataSourceAwsLbListener() *schema.Resource {
}

func dataSourceAwsLbListenerRead(d *schema.ResourceData, meta interface{}) error {
d.SetId(d.Get("arn").(string))
listenerArn := d.Get("arn").(string)
lbArn := d.Get("load_balancer_arn").(string)

switch {
case listenerArn != "":
d.SetId(d.Get("arn").(string))
case lbArn != "":
elbconn := meta.(*AWSClient).elbv2conn

resp, err := elbconn.DescribeListeners(&elbv2.DescribeListenersInput{
LoadBalancerArn: aws.String(lbArn),
})

if err != nil {
if isListenerNotFound(err) {
log.Printf("[WARN] DescribeListeners - removing %s from state", d.Id())
d.SetId("")
return nil
}
return errwrap.Wrapf("Error retrieving Listener: {{err}}", err)
}

if len(resp.Listeners) != 1 {
return fmt.Errorf("Multiple listeners found for load balancer %s. This data source only supports single listener load balancers when searching by load balancer.", lbArn)
}

d.SetId(*resp.Listeners[0].ListenerArn)
}

return resourceAwsLbListenerRead(d, meta)

}
124 changes: 124 additions & 0 deletions aws/data_source_aws_lb_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ func TestAccDataSourceAWSLBListener_basic(t *testing.T) {
})
}

func TestAccDataSourceAWSLBListenerByLBArn(t *testing.T) {
lbName := fmt.Sprintf("testlistener-lbarn-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum))
targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAWSLBListenerConfigByLBArn(lbName, targetGroupName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.aws_lb_listener.front_end", "load_balancer_arn"),
resource.TestCheckResourceAttrSet("data.aws_lb_listener.front_end", "arn"),
resource.TestCheckResourceAttr("data.aws_lb_listener.front_end", "protocol", "HTTP"),
resource.TestCheckResourceAttr("data.aws_lb_listener.front_end", "port", "80"),
resource.TestCheckResourceAttr("data.aws_lb_listener.front_end", "default_action.#", "1"),
resource.TestCheckResourceAttr("data.aws_lb_listener.front_end", "default_action.0.type", "forward"),
resource.TestCheckResourceAttrSet("data.aws_lb_listener.front_end", "default_action.0.target_group_arn"),
),
},
},
})
}

func TestAccDataSourceAWSLBListenerBackwardsCompatibility(t *testing.T) {
lbName := fmt.Sprintf("testlistener-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum))
targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
Expand Down Expand Up @@ -184,6 +208,106 @@ data "aws_lb_listener" "front_end" {
}`, lbName, targetGroupName)
}

func testAccDataSourceAWSLBListenerConfigByLBArn(lbName, targetGroupName string) string {
return fmt.Sprintf(`resource "aws_lb_listener" "front_end" {
load_balancer_arn = "${aws_lb.alb_test.id}"
protocol = "HTTP"
port = "80"

default_action {
target_group_arn = "${aws_lb_target_group.test.id}"
type = "forward"
}
}

resource "aws_lb" "alb_test" {
name = "%s"
internal = true
security_groups = ["${aws_security_group.alb_test.id}"]
subnets = ["${aws_subnet.alb_test.*.id}"]

idle_timeout = 30
enable_deletion_protection = false

tags {
TestName = "TestAccAWSALB_basic"
}
}

resource "aws_lb_target_group" "test" {
name = "%s"
port = 8080
protocol = "HTTP"
vpc_id = "${aws_vpc.alb_test.id}"

health_check {
path = "/health"
interval = 60
port = 8081
protocol = "HTTP"
timeout = 3
healthy_threshold = 3
unhealthy_threshold = 3
matcher = "200-299"
}
}

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" {
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"
}
}

data "aws_lb_listener" "front_end" {
load_balancer_arn = "${aws_lb_listener.front_end.load_balancer_arn}"
}`, lbName, targetGroupName)
}

func testAccDataSourceAWSLBListenerConfigBackwardsCompatibility(lbName, targetGroupName string) string {
return fmt.Sprintf(`resource "aws_alb_listener" "front_end" {
load_balancer_arn = "${aws_alb.alb_test.id}"
Expand Down
17 changes: 16 additions & 1 deletion website/docs/d/lb_listener.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,26 @@ data "aws_lb_listener" "listener" {
}
```

```hcl
variable "lb_name" {
type = "string"
}

data "aws_lb" "load_balancer" {
name = "${var.lb_name}"
}

data "aws_lb_listener" "listener" {
load_balancer_arn = "${data.aws_lb.load_balancer.arn}"
}
```

## Argument Reference

The following arguments are supported:

* `arn` - (Required) The ARN of the listener.
* `arn` - (Optional) The ARN of the listener.
* `load_balancer_arn` - (Optional) The ARN of the load balancer.

## Attributes Reference

Expand Down