This repository was archived by the owner on Mar 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathvolume.go
201 lines (176 loc) · 6.56 KB
/
volume.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
package restclient
import (
"fmt"
"net/http"
"net/url"
"github.com/gluster/glusterd2/pkg/api"
)
// metadataFilter is a filter type
type metadataFilter uint32
// GetVolumes Filter Types
const (
noKeyAndValue metadataFilter = iota
onlyKey
onlyValue
keyAndValue
)
// VolumeCreate creates Gluster Volume
func (c *Client) VolumeCreate(req api.VolCreateReq) (api.VolumeCreateResp, error) {
var vol api.VolumeCreateResp
err := c.post("/v1/volumes", req, http.StatusCreated, &vol)
return vol, err
}
// getFilterType return the filter type for volume list/info
func getFilterType(filterParams map[string]string) metadataFilter {
_, key := filterParams["key"]
_, value := filterParams["value"]
if key && !value {
return onlyKey
} else if value && !key {
return onlyValue
} else if value && key {
return keyAndValue
}
return noKeyAndValue
}
// getQueryString returns the query string for filtering volumes
func getQueryString(filterParam map[string]string) string {
filterType := getFilterType(filterParam)
var queryString string
switch filterType {
case onlyKey:
queryString = fmt.Sprintf("?key=%s", url.QueryEscape(filterParam["key"]))
case onlyValue:
queryString = fmt.Sprintf("?value=%s", url.QueryEscape(filterParam["value"]))
case keyAndValue:
queryString = fmt.Sprintf("?key=%s&value=%s", url.QueryEscape(filterParam["key"]), url.QueryEscape(filterParam["value"]))
}
return queryString
}
// Volumes returns list of all volumes
func (c *Client) Volumes(volname string, filterParams ...map[string]string) (api.VolumeListResp, error) {
if volname == "" {
var vols api.VolumeListResp
var queryString string
if len(filterParams) > 0 {
queryString = getQueryString(filterParams[0])
}
url := fmt.Sprintf("/v1/volumes%s", queryString)
err := c.get(url, nil, http.StatusOK, &vols)
return vols, err
}
var vol api.VolumeGetResp
url := fmt.Sprintf("/v1/volumes/%s", volname)
err := c.get(url, nil, http.StatusOK, &vol)
return []api.VolumeGetResp{vol}, err
}
// BricksStatus returns the status of bricks that form a Gluster volume
func (c *Client) BricksStatus(volname string) (api.BricksStatusResp, error) {
url := fmt.Sprintf("/v1/volumes/%s/bricks", volname)
var resp api.BricksStatusResp
err := c.get(url, nil, http.StatusOK, &resp)
return resp, err
}
// VolumeStatus returns the status of a Gluster volume
func (c *Client) VolumeStatus(volname string) (api.VolumeStatusResp, error) {
url := fmt.Sprintf("/v1/volumes/%s/status", volname)
var volStatus api.VolumeStatusResp
err := c.get(url, nil, http.StatusOK, &volStatus)
return volStatus, err
}
// VolumeStart starts a Gluster Volume
func (c *Client) VolumeStart(volname string, force bool) error {
req := api.VolumeStartReq{
ForceStartBricks: force,
}
url := fmt.Sprintf("/v1/volumes/%s/start", volname)
return c.post(url, req, http.StatusOK, nil)
}
// VolumeStop stops a Gluster Volume
func (c *Client) VolumeStop(volname string) error {
url := fmt.Sprintf("/v1/volumes/%s/stop", volname)
return c.post(url, nil, http.StatusOK, nil)
}
// VolumeDelete deletes a Gluster Volume
func (c *Client) VolumeDelete(volname string) error {
url := fmt.Sprintf("/v1/volumes/%s", volname)
return c.del(url, nil, http.StatusNoContent, nil)
}
// VolumeSet sets an option for a Gluster Volume
func (c *Client) VolumeSet(volname string, req api.VolOptionReq) error {
url := fmt.Sprintf("/v1/volumes/%s/options", volname)
err := c.post(url, req, http.StatusOK, nil)
return err
}
// ClusterOptionSet sets cluster level options
func (c *Client) ClusterOptionSet(req api.ClusterOptionReq) error {
url := fmt.Sprintf("/v1/cluster/options")
return c.post(url, req, http.StatusOK, nil)
}
// VolumeGet gets volume options for a Gluster Volume
func (c *Client) VolumeGet(volname string, optname string) (api.VolumeOptionsGetResp, error) {
if optname == "all" {
var opts api.VolumeOptionsGetResp
url := fmt.Sprintf("/v1/volumes/%s/options", volname)
err := c.get(url, nil, http.StatusOK, &opts)
return opts, err
}
var opt api.VolumeOptionGetResp
url := fmt.Sprintf("/v1/volumes/%s/options/%s", volname, optname)
err := c.get(url, nil, http.StatusOK, &opt)
return []api.VolumeOptionGetResp{opt}, err
}
// VolumeExpand expands a Gluster Volume
func (c *Client) VolumeExpand(volname string, req api.VolExpandReq) (api.VolumeExpandResp, error) {
var vol api.VolumeExpandResp
url := fmt.Sprintf("/v1/volumes/%s/expand", volname)
err := c.post(url, req, http.StatusOK, &vol)
return vol, err
}
// ReplaceBrick replaces the old brick in volume with a new one
func (c *Client) ReplaceBrick(volname string, req api.ReplaceBrickReq) (api.ReplaceBrickResp, error) {
var resp api.ReplaceBrickResp
url := fmt.Sprintf("/v1/volumes/%s/replacebrick", volname)
err := c.post(url, req, http.StatusOK, &resp)
return resp, err
}
// VolumeStatedump takes statedump of various daemons
func (c *Client) VolumeStatedump(volname string, req api.VolStatedumpReq) error {
url := fmt.Sprintf("/v1/volumes/%s/statedump", volname)
return c.post(url, req, http.StatusOK, nil)
}
// OptionGroupCreate creates a new option group
func (c *Client) OptionGroupCreate(req api.OptionGroupReq) error {
return c.post("/v1/volumes/options-group", req, http.StatusOK, nil)
}
// OptionGroupList returns a list of all option groups
func (c *Client) OptionGroupList() (api.OptionGroupListResp, error) {
var l api.OptionGroupListResp
url := fmt.Sprintf("/v1/volumes/options-group")
err := c.get(url, nil, http.StatusOK, &l)
return l, err
}
// OptionGroupDelete deletes the specified option group
func (c *Client) OptionGroupDelete(group string) error {
url := fmt.Sprintf("/v1/volumes/options-group/%s", group)
return c.del(url, nil, http.StatusNoContent, nil)
}
// EditVolume edits the specified keys in volinfo of a volume
func (c *Client) EditVolume(volname string, req api.VolEditReq) (api.VolumeEditResp, error) {
var resp api.VolumeEditResp
url := fmt.Sprintf("/v1/volumes/%s/edit", volname)
err := c.post(url, req, http.StatusOK, &resp)
return resp, err
}
// VolumeReset resets volume options to their default values
func (c *Client) VolumeReset(volname string, req api.VolOptionResetReq) error {
url := fmt.Sprintf("/v1/volumes/%s/options", volname)
return c.del(url, req, http.StatusOK, nil)
}
//VolumeProfileInfo retrieves the stats about different file operations performed on a volume
func (c *Client) VolumeProfileInfo(volname string, option string) ([]api.BrickProfileInfo, error) {
var volumeProfileInfo []api.BrickProfileInfo
url := fmt.Sprintf("/v1/volumes/%s/profile/%s", volname, option)
err := c.get(url, nil, http.StatusOK, &volumeProfileInfo)
return volumeProfileInfo, err
}