forked from canonical/lxd
/
lxd_storage_volumes.go
883 lines (710 loc) · 26.6 KB
/
lxd_storage_volumes.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
package lxd
import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
"github.com/lxc/lxd/shared/cancel"
"github.com/lxc/lxd/shared/ioprogress"
"github.com/lxc/lxd/shared/units"
)
// Storage volumes handling function
// GetStoragePoolVolumeNames returns the names of all volumes in a pool
func (r *ProtocolLXD) GetStoragePoolVolumeNames(pool string) ([]string, error) {
if !r.HasExtension("storage") {
return nil, fmt.Errorf("The server is missing the required \"storage\" API extension")
}
urls := []string{}
// Fetch the raw value
_, err := r.queryStruct("GET", fmt.Sprintf("/storage-pools/%s/volumes", url.PathEscape(pool)), nil, "", &urls)
if err != nil {
return nil, err
}
// Parse it
names := []string{}
for _, uri := range urls {
fields := strings.Split(uri, fmt.Sprintf("/storage-pools/%s/volumes/", url.PathEscape(pool)))
names = append(names, fields[len(fields)-1])
}
return names, nil
}
// GetStoragePoolVolumes returns a list of StorageVolume entries for the provided pool
func (r *ProtocolLXD) GetStoragePoolVolumes(pool string) ([]api.StorageVolume, error) {
if !r.HasExtension("storage") {
return nil, fmt.Errorf("The server is missing the required \"storage\" API extension")
}
volumes := []api.StorageVolume{}
// Fetch the raw value
_, err := r.queryStruct("GET", fmt.Sprintf("/storage-pools/%s/volumes?recursion=1", url.PathEscape(pool)), nil, "", &volumes)
if err != nil {
return nil, err
}
return volumes, nil
}
// GetStoragePoolVolume returns a StorageVolume entry for the provided pool and volume name
func (r *ProtocolLXD) GetStoragePoolVolume(pool string, volType string, name string) (*api.StorageVolume, string, error) {
if !r.HasExtension("storage") {
return nil, "", fmt.Errorf("The server is missing the required \"storage\" API extension")
}
volume := api.StorageVolume{}
// Fetch the raw value
path := fmt.Sprintf("/storage-pools/%s/volumes/%s/%s", url.PathEscape(pool), url.PathEscape(volType), url.PathEscape(name))
etag, err := r.queryStruct("GET", path, nil, "", &volume)
if err != nil {
return nil, "", err
}
return &volume, etag, nil
}
// GetStoragePoolVolumeState returns a StorageVolumeState entry for the provided pool and volume name
func (r *ProtocolLXD) GetStoragePoolVolumeState(pool string, volType string, name string) (*api.StorageVolumeState, error) {
if !r.HasExtension("storage_volume_state") {
return nil, fmt.Errorf("The server is missing the required \"storage_volume_state\" API extension")
}
// Fetch the raw value
state := api.StorageVolumeState{}
path := fmt.Sprintf("/storage-pools/%s/volumes/%s/%s/state", url.PathEscape(pool), url.PathEscape(volType), url.PathEscape(name))
_, err := r.queryStruct("GET", path, nil, "", &state)
if err != nil {
return nil, err
}
return &state, nil
}
// CreateStoragePoolVolume defines a new storage volume
func (r *ProtocolLXD) CreateStoragePoolVolume(pool string, volume api.StorageVolumesPost) error {
if !r.HasExtension("storage") {
return fmt.Errorf("The server is missing the required \"storage\" API extension")
}
// Send the request
path := fmt.Sprintf("/storage-pools/%s/volumes/%s", url.PathEscape(pool), url.PathEscape(volume.Type))
_, _, err := r.query("POST", path, volume, "")
if err != nil {
return err
}
return nil
}
// CreateStoragePoolVolumeSnapshot defines a new storage volume
func (r *ProtocolLXD) CreateStoragePoolVolumeSnapshot(pool string, volumeType string, volumeName string, snapshot api.StorageVolumeSnapshotsPost) (Operation, error) {
if !r.HasExtension("storage_api_volume_snapshots") {
return nil, fmt.Errorf("The server is missing the required \"storage_api_volume_snapshots\" API extension")
}
// Send the request
path := fmt.Sprintf("/storage-pools/%s/volumes/%s/%s/snapshots",
url.PathEscape(pool),
url.PathEscape(volumeType),
url.PathEscape(volumeName))
op, _, err := r.queryOperation("POST", path, snapshot, "")
if err != nil {
return nil, err
}
return op, nil
}
// GetStoragePoolVolumeSnapshotNames returns a list of snapshot names for the
// storage volume
func (r *ProtocolLXD) GetStoragePoolVolumeSnapshotNames(pool string, volumeType string, volumeName string) ([]string, error) {
if !r.HasExtension("storage_api_volume_snapshots") {
return nil, fmt.Errorf("The server is missing the required \"storage_api_volume_snapshots\" API extension")
}
urls := []string{}
// Send the request
path := fmt.Sprintf("/storage-pools/%s/volumes/%s/%s/snapshots",
url.PathEscape(pool),
url.PathEscape(volumeType),
url.PathEscape(volumeName))
_, err := r.queryStruct("GET", path, nil, "", &urls)
if err != nil {
return nil, err
}
// Parse it
names := []string{}
for _, uri := range urls {
fields := strings.Split(uri, path)
names = append(names, fields[len(fields)-1])
}
return names, nil
}
// GetStoragePoolVolumeSnapshots returns a list of snapshots for the storage
// volume
func (r *ProtocolLXD) GetStoragePoolVolumeSnapshots(pool string, volumeType string, volumeName string) ([]api.StorageVolumeSnapshot, error) {
if !r.HasExtension("storage_api_volume_snapshots") {
return nil, fmt.Errorf("The server is missing the required \"storage_api_volume_snapshots\" API extension")
}
snapshots := []api.StorageVolumeSnapshot{}
path := fmt.Sprintf("/storage-pools/%s/volumes/%s/%s/snapshots?recursion=1",
url.PathEscape(pool),
url.PathEscape(volumeType),
url.PathEscape(volumeName))
_, err := r.queryStruct("GET", path, nil, "", &snapshots)
if err != nil {
return nil, err
}
return snapshots, nil
}
// GetStoragePoolVolumeSnapshot returns a snapshots for the storage volume
func (r *ProtocolLXD) GetStoragePoolVolumeSnapshot(pool string, volumeType string, volumeName string, snapshotName string) (*api.StorageVolumeSnapshot, string, error) {
if !r.HasExtension("storage_api_volume_snapshots") {
return nil, "", fmt.Errorf("The server is missing the required \"storage_api_volume_snapshots\" API extension")
}
snapshot := api.StorageVolumeSnapshot{}
path := fmt.Sprintf("/storage-pools/%s/volumes/%s/%s/snapshots/%s",
url.PathEscape(pool),
url.PathEscape(volumeType),
url.PathEscape(volumeName),
url.PathEscape(snapshotName))
etag, err := r.queryStruct("GET", path, nil, "", &snapshot)
if err != nil {
return nil, "", err
}
return &snapshot, etag, nil
}
// RenameStoragePoolVolumeSnapshot renames a storage volume snapshot
func (r *ProtocolLXD) RenameStoragePoolVolumeSnapshot(pool string, volumeType string, volumeName string, snapshotName string, snapshot api.StorageVolumeSnapshotPost) (Operation, error) {
if !r.HasExtension("storage_api_volume_snapshots") {
return nil, fmt.Errorf("The server is missing the required \"storage_api_volume_snapshots\" API extension")
}
path := fmt.Sprintf("/storage-pools/%s/volumes/%s/%s/snapshots/%s", url.PathEscape(pool), url.PathEscape(volumeType), url.PathEscape(volumeName), url.PathEscape(snapshotName))
// Send the request
op, _, err := r.queryOperation("POST", path, snapshot, "")
if err != nil {
return nil, err
}
return op, nil
}
// DeleteStoragePoolVolumeSnapshot deletes a storage volume snapshot
func (r *ProtocolLXD) DeleteStoragePoolVolumeSnapshot(pool string, volumeType string, volumeName string, snapshotName string) (Operation, error) {
if !r.HasExtension("storage_api_volume_snapshots") {
return nil, fmt.Errorf("The server is missing the required \"storage_api_volume_snapshots\" API extension")
}
// Send the request
path := fmt.Sprintf(
"/storage-pools/%s/volumes/%s/%s/snapshots/%s",
url.PathEscape(pool), url.PathEscape(volumeType), url.PathEscape(volumeName), url.PathEscape(snapshotName))
op, _, err := r.queryOperation("DELETE", path, nil, "")
if err != nil {
return nil, err
}
return op, nil
}
// UpdateStoragePoolVolumeSnapshot updates the volume to match the provided StoragePoolVolume struct
func (r *ProtocolLXD) UpdateStoragePoolVolumeSnapshot(pool string, volumeType string, volumeName string, snapshotName string, volume api.StorageVolumeSnapshotPut, ETag string) error {
if !r.HasExtension("storage_api_volume_snapshots") {
return fmt.Errorf("The server is missing the required \"storage_api_volume_snapshots\" API extension")
}
// Send the request
path := fmt.Sprintf("/storage-pools/%s/volumes/%s/%s/snapshots/%s", url.PathEscape(pool), url.PathEscape(volumeType), url.PathEscape(volumeName), url.PathEscape(snapshotName))
_, _, err := r.queryOperation("PUT", path, volume, ETag)
if err != nil {
return err
}
return nil
}
// MigrateStoragePoolVolume requests that LXD prepares for a storage volume migration
func (r *ProtocolLXD) MigrateStoragePoolVolume(pool string, volume api.StorageVolumePost) (Operation, error) {
if !r.HasExtension("storage_api_remote_volume_handling") {
return nil, fmt.Errorf("The server is missing the required \"storage_api_remote_volume_handling\" API extension")
}
// Quick check.
if !volume.Migration {
return nil, fmt.Errorf("Can't ask for a rename through MigrateStoragePoolVolume")
}
// Send the request
path := fmt.Sprintf("/storage-pools/%s/volumes/custom/%s", url.PathEscape(pool), volume.Name)
op, _, err := r.queryOperation("POST", path, volume, "")
if err != nil {
return nil, err
}
return op, nil
}
func (r *ProtocolLXD) tryMigrateStoragePoolVolume(source InstanceServer, pool string, req api.StorageVolumePost, urls []string) (RemoteOperation, error) {
if len(urls) == 0 {
return nil, fmt.Errorf("The source server isn't listening on the network")
}
rop := remoteOperation{
chDone: make(chan bool),
}
operation := req.Target.Operation
// Forward targetOp to remote op
go func() {
success := false
var errors []remoteOperationResult
for _, serverURL := range urls {
req.Target.Operation = fmt.Sprintf("%s/1.0/operations/%s", serverURL, url.PathEscape(operation))
// Send the request
top, err := source.MigrateStoragePoolVolume(pool, req)
if err != nil {
errors = append(errors, remoteOperationResult{URL: serverURL, Error: err})
continue
}
rop := remoteOperation{
targetOp: top,
chDone: make(chan bool),
}
for _, handler := range rop.handlers {
rop.targetOp.AddHandler(handler)
}
err = rop.targetOp.Wait()
if err != nil {
errors = append(errors, remoteOperationResult{URL: serverURL, Error: err})
if shared.IsConnectionError(err) {
continue
}
break
}
success = true
break
}
if !success {
rop.err = remoteOperationError("Failed storage volume creation", errors)
}
close(rop.chDone)
}()
return &rop, nil
}
func (r *ProtocolLXD) tryCreateStoragePoolVolume(pool string, req api.StorageVolumesPost, urls []string) (RemoteOperation, error) {
if len(urls) == 0 {
return nil, fmt.Errorf("The source server isn't listening on the network")
}
rop := remoteOperation{
chDone: make(chan bool),
}
operation := req.Source.Operation
// Forward targetOp to remote op
go func() {
success := false
var errors []remoteOperationResult
for _, serverURL := range urls {
req.Source.Operation = fmt.Sprintf("%s/1.0/operations/%s", serverURL, url.PathEscape(operation))
// Send the request
path := fmt.Sprintf("/storage-pools/%s/volumes/%s", url.PathEscape(pool), url.PathEscape(req.Type))
top, _, err := r.queryOperation("POST", path, req, "")
if err != nil {
errors = append(errors, remoteOperationResult{URL: serverURL, Error: err})
continue
}
rop := remoteOperation{
targetOp: top,
chDone: make(chan bool),
}
for _, handler := range rop.handlers {
rop.targetOp.AddHandler(handler)
}
err = rop.targetOp.Wait()
if err != nil {
errors = append(errors, remoteOperationResult{URL: serverURL, Error: err})
if shared.IsConnectionError(err) {
continue
}
break
}
success = true
break
}
if !success {
rop.err = remoteOperationError("Failed storage volume creation", errors)
}
close(rop.chDone)
}()
return &rop, nil
}
// CopyStoragePoolVolume copies an existing storage volume
func (r *ProtocolLXD) CopyStoragePoolVolume(pool string, source InstanceServer, sourcePool string, volume api.StorageVolume, args *StoragePoolVolumeCopyArgs) (RemoteOperation, error) {
if !r.HasExtension("storage_api_local_volume_handling") {
return nil, fmt.Errorf("The server is missing the required \"storage_api_local_volume_handling\" API extension")
}
if args != nil && args.VolumeOnly && !r.HasExtension("storage_api_volume_snapshots") {
return nil, fmt.Errorf("The target server is missing the required \"storage_api_volume_snapshots\" API extension")
}
req := api.StorageVolumesPost{
Name: args.Name,
Type: volume.Type,
Source: api.StorageVolumeSource{
Name: volume.Name,
Type: "copy",
Pool: sourcePool,
VolumeOnly: args.VolumeOnly,
},
}
req.Config = volume.Config
req.Description = volume.Description
req.ContentType = volume.ContentType
sourceInfo, err := source.GetConnectionInfo()
if err != nil {
return nil, fmt.Errorf("Failed to get source connection info: %v", err)
}
destInfo, err := r.GetConnectionInfo()
if err != nil {
return nil, fmt.Errorf("Failed to get destination connection info: %v", err)
}
if destInfo.URL == sourceInfo.URL && destInfo.SocketPath == sourceInfo.SocketPath {
// Project handling
if destInfo.Project != sourceInfo.Project {
if !r.HasExtension("storage_api_project") {
return nil, fmt.Errorf("The server is missing the required \"storage_api_project\" API extension")
}
req.Source.Project = sourceInfo.Project
}
// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("/storage-pools/%s/volumes/%s", url.PathEscape(pool), url.PathEscape(volume.Type)), req, "")
if err != nil {
return nil, err
}
rop := remoteOperation{
targetOp: op,
chDone: make(chan bool),
}
// Forward targetOp to remote op
go func() {
rop.err = rop.targetOp.Wait()
close(rop.chDone)
}()
return &rop, nil
}
if !r.HasExtension("storage_api_remote_volume_handling") {
return nil, fmt.Errorf("The server is missing the required \"storage_api_remote_volume_handling\" API extension")
}
sourceReq := api.StorageVolumePost{
Migration: true,
Name: volume.Name,
Pool: sourcePool,
VolumeOnly: args.VolumeOnly,
}
// Push mode migration
if args != nil && args.Mode == "push" {
// Get target server connection information
info, err := r.GetConnectionInfo()
if err != nil {
return nil, err
}
// Create the container
req.Source.Type = "migration"
req.Source.Mode = "push"
// Send the request
path := fmt.Sprintf("/storage-pools/%s/volumes/%s", url.PathEscape(pool), url.PathEscape(volume.Type))
// Send the request
op, _, err := r.queryOperation("POST", path, req, "")
if err != nil {
return nil, err
}
opAPI := op.Get()
targetSecrets := map[string]string{}
for k, v := range opAPI.Metadata {
targetSecrets[k] = v.(string)
}
// Prepare the source request
target := api.StorageVolumePostTarget{}
target.Operation = opAPI.ID
target.Websockets = targetSecrets
target.Certificate = info.Certificate
sourceReq.Target = &target
return r.tryMigrateStoragePoolVolume(source, sourcePool, sourceReq, info.Addresses)
}
// Get source server connection information
info, err := source.GetConnectionInfo()
if err != nil {
return nil, err
}
// Get secrets from source server
op, err := source.MigrateStoragePoolVolume(sourcePool, sourceReq)
if err != nil {
return nil, err
}
opAPI := op.Get()
// Prepare source server secrets for remote
sourceSecrets := map[string]string{}
for k, v := range opAPI.Metadata {
sourceSecrets[k] = v.(string)
}
// Relay mode migration
if args != nil && args.Mode == "relay" {
// Push copy source fields
req.Source.Type = "migration"
req.Source.Mode = "push"
// Send the request
path := fmt.Sprintf("/storage-pools/%s/volumes/%s", url.PathEscape(pool), url.PathEscape(volume.Type))
// Send the request
targetOp, _, err := r.queryOperation("POST", path, req, "")
if err != nil {
return nil, err
}
targetOpAPI := targetOp.Get()
// Extract the websockets
targetSecrets := map[string]string{}
for k, v := range targetOpAPI.Metadata {
targetSecrets[k] = v.(string)
}
// Launch the relay
err = r.proxyMigration(targetOp.(*operation), targetSecrets, source, op.(*operation), sourceSecrets)
if err != nil {
return nil, err
}
// Prepare a tracking operation
rop := remoteOperation{
targetOp: targetOp,
chDone: make(chan bool),
}
// Forward targetOp to remote op
go func() {
rop.err = rop.targetOp.Wait()
close(rop.chDone)
}()
return &rop, nil
}
// Pull mode migration
req.Source.Type = "migration"
req.Source.Mode = "pull"
req.Source.Operation = opAPI.ID
req.Source.Websockets = sourceSecrets
req.Source.Certificate = info.Certificate
return r.tryCreateStoragePoolVolume(pool, req, info.Addresses)
}
// MoveStoragePoolVolume renames or moves an existing storage volume
func (r *ProtocolLXD) MoveStoragePoolVolume(pool string, source InstanceServer, sourcePool string, volume api.StorageVolume, args *StoragePoolVolumeMoveArgs) (RemoteOperation, error) {
if !r.HasExtension("storage_api_local_volume_handling") {
return nil, fmt.Errorf("The server is missing the required \"storage_api_local_volume_handling\" API extension")
}
if r != source {
return nil, fmt.Errorf("Moving storage volumes between remotes is not implemented")
}
req := api.StorageVolumePost{
Name: args.Name,
Pool: pool,
}
// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("/storage-pools/%s/volumes/%s/%s", url.PathEscape(sourcePool), url.PathEscape(volume.Type), volume.Name), req, "")
if err != nil {
return nil, err
}
rop := remoteOperation{
targetOp: op,
chDone: make(chan bool),
}
// Forward targetOp to remote op
go func() {
rop.err = rop.targetOp.Wait()
close(rop.chDone)
}()
return &rop, nil
}
// UpdateStoragePoolVolume updates the volume to match the provided StoragePoolVolume struct
func (r *ProtocolLXD) UpdateStoragePoolVolume(pool string, volType string, name string, volume api.StorageVolumePut, ETag string) error {
if !r.HasExtension("storage") {
return fmt.Errorf("The server is missing the required \"storage\" API extension")
}
if volume.Restore != "" && !r.HasExtension("storage_api_volume_snapshots") {
return fmt.Errorf("The server is missing the required \"storage_api_volume_snapshots\" API extension")
}
// Send the request
path := fmt.Sprintf("/storage-pools/%s/volumes/%s/%s", url.PathEscape(pool), url.PathEscape(volType), url.PathEscape(name))
_, _, err := r.query("PUT", path, volume, ETag)
if err != nil {
return err
}
return nil
}
// DeleteStoragePoolVolume deletes a storage pool
func (r *ProtocolLXD) DeleteStoragePoolVolume(pool string, volType string, name string) error {
if !r.HasExtension("storage") {
return fmt.Errorf("The server is missing the required \"storage\" API extension")
}
// Send the request
path := fmt.Sprintf("/storage-pools/%s/volumes/%s/%s", url.PathEscape(pool), url.PathEscape(volType), url.PathEscape(name))
_, _, err := r.query("DELETE", path, nil, "")
if err != nil {
return err
}
return nil
}
// RenameStoragePoolVolume renames a storage volume
func (r *ProtocolLXD) RenameStoragePoolVolume(pool string, volType string, name string, volume api.StorageVolumePost) error {
if !r.HasExtension("storage_api_volume_rename") {
return fmt.Errorf("The server is missing the required \"storage_api_volume_rename\" API extension")
}
path := fmt.Sprintf("/storage-pools/%s/volumes/%s/%s", url.PathEscape(pool), url.PathEscape(volType), url.PathEscape(name))
// Send the request
_, _, err := r.query("POST", path, volume, "")
if err != nil {
return err
}
return nil
}
// GetStoragePoolVolumeBackupNames returns a list of volume backup names.
func (r *ProtocolLXD) GetStoragePoolVolumeBackupNames(pool string, volName string) ([]string, error) {
if !r.HasExtension("custom_volume_backup") {
return nil, fmt.Errorf("The server is missing the required \"custom_volume_backup\" API extension")
}
// Fetch the raw value
urls := []string{}
_, err := r.queryStruct("GET", fmt.Sprintf("/storage-pools/%s/volumes/custom/%s/backups", url.PathEscape(pool), url.PathEscape(volName)), nil, "", &urls)
if err != nil {
return nil, err
}
// Parse it
names := []string{}
for _, uri := range urls {
fields := strings.Split(uri, fmt.Sprintf("/storage-pools/%s/volumes/custom/%s/backups", url.PathEscape(pool), url.PathEscape(volName)))
names = append(names, fields[len(fields)-1])
}
return names, nil
}
// GetStoragePoolVolumeBackups returns a list of custom volume backups.
func (r *ProtocolLXD) GetStoragePoolVolumeBackups(pool string, volName string) ([]api.StoragePoolVolumeBackup, error) {
if !r.HasExtension("custom_volume_backup") {
return nil, fmt.Errorf("The server is missing the required \"custom_volume_backup\" API extension")
}
// Fetch the raw value
backups := []api.StoragePoolVolumeBackup{}
_, err := r.queryStruct("GET", fmt.Sprintf("/storage-pools/%s/volumes/custom/%s/backups?recursion=1", url.PathEscape(pool), url.PathEscape(volName)), nil, "", &backups)
if err != nil {
return nil, err
}
return backups, nil
}
// GetStoragePoolVolumeBackup returns a custom volume backup.
func (r *ProtocolLXD) GetStoragePoolVolumeBackup(pool string, volName string, name string) (*api.StoragePoolVolumeBackup, string, error) {
if !r.HasExtension("custom_volume_backup") {
return nil, "", fmt.Errorf("The server is missing the required \"custom_volume_backup\" API extension")
}
// Fetch the raw value
backup := api.StoragePoolVolumeBackup{}
etag, err := r.queryStruct("GET", fmt.Sprintf("/storage-pools/%s/volumes/custom/%s/backups/%s", url.PathEscape(pool), url.PathEscape(volName), url.PathEscape(name)), nil, "", &backup)
if err != nil {
return nil, "", err
}
return &backup, etag, nil
}
// CreateStoragePoolVolumeBackup creates new custom volume backup.
func (r *ProtocolLXD) CreateStoragePoolVolumeBackup(pool string, volName string, backup api.StoragePoolVolumeBackupsPost) (Operation, error) {
if !r.HasExtension("custom_volume_backup") {
return nil, fmt.Errorf("The server is missing the required \"custom_volume_backup\" API extension")
}
// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("/storage-pools/%s/volumes/custom/%s/backups", url.PathEscape(pool), url.PathEscape(volName)), backup, "")
if err != nil {
return nil, err
}
return op, nil
}
// RenameStoragePoolVolumeBackup renames a custom volume backup.
func (r *ProtocolLXD) RenameStoragePoolVolumeBackup(pool string, volName string, name string, backup api.StoragePoolVolumeBackupPost) (Operation, error) {
if !r.HasExtension("custom_volume_backup") {
return nil, fmt.Errorf("The server is missing the required \"custom_volume_backup\" API extension")
}
// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("/storage-pools/%s/volumes/custom/%s/backups/%s", url.PathEscape(pool), url.PathEscape(volName), url.PathEscape(name)), backup, "")
if err != nil {
return nil, err
}
return op, nil
}
// DeleteStoragePoolVolumeBackup deletes a custom volume backup.
func (r *ProtocolLXD) DeleteStoragePoolVolumeBackup(pool string, volName string, name string) (Operation, error) {
if !r.HasExtension("custom_volume_backup") {
return nil, fmt.Errorf("The server is missing the required \"custom_volume_backup\" API extension")
}
// Send the request
op, _, err := r.queryOperation("DELETE", fmt.Sprintf("/storage-pools/%s/volumes/custom/%s/backups/%s", url.PathEscape(pool), url.PathEscape(volName), url.PathEscape(name)), nil, "")
if err != nil {
return nil, err
}
return op, nil
}
// GetStoragePoolVolumeBackupFile requests the custom volume backup content.
func (r *ProtocolLXD) GetStoragePoolVolumeBackupFile(pool string, volName string, name string, req *BackupFileRequest) (*BackupFileResponse, error) {
if !r.HasExtension("custom_volume_backup") {
return nil, fmt.Errorf("The server is missing the required \"custom_volume_backup\" API extension")
}
// Build the URL
uri := fmt.Sprintf("%s/1.0/storage-pools/%s/volumes/custom/%s/backups/%s/export", r.httpHost, url.PathEscape(pool), url.PathEscape(volName), url.PathEscape(name))
if r.project != "" {
uri += fmt.Sprintf("?project=%s", url.QueryEscape(r.project))
}
// Prepare the download request
request, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
if r.httpUserAgent != "" {
request.Header.Set("User-Agent", r.httpUserAgent)
}
// Start the request
response, doneCh, err := cancel.CancelableDownload(req.Canceler, r.http, request)
if err != nil {
return nil, err
}
defer response.Body.Close()
defer close(doneCh)
if response.StatusCode != http.StatusOK {
_, _, err := lxdParseResponse(response)
if err != nil {
return nil, err
}
}
// Handle the data
body := response.Body
if req.ProgressHandler != nil {
body = &ioprogress.ProgressReader{
ReadCloser: response.Body,
Tracker: &ioprogress.ProgressTracker{
Length: response.ContentLength,
Handler: func(percent int64, speed int64) {
req.ProgressHandler(ioprogress.ProgressData{Text: fmt.Sprintf("%d%% (%s/s)", percent, units.GetByteSizeString(speed, 2))})
},
},
}
}
size, err := io.Copy(req.BackupFile, body)
if err != nil {
return nil, err
}
resp := BackupFileResponse{}
resp.Size = size
return &resp, nil
}
// CreateStoragePoolVolumeFromBackup creates a custom volume from a backup file.
func (r *ProtocolLXD) CreateStoragePoolVolumeFromBackup(pool string, args StoragePoolVolumeBackupArgs) (Operation, error) {
if !r.HasExtension("custom_volume_backup") {
return nil, fmt.Errorf(`The server is missing the required "custom_volume_backup" API extension`)
}
if args.Name != "" && !r.HasExtension("backup_override_name") {
return nil, fmt.Errorf(`The server is missing the required "backup_override_name" API extension`)
}
path := fmt.Sprintf("/storage-pools/%s/volumes/custom", url.PathEscape(pool))
// Prepare the HTTP request.
reqURL, err := r.setQueryAttributes(fmt.Sprintf("%s/1.0%s", r.httpHost, path))
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", reqURL, args.BackupFile)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/octet-stream")
if args.Name != "" {
req.Header.Set("X-LXD-name", args.Name)
}
// Send the request.
resp, err := r.do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Handle errors.
response, _, err := lxdParseResponse(resp)
if err != nil {
return nil, err
}
// Get to the operation.
respOperation, err := response.MetadataAsOperation()
if err != nil {
return nil, err
}
// Setup an Operation wrapper.
op := operation{
Operation: *respOperation,
r: r,
chActive: make(chan bool),
}
return &op, nil
}