-
Notifications
You must be signed in to change notification settings - Fork 8
/
storage.go
339 lines (274 loc) · 9.89 KB
/
storage.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
package request
import (
"encoding/json"
"fmt"
"time"
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
)
// Constants
const (
StorageImportSourceDirectUpload = "direct_upload"
StorageImportSourceHTTPImport = "http_import"
)
// GetStoragesRequest represents a request for retrieving all or some storages
type GetStoragesRequest struct {
// If specified, only storages with this access type will be retrieved
Access string
// If specified, only storages with this type will be retrieved
Type string
// If specified, only storages marked as favorite will be retrieved
Favorite bool
}
// RequestURL implements the Request interface
func (r *GetStoragesRequest) RequestURL() string {
if r.Access != "" {
return fmt.Sprintf("/storage/%s", r.Access)
}
if r.Type != "" {
return fmt.Sprintf("/storage/%s", r.Type)
}
if r.Favorite {
return "/storage/favorite"
}
return "/storage"
}
// GetStorageDetailsRequest represents a request for retrieving details about a piece of storage
type GetStorageDetailsRequest struct {
UUID string
}
// RequestURL implements the Request interface
func (r *GetStorageDetailsRequest) RequestURL() string {
return fmt.Sprintf("/storage/%s", r.UUID)
}
// CreateStorageRequest represents a request to create a storage device
type CreateStorageRequest struct {
Size int `json:"size,string"`
Tier string `json:"tier,omitempty"`
Title string `json:"title,omitempty"`
Zone string `json:"zone"`
BackupRule *upcloud.BackupRule `json:"backup_rule,omitempty"`
}
// RequestURL implements the Request interface
func (r *CreateStorageRequest) RequestURL() string {
return "/storage"
}
// MarshalJSON is a custom marshaller that deals with
// deeply embedded values.
func (r CreateStorageRequest) MarshalJSON() ([]byte, error) {
type localCreateStorageRequest CreateStorageRequest
v := struct {
CreateStorageRequest localCreateStorageRequest `json:"storage"`
}{}
v.CreateStorageRequest = localCreateStorageRequest(r)
return json.Marshal(&v)
}
// ModifyStorageRequest represents a request to modify a storage device
type ModifyStorageRequest struct {
UUID string `json:"-"`
Title string `json:"title,omitempty"`
Size int `json:"size,omitempty,string"`
BackupRule *upcloud.BackupRule `json:"backup_rule,omitempty"`
}
// MarshalJSON is a custom marshaller that deals with
// deeply embedded values.
func (r ModifyStorageRequest) MarshalJSON() ([]byte, error) {
type localModifyStorageRequest ModifyStorageRequest
v := struct {
ModifyStorageRequest localModifyStorageRequest `json:"storage"`
}{}
v.ModifyStorageRequest = localModifyStorageRequest(r)
return json.Marshal(&v)
}
// RequestURL implements the Request interface
func (r *ModifyStorageRequest) RequestURL() string {
return fmt.Sprintf("/storage/%s", r.UUID)
}
// AttachStorageRequest represents a request to attach a storage device to a server
type AttachStorageRequest struct {
ServerUUID string `json:"-"`
Type string `json:"type,omitempty"`
Address string `json:"address,omitempty"`
StorageUUID string `json:"storage,omitempty"`
BootDisk int `json:"boot_disk,omitempty,string"`
}
// RequestURL implements the Request interface
func (r *AttachStorageRequest) RequestURL() string {
return fmt.Sprintf("/server/%s/storage/attach", r.ServerUUID)
}
// MarshalJSON is a custom marshaller that deals with
// deeply embedded values.
func (r AttachStorageRequest) MarshalJSON() ([]byte, error) {
type localAttachStorageRequest AttachStorageRequest
v := struct {
AttachStorageRequest localAttachStorageRequest `json:"storage_device"`
}{}
v.AttachStorageRequest = localAttachStorageRequest(r)
return json.Marshal(&v)
}
// DetachStorageRequest represents a request to detach a storage device from a server
type DetachStorageRequest struct {
ServerUUID string `json:"-"`
Address string `json:"address"`
}
// RequestURL implements the Request interface
func (r *DetachStorageRequest) RequestURL() string {
return fmt.Sprintf("/server/%s/storage/detach", r.ServerUUID)
}
// MarshalJSON is a custom marshaller that deals with
// deeply embedded values.
func (r DetachStorageRequest) MarshalJSON() ([]byte, error) {
type localDetachStorageRequest DetachStorageRequest
v := struct {
DetachStorageRequest localDetachStorageRequest `json:"storage_device"`
}{}
v.DetachStorageRequest = localDetachStorageRequest(r)
return json.Marshal(&v)
}
// DeleteStorageRequest represents a request to delete a storage device
type DeleteStorageRequest struct {
UUID string
}
// RequestURL implements the Request interface
func (r *DeleteStorageRequest) RequestURL() string {
return fmt.Sprintf("/storage/%s", r.UUID)
}
// CloneStorageRequest represents a requests to clone a storage device
type CloneStorageRequest struct {
UUID string `json:"-"`
Zone string `json:"zone"`
Tier string `json:"tier,omitempty"`
Title string `json:"title"`
}
// RequestURL implements the Request interface
func (r *CloneStorageRequest) RequestURL() string {
return fmt.Sprintf("/storage/%s/clone", r.UUID)
}
// MarshalJSON is a custom marshaller that deals with
// deeply embedded values.
func (r CloneStorageRequest) MarshalJSON() ([]byte, error) {
type localCloneStorageRequest CloneStorageRequest
v := struct {
CloneStorageRequest localCloneStorageRequest `json:"storage"`
}{}
v.CloneStorageRequest = localCloneStorageRequest(r)
return json.Marshal(&v)
}
// TemplatizeStorageRequest represents a request to templatize a storage device
type TemplatizeStorageRequest struct {
UUID string `json:"-"`
Title string `json:"title"`
}
// RequestURL implements the Request interface
func (r *TemplatizeStorageRequest) RequestURL() string {
return fmt.Sprintf("/storage/%s/templatize", r.UUID)
}
// MarshalJSON is a custom marshaller that deals with
// deeply embedded values.
func (r TemplatizeStorageRequest) MarshalJSON() ([]byte, error) {
type localTemplatizeStorageRequest TemplatizeStorageRequest
v := struct {
TemplatizeStorageRequest localTemplatizeStorageRequest `json:"storage"`
}{}
v.TemplatizeStorageRequest = localTemplatizeStorageRequest(r)
return json.Marshal(&v)
}
// WaitForStorageStateRequest represents a request to wait for a storage to enter a specific state
type WaitForStorageStateRequest struct {
UUID string
DesiredState string
Timeout time.Duration
}
// LoadCDROMRequest represents a request to load a storage as a CD-ROM in the CD-ROM device of a server
type LoadCDROMRequest struct {
ServerUUID string `json:"-"`
StorageUUID string `json:"storage"`
}
// RequestURL implements the Request interface
func (r *LoadCDROMRequest) RequestURL() string {
return fmt.Sprintf("/server/%s/cdrom/load", r.ServerUUID)
}
// MarshalJSON is a custom marshaller that deals with
// deeply embedded values.
func (r LoadCDROMRequest) MarshalJSON() ([]byte, error) {
type localLoadCDROMRequest LoadCDROMRequest
v := struct {
LoadCDROMRequest localLoadCDROMRequest `json:"storage_device"`
}{}
v.LoadCDROMRequest = localLoadCDROMRequest(r)
return json.Marshal(&v)
}
// EjectCDROMRequest represents a request to load a storage as a CD-ROM in the CD-ROM device of a server
type EjectCDROMRequest struct {
ServerUUID string
}
// RequestURL implements the Request interface
func (r *EjectCDROMRequest) RequestURL() string {
return fmt.Sprintf("/server/%s/cdrom/eject", r.ServerUUID)
}
// CreateBackupRequest represents a request to create a backup of a storage device
type CreateBackupRequest struct {
UUID string `json:"-"`
Title string `json:"title"`
}
// RequestURL implements the Request interface
func (r *CreateBackupRequest) RequestURL() string {
return fmt.Sprintf("/storage/%s/backup", r.UUID)
}
// MarshalJSON is a custom marshaller that deals with
// deeply embedded values.
func (r CreateBackupRequest) MarshalJSON() ([]byte, error) {
type localCreateBackupRequest CreateBackupRequest
v := struct {
CreateBackupRequest localCreateBackupRequest `json:"storage"`
}{}
v.CreateBackupRequest = localCreateBackupRequest(r)
return json.Marshal(&v)
}
// RestoreBackupRequest represents a request to restore a storage from the specified backup
type RestoreBackupRequest struct {
UUID string
}
// RequestURL implements the Request interface
func (r *RestoreBackupRequest) RequestURL() string {
return fmt.Sprintf("/storage/%s/restore", r.UUID)
}
// ImportSourceLocation can be a string to a file or io.Reader in StorageImportSourceDirectUpload mode or a URL
// in StorageImportSourceHTTPImport mode
type ImportSourceLocation interface{}
// CreateStorageImportRequest represent a request to import storage.
type CreateStorageImportRequest struct {
StorageUUID string `json:"-"`
// ContentType can be given when using the StorageImportSourceDirectUpload mode
ContentType string `json:"-"`
Source string `json:"source"`
SourceLocation ImportSourceLocation `json:"source_location,omitempty"`
}
// MarshalJSON is a custom marshaller that deals with
// deeply embedded values.
func (r CreateStorageImportRequest) MarshalJSON() ([]byte, error) {
type localStorageImportRequest CreateStorageImportRequest
v := struct {
StorageImportRequest localStorageImportRequest `json:"storage_import"`
}{}
v.StorageImportRequest = localStorageImportRequest(r)
return json.Marshal(&v)
}
// RequestURL implements the Request interface
func (r *CreateStorageImportRequest) RequestURL() string {
return fmt.Sprintf("/storage/%s/import", r.StorageUUID)
}
// GetStorageImportDetailsRequest represents a request to get details
// about an import
type GetStorageImportDetailsRequest struct {
UUID string
}
// RequestURL implements the Request interface
func (r *GetStorageImportDetailsRequest) RequestURL() string {
return fmt.Sprintf("/storage/%s/import", r.UUID)
}
// WaitForStorageImportCompletionRequest represents a request to wait
// for storage import to complete.
type WaitForStorageImportCompletionRequest struct {
StorageUUID string
Timeout time.Duration
}