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

d/lb_listener: get listener from load balancer and listener port #2886

Merged
merged 1 commit into from
Jan 30, 2018
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
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