/
helpers_lb.go
658 lines (569 loc) · 19.3 KB
/
helpers_lb.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
package scaleway
import (
"bytes"
"context"
"errors"
"fmt"
"net"
"reflect"
"strings"
"time"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
lbSDK "github.com/scaleway/scaleway-sdk-go/api/lb/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
validator "github.com/scaleway/scaleway-sdk-go/validation"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/transport"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
)
const (
defaultLbLbTimeout = 10 * time.Minute
RetryLbIPInterval = 5 * time.Second
)
// lbAPIWithZone returns an lb API WITH zone for a Create request
func lbAPIWithZone(d *schema.ResourceData, m interface{}) (*lbSDK.ZonedAPI, scw.Zone, error) {
lbAPI := lbSDK.NewZonedAPI(meta.ExtractScwClient(m))
zone, err := meta.ExtractZone(d, m)
if err != nil {
return nil, "", err
}
return lbAPI, zone, nil
}
// LbAPIWithZoneAndID returns an lb API with zone and ID extracted from the state
func LbAPIWithZoneAndID(m interface{}, id string) (*lbSDK.ZonedAPI, scw.Zone, string, error) {
lbAPI := lbSDK.NewZonedAPI(meta.ExtractScwClient(m))
zone, ID, err := zonal.ParseID(id)
if err != nil {
return nil, "", "", err
}
return lbAPI, zone, ID, nil
}
func flattenLbBackendMarkdownAction(action lbSDK.OnMarkedDownAction) interface{} {
if action == lbSDK.OnMarkedDownActionOnMarkedDownActionNone {
return "none"
}
return action.String()
}
func flattenLbACL(acl *lbSDK.ACL) interface{} {
res := map[string]interface{}{
"name": acl.Name,
"match": flattenLbACLMatch(acl.Match),
"action": flattenLbACLAction(acl.Action),
}
return res
}
// expandLbACL transforms a state acl to an api one.
func expandLbACL(i interface{}) *lbSDK.ACL {
rawRule := i.(map[string]interface{})
acl := &lbSDK.ACL{
Name: rawRule["name"].(string),
Description: rawRule["description"].(string),
Match: expandLbACLMatch(rawRule["match"]),
Action: expandLbACLAction(rawRule["action"]),
CreatedAt: expandTimePtr(rawRule["created_at"]),
UpdatedAt: expandTimePtr(rawRule["updated_at"]),
}
if rawRule["index"] != nil {
acl.Index = int32(rawRule["index"].(int))
}
// remove http filter values if we do not pass any http filter
if acl.Match.HTTPFilter == "" || acl.Match.HTTPFilter == lbSDK.ACLHTTPFilterACLHTTPFilterNone {
acl.Match.HTTPFilter = lbSDK.ACLHTTPFilterACLHTTPFilterNone
acl.Match.HTTPFilterValue = []*string{}
}
return acl
}
func flattenLbACLAction(action *lbSDK.ACLAction) interface{} {
return []map[string]interface{}{
{
"type": action.Type,
"redirect": flattenLbACLActionRedirect(action.Redirect),
},
}
}
func expandLbACLAction(raw interface{}) *lbSDK.ACLAction {
if raw == nil || len(raw.([]interface{})) != 1 {
return nil
}
rawMap := raw.([]interface{})[0].(map[string]interface{})
return &lbSDK.ACLAction{
Type: lbSDK.ACLActionType(rawMap["type"].(string)),
Redirect: expandLbACLActionRedirect(rawMap["redirect"]),
}
}
func flattenLbACLActionRedirect(redirect *lbSDK.ACLActionRedirect) interface{} {
if redirect == nil {
return nil
}
return []map[string]interface{}{
{
"type": redirect.Type,
"target": redirect.Target,
"code": redirect.Code,
},
}
}
func expandLbACLActionRedirect(raw interface{}) *lbSDK.ACLActionRedirect {
if raw == nil || len(raw.([]interface{})) != 1 {
return nil
}
rawMap := raw.([]interface{})[0].(map[string]interface{})
return &lbSDK.ACLActionRedirect{
Type: lbSDK.ACLActionRedirectRedirectType(rawMap["type"].(string)),
Target: rawMap["target"].(string),
Code: types.ExpandInt32Ptr(rawMap["code"]),
}
}
func expandPrivateNetworks(data interface{}) ([]*lbSDK.PrivateNetwork, error) {
if data == nil {
return nil, nil
}
pns := []*lbSDK.PrivateNetwork(nil)
for _, pn := range data.(*schema.Set).List() {
rawPn := pn.(map[string]interface{})
privateNetwork := &lbSDK.PrivateNetwork{}
privateNetwork.PrivateNetworkID = locality.ExpandID(rawPn["private_network_id"].(string))
if staticConfig, hasStaticConfig := rawPn["static_config"]; hasStaticConfig && len(staticConfig.([]interface{})) > 0 {
privateNetwork.StaticConfig = expandLbPrivateNetworkStaticConfig(staticConfig)
} else {
privateNetwork.DHCPConfig = expandLbPrivateNetworkDHCPConfig(rawPn["dhcp_config"])
}
pns = append(pns, privateNetwork)
}
return pns, nil
}
func IsPrivateNetworkEqual(a, b interface{}) bool {
// Find out the diff Private Network or not
if _, ok := a.(*lbSDK.PrivateNetwork); ok {
if _, ok := b.(*lbSDK.PrivateNetwork); ok {
if a.(*lbSDK.PrivateNetwork).PrivateNetworkID == b.(*lbSDK.PrivateNetwork).PrivateNetworkID {
// if both has dhcp config should not update
if a.(*lbSDK.PrivateNetwork).DHCPConfig != nil && b.(*lbSDK.PrivateNetwork).DHCPConfig != nil {
return true
}
// check static config
aConfig := a.(*lbSDK.PrivateNetwork).StaticConfig
bConfig := b.(*lbSDK.PrivateNetwork).StaticConfig
if aConfig != nil && bConfig != nil {
// check if static config is different
return reflect.DeepEqual(aConfig.IPAddress, bConfig.IPAddress)
}
}
}
}
return false
}
func privateNetworksCompare(slice1, slice2 []*lbSDK.PrivateNetwork) []*lbSDK.PrivateNetwork {
var diff []*lbSDK.PrivateNetwork
m := make(map[string]struct{}, len(slice1))
for _, pn := range slice1 {
m[pn.PrivateNetworkID] = struct{}{}
}
// find the differences
for _, pn := range slice2 {
if _, foundID := m[pn.PrivateNetworkID]; !foundID || (foundID && !IsPrivateNetworkEqual(slice1, slice2)) {
diff = append(diff, pn)
}
}
return diff
}
func flattenPrivateNetworkConfigs(privateNetworks []*lbSDK.PrivateNetwork) interface{} {
if len(privateNetworks) == 0 || privateNetworks == nil {
return nil
}
pnI := []map[string]interface{}(nil)
var dhcpConfigExist bool
for _, pn := range privateNetworks {
if pn.DHCPConfig != nil {
dhcpConfigExist = true
}
pnRegion, err := pn.LB.Zone.Region()
if err != nil {
return diag.FromErr(err)
}
pnRegionalID := regional.NewIDString(pnRegion, pn.PrivateNetworkID)
pnI = append(pnI, map[string]interface{}{
"private_network_id": pnRegionalID,
"dhcp_config": dhcpConfigExist,
"status": pn.Status.String(),
"zone": pn.LB.Zone.String(),
"static_config": flattenLbPrivateNetworkStaticConfig(pn.StaticConfig),
})
}
return pnI
}
func flattenLbACLMatch(match *lbSDK.ACLMatch) interface{} {
if match == nil {
return nil
}
return []map[string]interface{}{
{
"ip_subnet": types.FlattenSliceStringPtr(match.IPSubnet),
"http_filter": match.HTTPFilter.String(),
"http_filter_value": types.FlattenSliceStringPtr(match.HTTPFilterValue),
"http_filter_option": match.HTTPFilterOption,
"invert": match.Invert,
},
}
}
func expandLbACLMatch(raw interface{}) *lbSDK.ACLMatch {
if raw == nil || len(raw.([]interface{})) != 1 {
return nil
}
rawMap := raw.([]interface{})[0].(map[string]interface{})
// scaleway api require ip subnet, so if we did not specify one, just put 0.0.0.0/0 instead
ipSubnet := types.ExpandSliceStringPtr(rawMap["ip_subnet"].([]interface{}))
if len(ipSubnet) == 0 {
ipSubnet = []*string{types.ExpandStringPtr("0.0.0.0/0")}
}
return &lbSDK.ACLMatch{
IPSubnet: ipSubnet,
HTTPFilter: lbSDK.ACLHTTPFilter(rawMap["http_filter"].(string)),
HTTPFilterValue: types.ExpandSliceStringPtr(rawMap["http_filter_value"].([]interface{})),
HTTPFilterOption: types.ExpandStringPtr(rawMap["http_filter_option"].(string)),
Invert: rawMap["invert"].(bool),
}
}
func expandLbBackendMarkdownAction(raw interface{}) lbSDK.OnMarkedDownAction {
if raw == "none" {
return lbSDK.OnMarkedDownActionOnMarkedDownActionNone
}
return lbSDK.OnMarkedDownAction(raw.(string))
}
func flattenLbProtocol(protocol lbSDK.Protocol) interface{} {
return protocol.String()
}
func expandLbProtocol(raw interface{}) lbSDK.Protocol {
return lbSDK.Protocol(raw.(string))
}
func flattenLbForwardPortAlgorithm(algo lbSDK.ForwardPortAlgorithm) interface{} {
return algo.String()
}
func expandLbForwardPortAlgorithm(raw interface{}) lbSDK.ForwardPortAlgorithm {
return lbSDK.ForwardPortAlgorithm(raw.(string))
}
func flattenLbStickySessionsType(t lbSDK.StickySessionsType) interface{} {
return t.String()
}
func expandLbStickySessionsType(raw interface{}) lbSDK.StickySessionsType {
return lbSDK.StickySessionsType(raw.(string))
}
func flattenLbHCTCP(config *lbSDK.HealthCheckTCPConfig) interface{} {
if config == nil {
return nil
}
return []map[string]interface{}{
{},
}
}
func expandLbHCTCP(raw interface{}) *lbSDK.HealthCheckTCPConfig {
if raw == nil || len(raw.([]interface{})) != 1 {
return nil
}
return &lbSDK.HealthCheckTCPConfig{}
}
func flattenLbHCHTTP(config *lbSDK.HealthCheckHTTPConfig) interface{} {
if config == nil {
return nil
}
return []map[string]interface{}{
{
"uri": config.URI,
"method": config.Method,
"code": types.FlattenInt32Ptr(config.Code),
"host_header": config.HostHeader,
},
}
}
func expandLbHCHTTP(raw interface{}) *lbSDK.HealthCheckHTTPConfig {
if raw == nil || len(raw.([]interface{})) != 1 {
return nil
}
rawMap := raw.([]interface{})[0].(map[string]interface{})
return &lbSDK.HealthCheckHTTPConfig{
URI: rawMap["uri"].(string),
Method: rawMap["method"].(string),
Code: types.ExpandInt32Ptr(rawMap["code"]),
HostHeader: rawMap["host_header"].(string),
}
}
func flattenLbHCHTTPS(config *lbSDK.HealthCheckHTTPSConfig) interface{} {
if config == nil {
return nil
}
return []map[string]interface{}{
{
"uri": config.URI,
"method": config.Method,
"code": types.FlattenInt32Ptr(config.Code),
"host_header": config.HostHeader,
"sni": config.Sni,
},
}
}
func expandLbHCHTTPS(raw interface{}) *lbSDK.HealthCheckHTTPSConfig {
if raw == nil || len(raw.([]interface{})) != 1 {
return nil
}
rawMap := raw.([]interface{})[0].(map[string]interface{})
return &lbSDK.HealthCheckHTTPSConfig{
URI: rawMap["uri"].(string),
Method: rawMap["method"].(string),
Code: types.ExpandInt32Ptr(rawMap["code"]),
HostHeader: rawMap["host_header"].(string),
Sni: rawMap["sni"].(string),
}
}
func expandLbLetsEncrypt(raw interface{}) *lbSDK.CreateCertificateRequestLetsencryptConfig {
if raw == nil || len(raw.([]interface{})) != 1 {
return nil
}
rawMap := raw.([]interface{})[0].(map[string]interface{})
alternativeNames := rawMap["subject_alternative_name"].([]interface{})
config := &lbSDK.CreateCertificateRequestLetsencryptConfig{
CommonName: rawMap["common_name"].(string),
}
for _, alternativeName := range alternativeNames {
config.SubjectAlternativeName = append(config.SubjectAlternativeName, alternativeName.(string))
}
return config
}
func expandLbCustomCertificate(raw interface{}) *lbSDK.CreateCertificateRequestCustomCertificate {
if raw == nil || len(raw.([]interface{})) != 1 {
return nil
}
rawMap := raw.([]interface{})[0].(map[string]interface{})
config := &lbSDK.CreateCertificateRequestCustomCertificate{
CertificateChain: rawMap["certificate_chain"].(string),
}
return config
}
func expandLbProxyProtocol(raw interface{}) lbSDK.ProxyProtocol {
return lbSDK.ProxyProtocol("proxy_protocol_" + raw.(string))
}
func flattenLbProxyProtocol(pp lbSDK.ProxyProtocol) interface{} {
return strings.TrimPrefix(pp.String(), "proxy_protocol_")
}
func lbUpgradeV1SchemaType() cty.Type {
return cty.Object(map[string]cty.Type{
"id": cty.String,
})
}
// lbUpgradeV1UpgradeFunc allow upgrade the from regional to a zoned resource.
func LbUpgradeV1SchemaUpgradeFunc(_ context.Context, rawState map[string]interface{}, _ interface{}) (map[string]interface{}, error) {
var err error
// element id: upgrade
ID, exist := rawState["id"]
if !exist {
return nil, errors.New("upgrade: id not exist")
}
rawState["id"], err = lbUpgradeV1RegionalToZonedID(ID.(string))
if err != nil {
return nil, err
}
// return rawState updated
return rawState, nil
}
func lbUpgradeV1RegionalToZonedID(element string) (string, error) {
loc, id, err := locality.ParseLocalizedID(element)
// return error if l cannot be parsed
if err != nil {
return "", fmt.Errorf("upgrade: could not retrieve the locality from `%s`", element)
}
// if locality is already zoned return
if validator.IsZone(loc) {
return element, nil
}
// append zone 1 as default: e.g. fr-par-1
return fmt.Sprintf("%s-1/%s", loc, id), nil
}
func expandLbPrivateNetworkStaticConfig(raw interface{}) *lbSDK.PrivateNetworkStaticConfig {
if raw == nil || len(raw.([]interface{})) < 1 {
return nil
}
return &lbSDK.PrivateNetworkStaticConfig{
IPAddress: types.ExpandStringsPtr(raw),
}
}
func flattenLbPrivateNetworkStaticConfig(cfg *lbSDK.PrivateNetworkStaticConfig) []string {
if cfg == nil {
return nil
}
return *cfg.IPAddress
}
func expandLbPrivateNetworkDHCPConfig(raw interface{}) *lbSDK.PrivateNetworkDHCPConfig {
if raw == nil || !raw.(bool) {
return nil
}
return &lbSDK.PrivateNetworkDHCPConfig{}
}
func waitForLB(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, lbID string, timeout time.Duration) (*lbSDK.LB, error) {
retryInterval := DefaultWaitLBRetryInterval
if transport.DefaultWaitRetryInterval != nil {
retryInterval = *transport.DefaultWaitRetryInterval
}
loadBalancer, err := lbAPI.WaitForLb(&lbSDK.ZonedAPIWaitForLBRequest{
LBID: lbID,
Zone: zone,
Timeout: scw.TimeDurationPtr(timeout),
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))
return loadBalancer, err
}
func waitForLbInstances(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, lbID string, timeout time.Duration) (*lbSDK.LB, error) {
retryInterval := DefaultWaitLBRetryInterval
if transport.DefaultWaitRetryInterval != nil {
retryInterval = *transport.DefaultWaitRetryInterval
}
loadBalancer, err := lbAPI.WaitForLbInstances(&lbSDK.ZonedAPIWaitForLBInstancesRequest{
Zone: zone,
LBID: lbID,
Timeout: scw.TimeDurationPtr(timeout),
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))
return loadBalancer, err
}
func waitForLBPN(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, lbID string, timeout time.Duration) ([]*lbSDK.PrivateNetwork, error) {
retryInterval := DefaultWaitLBRetryInterval
if transport.DefaultWaitRetryInterval != nil {
retryInterval = *transport.DefaultWaitRetryInterval
}
privateNetworks, err := lbAPI.WaitForLBPN(&lbSDK.ZonedAPIWaitForLBPNRequest{
LBID: lbID,
Zone: zone,
Timeout: scw.TimeDurationPtr(timeout),
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))
return privateNetworks, err
}
func waitForLBCertificate(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, id string, timeout time.Duration) (*lbSDK.Certificate, error) {
retryInterval := DefaultWaitLBRetryInterval
if transport.DefaultWaitRetryInterval != nil {
retryInterval = *transport.DefaultWaitRetryInterval
}
certificate, err := lbAPI.WaitForLBCertificate(&lbSDK.ZonedAPIWaitForLBCertificateRequest{
CertID: id,
Zone: zone,
Timeout: scw.TimeDurationPtr(timeout),
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))
return certificate, err
}
func attachLBPrivateNetworks(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, pnConfigs []*lbSDK.PrivateNetwork, lbID string, timeout time.Duration) ([]*lbSDK.PrivateNetwork, error) {
var privateNetworks []*lbSDK.PrivateNetwork
for i := range pnConfigs {
pn, err := lbAPI.AttachPrivateNetwork(&lbSDK.ZonedAPIAttachPrivateNetworkRequest{
Zone: zone,
LBID: lbID,
PrivateNetworkID: pnConfigs[i].PrivateNetworkID,
StaticConfig: pnConfigs[i].StaticConfig,
DHCPConfig: pnConfigs[i].DHCPConfig,
}, scw.WithContext(ctx))
if err != nil && !httperrors.Is404(err) {
return nil, err
}
privateNetworks, err = waitForLBPN(ctx, lbAPI, zone, pn.LB.ID, timeout)
if err != nil && !httperrors.Is404(err) {
return nil, err
}
for _, pn := range privateNetworks {
if pn.Status == lbSDK.PrivateNetworkStatusError {
err = lbAPI.DetachPrivateNetwork(&lbSDK.ZonedAPIDetachPrivateNetworkRequest{
Zone: zone,
LBID: pn.LB.ID,
PrivateNetworkID: pn.PrivateNetworkID,
}, scw.WithContext(ctx))
if err != nil && !httperrors.Is404(err) {
return nil, err
}
tflog.Debug(ctx, fmt.Sprintf("DHCP config: %v", pn.DHCPConfig))
tflog.Debug(ctx, fmt.Sprintf("Static config: %v", pn.StaticConfig))
return nil, fmt.Errorf("attaching private network with id: %s on error state. please check your config", pn.PrivateNetworkID)
}
}
}
return privateNetworks, nil
}
func flattenLbInstances(instances []*lbSDK.Instance) interface{} {
if instances == nil {
return nil
}
flattenedInstances := []map[string]interface{}(nil)
for _, instance := range instances {
flattenedInstances = append(flattenedInstances, map[string]interface{}{
"id": instance.ID,
"status": instance.Status.String(),
"ip_address": instance.IPAddress,
"created_at": types.FlattenTime(instance.CreatedAt),
"updated_at": types.FlattenTime(instance.UpdatedAt),
"zone": instance.Zone,
})
}
return flattenedInstances
}
func flattenLbIPs(ips []*lbSDK.IP) interface{} {
if ips == nil {
return nil
}
flattenedIPs := []map[string]interface{}(nil)
for _, ip := range ips {
flattenedIPs = append(flattenedIPs, map[string]interface{}{
"id": ip.ID,
"ip_address": ip.IPAddress,
"reverse": ip.Reverse,
"organization_id": ip.OrganizationID,
"project_id": ip.ProjectID,
"zone": ip.Zone,
"lb_id": types.FlattenStringPtr(ip.LBID),
})
}
return flattenedIPs
}
func ipv4Match(cidr, ipStr string) bool {
_, cidrNet, err := net.ParseCIDR(cidr)
if err != nil {
return false
}
ip := net.ParseIP(ipStr)
return cidrNet.Contains(ip)
}
func lbPrivateNetworkSetHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
if pnID, ok := m["private_network_id"]; ok {
buf.WriteString(locality.ExpandID(pnID))
}
if staticConfig, ok := m["static_config"]; ok && len(staticConfig.([]interface{})) > 0 {
for _, item := range staticConfig.([]interface{}) {
buf.WriteString(item.(string))
}
}
return StringHashcode(buf.String())
}
func diffSuppressFunc32SubnetMask(k, _, _ string, d *schema.ResourceData) bool {
baseKey := extractBaseKey(k)
oldList, newList := getStringListsFromState(baseKey, d)
oldList = normalizeIPSubnetList(oldList)
newList = normalizeIPSubnetList(newList)
return compareStringListsIgnoringOrder(oldList, newList)
}
func normalizeIPSubnetList(list []string) []string {
normalized := make([]string, len(list))
for i, ip := range list {
normalized[i] = normalizeIPSubnet(ip)
}
return normalized
}
func normalizeIPSubnet(ip string) string {
if strings.HasSuffix(ip, "/32") {
return strings.TrimSuffix(ip, "/32")
}
return ip
}