-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_is_lb_pool.go
446 lines (396 loc) · 16 KB
/
data_source_ibm_is_lb_pool.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
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"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 DataSourceIBMISLBPool() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsLbPoolRead,
Schema: map[string]*schema.Schema{
"lb": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The load balancer identifier.",
},
"identifier": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"name", "identifier"},
Description: "The pool identifier.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ExactlyOneOf: []string{"name", "identifier"},
Description: "The user-defined name for this load balancer pool.",
},
"algorithm": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The load balancing algorithm.",
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date and time that this pool was created.",
},
"health_monitor": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The health monitor of this pool.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"delay": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The health check interval in seconds. Interval must be greater than timeout value.",
},
"max_retries": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The health check max retries.",
},
"port": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The health check port number. If specified, this overrides the ports specified in the server member resources.",
},
"timeout": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The health check timeout in seconds.",
},
"type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The protocol type of this load balancer pool health monitor.The enumerated values for this property are expected to expand in the future. When processing this property, check for and log unknown values. Optionally halt processing and surface the error, or bypass the health monitor on which the unexpected property value was encountered.",
},
"url_path": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The health check URL path. Applicable only if the health monitor `type` is `http` or`https`. This value must be in the format of an [origin-form request target](https://tools.ietf.org/html/rfc7230#section-5.3.1).",
},
},
},
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The pool's canonical URL.",
},
"instance_group": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The instance group that is managing this pool.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this instance group.",
},
"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 URL for this instance group.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this instance group.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this instance group.",
},
},
},
},
"members": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The backend server members of the pool.",
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 member's canonical URL.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this load balancer pool member.",
},
},
},
},
"protocol": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The protocol used for this load balancer pool.The enumerated values for this property are expected to expand in the future. When processing this property, check for and log unknown values. Optionally halt processing and surface the error, or bypass the pool on which the unexpected property value was encountered.",
},
"provisioning_status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The provisioning status of this pool.",
},
"proxy_protocol": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The PROXY protocol setting for this pool:- `v1`: Enabled with version 1 (human-readable header format)- `v2`: Enabled with version 2 (binary header format)- `disabled`: DisabledSupported by load balancers in the `application` family (otherwise always `disabled`).",
},
"session_persistence": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The session persistence of this pool.The enumerated values for this property are expected to expand in the future. Whenprocessing this property, check for and log unknown values. Optionally haltprocessing and surface the error, or bypass the pool on which the unexpectedproperty value was encountered.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cookie_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The session persistence cookie name. Applicable only for type `app_cookie`. Names starting with `IBM` are not allowed.",
},
"type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The session persistence type. The `http_cookie` and `app_cookie` types are applicable only to the `http` and `https` protocols.",
},
},
},
},
},
}
}
func dataSourceIBMIsLbPoolRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return diag.FromErr(err)
}
var loadBalancerPool *vpcv1.LoadBalancerPool
if v, ok := d.GetOk("identifier"); ok {
getLoadBalancerPoolOptions := &vpcv1.GetLoadBalancerPoolOptions{}
getLoadBalancerPoolOptions.SetLoadBalancerID(d.Get("lb").(string))
getLoadBalancerPoolOptions.SetID(v.(string))
loadBalancerPoolInfo, response, err := sess.GetLoadBalancerPoolWithContext(context, getLoadBalancerPoolOptions)
if err != nil {
log.Printf("[DEBUG] GetLoadBalancerPoolWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetLoadBalancerPoolWithContext failed %s\n%s", err, response))
}
loadBalancerPool = loadBalancerPoolInfo
} else if v, ok := d.GetOk("name"); ok {
listLoadBalancerPoolsOptions := &vpcv1.ListLoadBalancerPoolsOptions{}
listLoadBalancerPoolsOptions.SetLoadBalancerID(d.Get("lb").(string))
loadBalancerPoolCollection, response, err := sess.ListLoadBalancerPoolsWithContext(context, listLoadBalancerPoolsOptions)
if err != nil {
log.Printf("[DEBUG] ListLoadBalancerPoolsWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("ListLoadBalancerPoolsWithContext failed %s\n%s", err, response))
}
name := v.(string)
for _, data := range loadBalancerPoolCollection.Pools {
if *data.Name == name {
loadBalancerPool = &data
break
}
}
if loadBalancerPool == nil {
log.Printf("[DEBUG] No LoadBalancerPool found with name (%s)", name)
return diag.FromErr(fmt.Errorf("No LoadBalancerPool found with name (%s)", name))
}
}
d.SetId(*loadBalancerPool.ID)
if err = d.Set("algorithm", loadBalancerPool.Algorithm); err != nil {
return diag.FromErr(fmt.Errorf("Error setting algorithm: %s", err))
}
if err = d.Set("created_at", flex.DateTimeToString(loadBalancerPool.CreatedAt)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting created_at: %s", err))
}
if loadBalancerPool.HealthMonitor != nil {
err = d.Set("health_monitor", dataSourceLoadBalancerPoolFlattenHealthMonitor(*loadBalancerPool.HealthMonitor))
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting health_monitor %s", err))
}
}
if err = d.Set("href", loadBalancerPool.Href); err != nil {
return diag.FromErr(fmt.Errorf("Error setting href: %s", err))
}
if loadBalancerPool.InstanceGroup != nil {
err = d.Set("instance_group", dataSourceLoadBalancerPoolFlattenInstanceGroup(*loadBalancerPool.InstanceGroup))
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting instance_group %s", err))
}
}
if loadBalancerPool.Members != nil {
err = d.Set("members", dataSourceLoadBalancerPoolFlattenMembers(loadBalancerPool.Members))
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting members %s", err))
}
}
if err = d.Set("identifier", loadBalancerPool.ID); err != nil {
return diag.FromErr(fmt.Errorf("Error setting identifier: %s", err))
}
if err = d.Set("name", loadBalancerPool.Name); err != nil {
return diag.FromErr(fmt.Errorf("Error setting name: %s", err))
}
if err = d.Set("protocol", loadBalancerPool.Protocol); err != nil {
return diag.FromErr(fmt.Errorf("Error setting protocol: %s", err))
}
if err = d.Set("provisioning_status", loadBalancerPool.ProvisioningStatus); err != nil {
return diag.FromErr(fmt.Errorf("Error setting provisioning_status: %s", err))
}
if err = d.Set("proxy_protocol", loadBalancerPool.ProxyProtocol); err != nil {
return diag.FromErr(fmt.Errorf("Error setting proxy_protocol: %s", err))
}
if loadBalancerPool.SessionPersistence != nil {
err = d.Set("session_persistence", dataSourceLoadBalancerPoolFlattenSessionPersistence(*loadBalancerPool.SessionPersistence))
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting session_persistence %s", err))
}
}
return nil
}
func dataSourceLoadBalancerPoolFlattenHealthMonitor(result vpcv1.LoadBalancerPoolHealthMonitor) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceLoadBalancerPoolHealthMonitorToMap(result)
finalList = append(finalList, finalMap)
return finalList
}
func dataSourceLoadBalancerPoolHealthMonitorToMap(healthMonitorItem vpcv1.LoadBalancerPoolHealthMonitor) (healthMonitorMap map[string]interface{}) {
healthMonitorMap = map[string]interface{}{}
if healthMonitorItem.Delay != nil {
healthMonitorMap["delay"] = healthMonitorItem.Delay
}
if healthMonitorItem.MaxRetries != nil {
healthMonitorMap["max_retries"] = healthMonitorItem.MaxRetries
}
if healthMonitorItem.Port != nil {
healthMonitorMap["port"] = healthMonitorItem.Port
}
if healthMonitorItem.Timeout != nil {
healthMonitorMap["timeout"] = healthMonitorItem.Timeout
}
if healthMonitorItem.Type != nil {
healthMonitorMap["type"] = healthMonitorItem.Type
}
if healthMonitorItem.URLPath != nil {
healthMonitorMap["url_path"] = healthMonitorItem.URLPath
}
return healthMonitorMap
}
func dataSourceLoadBalancerPoolFlattenInstanceGroup(result vpcv1.InstanceGroupReference) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceLoadBalancerPoolInstanceGroupToMap(result)
finalList = append(finalList, finalMap)
return finalList
}
func dataSourceLoadBalancerPoolInstanceGroupToMap(instanceGroupItem vpcv1.InstanceGroupReference) (instanceGroupMap map[string]interface{}) {
instanceGroupMap = map[string]interface{}{}
if instanceGroupItem.CRN != nil {
instanceGroupMap["crn"] = instanceGroupItem.CRN
}
if instanceGroupItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceLoadBalancerPoolInstanceGroupDeletedToMap(*instanceGroupItem.Deleted)
deletedList = append(deletedList, deletedMap)
instanceGroupMap["deleted"] = deletedList
}
if instanceGroupItem.Href != nil {
instanceGroupMap["href"] = instanceGroupItem.Href
}
if instanceGroupItem.ID != nil {
instanceGroupMap["id"] = instanceGroupItem.ID
}
if instanceGroupItem.Name != nil {
instanceGroupMap["name"] = instanceGroupItem.Name
}
return instanceGroupMap
}
func dataSourceLoadBalancerPoolInstanceGroupDeletedToMap(deletedItem vpcv1.InstanceGroupReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}
func dataSourceLoadBalancerPoolFlattenMembers(result []vpcv1.LoadBalancerPoolMemberReference) (members []map[string]interface{}) {
for _, membersItem := range result {
members = append(members, dataSourceLoadBalancerPoolMembersToMap(membersItem))
}
return members
}
func dataSourceLoadBalancerPoolMembersToMap(membersItem vpcv1.LoadBalancerPoolMemberReference) (membersMap map[string]interface{}) {
membersMap = map[string]interface{}{}
if membersItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceLoadBalancerPoolMembersDeletedToMap(*membersItem.Deleted)
deletedList = append(deletedList, deletedMap)
membersMap["deleted"] = deletedList
}
if membersItem.Href != nil {
membersMap["href"] = membersItem.Href
}
if membersItem.ID != nil {
membersMap["id"] = membersItem.ID
}
return membersMap
}
func dataSourceLoadBalancerPoolMembersDeletedToMap(deletedItem vpcv1.LoadBalancerPoolMemberReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}
func dataSourceLoadBalancerPoolFlattenSessionPersistence(result vpcv1.LoadBalancerPoolSessionPersistence) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceLoadBalancerPoolSessionPersistenceToMap(result)
finalList = append(finalList, finalMap)
return finalList
}
func dataSourceLoadBalancerPoolSessionPersistenceToMap(sessionPersistenceItem vpcv1.LoadBalancerPoolSessionPersistence) (sessionPersistenceMap map[string]interface{}) {
sessionPersistenceMap = map[string]interface{}{}
if sessionPersistenceItem.CookieName != nil {
sessionPersistenceMap["cookie_name"] = sessionPersistenceItem.CookieName
}
if sessionPersistenceItem.Type != nil {
sessionPersistenceMap["type"] = sessionPersistenceItem.Type
}
return sessionPersistenceMap
}