1package rabbithole
2
3import (
4	"encoding/json"
5	"net/http"
6)
7
8//
9// GET /api/vhosts
10//
11
12// Example response:
13
14// [
15//   {
16//     "message_stats": {
17//       "publish": 78,
18//       "publish_details": {
19//         "rate": 0
20//       }
21//     },
22//     "messages": 0,
23//     "messages_details": {
24//       "rate": 0
25//     },
26//     "messages_ready": 0,
27//     "messages_ready_details": {
28//       "rate": 0
29//     },
30//     "messages_unacknowledged": 0,
31//     "messages_unacknowledged_details": {
32//       "rate": 0
33//     },
34//     "recv_oct": 16653,
35//     "recv_oct_details": {
36//       "rate": 0
37//     },
38//     "send_oct": 40495,
39//     "send_oct_details": {
40//       "rate": 0
41//     },
42//     "name": "\/",
43//     "tracing": false
44//   },
45//   {
46//     "name": "29dd51888b834698a8b5bc3e7f8623aa1c9671f5",
47//     "tracing": false
48//   }
49// ]
50
51type VhostInfo struct {
52	// Virtual host name
53	Name string `json:"name"`
54	// True if tracing is enabled for this virtual host
55	Tracing bool `json:"tracing"`
56
57	// Total number of messages in queues of this virtual host
58	Messages        int         `json:"messages"`
59	MessagesDetails RateDetails `json:"messages_details"`
60
61	// Total number of messages ready to be delivered in queues of this virtual host
62	MessagesReady        int         `json:"messages_ready"`
63	MessagesReadyDetails RateDetails `json:"messages_ready_details"`
64
65	// Total number of messages pending acknowledgement from consumers in this virtual host
66	MessagesUnacknowledged        int         `json:"messages_unacknowledged"`
67	MessagesUnacknowledgedDetails RateDetails `json:"messages_unacknowledged_details"`
68
69	// Octets received
70	RecvOct uint64 `json:"recv_oct"`
71	// Octets sent
72	SendOct        uint64      `json:"send_oct"`
73	RecvCount      uint64      `json:"recv_cnt"`
74	SendCount      uint64      `json:"send_cnt"`
75	SendPending    uint64      `json:"send_pend"`
76	RecvOctDetails RateDetails `json:"recv_oct_details"`
77	SendOctDetails RateDetails `json:"send_oct_details"`
78}
79
80// Returns a list of virtual hosts.
81func (c *Client) ListVhosts() (rec []VhostInfo, err error) {
82	req, err := newGETRequest(c, "vhosts")
83	if err != nil {
84		return []VhostInfo{}, err
85	}
86
87	if err = executeAndParseRequest(c, req, &rec); err != nil {
88		return []VhostInfo{}, err
89	}
90
91	return rec, nil
92}
93
94//
95// GET /api/vhosts/{name}
96//
97
98// Returns information about a specific virtual host.
99func (c *Client) GetVhost(vhostname string) (rec *VhostInfo, err error) {
100	req, err := newGETRequest(c, "vhosts/"+PathEscape(vhostname))
101	if err != nil {
102		return nil, err
103	}
104
105	if err = executeAndParseRequest(c, req, &rec); err != nil {
106		return nil, err
107	}
108
109	return rec, nil
110}
111
112//
113// PUT /api/vhosts/{name}
114//
115
116// Settings used to create or modify virtual hosts.
117type VhostSettings struct {
118	// True if tracing should be enabled.
119	Tracing bool `json:"tracing"`
120}
121
122// Creates or updates a virtual host.
123func (c *Client) PutVhost(vhostname string, settings VhostSettings) (res *http.Response, err error) {
124	body, err := json.Marshal(settings)
125	if err != nil {
126		return nil, err
127	}
128
129	req, err := newRequestWithBody(c, "PUT", "vhosts/"+PathEscape(vhostname), body)
130	if err != nil {
131		return nil, err
132	}
133
134	res, err = executeRequest(c, req)
135	if err != nil {
136		return nil, err
137	}
138
139	return res, nil
140}
141
142//
143// DELETE /api/vhosts/{name}
144//
145
146// Deletes a virtual host.
147func (c *Client) DeleteVhost(vhostname string) (res *http.Response, err error) {
148	req, err := newRequestWithBody(c, "DELETE", "vhosts/"+PathEscape(vhostname), nil)
149	if err != nil {
150		return nil, err
151	}
152
153	res, err = executeRequest(c, req)
154	if err != nil {
155		return nil, err
156	}
157
158	return res, nil
159}
160