Skip to content

Commit

Permalink
Merge pull request #2886 from loivis/d-lb-listener
Browse files Browse the repository at this point in the history
d/lb_listener: get listener from load balancer and listener port
  • Loading branch information
bflad authored Jan 30, 2018
2 parents d68dc8c + 37b3648 commit 10bf438
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 85 deletions.
56 changes: 47 additions & 9 deletions aws/data_source_aws_lb_listener.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
package aws

import "github.com/hashicorp/terraform/helper/schema"
import (
"errors"
"fmt"

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

func dataSourceAwsLbListener() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsLbListenerRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"load_balancer_arn", "port"},
},

"load_balancer_arn": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"arn"},
},
"port": {
Type: schema.TypeInt,
Computed: true,
Type: schema.TypeInt,
Optional: true,
ConflictsWith: []string{"arn"},
},

"protocol": {
Expand Down Expand Up @@ -57,6 +67,34 @@ func dataSourceAwsLbListener() *schema.Resource {
}

func dataSourceAwsLbListenerRead(d *schema.ResourceData, meta interface{}) error {
d.SetId(d.Get("arn").(string))
return resourceAwsLbListenerRead(d, meta)
if _, ok := d.GetOk("arn"); ok {
d.SetId(d.Get("arn").(string))
//log.Printf("[DEBUG] read listener %s", d.Get("arn").(string))
return resourceAwsLbListenerRead(d, meta)
}

conn := meta.(*AWSClient).elbv2conn
lbArn, lbOk := d.GetOk("load_balancer_arn")
port, portOk := d.GetOk("port")
if !lbOk || !portOk {
return errors.New("both load_balancer_arn and port must be set")
}
resp, err := conn.DescribeListeners(&elbv2.DescribeListenersInput{
LoadBalancerArn: aws.String(lbArn.(string)),
})
if err != nil {
return err
}
if len(resp.Listeners) == 0 {
return fmt.Errorf("[DEBUG] no listener exists for load balancer: %s", lbArn)
}
for _, listener := range resp.Listeners {
if *listener.Port == int64(port.(int)) {
//log.Printf("[DEBUG] get listener arn for %s:%s: %s", lbArn, port, *listener.Port)
d.SetId(*listener.ListenerArn)
return resourceAwsLbListenerRead(d, meta)
}
}

return errors.New("failed to get listener arn with given arguments")
}
Loading

0 comments on commit 10bf438

Please sign in to comment.