-
Notifications
You must be signed in to change notification settings - Fork 108
/
vhosts.go
207 lines (177 loc) · 5.05 KB
/
vhosts.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
package rabbithole
import (
"encoding/json"
"net/http"
"net/url"
"strconv"
"strings"
)
//
// GET /api/vhosts
//
// Example response:
// [
// {
// "message_stats": {
// "publish": 78,
// "publish_details": {
// "rate": 0
// }
// },
// "messages": 0,
// "messages_details": {
// "rate": 0
// },
// "messages_ready": 0,
// "messages_ready_details": {
// "rate": 0
// },
// "messages_unacknowledged": 0,
// "messages_unacknowledged_details": {
// "rate": 0
// },
// "recv_oct": 16653,
// "recv_oct_details": {
// "rate": 0
// },
// "send_oct": 40495,
// "send_oct_details": {
// "rate": 0
// },
// "name": "\/",
// "description": "myvhost",
// "tags": "production,eu-west-1",
// "tracing": false
// },
// {
// "name": "29dd51888b834698a8b5bc3e7f8623aa1c9671f5",
// "tracing": false
// }
// ]
// VhostInfo represents a virtual host, its properties and key metrics.
type VhostInfo struct {
// Virtual host name
Name string `json:"name"`
// Virtual host description
Description string `json:"description"`
// Virtual host tags
Tags VhostTags `json:"tags"`
// Type of queue to create in virtual host when unspecified
DefaultQueueType string `json:"default_queue_type"`
// True if tracing is enabled for this virtual host
Tracing bool `json:"tracing"`
// Total number of messages in queues of this virtual host
Messages int `json:"messages"`
MessagesDetails RateDetails `json:"messages_details"`
// Total number of messages ready to be delivered in queues of this virtual host
MessagesReady int `json:"messages_ready"`
MessagesReadyDetails RateDetails `json:"messages_ready_details"`
// Total number of messages pending acknowledgement from consumers in this virtual host
MessagesUnacknowledged int `json:"messages_unacknowledged"`
MessagesUnacknowledgedDetails RateDetails `json:"messages_unacknowledged_details"`
// Octets received
RecvOct uint64 `json:"recv_oct"`
// Octets sent
SendOct uint64 `json:"send_oct"`
RecvCount uint64 `json:"recv_cnt"`
SendCount uint64 `json:"send_cnt"`
SendPending uint64 `json:"send_pend"`
RecvOctDetails RateDetails `json:"recv_oct_details"`
SendOctDetails RateDetails `json:"send_oct_details"`
// Cluster State
ClusterState map[string]string `json:"cluster_state"`
}
type VhostTags []string
// MarshalJSON can marshal an array of strings or a comma-separated list in a string
func (d VhostTags) MarshalJSON() ([]byte, error) {
return json.Marshal(strings.Join(d, ","))
}
// UnmarshalJSON can unmarshal an array of strings or a comma-separated list in a string
func (d *VhostTags) UnmarshalJSON(b []byte) error {
// the value is a comma-separated string
t, _ := strconv.Unquote(string(b))
if b[0] == '"' {
quotedTags := strings.Split(t, ",")
var tags []string
for _, qt := range quotedTags {
tags = append(tags, qt)
}
*d = tags
return nil
}
// the value is an array
var ary []string
if err := json.Unmarshal(b, &ary); err != nil {
return err
}
*d = ary
return nil
}
// ListVhosts returns a list of virtual hosts.
func (c *Client) ListVhosts() (rec []VhostInfo, err error) {
req, err := newGETRequest(c, "vhosts")
if err != nil {
return []VhostInfo{}, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return []VhostInfo{}, err
}
return rec, nil
}
//
// GET /api/vhosts/{name}
//
// GetVhost returns information about a specific virtual host.
func (c *Client) GetVhost(vhostname string) (rec *VhostInfo, err error) {
req, err := newGETRequest(c, "vhosts/"+url.PathEscape(vhostname))
if err != nil {
return nil, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return nil, err
}
return rec, nil
}
//
// PUT /api/vhosts/{name}
//
// VhostSettings are properties used to create or modify virtual hosts.
type VhostSettings struct {
// Virtual host description
Description string `json:"description"`
// Virtual host tags
Tags VhostTags `json:"tags"`
// Type of queue to create in virtual host when unspecified
DefaultQueueType string `json:"default_queue_type,omitempty"`
// True if tracing should be enabled.
Tracing bool `json:"tracing"`
}
// PutVhost creates or updates a virtual host.
func (c *Client) PutVhost(vhostname string, settings VhostSettings) (res *http.Response, err error) {
body, err := json.Marshal(settings)
if err != nil {
return nil, err
}
req, err := newRequestWithBody(c, "PUT", "vhosts/"+url.PathEscape(vhostname), body)
if err != nil {
return nil, err
}
if res, err = executeRequest(c, req); err != nil {
return nil, err
}
return res, nil
}
//
// DELETE /api/vhosts/{name}
//
// DeleteVhost deletes a virtual host.
func (c *Client) DeleteVhost(vhostname string) (res *http.Response, err error) {
req, err := newRequestWithBody(c, "DELETE", "vhosts/"+url.PathEscape(vhostname), nil)
if err != nil {
return nil, err
}
if res, err = executeRequest(c, req); err != nil {
return nil, err
}
return res, nil
}