-
Notifications
You must be signed in to change notification settings - Fork 669
/
data_source_ibm_is_instances.go
997 lines (917 loc) · 32.7 KB
/
data_source_ibm_is_instances.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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isInstances = "instances"
isInstanceGroupName = "instance_group_name"
)
func DataSourceIBMISInstances() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISInstancesRead,
Schema: map[string]*schema.Schema{
isInstanceGroup: {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"vpc", "vpc_crn", "vpc_name", isInstanceGroupName},
Description: "Instance group ID to filter the instances attached to it",
},
isInstanceGroupName: {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"vpc", "vpc_crn", "vpc_name", isInstanceGroup},
Description: "Instance group name to filter the instances attached to it",
},
"vpc_name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"vpc", "vpc_crn", "instance_group"},
Description: "Name of the vpc to filter the instances attached to it",
},
"vpc": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"vpc_name", "vpc_crn", "instance_group"},
Description: "VPC ID to filter the instances attached to it",
},
"vpc_crn": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"vpc_name", "vpc", "instance_group"},
Description: "VPC CRN to filter the instances attached to it",
},
"resource_group": {
Type: schema.TypeString,
Optional: true,
Description: "Instance resource group",
},
"dedicated_host_name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"dedicated_host"},
Description: "Name of the dedicated host to filter the instances attached to it",
},
"dedicated_host": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"dedicated_host_name"},
Description: "ID of the dedicated host to filter the instances attached to it",
},
"placement_group_name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"placement_group"},
Description: "Name of the placement group to filter the instances attached to it",
},
"placement_group": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"placement_group_name"},
Description: "ID of the placement group to filter the instances attached to it",
},
isInstances: {
Type: schema.TypeList,
Description: "List of instances",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Instance id",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Instance name",
},
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The crn for this Instance",
},
"memory": {
Type: schema.TypeInt,
Computed: true,
Description: "Instance memory",
},
isInstanceMetadataServiceEnabled: {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether the metadata service endpoint is available to the virtual server instance",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "Instance status",
},
isInstanceAvailablePolicyHostFailure: {
Type: schema.TypeString,
Computed: true,
Description: "The availability policy to use for this virtual server instance. The action to perform if the compute host experiences a failure.",
},
isInstanceLifecycleState: {
Type: schema.TypeString,
Computed: true,
Description: "The lifecycle state of the virtual server instance.",
},
isInstanceLifecycleReasons: {
Type: schema.TypeList,
Computed: true,
Description: "The reasons for the current lifecycle_state (if any).",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isInstanceLifecycleReasonsCode: {
Type: schema.TypeString,
Computed: true,
Description: "A snake case string succinctly identifying the reason for this lifecycle state.",
},
isInstanceLifecycleReasonsMessage: {
Type: schema.TypeString,
Computed: true,
Description: "An explanation of the reason for this lifecycle state.",
},
isInstanceLifecycleReasonsMoreInfo: {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about the reason for this lifecycle state.",
},
},
},
},
isInstanceStatusReasons: {
Type: schema.TypeList,
Computed: true,
Description: "The reasons for the current status (if any).",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isInstanceStatusReasonsCode: {
Type: schema.TypeString,
Computed: true,
Description: "A snake case string succinctly identifying the status reason",
},
isInstanceStatusReasonsMessage: {
Type: schema.TypeString,
Computed: true,
Description: "An explanation of the status reason",
},
isInstanceStatusReasonsMoreInfo: {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about this status reason",
},
},
},
},
"resource_group": {
Type: schema.TypeString,
Computed: true,
Description: "Instance resource group",
},
"vpc": {
Type: schema.TypeString,
Computed: true,
Description: "vpc attached to the instance",
},
"boot_volume": {
Type: schema.TypeList,
Computed: true,
Description: "Instance Boot Volume",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Instance Boot volume id",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Instance Boot volume name",
},
"device": {
Type: schema.TypeString,
Computed: true,
Description: "Instance Boot volume device",
},
"volume_id": {
Type: schema.TypeString,
Computed: true,
Description: "Instance Boot volume's volume id",
},
"volume_crn": {
Type: schema.TypeString,
Computed: true,
Description: "Instance Boot volume's volume CRN",
},
},
},
},
"volume_attachments": {
Type: schema.TypeList,
Computed: true,
Description: "Instance Volume Attachments",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Instance volume Attachment id",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Instance volume Attachment name",
},
"volume_id": {
Type: schema.TypeString,
Computed: true,
Description: "Instance volume Attachment's volume id",
},
"volume_name": {
Type: schema.TypeString,
Computed: true,
Description: "Instance volume Attachment's volume name",
},
"volume_crn": {
Type: schema.TypeString,
Computed: true,
Description: "Instance volume Attachment's volume CRN",
},
},
},
},
"primary_network_interface": {
Type: schema.TypeList,
Computed: true,
Description: "Instance Primary Network Interface",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Instance Primary Network interface id",
},
isInstanceNicName: {
Type: schema.TypeString,
Computed: true,
Description: "Instance Primary Network interface name",
},
isInstanceNicPrimaryIpv4Address: {
Type: schema.TypeString,
Computed: true,
Description: "Instance Primary Network interface IPV4 Address",
},
isInstanceNicPrimaryIP: {
Type: schema.TypeList,
Computed: true,
Description: "The primary IP address to bind to the network interface. This can be specified using an existing reserved IP, or a prototype object for a new reserved IP.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isInstanceNicReservedIpAddress: {
Type: schema.TypeString,
Computed: true,
Description: "The IP address to reserve, which must not already be reserved on the subnet.",
},
isInstanceNicReservedIpHref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this reserved IP",
},
isInstanceNicReservedIpName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this reserved IP. If unspecified, the name will be a hyphenated list of randomly-selected words. Names must be unique within the subnet the reserved IP resides in. ",
},
isInstanceNicReservedIpId: {
Type: schema.TypeString,
Computed: true,
Description: "Identifies a reserved IP by a unique property.",
},
isInstanceNicReservedIpResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type",
},
},
},
},
isInstanceNicSecurityGroups: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: "Instance Primary Network interface security groups",
},
isInstanceNicSubnet: {
Type: schema.TypeString,
Computed: true,
Description: "Instance Primary Network interface subnet",
},
},
},
},
"placement_target": {
Type: schema.TypeList,
Computed: true,
Description: "The placement restrictions for the virtual server instance.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this placement target resource.",
},
"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 placement target resource.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this placement target resource.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The unique user-defined name for this placement target resource. If unspecified, the name will be a hyphenated list of randomly-selected words.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of resource referenced.",
},
},
},
},
"network_interfaces": {
Type: schema.TypeList,
Computed: true,
Description: "Instance Network Interfaces",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Instance Network interface id",
},
isInstanceNicName: {
Type: schema.TypeString,
Computed: true,
Description: "Instance Network interface name",
},
isInstanceNicPrimaryIpv4Address: {
Type: schema.TypeString,
Computed: true,
Description: "Instance Network interface IPV4 Address",
},
isInstanceNicPrimaryIP: {
Type: schema.TypeList,
Computed: true,
Description: "The primary IP address to bind to the network interface. This can be specified using an existing reserved IP, or a prototype object for a new reserved IP.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isInstanceNicReservedIpAddress: {
Type: schema.TypeString,
Computed: true,
Description: "The IP address to reserve, which must not already be reserved on the subnet.",
},
isInstanceNicReservedIpHref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this reserved IP",
},
isInstanceNicReservedIpName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this reserved IP. If unspecified, the name will be a hyphenated list of randomly-selected words. Names must be unique within the subnet the reserved IP resides in. ",
},
isInstanceNicReservedIpId: {
Type: schema.TypeString,
Computed: true,
Description: "Identifies a reserved IP by a unique property.",
},
isInstanceNicReservedIpResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type",
},
},
},
},
isInstanceNicSecurityGroups: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: "Instance Network interface security groups",
},
isInstanceNicSubnet: {
Type: schema.TypeString,
Computed: true,
Description: "Instance Network interface subnet",
},
},
},
},
"profile": {
Type: schema.TypeString,
Computed: true,
Description: "Instance Profile",
},
isInstanceTotalVolumeBandwidth: {
Type: schema.TypeInt,
Computed: true,
Description: "The amount of bandwidth (in megabits per second) allocated exclusively to instance storage volumes",
},
isInstanceBandwidth: {
Type: schema.TypeInt,
Computed: true,
Description: "The total bandwidth (in megabits per second) shared across the instance's network interfaces and storage volumes",
},
isInstanceTotalNetworkBandwidth: {
Type: schema.TypeInt,
Computed: true,
Description: "The amount of bandwidth (in megabits per second) allocated exclusively to instance network interfaces.",
},
"vcpu": {
Type: schema.TypeList,
Computed: true,
Description: "Instance vcpu",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"architecture": {
Type: schema.TypeString,
Computed: true,
Description: "Instance vcpu architecture",
},
"count": {
Type: schema.TypeInt,
Computed: true,
Description: "Instance vcpu count",
},
},
},
},
"zone": {
Type: schema.TypeString,
Computed: true,
Description: "Instance zone",
},
"image": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Instance Image",
},
isInstanceGpu: {
Type: schema.TypeList,
Computed: true,
Description: "Instance GPU",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isInstanceGpuCount: {
Type: schema.TypeInt,
Computed: true,
Description: "Instance GPU Count",
},
isInstanceGpuMemory: {
Type: schema.TypeInt,
Computed: true,
Description: "Instance GPU Memory",
},
isInstanceGpuManufacturer: {
Type: schema.TypeString,
Computed: true,
Description: "Instance GPU Manufacturer",
},
isInstanceGpuModel: {
Type: schema.TypeString,
Computed: true,
Description: "Instance GPU Model",
},
},
},
},
isInstanceDisks: {
Type: schema.TypeList,
Computed: true,
Description: "Collection of the instance's disks.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"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 instance disk.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this instance disk.",
},
"interface_type": {
Type: schema.TypeString,
Computed: true,
Description: "The disk interface used for attaching the disk.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 resource on which the unexpected property value was encountered.",
},
"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.",
},
"size": {
Type: schema.TypeInt,
Computed: true,
Description: "The size of the disk in GB (gigabytes).",
},
},
},
},
},
},
},
},
}
}
func dataSourceIBMISInstancesRead(d *schema.ResourceData, meta interface{}) error {
err := instancesList(d, meta)
if err != nil {
return err
}
return nil
}
func instancesList(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
var vpcName, vpcID, vpcCrn, resourceGroup, insGrp, dHostNameStr, dHostIdStr, placementGrpNameStr, placementGrpIdStr string
if vpc, ok := d.GetOk("vpc_name"); ok {
vpcName = vpc.(string)
}
if vpc, ok := d.GetOk("vpc"); ok {
vpcID = vpc.(string)
}
if vpccrn, ok := d.GetOk("vpc_crn"); ok {
vpcCrn = vpccrn.(string)
}
if rg, ok := d.GetOk("resource_group"); ok {
resourceGroup = rg.(string)
}
if dHostNameIntf, ok := d.GetOk("dedicated_host_name"); ok {
dHostNameStr = dHostNameIntf.(string)
}
if dHostIdIntf, ok := d.GetOk("dedicated_host"); ok {
dHostIdStr = dHostIdIntf.(string)
}
if placementGrpNameIntf, ok := d.GetOk("placement_group_name"); ok {
placementGrpNameStr = placementGrpNameIntf.(string)
}
if placementGrpIdIntf, ok := d.GetOk("placement_group"); ok {
placementGrpIdStr = placementGrpIdIntf.(string)
}
if insGrpInf, ok := d.GetOk(isInstanceGroup); ok {
insGrp = insGrpInf.(string)
} else if insGrpNameInf, ok := d.GetOk(isInstanceGroupName); ok {
insGrpName := insGrpNameInf.(string)
start := ""
allrecs := []vpcv1.InstanceGroup{}
for {
listInstanceGroupOptions := vpcv1.ListInstanceGroupsOptions{}
if start != "" {
listInstanceGroupOptions.Start = &start
}
instanceGroupsCollection, response, err := sess.ListInstanceGroups(&listInstanceGroupOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching InstanceGroups %s\n%s", err, response)
}
start = flex.GetNext(instanceGroupsCollection.Next)
allrecs = append(allrecs, instanceGroupsCollection.InstanceGroups...)
if start == "" {
break
}
}
for _, instanceGroup := range allrecs {
if *instanceGroup.Name == insGrpName {
insGrp = *instanceGroup.ID
break
}
}
}
listInstancesOptions := &vpcv1.ListInstancesOptions{}
if vpcName != "" {
listInstancesOptions.VPCName = &vpcName
}
if vpcID != "" {
listInstancesOptions.VPCID = &vpcID
}
if resourceGroup != "" {
listInstancesOptions.ResourceGroupID = &resourceGroup
}
if vpcCrn != "" {
listInstancesOptions.VPCCRN = &vpcCrn
}
if dHostNameStr != "" {
listInstancesOptions.DedicatedHostName = &dHostNameStr
}
if dHostIdStr != "" {
listInstancesOptions.DedicatedHostID = &dHostIdStr
}
if placementGrpNameStr != "" {
listInstancesOptions.PlacementGroupName = &placementGrpNameStr
}
if placementGrpIdStr != "" {
listInstancesOptions.PlacementGroupID = &placementGrpIdStr
}
start := ""
allrecs := []vpcv1.Instance{}
for {
if start != "" {
listInstancesOptions.Start = &start
}
instances, response, err := sess.ListInstances(listInstancesOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching Instances %s\n%s", err, response)
}
start = flex.GetNext(instances.Next)
allrecs = append(allrecs, instances.Instances...)
if start == "" {
break
}
}
if insGrp != "" {
membershipMap := map[string]bool{}
start := ""
for {
listInstanceGroupMembershipsOptions := vpcv1.ListInstanceGroupMembershipsOptions{
InstanceGroupID: &insGrp,
}
if start != "" {
listInstanceGroupMembershipsOptions.Start = &start
}
instanceGroupMembershipCollection, response, err := sess.ListInstanceGroupMemberships(&listInstanceGroupMembershipsOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Getting InstanceGroup Membership Collection %s\n%s", err, response)
}
start = flex.GetNext(instanceGroupMembershipCollection.Next)
for _, membershipItem := range instanceGroupMembershipCollection.Memberships {
membershipMap[*membershipItem.Instance.ID] = true
}
if start == "" {
break
}
}
//Filtering instance allrecs to contain instance group members only
i := 0
for _, ins := range allrecs {
if membershipMap[*ins.ID] {
allrecs[i] = ins
i++
}
}
allrecs = allrecs[:i]
}
instancesInfo := make([]map[string]interface{}, 0)
for _, instance := range allrecs {
id := *instance.ID
l := map[string]interface{}{}
l["id"] = id
l["crn"] = *instance.CRN
l["name"] = *instance.Name
l["memory"] = *instance.Memory
if instance.MetadataService != nil {
l[isInstanceMetadataServiceEnabled] = *instance.MetadataService.Enabled
}
l["status"] = *instance.Status
l["resource_group"] = *instance.ResourceGroup.ID
l["vpc"] = *instance.VPC.ID
if instance.AvailabilityPolicy != nil && instance.AvailabilityPolicy.HostFailure != nil {
l[isInstanceAvailablePolicyHostFailure] = *instance.AvailabilityPolicy.HostFailure
}
if instance.PlacementTarget != nil {
placementTargetMap := resourceIbmIsInstanceInstancePlacementToMap(*instance.PlacementTarget.(*vpcv1.InstancePlacementTarget))
l["placement_target"] = []map[string]interface{}{placementTargetMap}
}
if instance.Bandwidth != nil {
l[isInstanceBandwidth] = int(*instance.Bandwidth)
}
if instance.TotalNetworkBandwidth != nil {
l[isInstanceTotalNetworkBandwidth] = int(*instance.TotalNetworkBandwidth)
}
if instance.TotalVolumeBandwidth != nil {
l[isInstanceTotalVolumeBandwidth] = int(*instance.TotalVolumeBandwidth)
}
if instance.BootVolumeAttachment != nil {
bootVolList := make([]map[string]interface{}, 0)
bootVol := map[string]interface{}{}
bootVol["id"] = *instance.BootVolumeAttachment.ID
bootVol["name"] = *instance.BootVolumeAttachment.Name
if instance.BootVolumeAttachment.Device != nil {
bootVol["device"] = *instance.BootVolumeAttachment.Device.ID
}
if instance.BootVolumeAttachment.Volume != nil {
bootVol["volume_id"] = *instance.BootVolumeAttachment.Volume.ID
bootVol["volume_crn"] = *instance.BootVolumeAttachment.Volume.CRN
}
bootVolList = append(bootVolList, bootVol)
l["boot_volume"] = bootVolList
}
//set the status reasons
statusReasonsList := make([]map[string]interface{}, 0)
if instance.StatusReasons != nil {
for _, sr := range instance.StatusReasons {
currentSR := map[string]interface{}{}
if sr.Code != nil && sr.Message != nil {
currentSR[isInstanceStatusReasonsCode] = *sr.Code
currentSR[isInstanceStatusReasonsMessage] = *sr.Message
if sr.MoreInfo != nil {
currentSR[isInstanceStatusReasonsMoreInfo] = *sr.MoreInfo
}
statusReasonsList = append(statusReasonsList, currentSR)
}
}
}
l[isInstanceStatusReasons] = statusReasonsList
if instance.VolumeAttachments != nil {
volList := make([]map[string]interface{}, 0)
for _, volume := range instance.VolumeAttachments {
vol := map[string]interface{}{}
if volume.Volume != nil {
vol["id"] = *volume.ID
vol["volume_id"] = *volume.Volume.ID
vol["name"] = *volume.Name
vol["volume_name"] = *volume.Volume.Name
vol["volume_crn"] = *volume.Volume.CRN
volList = append(volList, vol)
}
}
l["volume_attachments"] = volList
}
if instance.PrimaryNetworkInterface != nil {
primaryNicList := make([]map[string]interface{}, 0)
currentPrimNic := map[string]interface{}{}
currentPrimNic["id"] = *instance.PrimaryNetworkInterface.ID
currentPrimNic[isInstanceNicName] = *instance.PrimaryNetworkInterface.Name
// reserved ip changes
primaryIpList := make([]map[string]interface{}, 0)
currentPrimIp := map[string]interface{}{}
if instance.PrimaryNetworkInterface.PrimaryIP.Address != nil {
currentPrimNic[isInstanceNicPrimaryIpv4Address] = *instance.PrimaryNetworkInterface.PrimaryIP.Address
currentPrimIp[isInstanceNicReservedIpAddress] = *instance.PrimaryNetworkInterface.PrimaryIP.Address
}
if instance.PrimaryNetworkInterface.PrimaryIP.Href != nil {
currentPrimIp[isInstanceNicReservedIpHref] = *instance.PrimaryNetworkInterface.PrimaryIP.Href
}
if instance.PrimaryNetworkInterface.PrimaryIP.Name != nil {
currentPrimIp[isInstanceNicReservedIpName] = *instance.PrimaryNetworkInterface.PrimaryIP.Name
}
if instance.PrimaryNetworkInterface.PrimaryIP.ID != nil {
currentPrimIp[isInstanceNicReservedIpId] = *instance.PrimaryNetworkInterface.PrimaryIP.ID
}
if instance.PrimaryNetworkInterface.PrimaryIP.ResourceType != nil {
currentPrimIp[isInstanceNicReservedIpResourceType] = *instance.PrimaryNetworkInterface.PrimaryIP.ResourceType
}
primaryIpList = append(primaryIpList, currentPrimIp)
currentPrimNic[isInstanceNicPrimaryIP] = primaryIpList
getnicoptions := &vpcv1.GetInstanceNetworkInterfaceOptions{
InstanceID: &id,
ID: instance.PrimaryNetworkInterface.ID,
}
insnic, response, err := sess.GetInstanceNetworkInterface(getnicoptions)
if err != nil {
return fmt.Errorf("[ERROR] Error getting network interfaces attached to the instance %s\n%s", err, response)
}
currentPrimNic[isInstanceNicSubnet] = *insnic.Subnet.ID
if len(insnic.SecurityGroups) != 0 {
secgrpList := []string{}
for i := 0; i < len(insnic.SecurityGroups); i++ {
secgrpList = append(secgrpList, string(*(insnic.SecurityGroups[i].ID)))
}
currentPrimNic[isInstanceNicSecurityGroups] = flex.NewStringSet(schema.HashString, secgrpList)
}
primaryNicList = append(primaryNicList, currentPrimNic)
l["primary_network_interface"] = primaryNicList
}
if instance.NetworkInterfaces != nil {
interfacesList := make([]map[string]interface{}, 0)
for _, intfc := range instance.NetworkInterfaces {
if *intfc.ID != *instance.PrimaryNetworkInterface.ID {
currentNic := map[string]interface{}{}
currentNic["id"] = *intfc.ID
currentNic[isInstanceNicName] = *intfc.Name
// reserved ip changes
primaryIpList := make([]map[string]interface{}, 0)
currentPrimIp := map[string]interface{}{}
if intfc.PrimaryIP.Address != nil {
currentPrimIp[isInstanceNicReservedIpAddress] = *intfc.PrimaryIP.Address
currentNic[isInstanceNicPrimaryIpv4Address] = *intfc.PrimaryIP.Address
}
if intfc.PrimaryIP.Href != nil {
currentPrimIp[isInstanceNicReservedIpHref] = *intfc.PrimaryIP.Href
}
if intfc.PrimaryIP.Name != nil {
currentPrimIp[isInstanceNicReservedIpName] = *intfc.PrimaryIP.Name
}
if intfc.PrimaryIP.ID != nil {
currentPrimIp[isInstanceNicReservedIpId] = *intfc.PrimaryIP.ID
}
if intfc.PrimaryIP.ResourceType != nil {
currentPrimIp[isInstanceNicReservedIpResourceType] = *intfc.PrimaryIP.ResourceType
}
primaryIpList = append(primaryIpList, currentPrimIp)
currentNic[isInstanceNicPrimaryIP] = primaryIpList
getnicoptions := &vpcv1.GetInstanceNetworkInterfaceOptions{
InstanceID: &id,
ID: intfc.ID,
}
insnic, response, err := sess.GetInstanceNetworkInterface(getnicoptions)
if err != nil {
return fmt.Errorf("[ERROR] Error getting network interfaces attached to the instance %s\n%s", err, response)
}
currentNic[isInstanceNicSubnet] = *insnic.Subnet.ID
if len(insnic.SecurityGroups) != 0 {
secgrpList := []string{}
for i := 0; i < len(insnic.SecurityGroups); i++ {
secgrpList = append(secgrpList, string(*(insnic.SecurityGroups[i].ID)))
}
currentNic[isInstanceNicSecurityGroups] = flex.NewStringSet(schema.HashString, secgrpList)
}
interfacesList = append(interfacesList, currentNic)
}
}
l["network_interfaces"] = interfacesList
}
l["profile"] = *instance.Profile.Name
cpuList := make([]map[string]interface{}, 0)
if instance.Vcpu != nil {
currentCPU := map[string]interface{}{}
currentCPU["architecture"] = *instance.Vcpu.Architecture
currentCPU["count"] = *instance.Vcpu.Count
cpuList = append(cpuList, currentCPU)
}
l["vcpu"] = cpuList
gpuList := make([]map[string]interface{}, 0)
if instance.Gpu != nil {
currentGpu := map[string]interface{}{}
currentGpu[isInstanceGpuManufacturer] = instance.Gpu.Manufacturer
currentGpu[isInstanceGpuModel] = instance.Gpu.Model
currentGpu[isInstanceGpuCount] = instance.Gpu.Count
currentGpu[isInstanceGpuMemory] = instance.Gpu.Memory
gpuList = append(gpuList, currentGpu)
l[isInstanceGpu] = gpuList
}
//set the lifecycle status, reasons
if instance.LifecycleState != nil {
l[isInstanceLifecycleState] = *instance.LifecycleState
}
if instance.LifecycleReasons != nil {
l[isInstanceLifecycleReasons] = dataSourceInstanceFlattenLifecycleReasons(instance.LifecycleReasons)
}
l["zone"] = *instance.Zone.Name
if instance.Image != nil {
l["image"] = *instance.Image.ID
}
if instance.Disks != nil {
l[isInstanceDisks] = dataSourceInstanceFlattenDisks(instance.Disks)
}
instancesInfo = append(instancesInfo, l)
}
d.SetId(dataSourceIBMISInstancesID(d))
d.Set(isInstances, instancesInfo)
return nil
}
// dataSourceIBMISInstancesID returns a reasonable ID for a Instance list.
func dataSourceIBMISInstancesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}