2015-11-19 05:45:05 +00:00
|
|
|
package rabbithole
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Policy definition: additional arguments
|
|
|
|
// added to the entities (queues, exchanges or both)
|
|
|
|
// that match a policy.
|
|
|
|
type PolicyDefinition map[string]interface{}
|
|
|
|
|
|
|
|
type NodeNames []string
|
|
|
|
|
|
|
|
// Represents a configured policy.
|
|
|
|
type Policy struct {
|
|
|
|
// Virtual host this policy is in.
|
|
|
|
Vhost string `json:"vhost"`
|
|
|
|
// Regular expression pattern used to match queues and exchanges,
|
|
|
|
// , e.g. "^ha\..+"
|
|
|
|
Pattern string `json:"pattern"`
|
|
|
|
// What this policy applies to: "queues", "exchanges", etc.
|
|
|
|
ApplyTo string `json:"apply-to"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Priority int `json:"priority"`
|
|
|
|
// Additional arguments added to the entities (queues,
|
|
|
|
// exchanges or both) that match a policy
|
|
|
|
Definition PolicyDefinition `json:"definition"`
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// GET /api/policies
|
|
|
|
//
|
|
|
|
|
2019-04-13 07:44:06 +00:00
|
|
|
// Return all policies (across all virtual hosts).
|
2015-11-19 05:45:05 +00:00
|
|
|
func (c *Client) ListPolicies() (rec []Policy, err error) {
|
|
|
|
req, err := newGETRequest(c, "policies")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = executeAndParseRequest(c, req, &rec); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// GET /api/policies/{vhost}
|
|
|
|
//
|
|
|
|
|
2019-04-13 07:44:06 +00:00
|
|
|
// Returns policies in a specific virtual host.
|
2015-11-19 05:45:05 +00:00
|
|
|
func (c *Client) ListPoliciesIn(vhost string) (rec []Policy, err error) {
|
2017-03-31 00:03:13 +00:00
|
|
|
req, err := newGETRequest(c, "policies/"+PathEscape(vhost))
|
2015-11-19 05:45:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = executeAndParseRequest(c, req, &rec); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// GET /api/policies/{vhost}/{name}
|
|
|
|
//
|
|
|
|
|
2019-04-13 07:44:06 +00:00
|
|
|
// Returns individual policy in virtual host.
|
2015-11-19 05:45:05 +00:00
|
|
|
func (c *Client) GetPolicy(vhost, name string) (rec *Policy, err error) {
|
2017-03-31 00:03:13 +00:00
|
|
|
req, err := newGETRequest(c, "policies/"+PathEscape(vhost)+"/"+PathEscape(name))
|
2015-11-19 05:45:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = executeAndParseRequest(c, req, &rec); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// PUT /api/policies/{vhost}/{name}
|
|
|
|
//
|
|
|
|
|
|
|
|
// Updates a policy.
|
|
|
|
func (c *Client) PutPolicy(vhost string, name string, policy Policy) (res *http.Response, err error) {
|
|
|
|
body, err := json.Marshal(policy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:03:13 +00:00
|
|
|
req, err := newRequestWithBody(c, "PUT", "policies/"+PathEscape(vhost)+"/"+PathEscape(name), body)
|
2015-11-19 05:45:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err = executeRequest(c, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// DELETE /api/policies/{vhost}/{name}
|
|
|
|
//
|
|
|
|
|
|
|
|
// Deletes a policy.
|
|
|
|
func (c *Client) DeletePolicy(vhost, name string) (res *http.Response, err error) {
|
2017-03-31 00:03:13 +00:00
|
|
|
req, err := newRequestWithBody(c, "DELETE", "policies/"+PathEscape(vhost)+"/"+PathEscape(name), nil)
|
2015-11-19 05:45:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err = executeRequest(c, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|