-
Notifications
You must be signed in to change notification settings - Fork 649
/
resource_ibm_cbr_zone.go
694 lines (641 loc) · 25.7 KB
/
resource_ibm_cbr_zone.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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
// Copyright IBM Corp. 2022 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package contextbasedrestrictions
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/platform-services-go-sdk/contextbasedrestrictionsv1"
)
const (
cbrZoneReadPending = "pending"
cbrZoneReadComplete = "finished"
cbrZoneReadError = "error"
)
func ResourceIBMCbrZone() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMCbrZoneCreate,
ReadContext: resourceIBMCbrZoneRead,
UpdateContext: resourceIBMCbrZoneUpdate,
DeleteContext: resourceIBMCbrZoneDelete,
Importer: &schema.ResourceImporter{},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(2 * time.Minute),
Update: schema.DefaultTimeout(2 * time.Minute),
Delete: schema.DefaultTimeout(2 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.InvokeValidator("ibm_cbr_zone", "name"),
Description: "The name of the zone.",
},
"account_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.InvokeValidator("ibm_cbr_zone", "account_id"),
Description: "The id of the account owning this zone.",
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.InvokeValidator("ibm_cbr_zone", "description"),
Description: "The description of the zone.",
},
"addresses": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Description: "The list of addresses in the zone.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The type of address.",
},
"value": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The IP address.",
},
"ref": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "A service reference value.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"account_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The id of the account owning the service.",
},
"service_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The service type.",
},
"service_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The service name.",
},
"service_instance": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The service instance.",
},
"location": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The location.",
},
},
},
},
},
},
},
"excluded": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Description: "The list of excluded addresses in the zone. Only addresses of type `ipAddress`, `ipRange`, and `subnet` can be excluded.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The type of address.",
},
"value": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The IP address.",
},
},
},
},
"x_correlation_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.InvokeValidator("ibm_cbr_zone", "x_correlation_id"),
Description: "The supplied or generated value of this header is logged for a request and repeated in a response header for the corresponding response. The same value is used for downstream requests and retries of those requests. If a value of this headers is not supplied in a request, the service generates a random (version 4) UUID.",
},
"transaction_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.InvokeValidator("ibm_cbr_zone", "transaction_id"),
Description: "The `Transaction-Id` header behaves as the `X-Correlation-Id` header. It is supported for backward compatibility with other IBM platform services that support the `Transaction-Id` header only. If both `X-Correlation-Id` and `Transaction-Id` are provided, `X-Correlation-Id` has the precedence over `Transaction-Id`.",
},
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The zone CRN.",
},
"address_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The number of addresses in the zone.",
},
"excluded_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The number of excluded addresses in the zone.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The href link to the resource.",
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The time the resource was created.",
},
"created_by_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "IAM ID of the user or service which created the resource.",
},
"last_modified_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The last time the resource was modified.",
},
"last_modified_by_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "IAM ID of the user or service which modified the resource.",
},
"version": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func ResourceIBMCbrZoneValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "name",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Optional: true,
Regexp: `^[a-zA-Z0-9 \-_]+$`,
MinValueLength: 1,
MaxValueLength: 128,
},
validate.ValidateSchema{
Identifier: "account_id",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Optional: true,
Regexp: `^[a-zA-Z0-9\-]+$`,
MinValueLength: 1,
MaxValueLength: 128,
},
validate.ValidateSchema{
Identifier: "description",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Optional: true,
Regexp: `^[\x20-\xFE]*$`,
MinValueLength: 0,
MaxValueLength: 300,
},
validate.ValidateSchema{
Identifier: "x_correlation_id",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Optional: true,
Regexp: `^[a-zA-Z0-9 ,\-_]+$`,
MinValueLength: 1,
MaxValueLength: 1024,
},
validate.ValidateSchema{
Identifier: "transaction_id",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Optional: true,
Regexp: `^[a-zA-Z0-9 ,\-_]+$`,
MinValueLength: 1,
MaxValueLength: 1024,
},
)
resourceValidator := validate.ResourceValidator{ResourceName: "ibm_cbr_zone", Schema: validateSchema}
return &resourceValidator
}
// waitForCbrZoneRead will leverage use retry.StateChangeConf due to the service's eventual consistency
func waitForCbrZoneRead(cbrClient *contextbasedrestrictionsv1.ContextBasedRestrictionsV1, context context.Context, id string) (interface{}, error) {
stateConf := &retry.StateChangeConf{
Pending: []string{cbrRuleReadPending},
Target: []string{cbrRuleReadError, cbrRuleReadComplete, ""},
Refresh: func() (interface{}, string, error) {
log.Printf("[INFO] Retrying cbr rule (%s) read", id)
getZoneOptions := &contextbasedrestrictionsv1.GetZoneOptions{}
getZoneOptions.SetZoneID(id)
_, response, err := cbrClient.GetZoneWithContext(context, getZoneOptions)
if err != nil && response.StatusCode == 404 {
return response, cbrZoneReadPending, nil
} else if err != nil {
return response, cbrZoneReadError, err
} else {
return response, cbrZoneReadComplete, nil
}
},
Timeout: 120 * time.Second,
Delay: 20 * time.Second,
MinTimeout: 10 * time.Second,
}
return stateConf.WaitForStateContext(context)
}
func readNewCbrZone(cbrClient *contextbasedrestrictionsv1.ContextBasedRestrictionsV1, context context.Context, d *schema.ResourceData) diag.Diagnostics {
getZoneOptions := &contextbasedrestrictionsv1.GetZoneOptions{}
getZoneOptions.SetZoneID(d.Id())
_, response, err := cbrClient.GetZoneWithContext(context, getZoneOptions)
if response != nil && response.StatusCode == 404 {
// Manual change. leverage retry for the read due to eventual consistency of the service.
log.Printf("[INFO] Read cbr zone response status code: 404, provider will try again. %s", err)
_, err := waitForCbrZoneRead(cbrClient, context, d.Id())
if err != nil {
log.Printf("[DEBUG] GetZoneWithContext failed %s\n%s", err, response)
d.SetId("")
return diag.FromErr(fmt.Errorf("GetZoneWithContext failed %s\n%s", err, response))
}
}
return nil
}
func resourceIBMCbrZoneCreate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
contextBasedRestrictionsClient, err := meta.(conns.ClientSession).ContextBasedRestrictionsV1()
if err != nil {
return diag.FromErr(err)
}
createZoneOptions := &contextbasedrestrictionsv1.CreateZoneOptions{}
if _, ok := d.GetOk("name"); ok {
createZoneOptions.SetName(d.Get("name").(string))
}
if _, ok := d.GetOk("account_id"); ok {
createZoneOptions.SetAccountID(d.Get("account_id").(string))
}
if _, ok := d.GetOk("description"); ok {
createZoneOptions.SetDescription(d.Get("description").(string))
}
addresses := []contextbasedrestrictionsv1.AddressIntf{}
if _, ok := d.GetOk("addresses"); ok {
for _, e := range d.Get("addresses").([]interface{}) {
value := e.(map[string]interface{})
addressesItem, err := resourceIBMCbrZoneMapToAddress(value)
if err != nil {
return diag.FromErr(err)
}
addresses = append(addresses, addressesItem)
}
}
createZoneOptions.SetAddresses(addresses)
if _, ok := d.GetOk("excluded"); ok {
var excluded []contextbasedrestrictionsv1.AddressIntf
for _, e := range d.Get("excluded").([]interface{}) {
value := e.(map[string]interface{})
excludedItem, err := resourceIBMCbrZoneMapToAddress(value)
if err != nil {
return diag.FromErr(err)
}
excluded = append(excluded, excludedItem)
}
createZoneOptions.SetExcluded(excluded)
}
if _, ok := d.GetOk("x_correlation_id"); ok {
createZoneOptions.SetXCorrelationID(d.Get("x_correlation_id").(string))
}
if _, ok := d.GetOk("transaction_id"); ok {
createZoneOptions.SetTransactionID(d.Get("transaction_id").(string))
}
zone, response, err := contextBasedRestrictionsClient.CreateZoneWithContext(context, createZoneOptions)
if err != nil {
log.Printf("[DEBUG] CreateZoneWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("CreateZoneWithContext failed %s\n%s", err, response))
}
d.SetId(*zone.ID)
// handle Eventual consistency case
readNewCbrZone(contextBasedRestrictionsClient, context, d)
return resourceIBMCbrZoneRead(context, d, meta)
}
func resourceIBMCbrZoneRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
contextBasedRestrictionsClient, err := meta.(conns.ClientSession).ContextBasedRestrictionsV1()
if err != nil {
return diag.FromErr(err)
}
getZoneOptions := &contextbasedrestrictionsv1.GetZoneOptions{}
getZoneOptions.SetZoneID(d.Id())
zone, response, err := contextBasedRestrictionsClient.GetZoneWithContext(context, getZoneOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
log.Printf("[DEBUG] GetZoneWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetZoneWithContext failed %s\n%s", err, response))
}
if err = d.Set("x_correlation_id", getZoneOptions.XCorrelationID); err != nil {
return diag.FromErr(fmt.Errorf("Error setting x_correlation_id: %s", err))
}
if err = d.Set("transaction_id", getZoneOptions.TransactionID); err != nil {
return diag.FromErr(fmt.Errorf("Error setting transaction_id: %s", err))
}
if err = d.Set("name", zone.Name); err != nil {
return diag.FromErr(fmt.Errorf("Error setting name: %s", err))
}
if err = d.Set("account_id", zone.AccountID); err != nil {
return diag.FromErr(fmt.Errorf("Error setting account_id: %s", err))
}
if err = d.Set("description", zone.Description); err != nil {
return diag.FromErr(fmt.Errorf("Error setting description: %s", err))
}
addresses := []map[string]interface{}{}
if zone.Addresses != nil {
for _, addressesItem := range zone.Addresses {
addressesItemMap, err := resourceIBMCbrZoneAddressToMap(addressesItem)
if err != nil {
return diag.FromErr(err)
}
addresses = append(addresses, addressesItemMap)
}
}
if err = d.Set("addresses", addresses); err != nil {
return diag.FromErr(fmt.Errorf("Error setting addresses: %s", err))
}
excluded := []map[string]interface{}{}
if zone.Excluded != nil {
for _, excludedItem := range zone.Excluded {
excludedItemMap, err := resourceIBMCbrZoneAddressToMap(excludedItem)
if err != nil {
return diag.FromErr(err)
}
excluded = append(excluded, excludedItemMap)
}
}
if err = d.Set("excluded", excluded); err != nil {
return diag.FromErr(fmt.Errorf("Error setting excluded: %s", err))
}
if err = d.Set("crn", zone.CRN); err != nil {
return diag.FromErr(fmt.Errorf("Error setting crn: %s", err))
}
if err = d.Set("address_count", flex.IntValue(zone.AddressCount)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting address_count: %s", err))
}
if err = d.Set("excluded_count", flex.IntValue(zone.ExcludedCount)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting excluded_count: %s", err))
}
if err = d.Set("href", zone.Href); err != nil {
return diag.FromErr(fmt.Errorf("Error setting href: %s", err))
}
if err = d.Set("created_at", flex.DateTimeToString(zone.CreatedAt)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting created_at: %s", err))
}
if err = d.Set("created_by_id", zone.CreatedByID); err != nil {
return diag.FromErr(fmt.Errorf("Error setting created_by_id: %s", err))
}
if err = d.Set("last_modified_at", flex.DateTimeToString(zone.LastModifiedAt)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting last_modified_at: %s", err))
}
if err = d.Set("last_modified_by_id", zone.LastModifiedByID); err != nil {
return diag.FromErr(fmt.Errorf("Error setting last_modified_by_id: %s", err))
}
if err = d.Set("version", response.Headers.Get("Etag")); err != nil {
return diag.FromErr(fmt.Errorf("Error setting version: %s", err))
}
return nil
}
func resourceIBMCbrZoneUpdate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
contextBasedRestrictionsClient, err := meta.(conns.ClientSession).ContextBasedRestrictionsV1()
if err != nil {
return diag.FromErr(err)
}
replaceZoneOptions := &contextbasedrestrictionsv1.ReplaceZoneOptions{}
replaceZoneOptions.SetZoneID(d.Id())
if _, ok := d.GetOk("name"); ok {
replaceZoneOptions.SetName(d.Get("name").(string))
}
if _, ok := d.GetOk("account_id"); ok {
replaceZoneOptions.SetAccountID(d.Get("account_id").(string))
}
if _, ok := d.GetOk("description"); ok {
replaceZoneOptions.SetDescription(d.Get("description").(string))
}
addresses := []contextbasedrestrictionsv1.AddressIntf{}
if _, ok := d.GetOk("addresses"); ok {
for _, e := range d.Get("addresses").([]interface{}) {
value := e.(map[string]interface{})
addressesItem, err := resourceIBMCbrZoneMapToAddress(value)
if err != nil {
return diag.FromErr(err)
}
addresses = append(addresses, addressesItem)
}
}
replaceZoneOptions.SetAddresses(addresses)
if _, ok := d.GetOk("excluded"); ok {
var excluded []contextbasedrestrictionsv1.AddressIntf
for _, e := range d.Get("excluded").([]interface{}) {
value := e.(map[string]interface{})
excludedItem, err := resourceIBMCbrZoneMapToAddress(value)
if err != nil {
return diag.FromErr(err)
}
excluded = append(excluded, excludedItem)
}
replaceZoneOptions.SetExcluded(excluded)
}
if _, ok := d.GetOk("x_correlation_id"); ok {
replaceZoneOptions.SetXCorrelationID(d.Get("x_correlation_id").(string))
}
if _, ok := d.GetOk("transaction_id"); ok {
replaceZoneOptions.SetTransactionID(d.Get("transaction_id").(string))
}
replaceZoneOptions.SetIfMatch(d.Get("version").(string))
_, response, err := contextBasedRestrictionsClient.ReplaceZoneWithContext(context, replaceZoneOptions)
if err != nil {
log.Printf("[DEBUG] ReplaceZoneWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("ReplaceZoneWithContext failed %s\n%s", err, response))
}
return resourceIBMCbrZoneRead(context, d, meta)
}
func resourceIBMCbrZoneDelete(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
contextBasedRestrictionsClient, err := meta.(conns.ClientSession).ContextBasedRestrictionsV1()
if err != nil {
return diag.FromErr(err)
}
deleteZoneOptions := &contextbasedrestrictionsv1.DeleteZoneOptions{}
deleteZoneOptions.SetZoneID(d.Id())
response, err := contextBasedRestrictionsClient.DeleteZoneWithContext(context, deleteZoneOptions)
if err != nil {
log.Printf("[DEBUG] DeleteZoneWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("DeleteZoneWithContext failed %s\n%s", err, response))
}
d.SetId("")
return nil
}
func resourceIBMCbrZoneMapToAddress(modelMap map[string]interface{}) (contextbasedrestrictionsv1.AddressIntf, error) {
discValue, ok := modelMap["type"]
if ok {
if discValue == "ipAddress" {
return resourceIBMCbrZoneMapToAddressIPAddress(modelMap)
} else if discValue == "ipRange" {
return resourceIBMCbrZoneMapToAddressIPAddressRange(modelMap)
} else if discValue == "subnet" {
return resourceIBMCbrZoneMapToAddressSubnet(modelMap)
} else if discValue == "vpc" {
return resourceIBMCbrZoneMapToAddressVPC(modelMap)
} else if discValue == "serviceRef" {
return resourceIBMCbrZoneMapToAddressServiceRef(modelMap)
} else {
return nil, fmt.Errorf("unexpected value for discriminator property 'type' found in map: '%s'", discValue)
}
} else {
return nil, fmt.Errorf("discriminator property 'type' not found in map")
}
}
func resourceIBMCbrZoneMapToServiceRefValue(modelMap map[string]interface{}) (*contextbasedrestrictionsv1.ServiceRefValue, error) {
model := &contextbasedrestrictionsv1.ServiceRefValue{}
model.AccountID = core.StringPtr(modelMap["account_id"].(string))
if modelMap["service_type"] != nil && modelMap["service_type"].(string) != "" {
model.ServiceType = core.StringPtr(modelMap["service_type"].(string))
}
if modelMap["service_name"] != nil && modelMap["service_name"].(string) != "" {
model.ServiceName = core.StringPtr(modelMap["service_name"].(string))
}
if modelMap["service_instance"] != nil && modelMap["service_instance"].(string) != "" {
model.ServiceInstance = core.StringPtr(modelMap["service_instance"].(string))
}
if modelMap["location"] != nil && modelMap["location"].(string) != "" {
model.Location = core.StringPtr(modelMap["location"].(string))
}
return model, nil
}
func resourceIBMCbrZoneMapToAddressIPAddress(modelMap map[string]interface{}) (*contextbasedrestrictionsv1.AddressIPAddress, error) {
model := &contextbasedrestrictionsv1.AddressIPAddress{}
model.Type = core.StringPtr(modelMap["type"].(string))
model.Value = core.StringPtr(modelMap["value"].(string))
return model, nil
}
func resourceIBMCbrZoneMapToAddressServiceRef(modelMap map[string]interface{}) (*contextbasedrestrictionsv1.AddressServiceRef, error) {
model := &contextbasedrestrictionsv1.AddressServiceRef{}
model.Type = core.StringPtr(modelMap["type"].(string))
RefModel, err := resourceIBMCbrZoneMapToServiceRefValue(modelMap["ref"].([]interface{})[0].(map[string]interface{}))
if err != nil {
return model, err
}
model.Ref = RefModel
return model, nil
}
func resourceIBMCbrZoneMapToAddressSubnet(modelMap map[string]interface{}) (*contextbasedrestrictionsv1.AddressSubnet, error) {
model := &contextbasedrestrictionsv1.AddressSubnet{}
model.Type = core.StringPtr(modelMap["type"].(string))
model.Value = core.StringPtr(modelMap["value"].(string))
return model, nil
}
func resourceIBMCbrZoneMapToAddressIPAddressRange(modelMap map[string]interface{}) (*contextbasedrestrictionsv1.AddressIPAddressRange, error) {
model := &contextbasedrestrictionsv1.AddressIPAddressRange{}
model.Type = core.StringPtr(modelMap["type"].(string))
model.Value = core.StringPtr(modelMap["value"].(string))
return model, nil
}
func resourceIBMCbrZoneMapToAddressVPC(modelMap map[string]interface{}) (*contextbasedrestrictionsv1.AddressVPC, error) {
model := &contextbasedrestrictionsv1.AddressVPC{}
model.Type = core.StringPtr(modelMap["type"].(string))
model.Value = core.StringPtr(modelMap["value"].(string))
return model, nil
}
func resourceIBMCbrZoneAddressToMap(model contextbasedrestrictionsv1.AddressIntf) (map[string]interface{}, error) {
if _, ok := model.(*contextbasedrestrictionsv1.AddressIPAddress); ok {
return resourceIBMCbrZoneAddressIPAddressToMap(model.(*contextbasedrestrictionsv1.AddressIPAddress))
} else if _, ok := model.(*contextbasedrestrictionsv1.AddressIPAddressRange); ok {
return resourceIBMCbrZoneAddressIPAddressRangeToMap(model.(*contextbasedrestrictionsv1.AddressIPAddressRange))
} else if _, ok := model.(*contextbasedrestrictionsv1.AddressSubnet); ok {
return resourceIBMCbrZoneAddressSubnetToMap(model.(*contextbasedrestrictionsv1.AddressSubnet))
} else if _, ok := model.(*contextbasedrestrictionsv1.AddressVPC); ok {
return resourceIBMCbrZoneAddressVPCToMap(model.(*contextbasedrestrictionsv1.AddressVPC))
} else if _, ok := model.(*contextbasedrestrictionsv1.AddressServiceRef); ok {
return resourceIBMCbrZoneAddressServiceRefToMap(model.(*contextbasedrestrictionsv1.AddressServiceRef))
} else if _, ok := model.(*contextbasedrestrictionsv1.Address); ok {
modelMap := make(map[string]interface{})
model := model.(*contextbasedrestrictionsv1.Address)
if model.Type != nil {
modelMap["type"] = model.Type
}
if model.Value != nil {
modelMap["value"] = model.Value
}
if model.Ref != nil {
refMap, err := resourceIBMCbrZoneServiceRefValueToMap(model.Ref)
if err != nil {
return modelMap, err
}
modelMap["ref"] = []map[string]interface{}{refMap}
}
return modelMap, nil
} else {
return nil, fmt.Errorf("Unrecognized contextbasedrestrictionsv1.AddressIntf subtype encountered")
}
}
func resourceIBMCbrZoneServiceRefValueToMap(model *contextbasedrestrictionsv1.ServiceRefValue) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["account_id"] = model.AccountID
if model.ServiceType != nil {
modelMap["service_type"] = model.ServiceType
}
if model.ServiceName != nil {
modelMap["service_name"] = model.ServiceName
}
if model.ServiceInstance != nil {
modelMap["service_instance"] = model.ServiceInstance
}
if model.Location != nil {
modelMap["location"] = model.Location
}
return modelMap, nil
}
func resourceIBMCbrZoneAddressIPAddressToMap(model *contextbasedrestrictionsv1.AddressIPAddress) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["type"] = model.Type
modelMap["value"] = model.Value
return modelMap, nil
}
func resourceIBMCbrZoneAddressServiceRefToMap(model *contextbasedrestrictionsv1.AddressServiceRef) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["type"] = model.Type
refMap, err := resourceIBMCbrZoneServiceRefValueToMap(model.Ref)
if err != nil {
return modelMap, err
}
modelMap["ref"] = []map[string]interface{}{refMap}
return modelMap, nil
}
func resourceIBMCbrZoneAddressSubnetToMap(model *contextbasedrestrictionsv1.AddressSubnet) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["type"] = model.Type
modelMap["value"] = model.Value
return modelMap, nil
}
func resourceIBMCbrZoneAddressIPAddressRangeToMap(model *contextbasedrestrictionsv1.AddressIPAddressRange) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["type"] = model.Type
modelMap["value"] = model.Value
return modelMap, nil
}
func resourceIBMCbrZoneAddressVPCToMap(model *contextbasedrestrictionsv1.AddressVPC) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["type"] = model.Type
modelMap["value"] = model.Value
return modelMap, nil
}