-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_is_lb_listeners.go
456 lines (413 loc) · 16.7 KB
/
data_source_ibm_is_lb_listeners.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/vpc-go-sdk/vpcv1"
)
func DataSourceIBMISLBListeners() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsLbListenersRead,
Schema: map[string]*schema.Schema{
isLBListenerLBID: &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The load balancer identifier.",
},
"listeners": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Collection of listeners.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"accept_proxy_protocol": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "If set to `true`, this listener will accept and forward PROXY protocol information. Supported by load balancers in the `application` family (otherwise always `false`). Additional restrictions:- If this listener has `https_redirect` specified, its `accept_proxy_protocol` value must match the `accept_proxy_protocol` value of the `https_redirect` listener.- If this listener is the target of another listener's `https_redirect`, its `accept_proxy_protocol` value must match that listener's `accept_proxy_protocol` value.",
},
"certificate_instance": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The certificate instance used for SSL termination. It is applicable only to `https`protocol.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this certificate instance.",
},
},
},
},
"connection_limit": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The connection limit of the listener.",
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date and time that this listener was created.",
},
"default_pool": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The default pool associated with the listener.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"deleted": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The pool's canonical URL.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this load balancer pool.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this load balancer pool.",
},
},
},
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The listener's canonical URL.",
},
"https_redirect": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If specified, the target listener that requests are redirected to.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"http_status_code": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The HTTP status code for this redirect.",
},
"listener": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"deleted": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The listener's canonical URL.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this load balancer listener.",
},
},
},
},
"uri": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The redirect relative target URI.",
},
},
},
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this load balancer listener.",
},
"policies": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The policies for this listener.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"deleted": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The listener policy's canonical URL.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The policy's unique identifier.",
},
},
},
},
"port": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The listener port number, or the inclusive lower bound of the port range. Each listener in the load balancer must have a unique `port` and `protocol` combination.",
},
"port_max": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The inclusive upper bound of the range of ports used by this listener.Only load balancers in the `network` family support more than one port per listener.",
},
"port_min": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The inclusive lower bound of the range of ports used by this listener.Only load balancers in the `network` family support more than one port per listener.",
},
"protocol": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The listener protocol. Load balancers in the `network` family support `tcp`. Load balancers in the `application` family support `tcp`, `http`, and `https`. Each listener in the load balancer must have a unique `port` and `protocol` combination.",
},
"provisioning_status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The provisioning status of this listener.",
},
isLBListenerIdleConnectionTimeout: {
Type: schema.TypeInt,
Computed: true,
Description: "idle connection timeout of listener",
},
},
},
},
},
}
}
func dataSourceIBMIsLbListenersRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := vpcClient(meta)
if err != nil {
return diag.FromErr(err)
}
listLoadBalancerListenersOptions := &vpcv1.ListLoadBalancerListenersOptions{}
listLoadBalancerListenersOptions.SetLoadBalancerID(d.Get(isLBListenerLBID).(string))
loadBalancerListenerCollection, response, err := vpcClient.ListLoadBalancerListenersWithContext(context, listLoadBalancerListenersOptions)
if err != nil {
log.Printf("[DEBUG] ListLoadBalancerListenersWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("ListLoadBalancerListenersWithContext failed %s\n%s", err, response))
}
d.SetId(dataSourceIBMIsLbListenersID(d))
if loadBalancerListenerCollection.Listeners != nil {
err = d.Set("listeners", dataSourceLoadBalancerListenerCollectionFlattenListeners(loadBalancerListenerCollection.Listeners))
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting listeners %s", err))
}
}
return nil
}
// dataSourceIBMIsLbListenersID returns a reasonable ID for the list.
func dataSourceIBMIsLbListenersID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceLoadBalancerListenerCollectionFlattenListeners(result []vpcv1.LoadBalancerListener) (listeners []map[string]interface{}) {
for _, listenersItem := range result {
listeners = append(listeners, dataSourceLoadBalancerListenerCollectionListenersToMap(listenersItem))
}
return listeners
}
func dataSourceLoadBalancerListenerCollectionListenersToMap(listenersItem vpcv1.LoadBalancerListener) (listenersMap map[string]interface{}) {
listenersMap = map[string]interface{}{}
if listenersItem.AcceptProxyProtocol != nil {
listenersMap["accept_proxy_protocol"] = listenersItem.AcceptProxyProtocol
}
if listenersItem.CertificateInstance != nil {
certificateInstanceList := []map[string]interface{}{}
certificateInstanceMap := dataSourceLoadBalancerListenerCollectionListenersCertificateInstanceToMap(*listenersItem.CertificateInstance)
certificateInstanceList = append(certificateInstanceList, certificateInstanceMap)
listenersMap["certificate_instance"] = certificateInstanceList
}
if listenersItem.ConnectionLimit != nil {
listenersMap["connection_limit"] = listenersItem.ConnectionLimit
}
if listenersItem.IdleConnectionTimeout != nil {
listenersMap[isLBListenerIdleConnectionTimeout] = listenersItem.IdleConnectionTimeout
}
if listenersItem.CreatedAt != nil {
listenersMap["created_at"] = listenersItem.CreatedAt.String()
}
if listenersItem.DefaultPool != nil {
defaultPoolList := []map[string]interface{}{}
defaultPoolMap := dataSourceLoadBalancerListenerCollectionListenersDefaultPoolToMap(*listenersItem.DefaultPool)
defaultPoolList = append(defaultPoolList, defaultPoolMap)
listenersMap["default_pool"] = defaultPoolList
}
if listenersItem.Href != nil {
listenersMap["href"] = listenersItem.Href
}
if listenersItem.HTTPSRedirect != nil {
httpsRedirectList := []map[string]interface{}{}
httpsRedirectMap := dataSourceLoadBalancerListenerCollectionListenersHTTPSRedirectToMap(*listenersItem.HTTPSRedirect)
httpsRedirectList = append(httpsRedirectList, httpsRedirectMap)
listenersMap["https_redirect"] = httpsRedirectList
}
if listenersItem.ID != nil {
listenersMap["id"] = listenersItem.ID
}
if listenersItem.Policies != nil {
policiesList := []map[string]interface{}{}
for _, policiesItem := range listenersItem.Policies {
policiesList = append(policiesList, dataSourceLoadBalancerListenerCollectionListenersPoliciesToMap(policiesItem))
}
listenersMap["policies"] = policiesList
}
if listenersItem.Port != nil {
listenersMap["port"] = listenersItem.Port
}
if listenersItem.PortMax != nil {
listenersMap["port_max"] = listenersItem.PortMax
}
if listenersItem.PortMin != nil {
listenersMap["port_min"] = listenersItem.PortMin
}
if listenersItem.Protocol != nil {
listenersMap["protocol"] = listenersItem.Protocol
}
if listenersItem.ProvisioningStatus != nil {
listenersMap["provisioning_status"] = listenersItem.ProvisioningStatus
}
return listenersMap
}
func dataSourceLoadBalancerListenerCollectionListenersCertificateInstanceToMap(certificateInstanceItem vpcv1.CertificateInstanceReference) (certificateInstanceMap map[string]interface{}) {
certificateInstanceMap = map[string]interface{}{}
if certificateInstanceItem.CRN != nil {
certificateInstanceMap["crn"] = certificateInstanceItem.CRN
}
return certificateInstanceMap
}
func dataSourceLoadBalancerListenerCollectionListenersDefaultPoolToMap(defaultPoolItem vpcv1.LoadBalancerPoolReference) (defaultPoolMap map[string]interface{}) {
defaultPoolMap = map[string]interface{}{}
if defaultPoolItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceLoadBalancerListenerCollectionDefaultPoolDeletedToMap(*defaultPoolItem.Deleted)
deletedList = append(deletedList, deletedMap)
defaultPoolMap["deleted"] = deletedList
}
if defaultPoolItem.Href != nil {
defaultPoolMap["href"] = defaultPoolItem.Href
}
if defaultPoolItem.ID != nil {
defaultPoolMap["id"] = defaultPoolItem.ID
}
if defaultPoolItem.Name != nil {
defaultPoolMap["name"] = defaultPoolItem.Name
}
return defaultPoolMap
}
func dataSourceLoadBalancerListenerCollectionDefaultPoolDeletedToMap(deletedItem vpcv1.LoadBalancerPoolReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}
func dataSourceLoadBalancerListenerCollectionListenersHTTPSRedirectToMap(httpsRedirectItem vpcv1.LoadBalancerListenerHTTPSRedirect) (httpsRedirectMap map[string]interface{}) {
httpsRedirectMap = map[string]interface{}{}
if httpsRedirectItem.HTTPStatusCode != nil {
httpsRedirectMap["http_status_code"] = httpsRedirectItem.HTTPStatusCode
}
if httpsRedirectItem.Listener != nil {
listenerList := []map[string]interface{}{}
listenerMap := dataSourceLoadBalancerListenerCollectionHTTPSRedirectListenerToMap(*httpsRedirectItem.Listener)
listenerList = append(listenerList, listenerMap)
httpsRedirectMap["listener"] = listenerList
}
if httpsRedirectItem.URI != nil {
httpsRedirectMap["uri"] = httpsRedirectItem.URI
}
return httpsRedirectMap
}
func dataSourceLoadBalancerListenerCollectionHTTPSRedirectListenerToMap(listenerItem vpcv1.LoadBalancerListenerReference) (listenerMap map[string]interface{}) {
listenerMap = map[string]interface{}{}
if listenerItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceLoadBalancerListenerCollectionListenerDeletedToMap(*listenerItem.Deleted)
deletedList = append(deletedList, deletedMap)
listenerMap["deleted"] = deletedList
}
if listenerItem.Href != nil {
listenerMap["href"] = listenerItem.Href
}
if listenerItem.ID != nil {
listenerMap["id"] = listenerItem.ID
}
return listenerMap
}
func dataSourceLoadBalancerListenerCollectionListenerDeletedToMap(deletedItem vpcv1.LoadBalancerListenerReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}
func dataSourceLoadBalancerListenerCollectionListenersPoliciesToMap(policiesItem vpcv1.LoadBalancerListenerPolicyReference) (policiesMap map[string]interface{}) {
policiesMap = map[string]interface{}{}
if policiesItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceLoadBalancerListenerCollectionPoliciesDeletedToMap(*policiesItem.Deleted)
deletedList = append(deletedList, deletedMap)
policiesMap["deleted"] = deletedList
}
if policiesItem.Href != nil {
policiesMap["href"] = policiesItem.Href
}
if policiesItem.ID != nil {
policiesMap["id"] = policiesItem.ID
}
return policiesMap
}
func dataSourceLoadBalancerListenerCollectionPoliciesDeletedToMap(deletedItem vpcv1.LoadBalancerListenerPolicyReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}