forked from amarkwalder/docker-volume-glusterfs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglusterrestclient.go
187 lines (158 loc) · 4.08 KB
/
glusterrestclient.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
httpclient "github.com/ddliu/go-httpclient"
)
//Model
const (
volumesPath = "/api/1.0/volumes"
volumeGetPath = "/api/1.0/volume/%s"
volumeCreatePath = "/api/1.0/volume/%s"
volumeRemovePath = "/api/1.0/volume/%s"
volumeStopPath = "/api/1.0/volume/%s/stop"
)
type peer struct {
ID string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
}
type rvolume struct {
Name string `json:"name"`
UUID string `json:"uuid"`
Type string `json:"type"`
Status string `json:"status"`
NumBricks int `json:"num_bricks"`
Distribute int `json:"distribute"`
Stripe int `json:"stripe"`
Replica int `json:"replica"`
Transport string `json:"transport"`
}
type response struct {
Ok bool `json:"ok"`
Err string `json:"error,omitempty"`
}
type peerResponse struct {
Data []peer `json:"data,omitempty"`
response
}
type volumesResponse struct {
Data []rvolume `json:"data,omitempty"`
response
}
func responseCheck(resp *http.Response) error {
var p response
err := json.NewDecoder(resp.Body).Decode(&p)
if err != nil {
return err
}
if !p.Ok {
return fmt.Errorf(p.Err)
}
return nil
}
func responseCheckHTTPClient(resp *httpclient.Response) error {
var p response
err := json.NewDecoder(resp.Body).Decode(&p)
if err != nil {
return err
}
if !p.Ok {
return fmt.Errorf(p.Err)
}
return nil
}
//GetAddress returns the full url to contact
func (g GlusterRestClient) GetAddress(server string, url string) string {
return fmt.Sprintf("%s%s:%s%s", g.urlPrefix, server, strconv.Itoa(g.port), url)
}
//Get tries to HTTP GET on all servers
func (g GlusterRestClient) Get(url string) (*http.Response, error) {
for _, server := range g.servers {
res, err := http.Get(g.GetAddress(server, url))
if err != nil {
continue
}
return res, err
}
return nil, fmt.Errorf("Get(): could not diag with any server")
}
//PostForm tries to POST with application/x-www-form-urlencoded format
func (g GlusterRestClient) PostForm(url string, params url.Values) (*http.Response, error) {
for _, server := range g.servers {
address := g.GetAddress(server, url)
res, err := http.PostForm(address, params)
if err != nil {
continue
}
return res, err
}
return nil, fmt.Errorf("PostForm(): could not diag with any server")
}
//Delete tries to DELETE with application/x-www-form-urlencoded format
func (g GlusterRestClient) Delete(url string, params url.Values) (*httpclient.Response, error) {
for _, server := range g.servers {
client := httpclient.WithHeader("Content-Type", "application/x-www-form-urlencoded")
body := strings.NewReader(params.Encode())
res, err := client.Do("DELETE", g.GetAddress(server, url), nil, body)
if err != nil {
continue
}
return res, nil
}
return nil, fmt.Errorf("Delete(): could not diag with any server")
}
func (g GlusterRestClient) listVolumes() ([]string, error) {
res, err := g.Get(volumesPath)
if err != nil {
return nil, err
}
defer res.Body.Close()
var d volumesResponse
if err := json.NewDecoder(res.Body).Decode(&d); err != nil {
return nil, err
}
if !d.Ok {
return nil, fmt.Errorf(d.Err)
}
var ret []string
for _, volume := range d.Data {
ret = append(ret, volume.Name)
}
return ret, nil
}
func (g GlusterRestClient) volumeExists(name string) (bool, error) {
res, err := g.Get(fmt.Sprintf(volumeGetPath, name))
if err != nil {
return false, err
}
defer res.Body.Close()
var d volumesResponse
if err := json.NewDecoder(res.Body).Decode(&d); err != nil {
return false, err
}
if !d.Ok {
return false, nil
}
return true, nil
}
//GlusterRestClient struct
type GlusterRestClient struct {
servers []string
urlPrefix string
port int
base string
}
//NewGlusterRestClient constructor
func NewGlusterRestClient(servers []string, login, password string, port int, base string) GlusterRestClient {
client := GlusterRestClient{
servers: servers,
urlPrefix: fmt.Sprintf("http://%s:%s@", login, password),
port: port,
base: base}
return client
}