-
Notifications
You must be signed in to change notification settings - Fork 50
/
storage.go
250 lines (208 loc) · 6.56 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
package proxmox
import (
"context"
"fmt"
"os"
"path/filepath"
)
var validContent = map[string]struct{}{
"iso": {},
"vztmpl": {},
}
func (c *Client) ClusterStorages(ctx context.Context) (storages ClusterStorages, err error) {
err = c.Get(ctx, "/storage", &storages)
if err != nil {
return
}
for _, s := range storages {
s.client = c
}
return
}
func (c *Client) ClusterStorage(ctx context.Context, name string) (storage *ClusterStorage, err error) {
err = c.Get(ctx, fmt.Sprintf("/storage/%s", name), &storage)
if err != nil {
return
}
storage.client = c
return
}
func (c *Client) DeleteClusterStorage(ctx context.Context, name string) (*Task, error) {
var upid UPID
err := c.Delete(ctx, fmt.Sprintf("/storage/%s", name), &upid)
if err != nil {
return nil, err
}
return NewTask(upid, c), nil
}
func (c *Client) NewClusterStorage(ctx context.Context, options ...ClusterStorageOptions) (*Task, error) {
var upid UPID
data := make(map[string]interface{})
for _, option := range options {
data[option.Name] = option.Value
}
err := c.Post(ctx, "/storage", data, &upid)
if err != nil {
return nil, err
}
return NewTask(upid, c), nil
}
func (c *Client) UpdateClusterStorage(ctx context.Context, name string, options ...ClusterStorageOptions) (*Task, error) {
var upid UPID
data := make(map[string]interface{})
for _, option := range options {
data[option.Name] = option.Value
}
err := c.Put(ctx, fmt.Sprintf("/storage/%s", name), data, &upid)
if err != nil {
return nil, err
}
return NewTask(upid, c), nil
}
func (s *Storage) Upload(content, file string) (*Task, error) {
return s.upload(content, file, nil)
}
func (s *Storage) UploadWithName(content, file string, storageFilename string) (*Task, error) {
return s.upload(content, file, &map[string]string{"filename": storageFilename})
}
func (s *Storage) UploadWithHash(content, file string, storageFilename *string, checksum, checksumAlgorithm string) (*Task, error) {
extraArgs := map[string]string{
"checksum": checksum,
"checksum-algorithm": checksumAlgorithm,
}
if storageFilename != nil {
extraArgs["filename"] = *storageFilename
}
if storageFilename != nil {
return s.upload(content, file, &map[string]string{"filename": *storageFilename})
}
return s.upload(content, file, nil)
}
func (s *Storage) upload(content, file string, extraArgs *map[string]string) (*Task, error) {
if _, ok := validContent[content]; !ok {
return nil, fmt.Errorf("only iso and vztmpl allowed")
}
stat, err := os.Stat(file)
if err != nil {
return nil, err
}
if stat.IsDir() {
return nil, fmt.Errorf("file is a directory %s", file)
}
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()
var upid UPID
data := map[string]string{"content": content}
if extraArgs != nil {
for k, v := range *extraArgs {
data[k] = v
}
}
if err := s.client.Upload(fmt.Sprintf("/nodes/%s/storage/%s/upload", s.Node, s.Name),
data, f, &upid); err != nil {
return nil, err
}
return NewTask(upid, s.client), nil
}
func (s *Storage) DownloadURL(ctx context.Context, content, filename, url string) (*Task, error) {
return s.downloadURL(ctx, content, filename, url, nil)
}
func (s *Storage) DownloadURLWithHash(ctx context.Context, content, filename, url string, checksum, checksumAlgorithm string) (*Task, error) {
return s.downloadURL(ctx, content, filename, url, &map[string]string{
"checksum": checksum,
"checksum-algorithm": checksumAlgorithm,
})
}
func (s *Storage) downloadURL(ctx context.Context, content, filename, url string, extraArgs *map[string]string) (*Task, error) {
if _, ok := validContent[content]; !ok {
return nil, fmt.Errorf("only iso and vztmpl allowed")
}
var upid UPID
data := map[string]string{
"content": content,
"filename": filename,
"url": url,
}
if extraArgs != nil {
for k, v := range *extraArgs {
data[k] = v
}
}
err := s.client.Post(ctx, fmt.Sprintf("/nodes/%s/storage/%s/download-url", s.Node, s.Name), data, &upid)
if err != nil {
return nil, err
}
return NewTask(upid, s.client), nil
}
func (s *Storage) GetContent(ctx context.Context) (content []*StorageContent, err error) {
err = s.client.Get(ctx, fmt.Sprintf("/nodes/%s/storage/%s/content", s.Node, s.Name), &content)
return content, err
}
func (s *Storage) DeleteContent(ctx context.Context, content string) (*Task, error) {
var upid UPID
err := s.client.Delete(ctx, fmt.Sprintf("/nodes/%s/storage/%s/content/%s", s.Node, s.Name, content), &upid)
if err != nil {
return nil, err
}
return NewTask(upid, s.client), nil
}
func (s *Storage) ISO(ctx context.Context, name string) (iso *ISO, err error) {
err = s.client.Get(ctx, fmt.Sprintf("/nodes/%s/storage/%s/content/%s:%s/%s", s.Node, s.Name, s.Name, "iso", name), &iso)
if err != nil {
return nil, err
}
iso.client = s.client
iso.Node = s.Node
iso.Storage = s.Name
if iso.VolID == "" {
iso.VolID = fmt.Sprintf("%s:iso/%s", iso.Storage, name)
}
return
}
func (s *Storage) VzTmpl(ctx context.Context, name string) (vztmpl *VzTmpl, err error) {
err = s.client.Get(ctx, fmt.Sprintf("/nodes/%s/storage/%s/content/%s:%s/%s", s.Node, s.Name, s.Name, "vztmpl", name), &vztmpl)
if err != nil {
return nil, err
}
vztmpl.client = s.client
vztmpl.Node = s.Node
vztmpl.Storage = s.Name
if vztmpl.VolID == "" {
vztmpl.VolID = fmt.Sprintf("%s:vztmpl/%s", vztmpl.Storage, name)
}
return
}
func (s *Storage) Backup(ctx context.Context, name string) (backup *Backup, err error) {
err = s.client.Get(ctx, fmt.Sprintf("/nodes/%s/storage/%s/content/%s:%s/%s", s.Node, s.Name, s.Name, "backup", name), &backup)
if err != nil {
return nil, err
}
backup.client = s.client
backup.Node = s.Node
backup.Storage = s.Name
return
}
func (v *VzTmpl) Delete(ctx context.Context) (*Task, error) {
return deleteVolume(ctx, v.client, v.Node, v.Storage, v.VolID, v.Path, "vztmpl")
}
func (b *Backup) Delete(ctx context.Context) (*Task, error) {
return deleteVolume(ctx, b.client, b.Node, b.Storage, b.VolID, b.Path, "backup")
}
func (i *ISO) Delete(ctx context.Context) (*Task, error) {
return deleteVolume(ctx, i.client, i.Node, i.Storage, i.VolID, i.Path, "iso")
}
func deleteVolume(ctx context.Context, c *Client, n, s, v, p, t string) (*Task, error) {
var upid UPID
if v == "" && p == "" {
return nil, fmt.Errorf("volid or path required for a delete")
}
if v == "" {
// volid not returned in the volume endpoints, need to generate
v = fmt.Sprintf("%s:%s/%s", s, t, filepath.Base(p))
}
err := c.Delete(ctx, fmt.Sprintf("/nodes/%s/storage/%s/content/%s", n, s, v), &upid)
return NewTask(upid, c), err
}