-
Notifications
You must be signed in to change notification settings - Fork 669
/
resource_ibm_is_dedicated_host.go
837 lines (758 loc) · 30.9 KB
/
resource_ibm_is_dedicated_host.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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"time"
"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/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/vpc-go-sdk/vpcv1"
)
const (
isDedicatedHostStable = "stable"
isDedicatedHostDeleting = "deleting"
isDedicatedHostDeleteDone = "done"
isDedicatedHostFailed = "failed"
isDedicatedHostUpdating = "updating"
isDedicatedHostProvisioningDone = "done"
isDedicatedHostWaiting = "waiting"
isDedicatedHostSuspended = "suspended"
isDedicatedHostActionStatusStopping = "stopping"
isDedicatedHostActionStatusStopped = "stopped"
isDedicatedHostStatusPending = "pending"
isDedicatedHostStatusRunning = "running"
isDedicatedHostStatusFailed = "failed"
isDedicatedHostAccessTags = "access_tags"
isDedicatedHostUserTagType = "user"
isDedicatedHostAccessTagType = "access"
)
func ResourceIbmIsDedicatedHost() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIbmIsDedicatedHostCreate,
ReadContext: resourceIbmIsDedicatedHostRead,
UpdateContext: resourceIbmIsDedicatedHostUpdate,
DeleteContext: resourceIbmIsDedicatedHostDelete,
Importer: &schema.ResourceImporter{},
CustomizeDiff: customdiff.All(
customdiff.Sequence(
func(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
return flex.ResourceTagsCustomizeDiff(diff)
},
),
customdiff.Sequence(
func(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
return flex.ResourceValidateAccessTags(diff, v)
}),
),
Schema: map[string]*schema.Schema{
"instance_placement_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "If set to true, instances can be placed on this dedicated host.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.InvokeValidator("ibm_is_dedicated_host", "name"),
Description: "The unique user-defined name for this dedicated host. If unspecified, the name will be a hyphenated list of randomly-selected words.",
},
"profile": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The Globally unique name of the dedicated host profile to use for this dedicated host.",
},
"resource_group": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: "The unique identifier for the resource group to use. If unspecified, the account's [default resourcegroup](https://cloud.ibm.com/apidocs/resource-manager#introduction) is used.",
},
"host_group": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The unique identifier of the dedicated host group for this dedicated host.",
},
"available_memory": {
Type: schema.TypeInt,
Computed: true,
Description: "The amount of memory in gibibytes that is currently available for instances.",
},
"available_vcpu": {
Type: schema.TypeList,
Computed: true,
Description: "The available VCPU for the dedicated host.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"architecture": {
Type: schema.TypeString,
Computed: true,
Description: "The VCPU architecture.",
},
"count": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of VCPUs assigned.",
},
},
},
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the dedicated host was created.",
},
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this dedicated host.",
},
"disks": {
Type: schema.TypeList,
Computed: true,
Description: "Collection of the dedicated host's disks.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"available": {
Type: schema.TypeInt,
Computed: true,
Description: "The remaining space left for instance placement in GB (gigabytes).",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the disk was created.",
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this disk.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this disk.",
},
"instance_disks": {
Type: schema.TypeList,
Computed: true,
Description: "Instance disks that are on this dedicated host disk.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"deleted": {
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": {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this instance disk.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this instance disk.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this disk.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
"interface_type": {
Type: schema.TypeString,
Computed: true,
Description: "The disk interface used for attaching the diskThe 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 resource on which the unexpected property value was encountered.",
},
"lifecycle_state": {
Type: schema.TypeString,
Computed: true,
Description: "The lifecycle state of this dedicated host disk.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined or system-provided name for this disk.",
},
"provisionable": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this dedicated host disk is available for instance disk creation.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of resource referenced.",
},
"size": {
Type: schema.TypeInt,
Computed: true,
Description: "The size of the disk in GB (gigabytes).",
},
"supported_instance_interface_types": {
Type: schema.TypeList,
Computed: true,
Description: "The instance disk interfaces supported for this dedicated host disk.",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this dedicated host.",
},
"instances": {
Type: schema.TypeList,
Computed: true,
Description: "Array of instances that are allocated to this dedicated host.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this virtual server instance.",
},
"deleted": {
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": {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this virtual server instance.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this virtual server instance.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this virtual server instance (and default system hostname).",
},
},
},
},
"lifecycle_state": {
Type: schema.TypeString,
Computed: true,
Description: "The lifecycle state of the dedicated host resource.",
},
"memory": {
Type: schema.TypeInt,
Computed: true,
Description: "The total amount of memory in gibibytes for this host.",
},
"provisionable": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this dedicated host is available for instance creation.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of resource referenced.",
},
"socket_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The total number of sockets for this host.",
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: "The administrative state of the dedicated host.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 dedicated host on which the unexpected property value was encountered.",
},
"supported_instance_profiles": {
Type: schema.TypeList,
Computed: true,
Description: "Array of instance profiles that can be used by instances placed on this dedicated host.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this virtual server instance profile.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The globally unique name for this virtual server instance profile.",
},
},
},
},
"vcpu": {
Type: schema.TypeList,
Computed: true,
Description: "The total VCPU of the dedicated host.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"architecture": {
Type: schema.TypeString,
Computed: true,
Description: "The VCPU architecture.",
},
"count": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of VCPUs assigned.",
},
},
},
},
"zone": {
Type: schema.TypeString,
Computed: true,
Description: "The globally unique name of the zone this dedicated host resides in.",
},
isDedicatedHostAccessTags: {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString, ValidateFunc: validate.InvokeValidator("ibm_is_dedicated_host", "accesstag")},
Set: flex.ResourceIBMVPCHash,
Description: "List of access management tags",
},
},
}
}
func ResourceIbmIsDedicatedHostValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "name",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Optional: true,
Regexp: `^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$`,
MinValueLength: 1,
MaxValueLength: 63,
})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "accesstag",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Optional: true,
Regexp: `^([A-Za-z0-9_.-]|[A-Za-z0-9_.-][A-Za-z0-9_ .-]*[A-Za-z0-9_.-]):([A-Za-z0-9_.-]|[A-Za-z0-9_.-][A-Za-z0-9_ .-]*[A-Za-z0-9_.-])$`,
MinValueLength: 1,
MaxValueLength: 128})
resourceValidator := validate.ResourceValidator{ResourceName: "ibm_is_dedicated_host", Schema: validateSchema}
return &resourceValidator
}
func resourceIbmIsDedicatedHostCreate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
createDedicatedHostOptions := &vpcv1.CreateDedicatedHostOptions{}
dedicatedHostPrototype := vpcv1.DedicatedHostPrototype{}
if dhname, ok := d.GetOk("name"); ok {
namestr := dhname.(string)
dedicatedHostPrototype.Name = &namestr
}
if insplacementenabled, ok := d.GetOk("instance_placement_enabled"); ok {
insplacementenabledbool := insplacementenabled.(bool)
dedicatedHostPrototype.InstancePlacementEnabled = &insplacementenabledbool
}
if dhprofile, ok := d.GetOk("profile"); ok {
dhprofilename := dhprofile.(string)
dedicatedHostProfileIdentity := vpcv1.DedicatedHostProfileIdentity{
Name: &dhprofilename,
}
dedicatedHostPrototype.Profile = &dedicatedHostProfileIdentity
}
if dhgroup, ok := d.GetOk("host_group"); ok {
dhgroupid := dhgroup.(string)
dedicatedHostGroupIdentity := vpcv1.DedicatedHostGroupIdentity{
ID: &dhgroupid,
}
dedicatedHostPrototype.Group = &dedicatedHostGroupIdentity
}
if resgroup, ok := d.GetOk("resource_group"); ok {
resgroupid := resgroup.(string)
resourceGroupIdentity := vpcv1.ResourceGroupIdentity{
ID: &resgroupid,
}
dedicatedHostPrototype.ResourceGroup = &resourceGroupIdentity
}
createDedicatedHostOptions.SetDedicatedHostPrototype(&dedicatedHostPrototype)
dedicatedHost, response, err := vpcClient.CreateDedicatedHostWithContext(context, createDedicatedHostOptions)
if err != nil {
log.Printf("[DEBUG] CreateDedicatedHostWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
d.SetId(*dedicatedHost.ID)
if _, ok := d.GetOk(isDedicatedHostAccessTags); ok {
oldList, newList := d.GetChange(isDedicatedHostAccessTags)
err = flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, *dedicatedHost.CRN, "", isDedicatedHostAccessTagType)
if err != nil {
log.Printf(
"Error on create of resource dedicated host (%s) access tags: %s", d.Id(), err)
}
}
_, err = isWaitForDedicatedHostAvailable(vpcClient, d.Id(), d.Timeout(schema.TimeoutCreate), d)
if err != nil {
return diag.FromErr(err)
}
return resourceIbmIsDedicatedHostRead(context, d, meta)
}
func resourceIbmIsDedicatedHostRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
getDedicatedHostOptions := &vpcv1.GetDedicatedHostOptions{}
getDedicatedHostOptions.SetID(d.Id())
dedicatedHost, response, err := vpcClient.GetDedicatedHostWithContext(context, getDedicatedHostOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
log.Printf("[DEBUG] GetDedicatedHostWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
if err = d.Set("available_memory", flex.IntValue(dedicatedHost.AvailableMemory)); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting available_memory: %s", err))
}
availableVcpuMap := resourceIbmIsDedicatedHostVCPUToMap(*dedicatedHost.AvailableVcpu)
if err = d.Set("available_vcpu", []map[string]interface{}{availableVcpuMap}); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting available_vcpu: %s", err))
}
if err = d.Set("created_at", dedicatedHost.CreatedAt.String()); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting created_at: %s", err))
}
if err = d.Set("crn", dedicatedHost.CRN); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting crn: %s", err))
}
disks := []map[string]interface{}{}
for _, disksItem := range dedicatedHost.Disks {
disksItemMap := resourceIbmIsDedicatedHostDedicatedHostDiskToMap(disksItem)
disks = append(disks, disksItemMap)
}
if err = d.Set("disks", disks); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting disks: %s", err))
}
d.Set("host_group", *dedicatedHost.Group.ID)
if err = d.Set("href", dedicatedHost.Href); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting href: %s", err))
}
if err = d.Set("instance_placement_enabled", dedicatedHost.InstancePlacementEnabled); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting instance_placement_enabled: %s", err))
}
instances := []map[string]interface{}{}
for _, instancesItem := range dedicatedHost.Instances {
instancesItemMap := resourceIbmIsDedicatedHostInstanceReferenceToMap(instancesItem)
instances = append(instances, instancesItemMap)
}
if err = d.Set("instances", instances); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting instances: %s", err))
}
if err = d.Set("lifecycle_state", dedicatedHost.LifecycleState); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting lifecycle_state: %s", err))
}
if err = d.Set("memory", flex.IntValue(dedicatedHost.Memory)); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting memory: %s", err))
}
if err = d.Set("name", dedicatedHost.Name); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting name: %s", err))
}
if err = d.Set("profile", *dedicatedHost.Profile.Name); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting profile: %s", err))
}
if err = d.Set("provisionable", dedicatedHost.Provisionable); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting provisionable: %s", err))
}
if err = d.Set("resource_group", *dedicatedHost.ResourceGroup.ID); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting resource_group: %s", err))
}
if err = d.Set("resource_type", dedicatedHost.ResourceType); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting resource_type: %s", err))
}
if err = d.Set("socket_count", flex.IntValue(dedicatedHost.SocketCount)); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting socket_count: %s", err))
}
if err = d.Set("state", dedicatedHost.State); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting state: %s", err))
}
supportedInstanceProfiles := []map[string]interface{}{}
for _, supportedInstanceProfilesItem := range dedicatedHost.SupportedInstanceProfiles {
supportedInstanceProfilesItemMap := resourceIbmIsDedicatedHostInstanceProfileReferenceToMap(supportedInstanceProfilesItem)
supportedInstanceProfiles = append(supportedInstanceProfiles, supportedInstanceProfilesItemMap)
}
if err = d.Set("supported_instance_profiles", supportedInstanceProfiles); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting supported_instance_profiles: %s", err))
}
vcpuMap := resourceIbmIsDedicatedHostVCPUToMap(*dedicatedHost.Vcpu)
if err = d.Set("vcpu", []map[string]interface{}{vcpuMap}); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting vcpu: %s", err))
}
if err = d.Set("zone", *dedicatedHost.Zone.Name); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting zone: %s", err))
}
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *dedicatedHost.CRN, "", isDedicatedHostAccessTagType)
if err != nil {
log.Printf(
"Error on get of resource dedicated host (%s) access tags: %s", d.Id(), err)
}
d.Set(isDedicatedHostAccessTags, accesstags)
return nil
}
func resourceIbmIsDedicatedHostVCPUToMap(vCPU vpcv1.Vcpu) map[string]interface{} {
vCPUMap := map[string]interface{}{}
vCPUMap["architecture"] = vCPU.Architecture
vCPUMap["count"] = flex.IntValue(vCPU.Count)
return vCPUMap
}
func resourceIbmIsDedicatedHostInstanceReferenceToMap(instanceReference vpcv1.InstanceReference) map[string]interface{} {
instanceReferenceMap := map[string]interface{}{}
instanceReferenceMap["crn"] = instanceReference.CRN
if instanceReference.Deleted != nil {
DeletedMap := resourceIbmIsDedicatedHostInstanceReferenceDeletedToMap(*instanceReference.Deleted)
instanceReferenceMap["deleted"] = []map[string]interface{}{DeletedMap}
}
instanceReferenceMap["href"] = instanceReference.Href
instanceReferenceMap["id"] = instanceReference.ID
instanceReferenceMap["name"] = instanceReference.Name
return instanceReferenceMap
}
func resourceIbmIsDedicatedHostInstanceReferenceDeletedToMap(instanceReferenceDeleted vpcv1.InstanceReferenceDeleted) map[string]interface{} {
instanceReferenceDeletedMap := map[string]interface{}{}
instanceReferenceDeletedMap["more_info"] = instanceReferenceDeleted.MoreInfo
return instanceReferenceDeletedMap
}
func resourceIbmIsDedicatedHostInstanceProfileReferenceToMap(instanceProfileReference vpcv1.InstanceProfileReference) map[string]interface{} {
instanceProfileReferenceMap := map[string]interface{}{}
instanceProfileReferenceMap["href"] = instanceProfileReference.Href
instanceProfileReferenceMap["name"] = instanceProfileReference.Name
return instanceProfileReferenceMap
}
func resourceIbmIsDedicatedHostUpdate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
updateDedicatedHostOptions := &vpcv1.UpdateDedicatedHostOptions{}
updateDedicatedHostOptions.SetID(d.Id())
hasChange := false
dedicatedHostPrototypemap := map[string]interface{}{}
if d.HasChange("name") {
dedicatedHostPrototypemap["name"] = d.Get("name").(interface{})
hasChange = true
}
if d.HasChange("instance_placement_enabled") {
dedicatedHostPrototypemap["instance_placement_enabled"] = d.Get("instance_placement_enabled").(interface{})
hasChange = true
}
if d.HasChange("profile") {
dedicatedHostPrototypemap["profile"] = d.Get("profile").(interface{})
hasChange = true
}
if d.HasChange("resource_group") {
dedicatedHostPrototypemap["resource_group"] = d.Get("resource_group").(interface{})
hasChange = true
}
if d.HasChange("host_group") {
dedicatedHostPrototypemap["group"] = d.Get("host_group").(interface{})
hasChange = true
}
if hasChange {
updateDedicatedHostOptions.SetDedicatedHostPatch(dedicatedHostPrototypemap)
_, response, err := vpcClient.UpdateDedicatedHostWithContext(context, updateDedicatedHostOptions)
if err != nil {
log.Printf("[DEBUG] UpdateDedicatedHostWithContext fails %s\n%s", err, response)
return diag.FromErr(err)
}
}
if d.HasChange(isDedicatedHostAccessTags) {
oldList, newList := d.GetChange(isDedicatedHostAccessTags)
err := flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, d.Get("crn").(string), "", isDedicatedHostAccessTagType)
if err != nil {
log.Printf(
"Error on update of resource dedicated host (%s) access tags: %s", d.Id(), err)
}
}
return resourceIbmIsDedicatedHostRead(context, d, meta)
}
func resourceIbmIsDedicatedHostDelete(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
getDedicatedHostOptions := &vpcv1.GetDedicatedHostOptions{}
getDedicatedHostOptions.SetID(d.Id())
dedicatedHost, response, err := vpcClient.GetDedicatedHostWithContext(context, getDedicatedHostOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
log.Printf("[DEBUG] GetDedicatedHostWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
if dedicatedHost != nil && dedicatedHost.LifecycleState != nil && *dedicatedHost.LifecycleState != isDedicatedHostSuspended && *dedicatedHost.LifecycleState != isDedicatedHostFailed {
updateDedicatedHostOptions := &vpcv1.UpdateDedicatedHostOptions{}
dedicatedHostPrototypeMap := map[string]interface{}{}
dedicatedHostPrototypeMap["instance_placement_enabled"] = core.BoolPtr(false)
updateDedicatedHostOptions.SetID(d.Id())
updateDedicatedHostOptions.SetDedicatedHostPatch(dedicatedHostPrototypeMap)
_, updateresponse, err := vpcClient.UpdateDedicatedHostWithContext(context, updateDedicatedHostOptions)
if err != nil {
log.Printf("[DEBUG] Failed disabling instance placement %s\n%s", err, updateresponse)
return diag.FromErr(err)
}
}
deleteDedicatedHostOptions := &vpcv1.DeleteDedicatedHostOptions{}
deleteDedicatedHostOptions.SetID(d.Id())
response, err = vpcClient.DeleteDedicatedHostWithContext(context, deleteDedicatedHostOptions)
if err != nil {
log.Printf("[DEBUG] DeleteDedicatedHostWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
_, err = isWaitForDedicatedHostDelete(vpcClient, d, d.Id())
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
}
func isWaitForDedicatedHostDelete(instanceC *vpcv1.VpcV1, d *schema.ResourceData, id string) (interface{}, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{isDedicatedHostDeleting, isDedicatedHostStable},
Target: []string{isDedicatedHostDeleteDone, ""},
Refresh: func() (interface{}, string, error) {
getdhoptions := &vpcv1.GetDedicatedHostOptions{
ID: &id,
}
dedicatedhost, response, err := instanceC.GetDedicatedHost(getdhoptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
return dedicatedhost, isDedicatedHostDeleteDone, nil
}
return nil, "", fmt.Errorf("[ERROR] Error getting dedicated Host: %s\n%s", err, response)
}
if *dedicatedhost.State == isDedicatedHostFailed {
return dedicatedhost, *dedicatedhost.State, fmt.Errorf("[ERROR] The Dedicated host %s failed to delete: %v", d.Id(), err)
}
return dedicatedhost, isDedicatedHostDeleting, nil
},
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 10 * time.Second,
MinTimeout: 10 * time.Second,
}
return stateConf.WaitForState()
}
func isWaitForDedicatedHostAvailable(instanceC *vpcv1.VpcV1, id string, timeout time.Duration, d *schema.ResourceData) (interface{}, error) {
log.Printf("Waiting for dedicated host (%s) to be available.", id)
stateConf := &resource.StateChangeConf{
Pending: []string{isDedicatedHostStatusPending, isDedicatedHostUpdating, isDedicatedHostWaiting},
Target: []string{isDedicatedHostFailed, isDedicatedHostStable, isDedicatedHostSuspended},
Refresh: isDedicatedHostRefreshFunc(instanceC, id, d),
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 10 * time.Second,
}
return stateConf.WaitForState()
}
func isDedicatedHostRefreshFunc(instanceC *vpcv1.VpcV1, id string, d *schema.ResourceData) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
getinsOptions := &vpcv1.GetDedicatedHostOptions{
ID: &id,
}
dhost, response, err := instanceC.GetDedicatedHost(getinsOptions)
if dhost == nil || err != nil {
return nil, "", fmt.Errorf("[ERROR] Error getting dedicated host : %s\n%s", err, response)
}
d.Set("state", *dhost.State)
d.Set("lifecycle_state", *dhost.LifecycleState)
if *dhost.LifecycleState == isDedicatedHostSuspended || *dhost.LifecycleState == isDedicatedHostFailed {
return dhost, *dhost.LifecycleState, fmt.Errorf("status of dedicated host is %s : \n%s", *dhost.LifecycleState, response)
}
return dhost, *dhost.LifecycleState, nil
}
}
func resourceIbmIsDedicatedHostDedicatedHostDiskToMap(dedicatedHostDisk vpcv1.DedicatedHostDisk) map[string]interface{} {
dedicatedHostDiskMap := map[string]interface{}{}
dedicatedHostDiskMap["available"] = flex.IntValue(dedicatedHostDisk.Available)
dedicatedHostDiskMap["created_at"] = dedicatedHostDisk.CreatedAt.String()
dedicatedHostDiskMap["href"] = dedicatedHostDisk.Href
dedicatedHostDiskMap["id"] = dedicatedHostDisk.ID
instanceDisks := []map[string]interface{}{}
for _, instanceDisksItem := range dedicatedHostDisk.InstanceDisks {
instanceDisksItemMap := resourceIbmIsDedicatedHostInstanceDiskReferenceToMap(instanceDisksItem)
instanceDisks = append(instanceDisks, instanceDisksItemMap)
// TODO: handle InstanceDisks of type TypeList -- list of non-primitive, not model items
}
dedicatedHostDiskMap["instance_disks"] = instanceDisks
dedicatedHostDiskMap["interface_type"] = dedicatedHostDisk.InterfaceType
if dedicatedHostDisk.LifecycleState != nil {
dedicatedHostDiskMap["lifecycle_state"] = dedicatedHostDisk.LifecycleState
}
dedicatedHostDiskMap["name"] = dedicatedHostDisk.Name
dedicatedHostDiskMap["provisionable"] = dedicatedHostDisk.Provisionable
dedicatedHostDiskMap["resource_type"] = dedicatedHostDisk.ResourceType
dedicatedHostDiskMap["size"] = flex.IntValue(dedicatedHostDisk.Size)
dedicatedHostDiskMap["supported_instance_interface_types"] = dedicatedHostDisk.SupportedInstanceInterfaceTypes
return dedicatedHostDiskMap
}
func resourceIbmIsDedicatedHostInstanceDiskReferenceToMap(instanceDiskReference vpcv1.InstanceDiskReference) map[string]interface{} {
instanceDiskReferenceMap := map[string]interface{}{}
if instanceDiskReference.Deleted != nil {
DeletedMap := resourceIbmIsDedicatedHostInstanceDiskReferenceDeletedToMap(*instanceDiskReference.Deleted)
instanceDiskReferenceMap["deleted"] = []map[string]interface{}{DeletedMap}
}
instanceDiskReferenceMap["href"] = instanceDiskReference.Href
instanceDiskReferenceMap["id"] = instanceDiskReference.ID
instanceDiskReferenceMap["name"] = instanceDiskReference.Name
instanceDiskReferenceMap["resource_type"] = instanceDiskReference.ResourceType
return instanceDiskReferenceMap
}
func resourceIbmIsDedicatedHostInstanceDiskReferenceDeletedToMap(instanceDiskReferenceDeleted vpcv1.InstanceDiskReferenceDeleted) map[string]interface{} {
instanceDiskReferenceDeletedMap := map[string]interface{}{}
instanceDiskReferenceDeletedMap["more_info"] = instanceDiskReferenceDeleted.MoreInfo
return instanceDiskReferenceDeletedMap
}