-
Notifications
You must be signed in to change notification settings - Fork 16
/
volume.go
260 lines (237 loc) · 8.71 KB
/
volume.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
package profitbricks
import (
"context"
"github.com/ionos-cloud/sdk-go/v5"
"net/http"
)
// Volume object
type Volume struct {
ID string `json:"id,omitempty"`
PBType string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
Properties VolumeProperties `json:"properties,omitempty"`
Response string `json:"Response,omitempty"`
Headers *http.Header `json:"headers,omitempty"`
StatusCode int `json:"statuscode,omitempty"`
}
// VolumeProperties object
type VolumeProperties struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Size int `json:"size,omitempty"`
AvailabilityZone string `json:"availabilityZone,omitempty"`
Image string `json:"image,omitempty"`
ImageAlias string `json:"imageAlias,omitempty"`
ImagePassword string `json:"imagePassword,omitempty"`
SSHKeys []string `json:"sshKeys,omitempty"`
Bus string `json:"bus,omitempty"`
LicenceType string `json:"licenceType,omitempty"`
CPUHotPlug bool `json:"cpuHotPlug,omitempty"`
CPUHotUnplug bool `json:"cpuHotUnplug,omitempty"`
RAMHotPlug bool `json:"ramHotPlug,omitempty"`
RAMHotUnplug bool `json:"ramHotUnplug,omitempty"`
NicHotPlug bool `json:"nicHotPlug,omitempty"`
NicHotUnplug bool `json:"nicHotUnplug,omitempty"`
DiscVirtioHotPlug bool `json:"discVirtioHotPlug,omitempty"`
DiscVirtioHotUnplug bool `json:"discVirtioHotUnplug,omitempty"`
DiscScsiHotPlug bool `json:"discScsiHotPlug,omitempty"`
DiscScsiHotUnplug bool `json:"discScsiHotUnplug,omitempty"`
DeviceNumber int64 `json:"deviceNumber,omitempty"`
BackupunitId string `json:"backupunitId,omitempty"`
}
// Volumes object
type Volumes struct {
ID string `json:"id,omitempty"`
PBType string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Items []Volume `json:"items,omitempty"`
Response string `json:"Response,omitempty"`
Headers *http.Header `json:"headers,omitempty"`
StatusCode int `json:"statuscode,omitempty"`
}
// ListVolumes returns a Collection struct for volumes in the Datacenter
func (c *Client) ListVolumes(dcid string) (*Volumes, error) {
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.VolumeApi.DatacentersVolumesGet(ctx, dcid).Execute()
ret := Volumes{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
ret := &Volumes{}
return ret, c.GetOK(volumesPath(dcid), ret)
*/
}
// GetVolume gets a volume
func (c *Client) GetVolume(dcid string, volumeID string) (*Volume, error) {
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.VolumeApi.DatacentersVolumesFindById(ctx, dcid, volumeID).Execute()
ret := Volume{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
ret := &Volume{}
return ret, c.GetOK(volumePath(dcid, volumeID), ret)
*/
}
// UpdateVolume updates a volume
func (c *Client) UpdateVolume(dcid string, volid string, request VolumeProperties) (*Volume, error) {
input := ionoscloud.VolumeProperties{}
if errConvert := convertToCore(&request, &input); errConvert != nil {
return nil, errConvert
}
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.VolumeApi.DatacentersVolumesPatch(ctx, dcid, volid).Volume(input).Execute()
ret := Volume{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
ret := &Volume{}
return ret, c.PatchAcc(volumePath(dcid, volid), request, ret)
*/
}
// CreateVolume creates a volume
func (c *Client) CreateVolume(dcid string, request Volume) (*Volume, error) {
input := ionoscloud.Volume{}
if errConvert := convertToCore(&request, &input); errConvert != nil {
return nil, errConvert
}
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.VolumeApi.DatacentersVolumesPost(ctx, dcid).Volume(input).Execute()
ret := Volume{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
ret := &Volume{}
return ret, c.PostAcc(volumesPath(dcid), request, ret)
*/
}
// DeleteVolume deletes a volume
func (c *Client) DeleteVolume(dcid, volid string) (*http.Header, error) {
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
_, apiResponse, err := c.CoreSdk.VolumeApi.DatacentersVolumesDelete(ctx, dcid, volid).Execute()
if apiResponse != nil {
return &apiResponse.Header, err
} else {
return nil, err
}
/* return c.DeleteAcc(volumePath(dcid, volid)) */
}
// CreateSnapshot creates a volume snapshot
func (c *Client) CreateSnapshot(dcid string, volid string, name string, description string) (*Snapshot, error) {
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.VolumeApi.DatacentersVolumesCreateSnapshotPost(ctx, dcid, volid).Name(name).Description(description).Execute()
ret := Snapshot{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
ret := &Snapshot{}
req := c.Client.R().
SetFormData(map[string]string{"name": name, "description": description}).
SetResult(ret)
return ret, c.DoWithRequest(req, resty.MethodPost, createSnapshotPath(dcid, volid), http.StatusAccepted)
*/
}
// RestoreSnapshot restores a volume with provided snapshot
func (c *Client) RestoreSnapshot(dcid string, volid string, snapshotID string) (*http.Header, error) {
_, apiResponse, err := c.CoreSdk.VolumeApi.DatacentersVolumesRestoreSnapshotPost(
context.TODO(), dcid, volid).SnapshotId(snapshotID).Execute()
if apiResponse != nil {
return &apiResponse.Header, err
} else {
return nil, err
}
/*
ret := &Header{}
req := c.Client.R().
SetFormData(map[string]string{"snapshotId": snapshotID}).
SetResult(ret)
err := c.DoWithRequest(req, resty.MethodPost, restoreSnapshotPath(dcid, volid), http.StatusAccepted)
return ret.GetHeader(), err
*/
}
// CreateVolumeAndWait creates a volume and waits for the request to complete.
func (c *Client) CreateVolumeAndWait(ctx context.Context, dcid string, request Volume) (*Volume, error) {
volume, err := c.CreateVolume(dcid, request)
if err != nil {
return nil, err
}
if err := c.WaitTillProvisionedOrCanceled(ctx, volume.Headers.Get("location")); err != nil {
return volume, err
}
return c.GetVolume(dcid, volume.ID)
}
// CreateSnapshotAndWait creates a volume snapshot and waits for the request to
// complete.
func (c *Client) CreateSnapshotAndWait(ctx context.Context, dcId, volId, name, description string) (*Snapshot, error) {
snapshot, err := c.CreateSnapshot(dcId, volId, name, description)
if err != nil {
return nil, err
}
if err := c.WaitTillProvisionedOrCanceled(ctx, snapshot.Headers.Get("location")); err != nil {
return snapshot, err
}
return c.GetSnapshot(snapshot.ID)
}
// RestoreSnapshotAndWait restores a volume with the provided snapshot and
// waits for the request to complete.
func (c *Client) RestoreSnapshotAndWait(ctx context.Context, dcId, volId, snapshotId string) error {
ret, err := c.RestoreSnapshot(dcId, volId, snapshotId)
if err != nil {
return err
}
return c.WaitTillProvisionedOrCanceled(ctx, ret.Get("location"))
}
// IsSnapshotCreationRequested checks if an active create-snapshot request (QUEUED or RUNNING)
// exists for the given volume.
// Even though the snapshot lock prevents mulitple requests from being processed at the same time,
// this information is only visible by inspecting the request queue. This method can be used to
// verify that the snapshot lock is not held by any other client request.
func (c *Client) IsSnapshotCreationRequested(dcId, volId string) (bool, error) {
f := NewRequestListFilter().WithUrl(createSnapshotPath(dcId, volId)).WithMethod(http.MethodPost)
result, err := c.ListRequestsWithFilter(f.Clone().WithRequestStatus(RequestStatusQueued))
if err != nil {
return false, err
}
if len(result.Items) > 0 {
return true, nil
}
result, err = c.ListRequestsWithFilter(f.Clone().WithRequestStatus(RequestStatusRunning))
if err != nil {
return false, err
}
return len(result.Items) > 0, nil
}