-
Notifications
You must be signed in to change notification settings - Fork 83
/
volumes.go
169 lines (138 loc) · 3.69 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
package golinode
import (
"encoding/json"
"fmt"
"time"
"github.com/go-resty/resty"
)
// Volume represents a linode volume object
type Volume struct {
CreatedStr string `json:"created"`
UpdatedStr string `json:"updated"`
ID int
Label string
Status string
Region string
Size int
LinodeID int `json:"linode_id"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
}
type VolumeAttachOptions struct {
LinodeID int
ConfigID int
}
// LinodeVolumesPagedResponse represents a linode API response for listing of volumes
type VolumesPagedResponse struct {
*PageOptions
Data []*Volume
}
// Endpoint gets the endpoint URL for Volume
func (VolumesPagedResponse) Endpoint(c *Client) string {
endpoint, err := c.Volumes.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// AppendData appends Volumes when processing paginated Volume responses
func (resp *VolumesPagedResponse) AppendData(r *VolumesPagedResponse) {
(*resp).Data = append(resp.Data, r.Data...)
}
// SetResult sets the Resty response type of Volume
func (VolumesPagedResponse) SetResult(r *resty.Request) {
r.SetResult(VolumesPagedResponse{})
}
// ListVolumes lists Volumes
func (c *Client) ListVolumes(opts *ListOptions) ([]*Volume, error) {
response := VolumesPagedResponse{}
err := c.ListHelper(response, opts)
for _, el := range response.Data {
el.fixDates()
}
return response.Data, err
}
// fixDates converts JSON timestamps to Go time.Time values
func (v *Volume) fixDates() *Volume {
v.Created, _ = parseDates(v.CreatedStr)
v.Updated, _ = parseDates(v.UpdatedStr)
return v
}
// GetVolume gets the template with the provided ID
func (c *Client) GetVolume(id string) (*Volume, error) {
e, err := c.Volumes.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%s", e, id)
r, err := c.R().SetResult(&Volume{}).Get(e)
if err != nil {
return nil, err
}
return r.Result().(*Volume).fixDates(), nil
}
// AttachVolume attaches volume to linode instance
func (c *Client) AttachVolume(id int, options *VolumeAttachOptions) (bool, error) {
body := ""
if bodyData, err := json.Marshal(options); err == nil {
body = string(bodyData)
} else {
return false, err
}
e, err := c.Volumes.Endpoint()
if err != nil {
return false, err
}
e = fmt.Sprintf("%s/%d/attach", e, id)
resp, err := c.R().
SetHeader("Content-Type", "application/json").
SetBody(body).
Post(e)
return settleBoolResponseOrError(resp, err)
}
// CloneVolume clones a Linode instance
func (c *Client) CloneVolume(id int, label string) (*Volume, error) {
body := fmt.Sprintf("{\"label\":\"%s\"}", label)
e, err := c.Volumes.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d/clone", e, id)
req := c.R().SetResult(&Volume{})
resp, err := req.
SetHeader("Content-Type", "application/json").
SetBody(body).
Post(e)
if err != nil {
return nil, err
}
return resp.Result().(*Volume).fixDates(), nil
}
// DetachVolume detaches a Linode instance
func (c *Client) DetachVolume(id int) (bool, error) {
body := ""
e, err := c.Volumes.Endpoint()
if err != nil {
return false, err
}
e = fmt.Sprintf("%s/%d/detach", e, id)
resp, err := c.R().
SetHeader("Content-Type", "application/json").
SetBody(body).
Post(e)
return settleBoolResponseOrError(resp, err)
}
// ResizeVolume resizes an instance to new Linode type
func (c *Client) ResizeVolume(id int, size int) (bool, error) {
body := fmt.Sprintf("{\"size\":\"%d\"}", size)
e, err := c.Volumes.Endpoint()
if err != nil {
return false, err
}
e = fmt.Sprintf("%s/%d/resize", e, id)
resp, err := c.R().
SetHeader("Content-Type", "application/json").
SetBody(body).
Post(e)
return settleBoolResponseOrError(resp, err)
}