-
Notifications
You must be signed in to change notification settings - Fork 108
/
topic_permissions.go
136 lines (108 loc) · 3.56 KB
/
topic_permissions.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
package rabbithole
import (
"encoding/json"
"net/http"
"net/url"
)
//
// GET /api/topic-permissions
//
// TopicPermissionInfo represents a user's permissions on a topic.
type TopicPermissionInfo struct {
User string `json:"user"`
Vhost string `json:"vhost"`
// Configuration topic-permissions
Exchange string `json:"exchange"`
// Write topic-permissions
Write string `json:"write"`
// Read topic-permissions
Read string `json:"read"`
}
// ListTopicPermissions returns topic-permissions for all users and virtual hosts.
func (c *Client) ListTopicPermissions() (rec []TopicPermissionInfo, err error) {
req, err := newGETRequest(c, "topic-permissions/")
if err != nil {
return []TopicPermissionInfo{}, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return []TopicPermissionInfo{}, err
}
return rec, nil
}
//
// GET /api/users/{user}/topic-permissions
//
// ListTopicPermissionsOf returns topic-permissions of a specific user.
func (c *Client) ListTopicPermissionsOf(username string) (rec []TopicPermissionInfo, err error) {
req, err := newGETRequest(c, "users/"+url.PathEscape(username)+"/topic-permissions")
if err != nil {
return []TopicPermissionInfo{}, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return []TopicPermissionInfo{}, err
}
return rec, nil
}
//
// GET /api/topic-permissions/{vhost}/{user}
//
// GetTopicPermissionsIn returns topic-permissions of user in virtual host.
func (c *Client) GetTopicPermissionsIn(vhost, username string) (rec []TopicPermissionInfo, err error) {
req, err := newGETRequest(c, "topic-permissions/"+url.PathEscape(vhost)+"/"+url.PathEscape(username))
if err != nil {
return []TopicPermissionInfo{}, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return []TopicPermissionInfo{}, err
}
return rec, nil
}
//
// PUT /api/topic-permissions/{vhost}/{user}
//
// TopicPermissions represents a user's permissions on a topic.
type TopicPermissions struct {
Exchange string `json:"exchange"`
Write string `json:"write"`
Read string `json:"read"`
}
// UpdateTopicPermissionsIn updates topic-permissions of user in virtual host.
func (c *Client) UpdateTopicPermissionsIn(vhost, username string, TopicPermissions TopicPermissions) (res *http.Response, err error) {
body, err := json.Marshal(TopicPermissions)
if err != nil {
return nil, err
}
req, err := newRequestWithBody(c, "PUT", "topic-permissions/"+url.PathEscape(vhost)+"/"+url.PathEscape(username), body)
if err != nil {
return nil, err
}
if res, err = executeRequest(c, req); err != nil {
return nil, err
}
return res, nil
}
//
// DELETE /api/topic-permissions/{vhost}/{user}
//
// ClearTopicPermissionsIn clears (deletes) topic-permissions of user in virtual host.
func (c *Client) ClearTopicPermissionsIn(vhost, username string) (res *http.Response, err error) {
req, err := newRequestWithBody(c, "DELETE", "topic-permissions/"+url.PathEscape(vhost)+"/"+url.PathEscape(username), nil)
if err != nil {
return nil, err
}
if res, err = executeRequest(c, req); err != nil {
return nil, err
}
return res, nil
}
// DeleteTopicPermissionsIn delete topic-permissions of exchange for user in virtual host.
func (c *Client) DeleteTopicPermissionsIn(vhost, username string, exchange string) (res *http.Response, err error) {
req, err := newRequestWithBody(c, "DELETE", "topic-permissions/"+url.PathEscape(vhost)+"/"+url.PathEscape(username)+"/"+url.PathEscape(exchange), nil)
if err != nil {
return nil, err
}
if res, err = executeRequest(c, req); err != nil {
return nil, err
}
return res, nil
}