forked from bizflycloud/gobizfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud_server_volume.go
416 lines (358 loc) · 11.9 KB
/
cloud_server_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
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
// This file is part of gobizfly
package gobizfly
import (
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
const (
volumeBasePath = "/volumes"
volumeTypesBasePath = "/volume-types"
)
var _ VolumeService = (*volume)(nil)
// VolumeService is an interface to interact with Bizfly API Volume endpoint.
type VolumeService interface {
List(ctx context.Context, opts *VolumeListOptions) ([]*Volume, error)
Create(ctx context.Context, req *VolumeCreateRequest) (*Volume, error)
Get(ctx context.Context, id string) (*Volume, error)
Delete(ctx context.Context, id string) error
ExtendVolume(ctx context.Context, id string, newsize int) (*Task, error)
Attach(ctx context.Context, id string, serverID string) (*VolumeAttachDetachResponse, error)
Detach(ctx context.Context, id string, serverID string) (*VolumeAttachDetachResponse, error)
Restore(ctx context.Context, id string, snapshotID string) (*Task, error)
Patch(ctx context.Context, id string, req *VolumePatchRequest) (*Volume, error)
ListVolumeTypes(ctx context.Context, opts *ListVolumeTypesOptions) ([]*VolumeType, error)
}
// VolumeListOptions represents options to list volumes.
// Name is the filter to list volumes by name.
// Size is the filter to list volumes by size.
// Status is the filter to list volumes by status.
// AvailabilityZone is the filter to list volumes by availability zone.
// Category is the filter to list volumes by category.
// BillingPlan is the filter to list volumes by billing plan.
// Bootable is the filter to list volumes by bootable.
type VolumeListOptions struct {
Name string `json:"name"`
Size int `json:"size"`
Status string `json:"status"`
AvailabilityZone string `json:"availability_zone"`
Category string `json:"category"`
BillingPlan string `json:"billing_plan"`
Bootable *bool `json:"bootable"`
}
// VolumeCreateRequest represents create new volume request payload.
type VolumeCreateRequest struct {
Name string `json:"name"`
Description string `json:"description"`
Size int `json:"size"`
VolumeType string `json:"volume_type"`
VolumeCategory string `json:"category"`
AvailabilityZone string `json:"availability_zone"`
SnapshotID string `json:"snapshot_id,omitempty"`
ServerID string `json:"instance_uuid,omitempty"`
BillingPlan string `json:"billing_plan,omitempty"`
}
type VolumePatchRequest struct {
Name string `json:"name"`
Description string `json:"description"`
}
// VolumeAttachment contains volume attachment information.
type VolumeAttachment struct {
Server Server `json:"server"`
ServerID string `json:"server_id"`
AttachmentID string `json:"attachment_id"`
VolumeID string `json:"volume_id"`
Device string `json:"device"`
ID string `json:"id"`
}
type VolumeImageMetadata struct {
ContainerFormat string `json:"container_format"`
DiskFormat string `json:"disk_format"`
ImageID string `json:"image_id"`
ImageName string `json:"image_name"`
ImageType string `json:"image_type"`
MinDisk string `json:"min_disk"`
MinRam string `json:"min_ram"`
Size string `json:"size"`
}
// Volume contains volume information.
type Volume struct {
ID string `json:"id"`
Size int `json:"size"`
AttachedType string `json:"attached_type"`
Name string `json:"name"`
Type string `json:"type"`
VolumeType string `json:"volume_type"`
Description string `json:"description"`
SnapshotID string `json:"snapshot_id"`
Bootable bool `json:"bootable"`
AvailabilityZone string `json:"availability_zone"`
Status string `json:"status"`
UserID string `json:"user_id"`
ProjectID string `json:"os-vol-tenant-attr:tenant_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Metadata map[string]string `json:"metadata"`
Attachments []VolumeAttachment `json:"attachments"`
Category string `json:"category"`
BillingPlan string `json:"billing_plan"`
Encrypted bool `json:"encrypted"`
ImageMetadata VolumeImageMetadata `json:"volume_image_metadata"`
}
// VolumeType contains volume type information.
type VolumeType struct {
Name string `json:"name"`
Category string `json:"category"`
Type string `json:"type"`
AvailabilityZones []string `json:"availability_zones"`
}
// ListVolumeTypesOptions contains options for listing volume types.
type ListVolumeTypesOptions struct {
Category string `json:"category,omitempty"`
AvailabilityZone string `json:"availability_zone,omitempty"`
}
type volume struct {
client *Client
}
// List lists all volumes of users.
func (v *volume) List(ctx context.Context, opts *VolumeListOptions) ([]*Volume, error) {
req, err := v.client.NewRequest(ctx, http.MethodGet, serverServiceName, volumeBasePath, nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
if opts != nil {
if opts.Name != "" {
q.Add("name", opts.Name)
}
if opts.Size != 0 {
q.Add("size", strconv.Itoa(opts.Size))
}
if opts.Status != "" {
q.Add("status", opts.Status)
}
if opts.AvailabilityZone != "" {
q.Add("availability_zone", opts.AvailabilityZone)
}
if opts.Category != "" {
q.Add("category", opts.Category)
}
if opts.BillingPlan != "" {
q.Add("billing_plan", opts.BillingPlan)
}
if opts.Bootable != nil {
q.Add("bootable", strconv.FormatBool(*opts.Bootable))
}
}
req.URL.RawQuery = q.Encode()
resp, err := v.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var volumes []*Volume
if err := json.NewDecoder(resp.Body).Decode(&volumes); err != nil {
return nil, err
}
return volumes, nil
}
// Create creates a new volume.
func (v *volume) Create(ctx context.Context, vcr *VolumeCreateRequest) (*Volume, error) {
req, err := v.client.NewRequest(ctx, http.MethodPost, serverServiceName, volumeBasePath, &vcr)
if err != nil {
return nil, err
}
resp, err := v.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var volumeRespData *Volume
if err := json.NewDecoder(resp.Body).Decode(&volumeRespData); err != nil {
return nil, err
}
return volumeRespData, nil
}
// Get gets information of a volume.
func (v *volume) Get(ctx context.Context, id string) (*Volume, error) {
req, err := v.client.NewRequest(ctx, http.MethodGet, serverServiceName, volumeBasePath+"/"+id, nil)
if err != nil {
return nil, err
}
resp, err := v.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var volume *Volume
if err := json.NewDecoder(resp.Body).Decode(&volume); err != nil {
return nil, err
}
return volume, nil
}
// Delete deletes a volume.
func (v *volume) Delete(ctx context.Context, id string) error {
req, err := v.client.NewRequest(ctx, http.MethodDelete, serverServiceName, volumeBasePath+"/"+id, nil)
if err != nil {
return err
}
resp, err := v.client.Do(ctx, req)
if err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
return resp.Body.Close()
}
// VolumeAction represents volume action request payload.
type VolumeAction struct {
Type string `json:"type"`
NewSize int `json:"new_size,omitempty"`
ServerID string `json:"instance_uuid,omitempty"`
SnapshotID string `json:"snapshot_id,omitempty"`
}
// Task represents task response when perform an action.
type Task struct {
TaskID string `json:"task_id"`
}
// VolumeAttachDetachResponse contains information when detach or attach a volume from/to a server.
type VolumeAttachDetachResponse struct {
Message string `json:"message"`
VolumeDetail Volume `json:"volume_detail"`
}
func (v *volume) itemActionPath(id string) string {
return strings.Join([]string{volumeBasePath, id, "action"}, "/")
}
func (v *volume) itemPath(id string) string {
return strings.Join([]string{volumeBasePath, id}, "/")
}
// ExtendVolume extends capacity of a volume.
func (v *volume) ExtendVolume(ctx context.Context, id string, newsize int) (*Task, error) {
var payload = &VolumeAction{
Type: "extend",
NewSize: newsize}
req, err := v.client.NewRequest(ctx, http.MethodPost, serverServiceName, v.itemActionPath(id), payload)
if err != nil {
return nil, err
}
resp, err := v.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
t := &Task{}
if err := json.NewDecoder(resp.Body).Decode(t); err != nil {
return nil, err
}
return t, nil
}
// Attach attaches a volume to a server.
func (v *volume) Attach(ctx context.Context, id string, serverID string) (*VolumeAttachDetachResponse, error) {
var payload = &VolumeAction{
Type: "attach",
ServerID: serverID}
req, err := v.client.NewRequest(ctx, http.MethodPost, serverServiceName, v.itemActionPath(id), payload)
if err != nil {
return nil, err
}
resp, err := v.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
volumeAttachResponse := &VolumeAttachDetachResponse{}
if err := json.NewDecoder(resp.Body).Decode(volumeAttachResponse); err != nil {
return nil, err
}
return volumeAttachResponse, nil
}
// Detach detaches a volume from a server.
func (v *volume) Detach(ctx context.Context, id string, serverID string) (*VolumeAttachDetachResponse, error) {
var payload = &VolumeAction{
Type: "detach",
ServerID: serverID}
req, err := v.client.NewRequest(ctx, http.MethodPost, serverServiceName, v.itemActionPath(id), payload)
if err != nil {
return nil, err
}
resp, err := v.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
r := &VolumeAttachDetachResponse{}
if err := json.NewDecoder(resp.Body).Decode(r); err != nil {
return nil, err
}
return r, nil
}
// Restore restores a volume from a snapshot.
func (v *volume) Restore(ctx context.Context, id string, snapshotID string) (*Task, error) {
var payload = &VolumeAction{
Type: "restore_volume",
SnapshotID: snapshotID}
req, err := v.client.NewRequest(ctx, http.MethodPost, serverServiceName, v.itemActionPath(id), payload)
if err != nil {
return nil, err
}
resp, err := v.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
t := &Task{}
if err := json.NewDecoder(resp.Body).Decode(t); err != nil {
return nil, err
}
return t, nil
}
// Patch partially updates a volume.
func (v *volume) Patch(ctx context.Context, id string, bpr *VolumePatchRequest) (*Volume, error) {
req, err := v.client.NewRequest(ctx, http.MethodPatch, serverServiceName, v.itemPath(id), bpr)
if err != nil {
return nil, err
}
resp, err := v.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var volume *Volume
if err := json.NewDecoder(resp.Body).Decode(&volume); err != nil {
return nil, err
}
return volume, nil
}
// ListVolumeTypes lists volume types.
func (v *volume) ListVolumeTypes(ctx context.Context, opts *ListVolumeTypesOptions) ([]*VolumeType, error) {
req, err := v.client.NewRequest(ctx, http.MethodGet, serverServiceName, volumeTypesBasePath, nil)
if err != nil {
return nil, err
}
// Set query parameters from options.
query := req.URL.Query()
if opts != nil {
if opts.Category != "" {
query.Set("category", opts.Category)
}
if opts.AvailabilityZone != "" {
query.Set("availability_zone", opts.AvailabilityZone)
}
}
req.URL.RawQuery = query.Encode()
resp, err := v.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var volumeTypesResp struct {
VolumeTypes []*VolumeType `json:"volume_types"`
}
if err := json.NewDecoder(resp.Body).Decode(&volumeTypesResp); err != nil {
return nil, err
}
return volumeTypesResp.VolumeTypes, nil
}