This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
volumes.go
264 lines (222 loc) · 8.17 KB
/
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
package packngo
import (
"path"
)
const (
volumeBasePath = "/storage"
attachmentsBasePath = "/attachments"
)
// VolumeService interface defines available Volume methods
type VolumeService interface {
List(string, *ListOptions) ([]Volume, *Response, error)
Get(string, *GetOptions) (*Volume, *Response, error)
Update(string, *VolumeUpdateRequest) (*Volume, *Response, error)
Delete(string) (*Response, error)
Create(*VolumeCreateRequest, string) (*Volume, *Response, error)
Lock(string) (*Response, error)
Unlock(string) (*Response, error)
}
// VolumeAttachmentService defines attachment methdods
type VolumeAttachmentService interface {
Get(string, *GetOptions) (*VolumeAttachment, *Response, error)
Create(string, string) (*VolumeAttachment, *Response, error)
Delete(string) (*Response, error)
}
type volumesRoot struct {
Volumes []Volume `json:"volumes"`
Meta meta `json:"meta"`
}
// Volume represents a volume
type Volume struct {
Attachments []*VolumeAttachment `json:"attachments,omitempty"`
BillingCycle string `json:"billing_cycle,omitempty"`
Created string `json:"created_at,omitempty"`
Description string `json:"description,omitempty"`
Facility *Facility `json:"facility,omitempty"`
Href string `json:"href,omitempty"`
ID string `json:"id"`
Locked bool `json:"locked,omitempty"`
Name string `json:"name,omitempty"`
Plan *Plan `json:"plan,omitempty"`
Project *Project `json:"project,omitempty"`
Size int `json:"size,omitempty"`
SnapshotPolicies []*SnapshotPolicy `json:"snapshot_policies,omitempty"`
State string `json:"state,omitempty"`
Updated string `json:"updated_at,omitempty"`
}
// SnapshotPolicy used to execute actions on volume
type SnapshotPolicy struct {
ID string `json:"id"`
Href string `json:"href"`
SnapshotFrequency string `json:"snapshot_frequency,omitempty"`
SnapshotCount int `json:"snapshot_count,omitempty"`
}
func (v Volume) String() string {
return Stringify(v)
}
// VolumeCreateRequest type used to create an Equinix Metal volume
type VolumeCreateRequest struct {
BillingCycle string `json:"billing_cycle"`
Description string `json:"description,omitempty"`
Locked bool `json:"locked,omitempty"`
Size int `json:"size"`
PlanID string `json:"plan_id"`
FacilityID string `json:"facility_id"`
SnapshotPolicies []*SnapshotPolicy `json:"snapshot_policies,omitempty"`
}
func (v VolumeCreateRequest) String() string {
return Stringify(v)
}
// VolumeUpdateRequest type used to update an Equinix Metal volume
type VolumeUpdateRequest struct {
Description *string `json:"description,omitempty"`
PlanID *string `json:"plan_id,omitempty"`
Size *int `json:"size,omitempty"`
BillingCycle *string `json:"billing_cycle,omitempty"`
}
// VolumeAttachment is a type from Equinix Metal API
type VolumeAttachment struct {
Href string `json:"href"`
ID string `json:"id"`
Volume Volume `json:"volume"`
Device Device `json:"device"`
}
func (v VolumeUpdateRequest) String() string {
return Stringify(v)
}
// VolumeAttachmentServiceOp implements VolumeService
type VolumeAttachmentServiceOp struct {
client *Client
}
// VolumeServiceOp implements VolumeService
type VolumeServiceOp struct {
client *Client
}
// List returns the volumes for a project
func (v *VolumeServiceOp) List(projectID string, opts *ListOptions) (volumes []Volume, resp *Response, err error) {
if validateErr := ValidateUUID(projectID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(projectBasePath, projectID, volumeBasePath)
apiPathQuery := opts.WithQuery(endpointPath)
for {
subset := new(volumesRoot)
resp, err = v.client.DoRequest("GET", apiPathQuery, nil, subset)
if err != nil {
return nil, resp, err
}
volumes = append(volumes, subset.Volumes...)
if apiPathQuery = nextPage(subset.Meta, opts); apiPathQuery != "" {
continue
}
return
}
}
// Get returns a volume by id
func (v *VolumeServiceOp) Get(volumeID string, opts *GetOptions) (*Volume, *Response, error) {
if validateErr := ValidateUUID(volumeID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(volumeBasePath, volumeID)
apiPathQuery := opts.WithQuery(endpointPath)
volume := new(Volume)
resp, err := v.client.DoRequest("GET", apiPathQuery, nil, volume)
if err != nil {
return nil, resp, err
}
return volume, resp, err
}
// Update updates a volume
func (v *VolumeServiceOp) Update(id string, updateRequest *VolumeUpdateRequest) (*Volume, *Response, error) {
if validateErr := ValidateUUID(id); validateErr != nil {
return nil, nil, validateErr
}
apiPath := path.Join(volumeBasePath, id)
volume := new(Volume)
resp, err := v.client.DoRequest("PATCH", apiPath, updateRequest, volume)
if err != nil {
return nil, resp, err
}
return volume, resp, err
}
// Delete deletes a volume
func (v *VolumeServiceOp) Delete(volumeID string) (*Response, error) {
if validateErr := ValidateUUID(volumeID); validateErr != nil {
return nil, validateErr
}
apiPath := path.Join(volumeBasePath, volumeID)
return v.client.DoRequest("DELETE", apiPath, nil, nil)
}
// Create creates a new volume for a project
func (v *VolumeServiceOp) Create(createRequest *VolumeCreateRequest, projectID string) (*Volume, *Response, error) {
if validateErr := ValidateUUID(projectID); validateErr != nil {
return nil, nil, validateErr
}
url := path.Join(projectBasePath, projectID, volumeBasePath)
volume := new(Volume)
resp, err := v.client.DoRequest("POST", url, createRequest, volume)
if err != nil {
return nil, resp, err
}
return volume, resp, err
}
// Attachments
// Create Attachment, i.e. attach volume to a device
func (v *VolumeAttachmentServiceOp) Create(volumeID, deviceID string) (*VolumeAttachment, *Response, error) {
if validateErr := ValidateUUID(volumeID); validateErr != nil {
return nil, nil, validateErr
}
if validateErr := ValidateUUID(deviceID); validateErr != nil {
return nil, nil, validateErr
}
url := path.Join(volumeBasePath, volumeID, attachmentsBasePath)
volAttachParam := map[string]string{
"device_id": deviceID,
}
volumeAttachment := new(VolumeAttachment)
resp, err := v.client.DoRequest("POST", url, volAttachParam, volumeAttachment)
if err != nil {
return nil, resp, err
}
return volumeAttachment, resp, nil
}
// Get gets attachment by id
func (v *VolumeAttachmentServiceOp) Get(attachmentID string, opts *GetOptions) (*VolumeAttachment, *Response, error) {
if validateErr := ValidateUUID(attachmentID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(volumeBasePath, attachmentsBasePath, attachmentID)
apiPathQuery := opts.WithQuery(endpointPath)
volumeAttachment := new(VolumeAttachment)
resp, err := v.client.DoRequest("GET", apiPathQuery, nil, volumeAttachment)
if err != nil {
return nil, resp, err
}
return volumeAttachment, resp, nil
}
// Delete deletes attachment by id
func (v *VolumeAttachmentServiceOp) Delete(attachmentID string) (*Response, error) {
if validateErr := ValidateUUID(attachmentID); validateErr != nil {
return nil, validateErr
}
apiPath := path.Join(volumeBasePath, attachmentsBasePath, attachmentID)
return v.client.DoRequest("DELETE", apiPath, nil, nil)
}
// Lock sets a volume to "locked"
func (v *VolumeServiceOp) Lock(id string) (*Response, error) {
if validateErr := ValidateUUID(id); validateErr != nil {
return nil, validateErr
}
apiPath := path.Join(volumeBasePath, id)
action := lockType{Locked: true}
return v.client.DoRequest("PATCH", apiPath, action, nil)
}
// Unlock sets a volume to "unlocked"
func (v *VolumeServiceOp) Unlock(id string) (*Response, error) {
if validateErr := ValidateUUID(id); validateErr != nil {
return nil, validateErr
}
apiPath := path.Join(volumeBasePath, id)
action := lockType{Locked: false}
return v.client.DoRequest("PATCH", apiPath, action, nil)
}