-
Notifications
You must be signed in to change notification settings - Fork 523
/
compute_disk.go
127 lines (96 loc) 路 2.67 KB
/
compute_disk.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
package google
import (
"strings"
"github.com/infracost/infracost/internal/resources/google"
"github.com/infracost/infracost/internal/schema"
)
func getComputeDiskRegistryItem() *schema.RegistryItem {
return &schema.RegistryItem{
Name: "google_compute_disk",
CoreRFunc: newComputeDisk,
ReferenceAttributes: []string{"image", "snapshot"},
}
}
func newComputeDisk(d *schema.ResourceData) schema.CoreResource {
region := d.Get("region").String()
zone := d.Get("zone").String()
if zone != "" {
region = zoneToRegion(zone)
}
diskType := d.Get("type").String()
size := computeDiskSize(d)
iops := computeIOPS(d, diskType, size)
r := &google.ComputeDisk{
Address: d.Address,
Region: region,
Type: diskType,
Size: size,
IOPS: iops,
}
return r
}
func computeDiskSize(d *schema.ResourceData) float64 {
if d.Get("size").Exists() {
return d.Get("size").Float()
}
if len(d.References("image")) > 0 {
return computeImageDiskSize(d.References("image")[0])
}
if len(d.References("snapshot")) > 0 {
return computeSnapshotDiskSize(d.References("snapshot")[0])
}
return defaultDiskSize(d.Get("type").String())
}
func defaultDiskSize(diskType string) float64 {
diskType = strings.ToLower(diskType)
if diskType == "pd-balanced" || diskType == "pd-ssd" {
return 100
}
if diskType == "pd-extreme" || diskType == "hyperdisk-extreme" {
return 1000
}
// if diskType is not specificed, default value is pd-standard
return 500
}
func computeImageDiskSize(d *schema.ResourceData) float64 {
if d.Get("disk_size_gb").Exists() {
return d.Get("disk_size_gb").Float()
}
if len(d.References("source_disk")) > 0 {
return computeDiskSize(d.References("source_disk")[0])
}
if len(d.References("source_image")) > 0 {
return computeImageDiskSize(d.References("source_image")[0])
}
if len(d.References("source_snapshot")) > 0 {
return computeSnapshotDiskSize(d.References("source_snapshot")[0])
}
return 0
}
func computeSnapshotDiskSize(d *schema.ResourceData) float64 {
if len(d.References("source_disk")) > 0 {
return computeDiskSize(d.References("source_disk")[0])
}
return 0
}
func computeIOPS(d *schema.ResourceData, diskType string, diskSize float64) int64 {
if diskType == "pd-extreme" || diskType == "hyperdisk-extreme" {
if d.Get("provisioned_iops").Exists() {
return d.Get("provisioned_iops").Int()
}
return defaultIOPS(diskType, diskSize)
}
return 0
}
func defaultIOPS(diskType string, diskSize float64) int64 {
if diskType == "pd-extreme" {
return 100000
}
if diskType == "hyperdisk-extreme" {
if iops := diskSize * 1200; iops < 350000 {
return int64(iops)
}
return 350000
}
return 0
}