-
Notifications
You must be signed in to change notification settings - Fork 9
/
storage.go
450 lines (368 loc) · 14.1 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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package service
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"time"
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
)
type Storage interface {
GetStorages(r *request.GetStoragesRequest) (*upcloud.Storages, error)
GetStorageDetails(r *request.GetStorageDetailsRequest) (*upcloud.StorageDetails, error)
CreateStorage(r *request.CreateStorageRequest) (*upcloud.StorageDetails, error)
ModifyStorage(r *request.ModifyStorageRequest) (*upcloud.StorageDetails, error)
AttachStorage(r *request.AttachStorageRequest) (*upcloud.ServerDetails, error)
DetachStorage(r *request.DetachStorageRequest) (*upcloud.ServerDetails, error)
CloneStorage(r *request.CloneStorageRequest) (*upcloud.StorageDetails, error)
TemplatizeStorage(r *request.TemplatizeStorageRequest) (*upcloud.StorageDetails, error)
WaitForStorageState(r *request.WaitForStorageStateRequest) (*upcloud.StorageDetails, error)
LoadCDROM(r *request.LoadCDROMRequest) (*upcloud.ServerDetails, error)
EjectCDROM(r *request.EjectCDROMRequest) (*upcloud.ServerDetails, error)
CreateBackup(r *request.CreateBackupRequest) (*upcloud.StorageDetails, error)
RestoreBackup(r *request.RestoreBackupRequest) error
CreateStorageImport(r *request.CreateStorageImportRequest) (*upcloud.StorageImportDetails, error)
GetStorageImportDetails(r *request.GetStorageImportDetailsRequest) (*upcloud.StorageImportDetails, error)
WaitForStorageImportCompletion(r *request.WaitForStorageImportCompletionRequest) (*upcloud.StorageImportDetails, error)
DeleteStorage(*request.DeleteStorageRequest) error
ResizeStorageFilesystem(r *request.ResizeStorageFilesystemRequest) (*upcloud.ResizeStorageFilesystemBackup, error)
}
var _ Storage = (*Service)(nil)
// GetStorages returns all available storages
func (s *Service) GetStorages(r *request.GetStoragesRequest) (*upcloud.Storages, error) {
storages := upcloud.Storages{}
response, err := s.basicGetRequest(r.RequestURL())
if err != nil {
return nil, err
}
err = json.Unmarshal(response, &storages)
if err != nil {
return nil, err
}
return &storages, nil
}
// GetStorageDetails returns extended details about the specified piece of storage
func (s *Service) GetStorageDetails(r *request.GetStorageDetailsRequest) (*upcloud.StorageDetails, error) {
storageDetails := upcloud.StorageDetails{}
response, err := s.basicGetRequest(r.RequestURL())
if err != nil {
return nil, err
}
err = json.Unmarshal(response, &storageDetails)
if err != nil {
return nil, err
}
return &storageDetails, nil
}
// CreateStorage creates the specified storage
func (s *Service) CreateStorage(r *request.CreateStorageRequest) (*upcloud.StorageDetails, error) {
storageDetails := upcloud.StorageDetails{}
requestBody, _ := json.Marshal(r)
response, err := s.client.PerformJSONPostRequest(s.client.CreateRequestURL(r.RequestURL()), requestBody)
if err != nil {
return nil, parseJSONServiceError(err)
}
err = json.Unmarshal(response, &storageDetails)
if err != nil {
return nil, err
}
return &storageDetails, nil
}
// ModifyStorage modifies the specified storage device
func (s *Service) ModifyStorage(r *request.ModifyStorageRequest) (*upcloud.StorageDetails, error) {
storageDetails := upcloud.StorageDetails{}
requestBody, _ := json.Marshal(r)
response, err := s.client.PerformJSONPutRequest(s.client.CreateRequestURL(r.RequestURL()), requestBody)
if err != nil {
return nil, parseJSONServiceError(err)
}
err = json.Unmarshal(response, &storageDetails)
if err != nil {
return nil, err
}
return &storageDetails, nil
}
// AttachStorage attaches the specified storage to the specified server
func (s *Service) AttachStorage(r *request.AttachStorageRequest) (*upcloud.ServerDetails, error) {
serverDetails := upcloud.ServerDetails{}
requestBody, _ := json.Marshal(r)
response, err := s.client.PerformJSONPostRequest(s.client.CreateRequestURL(r.RequestURL()), requestBody)
if err != nil {
return nil, parseJSONServiceError(err)
}
err = json.Unmarshal(response, &serverDetails)
if err != nil {
return nil, err
}
return &serverDetails, nil
}
// DetachStorage detaches the specified storage from the specified server
func (s *Service) DetachStorage(r *request.DetachStorageRequest) (*upcloud.ServerDetails, error) {
serverDetails := upcloud.ServerDetails{}
requestBody, _ := json.Marshal(r)
response, err := s.client.PerformJSONPostRequest(s.client.CreateRequestURL(r.RequestURL()), requestBody)
if err != nil {
return nil, parseJSONServiceError(err)
}
err = json.Unmarshal(response, &serverDetails)
if err != nil {
return nil, err
}
return &serverDetails, nil
}
// DeleteStorage deletes the specified storage device
func (s *Service) DeleteStorage(r *request.DeleteStorageRequest) error {
err := s.client.PerformJSONDeleteRequest(s.client.CreateRequestURL(r.RequestURL()))
if err != nil {
return parseJSONServiceError(err)
}
return nil
}
// CloneStorage detaches the specified storage from the specified server
func (s *Service) CloneStorage(r *request.CloneStorageRequest) (*upcloud.StorageDetails, error) {
storageDetails := upcloud.StorageDetails{}
requestBody, _ := json.Marshal(r)
response, err := s.client.PerformJSONPostRequest(s.client.CreateRequestURL(r.RequestURL()), requestBody)
if err != nil {
return nil, parseJSONServiceError(err)
}
err = json.Unmarshal(response, &storageDetails)
if err != nil {
return nil, err
}
return &storageDetails, nil
}
// TemplatizeStorage detaches the specified storage from the specified server
func (s *Service) TemplatizeStorage(r *request.TemplatizeStorageRequest) (*upcloud.StorageDetails, error) {
storageDetails := upcloud.StorageDetails{}
requestBody, _ := json.Marshal(r)
response, err := s.client.PerformJSONPostRequest(s.client.CreateRequestURL(r.RequestURL()), requestBody)
if err != nil {
return nil, parseJSONServiceError(err)
}
err = json.Unmarshal(response, &storageDetails)
if err != nil {
return nil, err
}
return &storageDetails, nil
}
// WaitForStorageState blocks execution until the specified storage device has entered the specified state. If the
// state changes favorably, the new storage details is returned. The method will give up after the specified timeout
func (s *Service) WaitForStorageState(r *request.WaitForStorageStateRequest) (*upcloud.StorageDetails, error) {
attempts := 0
sleepDuration := time.Second * 5
for {
attempts++
storageDetails, err := s.GetStorageDetails(&request.GetStorageDetailsRequest{
UUID: r.UUID,
})
if err != nil {
return nil, err
}
if storageDetails.State == r.DesiredState {
return storageDetails, nil
}
time.Sleep(sleepDuration)
if time.Duration(attempts)*sleepDuration >= r.Timeout {
return nil, fmt.Errorf("timeout reached while waiting for storage to enter state \"%s\"", r.DesiredState)
}
}
}
// LoadCDROM loads a storage as a CD-ROM in the CD-ROM device of a server
func (s *Service) LoadCDROM(r *request.LoadCDROMRequest) (*upcloud.ServerDetails, error) {
serverDetails := upcloud.ServerDetails{}
requestBody, _ := json.Marshal(r)
response, err := s.client.PerformJSONPostRequest(s.client.CreateRequestURL(r.RequestURL()), requestBody)
if err != nil {
return nil, parseJSONServiceError(err)
}
err = json.Unmarshal(response, &serverDetails)
if err != nil {
return nil, err
}
return &serverDetails, nil
}
// EjectCDROM ejects the storage from the CD-ROM device of a server
func (s *Service) EjectCDROM(r *request.EjectCDROMRequest) (*upcloud.ServerDetails, error) {
serverDetails := upcloud.ServerDetails{}
response, err := s.client.PerformJSONPostRequest(s.client.CreateRequestURL(r.RequestURL()), nil)
if err != nil {
return nil, parseJSONServiceError(err)
}
err = json.Unmarshal(response, &serverDetails)
if err != nil {
return nil, err
}
return &serverDetails, nil
}
// CreateBackup creates a backup of the specified storage
func (s *Service) CreateBackup(r *request.CreateBackupRequest) (*upcloud.StorageDetails, error) {
storageDetails := upcloud.StorageDetails{}
requestBody, _ := json.Marshal(r)
response, err := s.client.PerformJSONPostRequest(s.client.CreateRequestURL(r.RequestURL()), requestBody)
if err != nil {
return nil, parseJSONServiceError(err)
}
err = json.Unmarshal(response, &storageDetails)
if err != nil {
return nil, err
}
return &storageDetails, nil
}
// RestoreBackup creates a backup of the specified storage
func (s *Service) RestoreBackup(r *request.RestoreBackupRequest) error {
_, err := s.client.PerformJSONPostRequest(s.client.CreateRequestURL(r.RequestURL()), nil)
if err != nil {
return parseJSONServiceError(err)
}
return nil
}
// CreateStorageImport begins the process of importing an image onto a storage device. A `upcloud.StorageImportSourceHTTPImport` source
// will import from an HTTP source. `upcloud.StorageImportSourceDirectUpload` will directly upload the file specified in `SourceLocation`.
func (s *Service) CreateStorageImport(r *request.CreateStorageImportRequest) (*upcloud.StorageImportDetails, error) {
if r.Source == request.StorageImportSourceDirectUpload {
switch r.SourceLocation.(type) {
case string, io.Reader:
return s.directStorageImport(r)
case nil:
return nil, errors.New("SourceLocation must be specified")
default:
return nil, fmt.Errorf("unsupported storage source location type %T", r.SourceLocation)
}
}
if _, isString := r.SourceLocation.(string); !isString {
return nil, fmt.Errorf("unsupported storage source location type %T", r.Source)
}
return s.doCreateStorageImport(r)
}
// doCreateStorageImport will POST the CreateStorageImport request and handle the error and normal response.
func (s *Service) doCreateStorageImport(r *request.CreateStorageImportRequest) (*upcloud.StorageImportDetails, error) {
storageImport := upcloud.StorageImportDetails{}
requestBody, _ := json.Marshal(r)
response, err := s.client.PerformJSONPostRequest(s.client.CreateRequestURL(r.RequestURL()), requestBody)
if err != nil {
return nil, parseJSONServiceError(err)
}
err = json.Unmarshal(response, &storageImport)
if err != nil {
return nil, err
}
return &storageImport, nil
}
// directStorageImport handles the direct upload logic including getting the upload URL and PUT the file data
// to that endpoint.
func (s *Service) directStorageImport(r *request.CreateStorageImportRequest) (*upcloud.StorageImportDetails, error) {
var bodyReader io.Reader
switch v := r.SourceLocation.(type) {
case string:
if v == "" {
return nil, errors.New("SourceLocation must be specified")
}
f, err := os.Open(v)
if err != nil {
return nil, fmt.Errorf("unable to open SourceLocation: %w", err)
}
bodyReader = f
defer f.Close()
case io.Reader:
bodyReader = v
default:
return nil, fmt.Errorf("unsupported source location type %T", r.SourceLocation)
}
r.SourceLocation = ""
storageImport, err := s.doCreateStorageImport(r)
if err != nil {
return nil, err
}
if storageImport.DirectUploadURL == "" {
return nil, errors.New("no DirectUploadURL found in response")
}
req, err := http.NewRequest(http.MethodPut, storageImport.DirectUploadURL, bodyReader)
if err != nil {
return nil, err
}
s.client.AddRequestHeaders(req)
req.Header.Set("Content-Type", r.ContentType)
if _, err := s.client.PerformRequest(req); err != nil {
return nil, err
}
storageImport, err = s.GetStorageImportDetails(&request.GetStorageImportDetailsRequest{
UUID: r.StorageUUID,
})
if err != nil {
return nil, err
}
return storageImport, nil
}
// GetStorageImportDetails gets updated details about the specified storage import.
func (s *Service) GetStorageImportDetails(r *request.GetStorageImportDetailsRequest) (*upcloud.StorageImportDetails, error) {
storageDetails := upcloud.StorageImportDetails{}
response, err := s.basicGetRequest(r.RequestURL())
if err != nil {
return nil, err
}
err = json.Unmarshal(response, &storageDetails)
if err != nil {
return nil, err
}
return &storageDetails, nil
}
// WaitForStorageImportCompletion waits for the importing storage to complete.
func (s *Service) WaitForStorageImportCompletion(r *request.WaitForStorageImportCompletionRequest) (*upcloud.StorageImportDetails, error) {
attempts := 0
sleepDuration := time.Second * 5
for {
attempts++
storageImportDetails, err := s.GetStorageImportDetails(&request.GetStorageImportDetailsRequest{
UUID: r.StorageUUID,
})
if err != nil {
return nil, err
}
switch storageImportDetails.State {
case upcloud.StorageImportStateCompleted:
return storageImportDetails, nil
case upcloud.StorageImportStateCancelled,
upcloud.StorageImportStateCancelling,
upcloud.StorageImportStateFailed:
if storageImportDetails.ErrorCode != "" || storageImportDetails.ErrorMessage != "" {
return storageImportDetails, &upcloud.Error{
ErrorCode: storageImportDetails.ErrorCode,
ErrorMessage: storageImportDetails.ErrorMessage,
}
}
return storageImportDetails, &upcloud.Error{
ErrorCode: storageImportDetails.State,
ErrorMessage: "Storage Import Failed",
}
default:
if time.Duration(attempts)*sleepDuration >= r.Timeout {
return nil, errors.New("timeout reached while waiting for import to complete")
}
time.Sleep(sleepDuration)
}
}
}
// ResizeStorageFilesystem resizes the last partition of a storage and the ext3/ext4/XFS/NTFS filesystem
// on that partition if the partition does not extend to the end of the storage yet.
//
// Before the resize is attempted, a backup is taken from the storage. If the resize
// succeeds, backup details are returned. It is advisable to keep the backup until
// you have ensured that everything works after the resize.
//
// If the resize fails, backup is used to restore the storage to the state where it
// was before the resize. After that the backup is deleted automatically.
func (s *Service) ResizeStorageFilesystem(r *request.ResizeStorageFilesystemRequest) (*upcloud.ResizeStorageFilesystemBackup, error) {
response, err := s.client.PerformJSONPostRequest(s.client.CreateRequestURL(r.RequestURL()), nil)
if err != nil {
return nil, parseJSONServiceError(err)
}
resizeBackup := upcloud.ResizeStorageFilesystemBackup{}
if err = json.Unmarshal(response, &resizeBackup); err != nil {
return &resizeBackup, err
}
return &resizeBackup, nil
}