-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathoperator_policies.go
More file actions
177 lines (145 loc) · 4.46 KB
/
operator_policies.go
File metadata and controls
177 lines (145 loc) · 4.46 KB
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
package rabbithole
import (
"encoding/json"
"net/http"
"net/url"
"regexp"
)
// OperatorPolicy represents an operator policy.
type OperatorPolicy struct {
// Virtual host this policy is in.
Vhost string `json:"vhost"`
// Regular expression pattern used to match queues.
Pattern string `json:"pattern"`
// What this policy applies to: "queues".
ApplyTo string `json:"apply-to"`
Name string `json:"name"`
Priority int `json:"priority"`
// Additional arguments added to matching queues.
Definition PolicyDefinition `json:"definition"`
}
// HasCMQKeys returns true if this policy's definition contains CMQ keys.
func (p OperatorPolicy) HasCMQKeys() bool {
return p.Definition.HasCMQKeys()
}
// DoesMatchName returns true if this policy would apply to a queue with the given name and target.
func (p OperatorPolicy) DoesMatchName(vhost, name string, target PolicyTarget) bool {
if p.Vhost != vhost {
return false
}
if !targetsMatch(PolicyTarget(p.ApplyTo), target) {
return false
}
matched, err := regexp.MatchString(p.Pattern, name)
return err == nil && matched
}
//
// GET /api/operator-policies
//
// ListOperatorPolicies returns all operator policies (across all virtual hosts).
func (c *Client) ListOperatorPolicies() (rec []OperatorPolicy, err error) {
req, err := newGETRequest(c, "operator-policies")
if err != nil {
return nil, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return nil, err
}
return rec, nil
}
//
// GET /api/operator-policies/{vhost}
//
// ListOperatorPoliciesIn returns operator policies in a specific virtual host.
func (c *Client) ListOperatorPoliciesIn(vhost string) (rec []OperatorPolicy, err error) {
req, err := newGETRequest(c, "operator-policies/"+url.PathEscape(vhost))
if err != nil {
return nil, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return nil, err
}
return rec, nil
}
//
// GET /api/operator-policies/{vhost}/{name}
//
// GetOperatorPolicy returns an operator policy by name.
func (c *Client) GetOperatorPolicy(vhost, name string) (rec *OperatorPolicy, err error) {
req, err := newGETRequest(c, "operator-policies/"+url.PathEscape(vhost)+"/"+url.PathEscape(name))
if err != nil {
return nil, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return nil, err
}
return rec, nil
}
//
// PUT /api/operator-policies/{vhost}/{name}
//
// PutOperatorPolicy creates or updates an operator policy.
func (c *Client) PutOperatorPolicy(vhost string, name string, operatorPolicy OperatorPolicy) (res *http.Response, err error) {
body, err := json.Marshal(operatorPolicy)
if err != nil {
return nil, err
}
req, err := newRequestWithBody(c, "PUT", "operator-policies/"+url.PathEscape(vhost)+"/"+url.PathEscape(name), body)
if err != nil {
return nil, err
}
if res, err = executeRequest(c, req); err != nil {
return nil, err
}
return res, nil
}
//
// DELETE /api/operator-policies/{vhost}/{name}
//
// DeleteOperatorPolicy deletes an operator policy.
func (c *Client) DeleteOperatorPolicy(vhost, name string) (res *http.Response, err error) {
req, err := newRequestWithBody(c, "DELETE", "operator-policies/"+url.PathEscape(vhost)+"/"+url.PathEscape(name), nil)
if err != nil {
return nil, err
}
if res, err = executeRequest(c, req); err != nil {
return nil, err
}
return res, nil
}
// ListOperatorPoliciesForTarget returns operator policies in a virtual host filtered by their apply-to target.
func (c *Client) ListOperatorPoliciesForTarget(vhost string, target PolicyTarget) (rec []OperatorPolicy, err error) {
policies, err := c.ListOperatorPoliciesIn(vhost)
if err != nil {
return nil, err
}
for _, p := range policies {
if PolicyTarget(p.ApplyTo) == target {
rec = append(rec, p)
}
}
return rec, nil
}
// ListMatchingOperatorPolicies returns operator policies that would match a given entity name and target.
func (c *Client) ListMatchingOperatorPolicies(vhost, name string, target PolicyTarget) (rec []OperatorPolicy, err error) {
policies, err := c.ListOperatorPoliciesIn(vhost)
if err != nil {
return nil, err
}
for _, p := range policies {
if p.DoesMatchName(vhost, name, target) {
rec = append(rec, p)
}
}
return rec, nil
}
// DeleteOperatorPoliciesIn deletes multiple operator policies in a virtual host by name.
func (c *Client) DeleteOperatorPoliciesIn(vhost string, names []string) error {
for _, name := range names {
_, err := c.DeleteOperatorPolicy(vhost, name)
if err != nil {
return err
}
}
return nil
}