-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_is_bare_metal_server.go
779 lines (731 loc) · 28.2 KB
/
data_source_ibm_is_bare_metal_server.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"reflect"
"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/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMIsBareMetalServer() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMISBareMetalServerRead,
Schema: map[string]*schema.Schema{
"identifier": {
Type: schema.TypeString,
Optional: true,
Computed: true,
AtLeastOneOf: []string{isBareMetalServerName, "identifier"},
ConflictsWith: []string{isBareMetalServerName},
ValidateFunc: validate.InvokeDataSourceValidator("ibm_is_bare_metal_server", "identifier"),
},
isBareMetalServerName: {
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{isBareMetalServerName, "identifier"},
Computed: true,
ConflictsWith: []string{"identifier"},
ValidateFunc: validate.InvokeValidator("ibm_is_bare_metal_server", isBareMetalServerName),
Description: "Bare metal server name",
},
isBareMetalServerBandwidth: {
Type: schema.TypeInt,
Computed: true,
Description: "The total bandwidth (in megabits per second)",
},
isBareMetalServerEnableSecureBoot: {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether secure boot is enabled. If enabled, the image must support secure boot or the server will fail to boot.",
},
isBareMetalServerTrustedPlatformModule: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isBareMetalServerTrustedPlatformModuleMode: {
Type: schema.TypeString,
Computed: true,
Description: "The trusted platform module mode to use. The specified value must be listed in the bare metal server profile's supported_trusted_platform_module_modes",
},
isBareMetalServerTrustedPlatformModuleEnabled: {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether the trusted platform module is enabled.",
},
isBareMetalServerTrustedPlatformModuleSupportedModes: {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Computed: true,
Description: "The trusted platform module (TPM) mode:: disabled: No TPM functionality, tpm_2: TPM 2.0. 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. Enum: [ disabled, tpm_2 ]",
},
},
},
},
isBareMetalServerBootTarget: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this bare metal server disk",
},
isBareMetalServerCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the bare metal server was created",
},
isBareMetalServerCPU: {
Type: schema.TypeList,
Computed: true,
Description: "The bare metal server CPU configuration",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isBareMetalServerCPUArchitecture: {
Type: schema.TypeString,
Computed: true,
Description: "The CPU architecture",
},
isBareMetalServerCPUCoreCount: {
Type: schema.TypeInt,
Computed: true,
Description: "The total number of cores",
},
isBareMetalServerCpuSocketCount: {
Type: schema.TypeInt,
Computed: true,
Description: "The total number of CPU sockets",
},
isBareMetalServerCpuThreadPerCore: {
Type: schema.TypeInt,
Computed: true,
Description: "The total number of hardware threads per core",
},
},
},
},
isBareMetalServerCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this bare metal server",
},
isBareMetalServerDisks: {
Type: schema.TypeList,
Computed: true,
Description: "The disks for this bare metal server, including any disks that are associated with the boot_target.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isBareMetalServerDiskHref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this bare metal server disk",
},
isBareMetalServerDiskID: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this bare metal server disk",
},
isBareMetalServerDiskInterfaceType: {
Type: schema.TypeString,
Computed: true,
Description: "The disk interface used for attaching the disk. Supported values are [ nvme, sata ]",
},
isBareMetalServerDiskName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this disk",
},
isBareMetalServerDiskResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type",
},
isBareMetalServerDiskSize: {
Type: schema.TypeInt,
Computed: true,
Description: "The size of the disk in GB (gigabytes)",
},
},
},
},
isBareMetalServerHref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this bare metal server",
},
isBareMetalServerMemory: {
Type: schema.TypeInt,
Computed: true,
Description: "The amount of memory, truncated to whole gibibytes",
},
isBareMetalServerPrimaryNetworkInterface: {
Type: schema.TypeList,
Computed: true,
Description: "Primary Network interface info",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
isBareMetalServerNicAllowIPSpoofing: {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether IP spoofing is allowed on this interface.",
},
isBareMetalServerNicName: {
Type: schema.TypeString,
Computed: true,
},
isBareMetalServerNicPortSpeed: {
Type: schema.TypeInt,
Computed: true,
},
isBareMetalServerNicHref: {
Type: schema.TypeString,
Computed: true,
Deprecated: "This URL of the interface",
},
isBareMetalServerNicSecurityGroups: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
isBareMetalServerNicSubnet: {
Type: schema.TypeString,
Computed: true,
},
isBareMetalServerNicPrimaryIP: {
Type: schema.TypeList,
Computed: true,
Description: "IPv4, The IP address. ",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isBareMetalServerNicIpAddress: {
Type: schema.TypeString,
Computed: true,
Description: "The globally unique IP address",
},
isBareMetalServerNicIpHref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this reserved IP",
},
isBareMetalServerNicIpName: {
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. ",
},
isBareMetalServerNicIpID: {
Type: schema.TypeString,
Computed: true,
Description: "Identifies a reserved IP by a unique property.",
},
isBareMetalServerNicResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type",
},
},
},
},
},
},
},
isBareMetalServerNetworkInterfaces: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
isBareMetalServerNicHref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this network interface",
},
isBareMetalServerNicAllowIPSpoofing: {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether IP spoofing is allowed on this interface.",
},
isBareMetalServerNicName: {
Type: schema.TypeString,
Computed: true,
},
isBareMetalServerNicSecurityGroups: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
isBareMetalServerNicSubnet: {
Type: schema.TypeString,
Computed: true,
},
isBareMetalServerNicPrimaryIP: {
Type: schema.TypeList,
Computed: true,
Description: "IPv4, The IP address. ",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isBareMetalServerNicIpAddress: {
Type: schema.TypeString,
Computed: true,
Description: "The globally unique IP address",
},
isBareMetalServerNicIpHref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this reserved IP",
},
isBareMetalServerNicIpName: {
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. ",
},
isBareMetalServerNicIpID: {
Type: schema.TypeString,
Computed: true,
Description: "Identifies a reserved IP by a unique property.",
},
isBareMetalServerNicResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type",
},
},
},
},
},
},
},
isBareMetalServerKeys: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: "SSH key Ids for the bare metal server",
},
isBareMetalServerImage: {
Type: schema.TypeString,
Computed: true,
Description: "image name",
},
isBareMetalServerProfile: {
Type: schema.TypeString,
Computed: true,
Description: "profil name",
},
isBareMetalServerZone: {
Type: schema.TypeString,
Computed: true,
Description: "Zone name",
},
isBareMetalServerVPC: {
Type: schema.TypeString,
Computed: true,
Description: "The VPC the bare metal server is to be a part of",
},
isBareMetalServerResourceGroup: {
Type: schema.TypeString,
Computed: true,
Description: "Resource group name",
},
isBareMetalServerResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "Resource type name",
},
isBareMetalServerStatus: {
Type: schema.TypeString,
Computed: true,
Description: "Bare metal server status",
},
isBareMetalServerStatusReasons: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isBareMetalServerStatusReasonsCode: {
Type: schema.TypeString,
Computed: true,
Description: "A snake case string succinctly identifying the status reason",
},
isBareMetalServerStatusReasonsMessage: {
Type: schema.TypeString,
Computed: true,
Description: "An explanation of the status reason",
},
isBareMetalServerStatusReasonsMoreInfo: {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about this status reason",
},
},
},
},
isBareMetalServerTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString, ValidateFunc: validate.InvokeValidator("ibm_is_bare_metal_server", "tags")},
Set: flex.ResourceIBMVPCHash,
Description: "Tags for the Bare metal server",
},
isBareMetalServerAccessTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "List of access tags",
},
},
}
}
func DataSourceIBMIsBareMetalServerValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "identifier",
ValidateFunctionIdentifier: validate.ValidateNoZeroValues,
Type: validate.TypeString})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isBareMetalServerName,
ValidateFunctionIdentifier: validate.ValidateNoZeroValues,
Type: validate.TypeString})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "tags",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Optional: true,
Regexp: `^[A-Za-z0-9:_ .-]+$`,
MinValueLength: 1,
MaxValueLength: 128})
ibmISBMSDataSourceValidator := validate.ResourceValidator{ResourceName: "ibm_is_bare_metal_server", Schema: validateSchema}
return &ibmISBMSDataSourceValidator
}
func dataSourceIBMISBareMetalServerRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
id := d.Get("identifier").(string)
name := d.Get(isBareMetalServerName).(string)
sess, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
var bms *vpcv1.BareMetalServer
if id != "" {
options := &vpcv1.GetBareMetalServerOptions{}
options.ID = &id
server, response, err := sess.GetBareMetalServerWithContext(context, options)
if err != nil {
log.Printf("[DEBUG] GetBareMetalServerWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("[ERROR] Error Getting Bare Metal Server (%s): %s\n%s", id, err, response))
}
bms = server
} else if name != "" {
options := &vpcv1.ListBareMetalServersOptions{}
options.Name = &name
bmservers, response, err := sess.ListBareMetalServersWithContext(context, options)
if err != nil {
log.Printf("[DEBUG] ListBareMetalServersWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("[ERROR] Error Listing Bare Metal Server (%s): %s\n%s", name, err, response))
}
if len(bmservers.BareMetalServers) == 0 {
return diag.FromErr(fmt.Errorf("[ERROR] No bare metal servers found with name %s", name))
}
bms = &bmservers.BareMetalServers[0]
}
d.SetId(*bms.ID)
d.Set(isBareMetalServerBandwidth, bms.Bandwidth)
bmsBootTargetIntf := bms.BootTarget.(*vpcv1.BareMetalServerBootTarget)
bmsBootTarget := bmsBootTargetIntf.ID
d.Set(isBareMetalServerBootTarget, bmsBootTarget)
// set keys and image using initialization
optionsInitialization := &vpcv1.GetBareMetalServerInitializationOptions{
ID: bms.ID,
}
initialization, response, err := sess.GetBareMetalServerInitialization(optionsInitialization)
if err != nil || initialization == nil {
return diag.FromErr(fmt.Errorf("[Error] Error getting Bare Metal Server (%s) initialization : %s\n%s", *bms.ID, err, response))
}
d.Set(isBareMetalServerImage, initialization.Image.ID)
keyListList := []string{}
for i := 0; i < len(initialization.Keys); i++ {
keyListList = append(keyListList, string(*(initialization.Keys[i].ID)))
}
d.Set(isBareMetalServerKeys, keyListList)
cpuList := make([]map[string]interface{}, 0)
if bms.Cpu != nil {
currentCPU := map[string]interface{}{}
currentCPU[isBareMetalServerCPUArchitecture] = *bms.Cpu.Architecture
currentCPU[isBareMetalServerCPUCoreCount] = *bms.Cpu.CoreCount
currentCPU[isBareMetalServerCpuSocketCount] = *bms.Cpu.SocketCount
currentCPU[isBareMetalServerCpuThreadPerCore] = *bms.Cpu.ThreadsPerCore
cpuList = append(cpuList, currentCPU)
}
d.Set(isBareMetalServerCPU, cpuList)
if err = d.Set(isBareMetalServerCreatedAt, bms.CreatedAt.String()); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting created_at: %s", err))
}
if err = d.Set(isBareMetalServerCRN, bms.CRN); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting crn: %s", err))
}
diskList := make([]map[string]interface{}, 0)
if bms.Disks != nil {
for _, disk := range bms.Disks {
currentDisk := map[string]interface{}{
isBareMetalServerDiskHref: disk.Href,
isBareMetalServerDiskID: disk.ID,
isBareMetalServerDiskInterfaceType: disk.InterfaceType,
isBareMetalServerDiskName: disk.Name,
isBareMetalServerDiskResourceType: disk.ResourceType,
isBareMetalServerDiskSize: disk.Size,
}
diskList = append(diskList, currentDisk)
}
}
d.Set(isBareMetalServerDisks, diskList)
if err = d.Set(isBareMetalServerHref, bms.Href); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting href: %s", err))
}
if err = d.Set(isBareMetalServerMemory, bms.Memory); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting memory: %s", err))
}
if err = d.Set(isBareMetalServerName, *bms.Name); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting name: %s", err))
}
if err = d.Set("identifier", *bms.ID); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting identifier: %s", err))
}
//enable secure boot
if err = d.Set(isBareMetalServerEnableSecureBoot, bms.EnableSecureBoot); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting enable_secure_boot: %s", err))
}
// tpm
if bms.TrustedPlatformModule != nil {
trustedPlatformModuleMap, err := resourceIBMIsBareMetalServerBareMetalServerTrustedPlatformModulePrototypeToMap(bms.TrustedPlatformModule)
if err != nil {
return diag.FromErr(err)
}
if err = d.Set(isBareMetalServerTrustedPlatformModule, []map[string]interface{}{trustedPlatformModuleMap}); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting trusted_platform_module: %s", err))
}
}
//pni
if bms.PrimaryNetworkInterface != nil {
primaryNicList := make([]map[string]interface{}, 0)
currentPrimNic := map[string]interface{}{}
currentPrimNic["id"] = *bms.PrimaryNetworkInterface.ID
currentPrimNic[isBareMetalServerNicHref] = *bms.PrimaryNetworkInterface.Href
currentPrimNic[isBareMetalServerNicName] = *bms.PrimaryNetworkInterface.Name
currentPrimNic[isBareMetalServerNicHref] = *bms.PrimaryNetworkInterface.Href
currentPrimNic[isBareMetalServerNicSubnet] = *bms.PrimaryNetworkInterface.Subnet.ID
if bms.PrimaryNetworkInterface.PrimaryIP != nil {
primaryIpList := make([]map[string]interface{}, 0)
currentIP := map[string]interface{}{}
if bms.PrimaryNetworkInterface.PrimaryIP.Href != nil {
currentIP[isBareMetalServerNicIpAddress] = *bms.PrimaryNetworkInterface.PrimaryIP.Address
}
if bms.PrimaryNetworkInterface.PrimaryIP.Href != nil {
currentIP[isBareMetalServerNicIpHref] = *bms.PrimaryNetworkInterface.PrimaryIP.Href
}
if bms.PrimaryNetworkInterface.PrimaryIP.Name != nil {
currentIP[isBareMetalServerNicIpName] = *bms.PrimaryNetworkInterface.PrimaryIP.Name
}
if bms.PrimaryNetworkInterface.PrimaryIP.ID != nil {
currentIP[isBareMetalServerNicIpID] = *bms.PrimaryNetworkInterface.PrimaryIP.ID
}
if bms.PrimaryNetworkInterface.PrimaryIP.ResourceType != nil {
currentIP[isBareMetalServerNicResourceType] = *bms.PrimaryNetworkInterface.PrimaryIP.ResourceType
}
primaryIpList = append(primaryIpList, currentIP)
currentPrimNic[isBareMetalServerNicPrimaryIP] = primaryIpList
}
getnicoptions := &vpcv1.GetBareMetalServerNetworkInterfaceOptions{
BareMetalServerID: bms.ID,
ID: bms.PrimaryNetworkInterface.ID,
}
bmsnic, response, err := sess.GetBareMetalServerNetworkInterface(getnicoptions)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error getting network interfaces attached to the bare metal server %s\n%s", err, response))
}
switch reflect.TypeOf(bmsnic).String() {
case "*vpcv1.BareMetalServerNetworkInterfaceByPci":
{
primNic := bmsnic.(*vpcv1.BareMetalServerNetworkInterfaceByPci)
currentPrimNic[isInstanceNicAllowIPSpoofing] = *primNic.AllowIPSpoofing
currentPrimNic[isBareMetalServerNicPortSpeed] = *primNic.PortSpeed
if len(primNic.SecurityGroups) != 0 {
secgrpList := []string{}
for i := 0; i < len(primNic.SecurityGroups); i++ {
secgrpList = append(secgrpList, string(*(primNic.SecurityGroups[i].ID)))
}
currentPrimNic[isInstanceNicSecurityGroups] = flex.NewStringSet(schema.HashString, secgrpList)
}
}
case "*vpcv1.BareMetalServerNetworkInterfaceByVlan":
{
primNic := bmsnic.(*vpcv1.BareMetalServerNetworkInterfaceByVlan)
currentPrimNic[isInstanceNicAllowIPSpoofing] = *primNic.AllowIPSpoofing
currentPrimNic[isBareMetalServerNicPortSpeed] = *primNic.PortSpeed
if len(primNic.SecurityGroups) != 0 {
secgrpList := []string{}
for i := 0; i < len(primNic.SecurityGroups); i++ {
secgrpList = append(secgrpList, string(*(primNic.SecurityGroups[i].ID)))
}
currentPrimNic[isInstanceNicSecurityGroups] = flex.NewStringSet(schema.HashString, secgrpList)
}
}
case "*vpcv1.BareMetalServerNetworkInterfaceByHiperSocket":
{
primNic := bmsnic.(*vpcv1.BareMetalServerNetworkInterfaceByHiperSocket)
currentPrimNic[isInstanceNicAllowIPSpoofing] = *primNic.AllowIPSpoofing
if len(primNic.SecurityGroups) != 0 {
secgrpList := []string{}
for i := 0; i < len(primNic.SecurityGroups); i++ {
secgrpList = append(secgrpList, string(*(primNic.SecurityGroups[i].ID)))
}
currentPrimNic[isInstanceNicSecurityGroups] = flex.NewStringSet(schema.HashString, secgrpList)
}
}
}
primaryNicList = append(primaryNicList, currentPrimNic)
d.Set(isBareMetalServerPrimaryNetworkInterface, primaryNicList)
}
//ni
interfacesList := make([]map[string]interface{}, 0)
for _, intfc := range bms.NetworkInterfaces {
if intfc.ID != nil && *intfc.ID != *bms.PrimaryNetworkInterface.ID {
currentNic := map[string]interface{}{}
currentNic["id"] = *intfc.ID
currentNic[isBareMetalServerNicHref] = *intfc.Href
currentNic[isBareMetalServerNicName] = *intfc.Name
if intfc.PrimaryIP != nil {
primaryIpList := make([]map[string]interface{}, 0)
currentIP := map[string]interface{}{}
if intfc.PrimaryIP.Href != nil {
currentIP[isBareMetalServerNicIpAddress] = *intfc.PrimaryIP.Address
}
if intfc.PrimaryIP.Href != nil {
currentIP[isBareMetalServerNicIpHref] = *intfc.PrimaryIP.Href
}
if intfc.PrimaryIP.Name != nil {
currentIP[isBareMetalServerNicIpName] = *intfc.PrimaryIP.Name
}
if intfc.PrimaryIP.ID != nil {
currentIP[isBareMetalServerNicIpID] = *intfc.PrimaryIP.ID
}
if intfc.PrimaryIP.ResourceType != nil {
currentIP[isBareMetalServerNicResourceType] = *intfc.PrimaryIP.ResourceType
}
primaryIpList = append(primaryIpList, currentIP)
currentNic[isBareMetalServerNicPrimaryIP] = primaryIpList
}
getnicoptions := &vpcv1.GetBareMetalServerNetworkInterfaceOptions{
BareMetalServerID: bms.ID,
ID: intfc.ID,
}
bmsnicintf, response, err := sess.GetBareMetalServerNetworkInterface(getnicoptions)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error getting network interfaces attached to the bare metal server %s\n%s", err, response))
}
switch reflect.TypeOf(bmsnicintf).String() {
case "*vpcv1.BareMetalServerNetworkInterfaceByPci":
{
bmsnic := bmsnicintf.(*vpcv1.BareMetalServerNetworkInterfaceByPci)
currentNic[isBareMetalServerNicAllowIPSpoofing] = *bmsnic.AllowIPSpoofing
currentNic[isBareMetalServerNicSubnet] = *bmsnic.Subnet.ID
if len(bmsnic.SecurityGroups) != 0 {
secgrpList := []string{}
for i := 0; i < len(bmsnic.SecurityGroups); i++ {
secgrpList = append(secgrpList, string(*(bmsnic.SecurityGroups[i].ID)))
}
currentNic[isBareMetalServerNicSecurityGroups] = flex.NewStringSet(schema.HashString, secgrpList)
}
}
case "*vpcv1.BareMetalServerNetworkInterfaceByVlan":
{
bmsnic := bmsnicintf.(*vpcv1.BareMetalServerNetworkInterfaceByVlan)
currentNic[isBareMetalServerNicAllowIPSpoofing] = *bmsnic.AllowIPSpoofing
currentNic[isBareMetalServerNicSubnet] = *bmsnic.Subnet.ID
if len(bmsnic.SecurityGroups) != 0 {
secgrpList := []string{}
for i := 0; i < len(bmsnic.SecurityGroups); i++ {
secgrpList = append(secgrpList, string(*(bmsnic.SecurityGroups[i].ID)))
}
currentNic[isBareMetalServerNicSecurityGroups] = flex.NewStringSet(schema.HashString, secgrpList)
}
}
case "*vpcv1.BareMetalServerNetworkInterfaceByHiperSocket":
{
bmsnic := bmsnicintf.(*vpcv1.BareMetalServerNetworkInterfaceByHiperSocket)
currentNic[isBareMetalServerNicAllowIPSpoofing] = *bmsnic.AllowIPSpoofing
currentNic[isBareMetalServerNicSubnet] = *bmsnic.Subnet.ID
if len(bmsnic.SecurityGroups) != 0 {
secgrpList := []string{}
for i := 0; i < len(bmsnic.SecurityGroups); i++ {
secgrpList = append(secgrpList, string(*(bmsnic.SecurityGroups[i].ID)))
}
currentNic[isBareMetalServerNicSecurityGroups] = flex.NewStringSet(schema.HashString, secgrpList)
}
}
}
interfacesList = append(interfacesList, currentNic)
}
}
d.Set(isBareMetalServerNetworkInterfaces, interfacesList)
if err = d.Set(isBareMetalServerProfile, *bms.Profile.Name); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting profile: %s", err))
}
if bms.ResourceGroup != nil {
d.Set(isBareMetalServerResourceGroup, *bms.ResourceGroup.ID)
}
if err = d.Set(isBareMetalServerResourceType, bms.ResourceType); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting resource_type: %s", err))
}
if err = d.Set(isBareMetalServerStatus, *bms.Status); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting status: %s", err))
}
statusReasonsList := make([]map[string]interface{}, 0)
if bms.StatusReasons != nil {
for _, sr := range bms.StatusReasons {
currentSR := map[string]interface{}{}
if sr.Code != nil && sr.Message != nil {
currentSR[isBareMetalServerStatusReasonsCode] = *sr.Code
currentSR[isBareMetalServerStatusReasonsMessage] = *sr.Message
if sr.MoreInfo != nil {
currentSR[isBareMetalServerStatusReasonsMoreInfo] = *sr.MoreInfo
}
statusReasonsList = append(statusReasonsList, currentSR)
}
}
}
d.Set(isBareMetalServerStatusReasons, statusReasonsList)
if err = d.Set(isBareMetalServerVPC, *bms.VPC.ID); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting vpc: %s", err))
}
if err = d.Set(isBareMetalServerZone, *bms.Zone.Name); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting zone: %s", err))
}
tags, err := flex.GetGlobalTagsUsingCRN(meta, *bms.CRN, "", isBareMetalServerAccessTagType)
if err != nil {
log.Printf(
"[ERROR] Error on get of resource bare metal server (%s) tags: %s", d.Id(), err)
}
d.Set(isBareMetalServerTags, tags)
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *bms.CRN, "", isBareMetalServerAccessTagType)
if err != nil {
log.Printf(
"[ERROR] Error on get of resource bare metal server (%s) tags: %s", d.Id(), err)
}
d.Set(isBareMetalServerAccessTags, accesstags)
return nil
}