-
Notifications
You must be signed in to change notification settings - Fork 523
/
compute_instance.go
93 lines (77 loc) 路 2.63 KB
/
compute_instance.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
package google
import (
"github.com/tidwall/gjson"
"github.com/infracost/infracost/internal/resources/google"
"github.com/infracost/infracost/internal/schema"
)
func getComputeInstanceRegistryItem() *schema.RegistryItem {
return &schema.RegistryItem{
Name: "google_compute_instance",
CoreRFunc: newComputeInstance,
ReferenceAttributes: []string{
"network_interface.0.access_config.0.nat_ip", // google_compute_address
},
Notes: []string{
"Sustained use discounts are applied to monthly costs, but not to hourly costs.",
"Costs associated with non-standard Linux images, such as Windows and RHEL are not supported.",
"Custom machine types are not supported.",
"Sole-tenant VMs are not supported.",
},
}
}
func newComputeInstance(d *schema.ResourceData) schema.CoreResource {
machineType := d.Get("machine_type").String()
region := d.Get("region").String()
size := int64(1)
zone := d.Get("zone").String()
if zone != "" {
region = zoneToRegion(zone)
}
purchaseOption := getComputePurchaseOption(d.RawValues)
initializeParams := d.Get("boot_disk.0.initialize_params.0")
bootDiskSize := float64(defaultVolumeSize)
var bootDiskType string
hasBootDisk := initializeParams.Exists()
if hasBootDisk {
if initializeParams.Get("size").Exists() {
bootDiskSize = initializeParams.Get("size").Float()
}
bootDiskType = initializeParams.Get("type").String()
}
scratchDisks := len(d.Get("scratch_disk").Array())
guestAccelerators := collectComputeGuestAccelerators(d.Get("guest_accelerator"))
r := &google.ComputeInstance{
Address: d.Address,
Region: region,
MachineType: machineType,
PurchaseOption: purchaseOption,
Size: size,
HasBootDisk: hasBootDisk,
BootDiskSize: bootDiskSize,
BootDiskType: bootDiskType,
ScratchDisks: scratchDisks,
GuestAccelerators: guestAccelerators,
}
return r
}
// getComputePurchaseOption determines the purchase option for Compute
// resources.
func getComputePurchaseOption(d gjson.Result) string {
purchaseOption := "on_demand"
if d.Get("scheduling.0.preemptible").Bool() {
purchaseOption = "preemptible"
}
return purchaseOption
}
// collectComputeGuestAccelerators collects Guest Accelerator data for Compute
// resources.
func collectComputeGuestAccelerators(d gjson.Result) []*google.ComputeGuestAccelerator {
guestAccelerators := []*google.ComputeGuestAccelerator{}
for _, guestAccel := range d.Array() {
guestAccelerators = append(guestAccelerators, &google.ComputeGuestAccelerator{
Type: guestAccel.Get("type").String(),
Count: guestAccel.Get("count").Int(),
})
}
return guestAccelerators
}