-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathvirtual_file_system_storage.go
255 lines (208 loc) · 9.02 KB
/
virtual_file_system_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
package govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
const virtualFileSystemStoragePath = "/v2/vfs"
// VirtualFileSystemStorageService is the interface to interact with virtual
// file system endpoint on the Vultr API
// Link : https://www.vultr.com/api/#tag/vfs
type VirtualFileSystemStorageService interface {
Create(ctx context.Context, vfsReq *VirtualFileSystemStorageReq) (*VirtualFileSystemStorage, *http.Response, error)
Get(ctx context.Context, vfsID string) (*VirtualFileSystemStorage, *http.Response, error)
Update(ctx context.Context, vfsID string, vfsUpdateReq *VirtualFileSystemStorageUpdateReq) (*VirtualFileSystemStorage, *http.Response, error) //nolint:lll
Delete(ctx context.Context, vfsID string) error
List(ctx context.Context, options *ListOptions) ([]VirtualFileSystemStorage, *Meta, *http.Response, error)
AttachmentList(ctx context.Context, vfsID string) ([]VirtualFileSystemStorageAttachment, *http.Response, error)
AttachmentGet(ctx context.Context, vfsID, targetID string) (*VirtualFileSystemStorageAttachment, *http.Response, error)
Attach(ctx context.Context, vfsID, targetID string) (*VirtualFileSystemStorageAttachment, *http.Response, error)
Detach(ctx context.Context, vfsID, targetID string) error
}
// VirtualFileSystemStorageServiceHandler handles interaction with the virtual
// file system methods for the Vultr API.
type VirtualFileSystemStorageServiceHandler struct {
client *Client
}
// VirtualFileSystemStorage represents a virtual file system storage.
type VirtualFileSystemStorage struct {
ID string `json:"id"`
Region string `json:"region"`
DateCreated string `json:"date_created"`
Status string `json:"status"`
Label string `json:"label"`
Tags []string `json:"tags"`
DiskType string `json:"disk_type"`
StorageSize VirtualFileSystemStorageSize `json:"storage_size"`
StorageUsed VirtualFileSystemStorageSize `json:"storage_used"`
Billing VirtualFileSystemStorageBilling `json:"billing"`
}
// VirtualFileSystemStorageSize represents the on disk size of a virtual file
// system storage.
type VirtualFileSystemStorageSize struct {
SizeBytes int `json:"bytes,omitempty"`
SizeGB int `json:"gb"`
}
// VirtualFileSystemStorageBilling represents the billing data of a virtual
// file system storage.
type VirtualFileSystemStorageBilling struct {
Charges float32 `json:"charges"`
Monthly float32 `json:"monthly"`
}
type virtualFileSystemStoragesBase struct {
VFSs []VirtualFileSystemStorage `json:"vfs"`
Meta *Meta `json:"meta"`
}
// VirtualFileSystemStorageReq struct represents the request used when creating
// a virtual file system storage.
type VirtualFileSystemStorageReq struct {
Region string `json:"region"`
Label string `json:"label"`
StorageSize VirtualFileSystemStorageSize `json:"storage_size"`
DiskType string `json:"disk_type,omitempty"`
Tags []string `json:"tags,omitempty"`
}
// VirtualFileSystemStorageUpdateReq struct represents the request used when
// updating virtual file system storage.
type VirtualFileSystemStorageUpdateReq struct {
Label string `json:"label,omitempty"`
StorageSize VirtualFileSystemStorageSize `json:"storage_size"`
}
// VirtualFileSystemStorageAttachment represents an attachment for a virtual
// file system.
type VirtualFileSystemStorageAttachment struct {
ID string `json:"vfs_id"`
State string `json:"state"`
TargetID string `json:"target_id"`
MountTag int `json:"mount_tag"`
}
type virtualFileSystemStorageAttachmentsBase struct {
Attachments []VirtualFileSystemStorageAttachment `json:"attachments"`
}
// Create sends a create request for a virtual file system storage.
func (f *VirtualFileSystemStorageServiceHandler) Create(ctx context.Context, vfsReq *VirtualFileSystemStorageReq) (*VirtualFileSystemStorage, *http.Response, error) { //nolint:lll
req, err := f.client.NewRequest(ctx, http.MethodPost, virtualFileSystemStoragePath, vfsReq)
if err != nil {
return nil, nil, err
}
vfs := new(VirtualFileSystemStorage)
resp, err := f.client.DoWithContext(ctx, req, vfs)
if err != nil {
return nil, resp, err
}
return vfs, resp, nil
}
// Get retrieves a single virtual file system storage.
func (f *VirtualFileSystemStorageServiceHandler) Get(ctx context.Context, vfsID string) (*VirtualFileSystemStorage, *http.Response, error) {
uri := fmt.Sprintf("%s/%s", virtualFileSystemStoragePath, vfsID)
req, err := f.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
vfs := new(VirtualFileSystemStorage)
resp, err := f.client.DoWithContext(ctx, req, vfs)
if err != nil {
return nil, resp, err
}
return vfs, resp, nil
}
// Update sends a update request for a virtual file system storage.
func (f *VirtualFileSystemStorageServiceHandler) Update(ctx context.Context, vfsID string, vfsUpdateReq *VirtualFileSystemStorageUpdateReq) (*VirtualFileSystemStorage, *http.Response, error) { //nolint:lll
uri := fmt.Sprintf("%s/%s", virtualFileSystemStoragePath, vfsID)
req, err := f.client.NewRequest(ctx, http.MethodPut, uri, vfsUpdateReq)
if err != nil {
return nil, nil, err
}
vfs := new(VirtualFileSystemStorage)
resp, err := f.client.DoWithContext(ctx, req, vfs)
if err != nil {
return nil, resp, err
}
return vfs, resp, err
}
// Delete sends a delete request for a virtual file system storage.
func (f *VirtualFileSystemStorageServiceHandler) Delete(ctx context.Context, vfsID string) error {
uri := fmt.Sprintf("%s/%s", virtualFileSystemStoragePath, vfsID)
req, err := f.client.NewRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
_, err = f.client.DoWithContext(ctx, req, nil)
return err
}
// List retrieves a list of all virtual file system storages.
func (f *VirtualFileSystemStorageServiceHandler) List(ctx context.Context, options *ListOptions) ([]VirtualFileSystemStorage, *Meta, *http.Response, error) { //nolint:dupl,lll
req, err := f.client.NewRequest(ctx, http.MethodGet, virtualFileSystemStoragePath, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
vfsStorages := new(virtualFileSystemStoragesBase)
resp, err := f.client.DoWithContext(ctx, req, vfsStorages)
if err != nil {
return nil, nil, resp, err
}
return vfsStorages.VFSs, vfsStorages.Meta, resp, nil
}
// Attach attaches a virtual file system storage to another instance.
func (f *VirtualFileSystemStorageServiceHandler) Attach(ctx context.Context, vfsID, targetID string) (*VirtualFileSystemStorageAttachment, *http.Response, error) { //nolint:lll
uri := fmt.Sprintf("%s/%s/attachments/%s", virtualFileSystemStoragePath, vfsID, targetID)
req, err := f.client.NewRequest(ctx, http.MethodPut, uri, nil)
if err != nil {
return nil, nil, err
}
att := new(VirtualFileSystemStorageAttachment)
resp, err := f.client.DoWithContext(ctx, req, att)
if err != nil {
return nil, resp, err
}
return att, resp, err
}
// AttachmentList retrieves a list the active attachments on a virtual file
// system storage.
func (f *VirtualFileSystemStorageServiceHandler) AttachmentList(ctx context.Context, vfsID string) ([]VirtualFileSystemStorageAttachment, *http.Response, error) { //nolint:lll
uri := fmt.Sprintf("%s/%s/attachments", virtualFileSystemStoragePath, vfsID)
req, err := f.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
atts := new(virtualFileSystemStorageAttachmentsBase)
resp, err := f.client.DoWithContext(ctx, req, atts)
if err != nil {
return nil, resp, err
}
return atts.Attachments, resp, err
}
// AttachmentGet retrieves the attachment of a virtual file system storage and
// its attached instance.
func (f *VirtualFileSystemStorageServiceHandler) AttachmentGet(ctx context.Context, vfsID, targetID string) (*VirtualFileSystemStorageAttachment, *http.Response, error) { //nolint:lll
uri := fmt.Sprintf("%s/%s/attachments/%s", virtualFileSystemStoragePath, vfsID, targetID)
req, err := f.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
att := new(VirtualFileSystemStorageAttachment)
resp, err := f.client.DoWithContext(ctx, req, att)
if err != nil {
return nil, resp, err
}
return att, resp, err
}
// Detach sends a delete request for an attachment of a virtual file system
// storage, detaching it from its instance.
func (f *VirtualFileSystemStorageServiceHandler) Detach(ctx context.Context, vfsID, targetID string) error {
uri := fmt.Sprintf("%s/%s/attachments/%s", virtualFileSystemStoragePath, vfsID, targetID)
req, err := f.client.NewRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
if _, err := f.client.DoWithContext(ctx, req, nil); err != nil {
return err
}
return nil
}