-
Notifications
You must be signed in to change notification settings - Fork 18
/
client.go
88 lines (76 loc) · 2.26 KB
/
client.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
package rabbit
import (
"context"
"encoding/base64"
"net/http"
"net/url"
"time"
"github.com/circleci/ex/config/secret"
"github.com/circleci/ex/httpclient"
)
type Client struct {
client *httpclient.Client
}
type VHostInfo struct {
Name string `json:"name"`
Description string `json:"description"`
}
func NewClient(baseURL, username string, password secret.String) *Client {
return &Client{client: httpclient.New(httpclient.Config{
Name: "rabbitmq",
BaseURL: baseURL,
AuthHeader: "Authorization",
AuthToken: "Basic " + basicAuth(username, password),
Timeout: 30 * time.Second,
})}
}
func basicAuth(username string, password secret.String) string {
auth := username + ":" + password.Value()
return base64.StdEncoding.EncodeToString([]byte(auth))
}
func (c *Client) ListVHosts(ctx context.Context) (info []VHostInfo, err error) {
err = c.client.Call(ctx, httpclient.NewRequest("GET", "/api/vhosts",
httpclient.JSONDecoder(&info),
))
if err != nil {
return nil, err
}
return info, nil
}
func (c *Client) DeleteVHost(ctx context.Context, name string) error {
err := c.client.Call(ctx, httpclient.NewRequest("DELETE", "/api/vhosts/%s",
httpclient.RouteParams(url.PathEscape(name)),
httpclient.Timeout(5*time.Second),
))
if httpclient.HasStatusCode(err, http.StatusNotFound) {
return nil
}
if httpclient.IsNoContent(err) {
return nil
}
return err
}
type VHostSettings struct {
Description string `json:"description"`
Tags []string `json:"tags"`
Tracing bool `json:"tracing"`
}
func (c *Client) PutVHost(ctx context.Context, name string, settings VHostSettings) error {
return c.client.Call(ctx, httpclient.NewRequest("PUT", "/api/vhosts/%s",
httpclient.RouteParams(url.PathEscape(name)),
httpclient.Timeout(5*time.Second),
httpclient.Body(settings),
))
}
type Permissions struct {
Configure string `json:"configure"`
Write string `json:"write"`
Read string `json:"read"`
}
func (c *Client) UpdatePermissionsIn(ctx context.Context, vhost, username string, p Permissions) error {
return c.client.Call(ctx, httpclient.NewRequest("PUT", "/api/permissions/%s/%s",
httpclient.RouteParams(url.PathEscape(vhost), url.PathEscape(username)),
httpclient.Timeout(5*time.Second),
httpclient.Body(p),
))
}