forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxd_storage_volumes.go
233 lines (191 loc) · 6.68 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
package lxd
import (
"fmt"
"net/url"
"strings"
"github.com/lxc/lxd/shared/api"
)
// 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.QueryEscape(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.QueryEscape(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.QueryEscape(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.QueryEscape(pool), url.QueryEscape(volType), url.QueryEscape(name))
if r.clusterTarget != "" {
path += fmt.Sprintf("?target=%s", r.clusterTarget)
}
etag, err := r.queryStruct("GET", path, nil, "", &volume)
if err != nil {
return nil, "", err
}
return &volume, etag, 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.QueryEscape(pool), url.QueryEscape(volume.Type))
if r.clusterTarget != "" {
path += fmt.Sprintf("?target=%s", r.clusterTarget)
}
_, _, err := r.query("POST", path, volume, "")
if err != nil {
return err
}
return nil
}
// CopyStoragePoolVolume copies an existing storage volume
func (r *ProtocolLXD) CopyStoragePoolVolume(pool string, source ContainerServer, 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 r != source {
return nil, fmt.Errorf("Copying storage volumes between remotes is not implemented")
}
req := api.StorageVolumesPost{
Name: args.Name,
Type: volume.Type,
Source: api.StorageVolumeSource{
Name: volume.Name,
Type: volume.Type,
Pool: sourcePool,
},
}
// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("/storage-pools/%s/volumes/%s", url.QueryEscape(pool), url.QueryEscape(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
}
// MoveStoragePoolVolume renames or moves an existing storage volume
func (r *ProtocolLXD) MoveStoragePoolVolume(pool string, source ContainerServer, 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.QueryEscape(sourcePool), url.QueryEscape(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")
}
// Send the request
path := fmt.Sprintf(
"/storage-pools/%s/volumes/%s/%s",
url.QueryEscape(pool), url.QueryEscape(volType), url.QueryEscape(name))
if r.clusterTarget != "" {
path += fmt.Sprintf("?target=%s", r.clusterTarget)
}
_, _, 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.QueryEscape(pool), url.QueryEscape(volType), url.QueryEscape(name))
if r.clusterTarget != "" {
path += fmt.Sprintf("?target=%s", r.clusterTarget)
}
_, _, 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.QueryEscape(pool), url.QueryEscape(volType), url.QueryEscape(name))
if r.clusterTarget != "" {
path += fmt.Sprintf("?target=%s", r.clusterTarget)
}
// Send the request
_, _, err := r.query("POST", path, volume, "")
if err != nil {
return err
}
return nil
}