/
volumes.go
223 lines (189 loc) · 5 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
//
// Copyright 2020 Joyent Inc.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
package compute
import (
"context"
"encoding/json"
"net/http"
"net/url"
"path"
"github.com/joyent/triton-go/client"
"github.com/pkg/errors"
)
type VolumesClient struct {
client *client.Client
}
type Volume struct {
ID string `json:"id"`
Name string `json:"name"`
Owner string `json:"owner_uuid"`
Type string `json:"type"`
FileSystemPath string `json:"filesystem_path"`
Size int64 `json:"size"`
State string `json:"state"`
Networks []string `json:"networks"`
Refs []string `json:"refs"`
Tags map[string]string `json:"tags"`
}
type ListVolumesInput struct {
Name string
Size string
State string
Type string
}
func (c *VolumesClient) List(ctx context.Context, input *ListVolumesInput) ([]*Volume, error) {
fullPath := path.Join("/", c.client.AccountName, "volumes")
query := &url.Values{}
if input.Name != "" {
query.Set("name", input.Name)
}
if input.Size != "" {
query.Set("size", input.Size)
}
if input.State != "" {
query.Set("state", input.State)
}
if input.Type != "" {
query.Set("type", input.Type)
}
reqInputs := client.RequestInput{
Method: http.MethodGet,
Path: fullPath,
Query: query,
}
resp, err := c.client.ExecuteRequest(ctx, reqInputs)
if resp != nil {
defer resp.Close()
}
if err != nil {
return nil, errors.Wrap(err, "unable to list volumes")
}
var result []*Volume
decoder := json.NewDecoder(resp)
if err = decoder.Decode(&result); err != nil {
return nil, errors.Wrap(err, "unable to decode list volumes response")
}
return result, nil
}
type CreateVolumeInput struct {
Name string
Size int64
Networks []string
Type string
Tags map[string]string `json:"tags"`
}
func (input *CreateVolumeInput) toAPI() map[string]interface{} {
result := make(map[string]interface{}, 0)
if input.Name != "" {
result["name"] = input.Name
}
if input.Size != 0 {
result["size"] = input.Size
}
if input.Type != "" {
result["type"] = input.Type
}
if len(input.Networks) > 0 {
result["networks"] = input.Networks
}
if input.Tags != nil {
result["tags"] = input.Tags
}
return result
}
func (c *VolumesClient) Create(ctx context.Context, input *CreateVolumeInput) (*Volume, error) {
fullPath := path.Join("/", c.client.AccountName, "volumes")
reqInputs := client.RequestInput{
Method: http.MethodPost,
Path: fullPath,
Body: input.toAPI(),
}
resp, err := c.client.ExecuteRequest(ctx, reqInputs)
if err != nil {
return nil, errors.Wrap(err, "unable to create volume")
}
if resp != nil {
defer resp.Close()
}
var result *Volume
decoder := json.NewDecoder(resp)
if err = decoder.Decode(&result); err != nil {
return nil, errors.Wrap(err, "unable to decode create volume response")
}
return result, nil
}
type DeleteVolumeInput struct {
ID string
}
func (c *VolumesClient) Delete(ctx context.Context, input *DeleteVolumeInput) error {
fullPath := path.Join("/", c.client.AccountName, "volumes", input.ID)
reqInputs := client.RequestInput{
Method: http.MethodDelete,
Path: fullPath,
}
resp, err := c.client.ExecuteRequestRaw(ctx, reqInputs)
if err != nil {
return errors.Wrap(err, "unable to delete volume")
}
if resp == nil {
return errors.Wrap(err, "unable to delete volume")
}
if resp.Body != nil {
defer resp.Body.Close()
}
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusGone {
return nil
}
return nil
}
type GetVolumeInput struct {
ID string
}
func (c *VolumesClient) Get(ctx context.Context, input *GetVolumeInput) (*Volume, error) {
fullPath := path.Join("/", c.client.AccountName, "volumes", input.ID)
reqInputs := client.RequestInput{
Method: http.MethodGet,
Path: fullPath,
}
resp, err := c.client.ExecuteRequestRaw(ctx, reqInputs)
if err != nil {
return nil, errors.Wrap(err, "unable to get volume")
}
if resp == nil {
return nil, errors.Wrap(err, "unable to get volume")
}
if resp.Body != nil {
defer resp.Body.Close()
}
var result *Volume
decoder := json.NewDecoder(resp.Body)
if err = decoder.Decode(&result); err != nil {
return nil, errors.Wrap(err, "unable to decode get volume volume")
}
return result, nil
}
type UpdateVolumeInput struct {
ID string `json:"-"`
Name string `json:"name"`
}
func (c *VolumesClient) Update(ctx context.Context, input *UpdateVolumeInput) error {
fullPath := path.Join("/", c.client.AccountName, "volumes", input.ID)
reqInputs := client.RequestInput{
Method: http.MethodPost,
Path: fullPath,
Body: input,
}
resp, err := c.client.ExecuteRequest(ctx, reqInputs)
if err != nil {
return errors.Wrap(err, "unable to update volume")
}
if resp != nil {
defer resp.Close()
}
return nil
}