This repository has been archived by the owner on Mar 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
instances.go
665 lines (565 loc) · 16.5 KB
/
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
package opc
import (
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/structure"
"github.com/hashicorp/terraform/helper/validation"
)
func orchestrationInstanceSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"shape": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
/////////////////////////
// Optional Attributes //
/////////////////////////
"persistent": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"instance_attributes": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.ValidateJsonString,
DiffSuppressFunc: structure.SuppressJsonDiff,
},
"boot_order": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeInt},
},
"hostname": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"image_list": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"label": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"networking_info": {
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"dns": {
// Required for Shared Network Interface, will default if unspecified, however
// Optional for IP Network Interface
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"index": {
Type: schema.TypeInt,
ForceNew: true,
Required: true,
},
"ip_address": {
// Optional, IP Network only
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},
"ip_network": {
// Required for an IP Network Interface
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},
"is_default_gateway": {
// Optional, IP Network only
Type: schema.TypeBool,
ForceNew: true,
Optional: true,
},
"mac_address": {
// Optional, IP Network Only
Type: schema.TypeString,
ForceNew: true,
Computed: true,
Optional: true,
},
"name_servers": {
// Optional, IP Network + Shared Network
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"nat": {
// Optional for IP Network
// Required for Shared Network
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"search_domains": {
// Optional, IP Network + Shared Network
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"sec_lists": {
// Required, Shared Network only. Will default if unspecified however
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"shared_network": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: false,
},
"vnic": {
// Optional, IP Network only.
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},
"vnic_sets": {
// Optional, IP Network only.
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"reverse_dns": {
Type: schema.TypeBool,
Optional: true,
Default: true,
ForceNew: true,
},
"ssh_keys": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"storage": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"index": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
ValidateFunc: validation.IntBetween(1, 10),
},
"volume": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"tags": tagsForceNewSchema(),
/////////////////////////
// Computed Attributes //
/////////////////////////
"attributes": {
Type: schema.TypeString,
Computed: true,
},
"availability_domain": {
Type: schema.TypeString,
Computed: true,
},
"domain": {
Type: schema.TypeString,
Computed: true,
},
"entry": {
Type: schema.TypeInt,
Computed: true,
},
"fingerprint": {
Type: schema.TypeString,
Computed: true,
},
"fqdn": {
Type: schema.TypeString,
Computed: true,
},
"image_format": {
Type: schema.TypeString,
Computed: true,
},
"ip_address": {
Type: schema.TypeString,
Computed: true,
},
"placement_requirements": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"platform": {
Type: schema.TypeString,
Computed: true,
},
"priority": {
Type: schema.TypeString,
Computed: true,
},
"quota_reservation": {
Type: schema.TypeString,
Computed: true,
},
"relationships": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"resolvers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"site": {
Type: schema.TypeString,
Computed: true,
},
"start_time": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"vcable": {
Type: schema.TypeString,
Computed: true,
},
"virtio": {
Type: schema.TypeBool,
Computed: true,
},
"vnc_address": {
Type: schema.TypeString,
Computed: true,
},
},
},
}
}
// We can create multiple instances with an orchestration so we pass a prefix in to obtain
// the CreateInput for each instance.
func expandCreateInstanceInput(prefix string, d *schema.ResourceData) (*compute.CreateInstanceInput, error) {
// Get Required Attributes
input := &compute.CreateInstanceInput{
Name: d.Get(fmt.Sprintf("%s.name", prefix)).(string),
Shape: d.Get(fmt.Sprintf("%s.shape", prefix)).(string),
}
bootOrder := getIntList(d, fmt.Sprintf("%s.boot_order", prefix))
imageList := d.Get(fmt.Sprintf("%s.image_list", prefix))
if len(bootOrder) == 0 && imageList.(string) == "" {
return nil, fmt.Errorf("One of boot_order or image_list must be set for instance to be created")
}
if len(bootOrder) > 0 {
input.BootOrder = bootOrder
}
if imageList != nil {
input.ImageList = imageList.(string)
}
if v, ok := d.GetOk(fmt.Sprintf("%s.hostname", prefix)); ok {
input.Hostname = v.(string)
}
if v, ok := d.GetOk(fmt.Sprintf("%s.label", prefix)); ok {
input.Label = v.(string)
}
interfaces, err := expandNetworkInterfacesFromConfig(d, prefix)
if err != nil {
return nil, err
}
if interfaces != nil {
input.Networking = interfaces
}
if v, ok := d.GetOk(fmt.Sprintf("%s.reverse_dns", prefix)); ok {
input.ReverseDNS = v.(bool)
}
if sshKeys := getStringList(d, fmt.Sprintf("%s.ssh_keys", prefix)); len(sshKeys) > 0 {
input.SSHKeys = sshKeys
}
storage := expandStorageAttachments(d, prefix)
if len(storage) > 0 {
input.Storage = storage
}
if tags := getStringList(d, fmt.Sprintf("%s.tags", prefix)); len(tags) > 0 {
input.Tags = tags
}
if attributes, ok := d.GetOk(fmt.Sprintf("%s.instance_attributes", prefix)); ok {
attrs, err := structure.ExpandJsonFromString(attributes.(string))
if err != nil {
return nil, fmt.Errorf("Cannot parse attributes as json: %s", err)
}
if attrs != nil {
input.Attributes = attrs
}
}
return input, nil
}
// Populates and validates shared network and ip network interfaces to return the of map
// objects needed to create/update an instance's networking_info
func expandNetworkInterfacesFromConfig(d *schema.ResourceData, prefix string) (map[string]compute.NetworkingInfo, error) {
interfaces := make(map[string]compute.NetworkingInfo)
if v, ok := d.GetOk(fmt.Sprintf("%s.networking_info", prefix)); ok {
vL := v.([]interface{})
for _, v := range vL {
ni := v.(map[string]interface{})
index, ok := ni["index"].(int)
if !ok {
return nil, fmt.Errorf("Index not specified for network interface: %v", ni)
}
deviceIndex := fmt.Sprintf("eth%d", index)
// Verify that the network interface doesn't already exist
if _, ok := interfaces[deviceIndex]; ok {
return nil, fmt.Errorf("Duplicate Network interface at eth%d already specified", index)
}
// Determine if we're creating a shared network interface or an IP Network interface
info := compute.NetworkingInfo{}
var err error
if ni["shared_network"].(bool) {
// Populate shared network parameters
info, err = readSharedNetworkFromConfig(ni)
// Set 'model' since we're configuring a shared network interface
info.Model = compute.NICDefaultModel
} else {
// Populate IP Network Parameters
info, err = readIPNetworkFromConfig(ni)
}
if err != nil {
return nil, err
}
// And you may find yourself in a beautiful house, with a beautiful wife
// And you may ask yourself, well, how did I get here?
interfaces[deviceIndex] = info
}
}
return interfaces, nil
}
func expandStorageAttachments(d *schema.ResourceData, prefix string) []compute.StorageAttachmentInput {
storageAttachments := []compute.StorageAttachmentInput{}
storage := d.Get(fmt.Sprintf("%s.storage", prefix)).([]interface{})
for _, i := range storage {
attrs := i.(map[string]interface{})
storageAttachments = append(storageAttachments, compute.StorageAttachmentInput{
Index: attrs["index"].(int),
Volume: attrs["volume"].(string),
})
}
return storageAttachments
}
func flattenOrchestratedInstances(d *schema.ResourceData, meta interface{}, objects []compute.Object) (interface{}, error) {
instanceClient := meta.(*Client).computeClient.Instances()
result := make([]interface{}, len(objects))
for i := range objects {
v := make(map[string]interface{})
getIDInput := &compute.GetInstanceIDInput{
// Oracle's api returns an unordered list so we'll find out instances through the config file name
Name: d.Get(fmt.Sprintf("instance.%d.name", i)).(string),
}
instance, err := instanceClient.GetInstanceFromName(getIDInput)
if err != nil {
return nil, err
}
v["name"] = instance.Name
v["persistent"] = objects[i].Persistent
v["shape"] = instance.Shape
v["id"] = instance.ID
if attrs, ok := d.GetOk(fmt.Sprintf("instance.%d.instance_attributes", i)); ok && attrs != nil {
v["instance_attributes"] = attrs.(string)
} else {
instanceAttributes, err := flattenInstanceAttributes(instance.Attributes)
if err != nil {
return nil, err
}
v["instance_attributes"] = instanceAttributes
}
sort.Ints(instance.BootOrder)
v["boot_order"] = instance.BootOrder
splitHostname := strings.Split(instance.Hostname, ".")
if len(splitHostname) == 0 {
return nil, fmt.Errorf("Unable to parse hostname: %s", instance.Hostname)
}
v["hostname"] = splitHostname[0]
v["fqdn"] = instance.Hostname
v["image_list"] = instance.ImageList
v["label"] = instance.Label
networkInterfaces, err := flattenNetworkInterfaces(instance.Networking)
if err != nil {
return nil, err
}
if len(networkInterfaces) > 0 {
v["networking_info"] = networkInterfaces
}
sort.Strings(instance.SSHKeys)
v["ssh_keys"] = instance.SSHKeys
v["reverse_dns"] = instance.ReverseDNS
v["storage"] = flattenInstanceStorageAttachments(instance.Storage)
sort.Strings(instance.Tags)
v["tags"] = instance.Tags
v["availability_domain"] = instance.AvailabilityDomain
v["domain"] = instance.Domain
v["entry"] = instance.Entry
v["fingerprint"] = instance.Fingerprint
v["image_format"] = instance.ImageFormat
v["ip_address"] = instance.IPAddress
sort.Strings(instance.PlacementRequirements)
v["placement_requirements"] = instance.PlacementRequirements
v["platform"] = instance.Platform
v["priority"] = instance.Priority
v["quota_reservation"] = instance.QuotaReservation
sort.Strings(instance.Relationships)
v["relationships"] = instance.Relationships
sort.Strings(instance.Resolvers)
v["resolvers"] = instance.Resolvers
v["site"] = instance.Site
v["start_time"] = instance.StartTime
v["state"] = instance.State
v["vcable"] = instance.VCableID
v["virtio"] = instance.Virtio
v["vnc_address"] = instance.VNC
result[i] = v
}
return result, nil
}
// Flattens attributes from the returned instance object, and sets the computed attributes string
// as JSON
func flattenInstanceAttributes(attributes map[string]interface{}) (string, error) {
// Shouldn't ever get nil attributes on an instance, but protect against the case either way
if attributes == nil {
return "", nil
}
b, err := json.Marshal(attributes)
if err != nil {
return "", fmt.Errorf("Error marshalling returned attributes: %s", err)
}
return string(b), nil
}
// Flattens network interfaces
func flattenNetworkInterfaces(ifaces map[string]compute.NetworkingInfo) ([]map[string]interface{}, error) {
result := make([]map[string]interface{}, 0)
// Nil check for import case
if ifaces == nil {
return result, nil
}
for index, iface := range ifaces {
res := make(map[string]interface{})
// The index returned from the SDK holds the full device_index from the instance.
// For users convenience, we simply allow them to specify the integer equivalent of the device_index
// so a user could implement several network interfaces via `count`.
// Convert the full device_index `ethN` to `N` as an integer.
index := strings.TrimPrefix(index, "eth")
indexInt, err := strconv.Atoi(index)
if err != nil {
return nil, err
}
res["index"] = indexInt
res["is_default_gateway"] = iface.IsDefaultGateway
// Set the proper attributes for this specific network interface
if iface.DNS != nil {
res["dns"] = iface.DNS
}
if iface.IPAddress != "" {
res["ip_address"] = iface.IPAddress
}
if iface.IPNetwork != "" {
res["ip_network"] = iface.IPNetwork
}
if iface.MACAddress != "" {
res["mac_address"] = iface.MACAddress
}
if iface.Model != "" {
// Model can only be set on Shared networks
res["shared_network"] = true
}
if iface.NameServers != nil {
res["name_servers"] = iface.NameServers
}
if iface.Nat != nil {
res["nat"] = iface.Nat
}
if iface.SearchDomains != nil {
res["search_domains"] = iface.SearchDomains
}
if iface.SecLists != nil {
res["sec_lists"] = iface.SecLists
}
if iface.Vnic != "" {
res["vnic"] = iface.Vnic
// VNIC can only be set on an IP Network
res["shared_network"] = false
}
if iface.VnicSets != nil {
res["vnic_sets"] = iface.VnicSets
}
result = append(result, res)
}
return result, nil
}
// Flattens the returned slice of storage attachments to a map
func flattenInstanceStorageAttachments(attachments []compute.StorageAttachment) []map[string]interface{} {
result := make([]map[string]interface{}, 0)
if attachments == nil || len(attachments) == 0 {
return nil
}
for _, attachment := range attachments {
res := make(map[string]interface{})
res["index"] = attachment.Index
res["volume"] = attachment.StorageVolumeName
res["name"] = attachment.Name
result = append(result, res)
}
return result
}