-
Notifications
You must be signed in to change notification settings - Fork 9k
/
data_source_aws_lb_listener.go
100 lines (88 loc) · 2.31 KB
/
data_source_aws_lb_listener.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package aws
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,
Optional: true,
ConflictsWith: []string{"load_balancer_arn", "port"},
},
"load_balancer_arn": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"arn"},
},
"port": {
Type: schema.TypeInt,
Optional: true,
ConflictsWith: []string{"arn"},
},
"protocol": {
Type: schema.TypeString,
Computed: true,
},
"ssl_policy": {
Type: schema.TypeString,
Computed: true,
},
"certificate_arn": {
Type: schema.TypeString,
Computed: true,
},
"default_action": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target_group_arn": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceAwsLbListenerRead(d *schema.ResourceData, meta interface{}) error {
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")
}