/
resource_bizflycloud_loadbalancer_pool.go
676 lines (638 loc) · 20.1 KB
/
resource_bizflycloud_loadbalancer_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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
package bizflycloud
import (
"context"
"fmt"
"log"
"time"
"github.com/bizflycloud/gobizfly"
"github.com/bizflycloud/terraform-provider-bizflycloud/constants"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"k8s.io/apimachinery/pkg/util/wait"
)
func resourceBizflyCloudLoadBalancerPool() *schema.Resource {
return &schema.Resource{
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Create: resourceBizflyCloudLoadBalancerPoolCreate,
Update: resourceBizflyCloudLoadBalancerPoolUpdate,
Read: resourceBizflyCloudLoadBalancerPoolRead,
Delete: resourceBizflyCloudLoadBalancerPoolDelete,
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"algorithm": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(constants.ValidAlgorithms, false),
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"protocol": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(constants.ValidPoolProtocols, false),
},
"load_balancer_id": {
Type: schema.TypeString,
Required: true,
},
"members": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: loadbalancerMemberSchema(),
},
},
"health_monitor": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: getHealthMonitorSchema(),
},
},
"persistent": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: getPersistentSchema(),
},
},
},
}
}
// Get member schema
func loadbalancerMemberSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"weight": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
ValidateFunc: validation.IntBetween(0, 256),
},
"address": {
Type: schema.TypeString,
Required: true,
},
"protocol_port": {
Type: schema.TypeInt,
Required: true,
},
"backup": {
Type: schema.TypeBool,
Required: false,
Optional: true,
},
"operating_status": {
Type: schema.TypeString,
Computed: true,
},
"provisioning_status": {
Type: schema.TypeString,
Computed: true,
},
"subnet_id": {
Type: schema.TypeString,
Computed: true,
},
"project_id": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
}
}
// Get health monitor schema
func getHealthMonitorSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Default: "pool-monitor",
Optional: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(constants.ValidHealthMonitorProtocols, false),
},
"timeout": {
Type: schema.TypeInt,
Default: 5,
Optional: true,
},
"max_retries": {
Type: schema.TypeInt,
Default: 3,
Optional: true,
ValidateFunc: validation.IntBetween(1, 10),
},
"max_retries_down": {
Type: schema.TypeInt,
Default: 3,
Optional: true,
ValidateFunc: validation.IntBetween(1, 10),
},
"delay": {
Type: schema.TypeInt,
Default: 5,
Optional: true,
},
"http_method": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(constants.ValidHealthMonitorMethods, false),
},
"url_path": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringMatch(constants.ValidUrlPathRegex, "url_path must start with '/'"),
},
"expected_code": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(constants.ValidHealthMonitorExceptedCodes, false),
},
"operating_status": {
Type: schema.TypeString,
Computed: true,
},
"provisioning_status": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
}
}
// Get persistent schema
func getPersistentSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(constants.ValidStickySessions, false),
},
"cookie_name": {
Type: schema.TypeString,
Optional: true,
},
}
}
// Create pool resource
func resourceBizflyCloudLoadBalancerPoolCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).gobizflyClient()
lbID := d.Get("load_balancer_id").(string)
_, err := waitLoadbalancerActiveProvisioningStatus(client, lbID, loadbalancerResource)
if err != nil {
return err
}
createReq := getCreatePoolPayloadFromConfig(d)
log.Printf("[DEBUG] Create pool payload: %+v", createReq)
pool, err := client.Pool.Create(context.Background(), lbID, &createReq)
if err != nil {
return fmt.Errorf("Error when create pool for loadbalancer %s: %+v", lbID, err)
}
poolID := pool.ID
err = checkLoadbalancerPoolActiveStatus(client, poolID, lbID)
if err != nil {
return err
}
d.SetId(poolID)
// Update description if could not create pool with description
description := d.Get("description").(string)
if description != pool.Description {
updateReq := &gobizfly.PoolUpdateRequest{
Description: &description,
}
persistent := getSessionPersistentPayloadFromConfig(d)
updateReq.SessionPersistence = persistent
_, err = client.Pool.Update(context.Background(), poolID, updateReq)
if err != nil {
log.Printf("[ERROR] Update pool %s failed: %v", poolID, err)
return err
}
err = checkLoadbalancerPoolActiveStatus(client, poolID, lbID)
if err != nil {
return err
}
}
// Create health monitor
healthMonitorPayload := getCreateHealthMonitorPayloadFromConfig(d)
if healthMonitorPayload != nil {
_, err = client.HealthMonitor.Create(context.Background(), poolID, healthMonitorPayload)
if err != nil {
log.Printf("[ERROR] Create health monitor for pool %s failed: %+v", poolID, err)
return err
}
err = checkLoadbalancerPoolActiveStatus(client, pool.ID, lbID)
if err != nil {
return err
}
}
return resourceBizflyCloudLoadBalancerPoolRead(d, meta)
}
// Read pool resource
func resourceBizflyCloudLoadBalancerPoolRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).gobizflyClient()
pool, err := client.Pool.Get(context.Background(), d.Id())
if err != nil {
return fmt.Errorf("Error when retrieving load balancer pool %s: %v", d.Id(), err)
}
if err != nil {
return fmt.Errorf("Error when list vpc networks: %v", err)
}
healthMonitor := convertHealthMonitor(pool.HealthMonitor)
persistent := convertSessionPersistent(pool.SessionPersistence)
members := convertMember(pool.Members)
_ = d.Set("name", pool.Name)
_ = d.Set("algorithm", pool.LBAlgorithm)
_ = d.Set("description", pool.Description)
_ = d.Set("protocol", pool.Protocol)
_ = d.Set("load_balancer_id", pool.LoadBalancers[0].ID)
_ = d.Set("health_monitor", healthMonitor)
_ = d.Set("persistent", persistent)
_ = d.Set("members", members)
return nil
}
// Change pool resource
func resourceBizflyCloudLoadBalancerPoolUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).gobizflyClient()
poolID := d.Id()
lbID := d.Get("load_balancer_id").(string)
err := checkLoadbalancerPoolActiveStatus(client, poolID, lbID)
if err != nil {
return err
}
// Update pool
updateReq := getUpdatePoolPayloadFromConfig(d)
_, err = client.Pool.Update(context.Background(), poolID, &updateReq)
if err != nil {
log.Printf("[ERROR] Update pool %s failed: %+v", poolID, err)
return err
}
err = checkLoadbalancerPoolActiveStatus(client, poolID, lbID)
if err != nil {
return err
}
// update health monitor of pool
if d.HasChange("health_monitor") {
healthMonitorID, healthMonitorReq := getUpdateHealthMonitorFromConfig(d)
if healthMonitorReq != nil {
if healthMonitorID == "" {
// Create health monitor for pool
createHealthMonitorReq := getCreateHealthMonitorPayloadFromConfig(d)
_, err = client.HealthMonitor.Create(context.Background(), poolID, createHealthMonitorReq)
if err != nil {
log.Printf("[ERROR] Create health monitor %s failed: %v", healthMonitorID, err)
return err
}
} else {
// Update health monitor for pool
_, err = client.HealthMonitor.Update(context.Background(), healthMonitorID, healthMonitorReq)
if err != nil {
log.Printf("[ERROR] Update health monitor %s failed: %v", healthMonitorID, err)
return err
}
}
err = checkLoadbalancerPoolActiveStatus(client, poolID, lbID)
if err != nil {
return err
}
}
}
// Update merbers of pool
if d.HasChange("members") {
oldMembers, newMembers := d.GetChange("members")
// Delete all current members of pool
for _, member := range oldMembers.([]interface{}) {
castedMember := member.(map[string]interface{})
memberID := castedMember["id"].(string)
if err != nil {
log.Printf("[ERROR] check pool active status before delete member")
return err
}
err = client.Member.Delete(context.Background(), poolID, memberID)
if err != nil {
return fmt.Errorf("Error when delete old member %s: %v", memberID, err)
}
err = checkLoadbalancerPoolActiveStatus(client, poolID, lbID)
if err != nil {
return err
}
}
// Create new members for pool
for _, member := range newMembers.([]interface{}) {
castedMember := member.(map[string]interface{})
updateMemberReq := &gobizfly.MemberCreateRequest{
Name: castedMember["name"].(string),
Weight: castedMember["weight"].(int),
Address: castedMember["address"].(string),
ProtocolPort: castedMember["protocol_port"].(int),
Backup: castedMember["backup"].(bool),
}
_, err = client.Member.Create(context.Background(), poolID, updateMemberReq)
if err != nil {
return fmt.Errorf("Error when create new member: %v", err)
}
err = checkLoadbalancerPoolActiveStatus(client, poolID, lbID)
if err != nil {
return err
}
}
}
return resourceBizflyCloudLoadBalancerPoolRead(d, meta)
}
// Delete pool resource
func resourceBizflyCloudLoadBalancerPoolDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).gobizflyClient()
lbID := d.Get("load_balancer_id").(string)
_, _ = waitLoadbalancerActiveProvisioningStatus(client, lbID, loadbalancerResource)
err := client.Pool.Delete(context.Background(), d.Id())
if err != nil {
return fmt.Errorf("Error when deleting load balancer pool: %v", err)
}
return nil
}
// Get create pool payload from config
func getCreatePoolPayloadFromConfig(d *schema.ResourceData) gobizfly.PoolCreateRequest {
poolName := d.Get("name").(string)
poolMembersReq := getMembersPayloadFromConfig(d)
stickySessionReq := getSessionPersistentPayloadFromConfig(d)
poolReq := gobizfly.PoolCreateRequest{
Name: &poolName,
LBAlgorithm: d.Get("algorithm").(string),
Protocol: d.Get("protocol").(string),
Members: poolMembersReq,
SessionPersistence: stickySessionReq,
}
return poolReq
}
// Get update pool payload from config
func getUpdatePoolPayloadFromConfig(d *schema.ResourceData) gobizfly.PoolUpdateRequest {
poolReq := gobizfly.PoolUpdateRequest{}
if d.HasChange("name") {
_, newName := d.GetChange("name")
castedNewName := newName.(string)
poolReq.Name = &castedNewName
}
if d.HasChange("description") {
_, newDescription := d.GetChange("description")
castedNewDescription := newDescription.(string)
poolReq.Description = &castedNewDescription
}
if d.HasChange("algorithm") {
_, newAlgorithm := d.GetChange("algorithm")
castedNewAlgorithm := newAlgorithm.(string)
poolReq.LBAlgorithm = &castedNewAlgorithm
}
persistent := getSessionPersistentPayloadFromConfig(d)
poolReq.SessionPersistence = persistent
return poolReq
}
// Get create health monitor payload from config
func getCreateHealthMonitorPayloadFromConfig(d *schema.ResourceData) *gobizfly.HealthMonitorCreateRequest {
healthMonitors := d.Get("health_monitor").([]interface{})
if len(healthMonitors) == 0 {
return nil
}
healthMonitor := healthMonitors[0].(map[string]interface{})
healthMonitorType := healthMonitor["type"].(string)
healthMonitorReq := gobizfly.HealthMonitorCreateRequest{
Name: healthMonitor["name"].(string),
Type: healthMonitorType,
TimeOut: healthMonitor["timeout"].(int),
PoolID: d.Id(),
Delay: healthMonitor["delay"].(int),
MaxRetries: healthMonitor["max_retries"].(int),
MaxRetriesDown: healthMonitor["max_retries_down"].(int),
}
if (healthMonitorType != constants.TcpProtocol) &&
(healthMonitorType != constants.UdpConnectProtocol) {
healthMonitorReq.HTTPMethod = healthMonitor["http_method"].(string)
healthMonitorReq.URLPath = healthMonitor["url_path"].(string)
healthMonitorReq.ExpectedCodes = healthMonitor["expected_code"].(string)
}
return &healthMonitorReq
}
// Get update health monitor payload from config
func getUpdateHealthMonitorFromConfig(d *schema.ResourceData) (string, *gobizfly.HealthMonitorUpdateRequest) {
healthMonitor := d.Get("health_monitor").([]interface{})
healthMonitorLen := len(healthMonitor)
if healthMonitorLen == 0 {
return "", nil
}
healthMonitorMap := healthMonitor[0].(map[string]interface{})
updateReq := gobizfly.HealthMonitorUpdateRequest{
Name: healthMonitorMap["name"].(string),
}
timeoutInt := healthMonitorMap["timeout"].(int)
if timeoutInt != 0 {
updateReq.TimeOut = &timeoutInt
}
delayInt := healthMonitorMap["delay"].(int)
if delayInt != 0 {
updateReq.Delay = &delayInt
}
maxRetriesInt := healthMonitorMap["max_retries"].(int)
if maxRetriesInt != 0 {
updateReq.MaxRetries = &maxRetriesInt
}
maxRetriesDownInt := healthMonitorMap["max_retries_down"].(int)
if maxRetriesDownInt != 0 {
updateReq.MaxRetriesDown = &maxRetriesDownInt
}
healthMonitorType := healthMonitorMap["type"].(string)
if (healthMonitorType != constants.TcpProtocol) &&
(healthMonitorType != constants.UdpConnectProtocol) {
httpMethodStr := healthMonitorMap["http_method"].(string)
if httpMethodStr != "" {
updateReq.HTTPMethod = &httpMethodStr
}
urlPathStr := healthMonitorMap["url_path"].(string)
if urlPathStr != "" {
updateReq.URLPath = &urlPathStr
}
expectedCodeStr := healthMonitorMap["expected_code"].(string)
if expectedCodeStr != "" {
updateReq.ExpectedCodes = &expectedCodeStr
}
}
poolHealthMonitorId := healthMonitorMap["id"].(string)
log.Printf("[DEBUG] Update health monitor %s payload: %+v", poolHealthMonitorId, updateReq)
return poolHealthMonitorId, &updateReq
}
// Get session persistent payload from config
func getSessionPersistentPayloadFromConfig(d *schema.ResourceData) *gobizfly.SessionPersistence {
sessionPersistents := d.Get("persistent").([]interface{})
if len(sessionPersistents) == 0 {
return nil
}
sessionPersistent := sessionPersistents[0].(map[string]interface{})
sesstionType := sessionPersistent["type"].(string)
sessionPersistentReq := gobizfly.SessionPersistence{
Type: sesstionType,
}
if sesstionType == constants.AppCookie {
cookieName := sessionPersistent["cookie_name"].(string)
sessionPersistentReq.CookieName = &cookieName
}
return &sessionPersistentReq
}
// Get members payload from config
func getMembersPayloadFromConfig(d *schema.ResourceData) []gobizfly.PoolMemberRequest {
poolMembers := d.Get("members").([]interface{})
if len(poolMembers) == 0 {
return nil
}
poolMembersReq := make([]gobizfly.PoolMemberRequest, 0)
for idx, member := range poolMembers {
castedMember := member.(map[string]interface{})
memberReq := gobizfly.PoolMemberRequest{
ID: idx,
Name: castedMember["name"].(string),
Address: castedMember["address"].(string),
Weight: castedMember["weight"].(int),
Port: castedMember["protocol_port"].(int),
}
poolMembersReq = append(poolMembersReq, memberReq)
}
return poolMembersReq
}
// Convert members to update state
func convertMember(members []gobizfly.Member) []map[string]interface{} {
result := make([]map[string]interface{}, len(members))
for i, v := range members {
result[i] = map[string]interface{}{
"id": v.ID,
"name": v.Name,
"weight": v.Weight,
"address": v.Address,
"protocol_port": v.ProtocolPort,
"backup": v.Backup,
"operating_status": v.OperatingStatus,
"provisioning_status": v.ProvisoningStatus,
"subnet_id": v.SubnetID,
"project_id": v.ProjectID,
"created_at": v.CreatedAt,
"updated_at": v.UpdatedAt,
}
}
return result
}
// Convert health monitor to update state
func convertHealthMonitor(healthMonitor *gobizfly.HealthMonitor) []map[string]interface{} {
results := make([]map[string]interface{}, 0, 1)
if healthMonitor == nil {
return nil
}
result := map[string]interface{}{
"id": healthMonitor.ID,
"name": healthMonitor.Name,
"type": healthMonitor.Type,
"timeout": healthMonitor.TimeOut,
"max_retries": healthMonitor.MaxRetries,
"max_retries_down": healthMonitor.MaxRetriesDown,
"delay": healthMonitor.Delay,
"http_method": healthMonitor.HTTPMethod,
"url_path": healthMonitor.UrlPath,
"expected_code": healthMonitor.ExpectedCodes,
"operating_status": healthMonitor.OperatingStatus,
"provisioning_status": healthMonitor.ProvisioningStatus,
"created_at": healthMonitor.CreatedAt,
"updated_at": healthMonitor.UpdatedAt,
}
results = append(results, result)
return results
}
// Convert session persistent to update state
func convertSessionPersistent(sessionPersistent *gobizfly.SessionPersistence) []map[string]interface{} {
results := make([]map[string]interface{}, 0, 1)
if sessionPersistent == nil {
return nil
}
result := map[string]interface{}{
"type": sessionPersistent.Type,
"cookie_name": sessionPersistent.CookieName,
}
results = append(results, result)
return results
}
// Wait loadbalancer pool active status
func waitPoolActiveProvisioningStatus(client *gobizfly.Client, poolID string) (*gobizfly.Pool, error) {
var pool *gobizfly.Pool
backoff := wait.Backoff{
Duration: loadbalancerActiveInitDelay,
Factor: loadbalancerActiveFactor,
Steps: loadbalancerActiveSteps,
}
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
pool, err := client.Pool.Get(context.Background(), poolID)
if err != nil {
return true, err
}
if pool.ProvisoningStatus == activeStatus {
return true, nil
} else if pool.ProvisoningStatus == errorStatus {
return true, fmt.Errorf("Loadbalancer Pool %s has gone into ERROR state", poolID)
} else {
return false, nil
}
})
if err != nil {
if err == wait.ErrWaitTimeout {
err = fmt.Errorf("Loadbalancer Pool %s failed to go into ACTIVE provisioning status within allotted time", poolID)
}
return nil, err
}
return pool, err
}
// Check loadbalancer and pool resource active status
func checkLoadbalancerPoolActiveStatus(client *gobizfly.Client, poolID, loadbalancerID string) error {
_, err := waitPoolActiveProvisioningStatus(client, poolID)
if err != nil {
log.Printf("[ERROR] wait pool %s active status: %v", poolID, err)
return err
}
_, err = waitLoadbalancerActiveProvisioningStatus(client, loadbalancerID, loadbalancerResource)
if err != nil {
log.Printf("[ERROR] wait loadbalancer %s active status: %v", loadbalancerID, err)
return err
}
return nil
}