/
data_source_edgecenter_lblistener.go
140 lines (127 loc) · 3.97 KB
/
data_source_edgecenter_lblistener.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package edgecenter
import (
"context"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
edgecloudV2 "github.com/Edge-Center/edgecentercloud-go/v2"
)
func dataSourceLBListener() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceLBListenerRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeInt,
Optional: true,
Description: "The uuid of the project. Either 'project_id' or 'project_name' must be specified.",
ExactlyOneOf: []string{"project_id", "project_name"},
},
"project_name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the project. Either 'project_id' or 'project_name' must be specified.",
ExactlyOneOf: []string{"project_id", "project_name"},
},
"region_id": {
Type: schema.TypeInt,
Optional: true,
Description: "The uuid of the region. Either 'region_id' or 'region_name' must be specified.",
ExactlyOneOf: []string{"region_id", "region_name"},
},
"region_name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the region. Either 'region_id' or 'region_name' must be specified.",
ExactlyOneOf: []string{"region_id", "region_name"},
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the load balancer listener.",
},
"loadbalancer_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The uuid for the load balancer.",
},
"protocol": {
Type: schema.TypeString,
Computed: true,
Description: "Available values is 'HTTP', 'HTTPS', 'TCP', 'UDP'",
},
"protocol_port": {
Type: schema.TypeInt,
Computed: true,
Description: "The port on which the protocol is bound.",
},
"pool_count": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of pools associated with the load balancer.",
},
"operating_status": {
Type: schema.TypeString,
Computed: true,
Description: "The current operational status of the load balancer.",
},
"provisioning_status": {
Type: schema.TypeString,
Computed: true,
Description: "The current provisioning status of the load balancer.",
},
"allowed_cidrs": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Description: "The allowed CIDRs for listener.",
},
},
}
}
func dataSourceLBListenerRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Println("[DEBUG] Start LBListener reading")
var diags diag.Diagnostics
config := m.(*Config)
clientV2 := config.CloudClient
var err error
clientV2.Region, clientV2.Project, err = GetRegionIDandProjectID(ctx, clientV2, d)
if err != nil {
return diag.FromErr(err)
}
var opts edgecloudV2.ListenerListOptions
name := d.Get("name").(string)
lbID := d.Get("loadbalancer_id").(string)
if lbID != "" {
opts.LoadbalancerID = lbID
}
ls, _, err := clientV2.Loadbalancers.ListenerList(ctx, &opts)
if err != nil {
return diag.FromErr(err)
}
var found bool
var listener edgecloudV2.Listener
for _, l := range ls {
if l.Name == name {
listener = l
found = true
break
}
}
if !found {
return diag.Errorf("lb listener with name %s not found", name)
}
d.SetId(listener.ID)
d.Set("name", listener.Name)
d.Set("protocol", listener.Protocol)
d.Set("protocol_port", listener.ProtocolPort)
d.Set("pool_count", listener.PoolCount)
d.Set("operating_status", listener.OperatingStatus)
d.Set("provisioning_status", listener.ProvisioningStatus)
d.Set("loadbalancer_id", lbID)
d.Set("project_id", d.Get("project_id").(int))
d.Set("region_id", d.Get("region_id").(int))
d.Set("allowed_cidrs", listener.AllowedCIDRs)
log.Println("[DEBUG] Finish LBListener reading")
return diags
}