forked from kubernetes/kops
/
persistentdisk.go
129 lines (110 loc) · 2.94 KB
/
persistentdisk.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
package gcetasks
import (
"fmt"
"google.golang.org/api/compute/v1"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
"strings"
)
//go:generate fitask -type=PersistentDisk
type PersistentDisk struct {
Name *string
VolumeType *string
SizeGB *int64
Zone *string
}
var _ fi.CompareWithID = &PersistentDisk{}
func (e *PersistentDisk) CompareWithID() *string {
return e.Name
}
// Returns the last component of a URL, i.e. anything after the last slash
// If there is no slash, returns the whole string
func lastComponent(s string) string {
lastSlash := strings.LastIndex(s, "/")
if lastSlash != -1 {
s = s[lastSlash+1:]
}
return s
}
func (e *PersistentDisk) Find(c *fi.Context) (*PersistentDisk, error) {
cloud := c.Cloud.(*gce.GCECloud)
r, err := cloud.Compute.Disks.Get(cloud.Project, *e.Zone, *e.Name).Do()
if err != nil {
if gce.IsNotFound(err) {
return nil, nil
}
return nil, fmt.Errorf("error listing PersistentDisks: %v", err)
}
actual := &PersistentDisk{}
actual.Name = &r.Name
actual.VolumeType = fi.String(lastComponent(r.Type))
actual.Zone = fi.String(lastComponent(r.Zone))
actual.SizeGB = &r.SizeGb
return actual, nil
}
func (e *PersistentDisk) URL(project string) string {
u := &gce.GoogleCloudURL{
Project: project,
Zone: *e.Zone,
Type: "disks",
Name: *e.Name,
}
return u.BuildURL()
}
func (e *PersistentDisk) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (_ *PersistentDisk) CheckChanges(a, e, changes *PersistentDisk) error {
if a != nil {
if changes.SizeGB != nil {
return fi.CannotChangeField("SizeGB")
}
if changes.Zone != nil {
return fi.CannotChangeField("Zone")
}
if changes.VolumeType != nil {
return fi.CannotChangeField("VolumeType")
}
} else {
if e.Zone == nil {
return fi.RequiredField("Zone")
}
}
return nil
}
func (_ *PersistentDisk) RenderGCE(t *gce.GCEAPITarget, a, e, changes *PersistentDisk) error {
typeURL := fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/diskTypes/%s",
t.Cloud.Project,
*e.Zone,
*e.VolumeType)
disk := &compute.Disk{
Name: *e.Name,
SizeGb: *e.SizeGB,
Type: typeURL,
}
if a == nil {
_, err := t.Cloud.Compute.Disks.Insert(t.Cloud.Project, *e.Zone, disk).Do()
if err != nil {
return fmt.Errorf("error creating PersistentDisk: %v", err)
}
} else {
return fmt.Errorf("Cannot apply changes to PersistentDisk: %v", changes)
}
return nil
}
type terraformDisk struct {
Name *string `json:"name"`
VolumeType *string `json:"type"`
SizeGB *int64 `json:"size"`
Zone *string `json:"zone"`
}
func (_ *PersistentDisk) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *PersistentDisk) error {
tf := &terraformDisk{
Name: e.Name,
VolumeType: e.VolumeType,
SizeGB: e.SizeGB,
Zone: e.Zone,
}
return t.RenderResource("google_compute_disk", *e.Name, tf)
}