This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
ip_role.go
212 lines (168 loc) · 5.41 KB
/
ip_role.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
208
209
210
211
212
package twiliogo
import (
"encoding/json"
"net/url"
)
// IPRole is a IP Messaging Role resource.
type IPRole struct {
Sid string `json:"sid"`
AccountSid string `json:"account_sid"`
ServiceSid string `json:"service_sid"`
FriendlyName string `json:"friendly_name"`
Type string `json:"type"`
Permissions []string `json:"permissions"`
DateCreated string `json:"date_created"`
DateUpdated string `json:"date_updated"`
URL string `json:"url"`
}
// IPRoleList gives the results for querying the set of roles. Returns the first page
// by default.
type IPRoleList struct {
Client *TwilioIPMessagingClient
Roles []IPRole `json:"roles"`
Meta Meta `json:"meta"`
}
// Permissions allowed for IP Roles.
const (
PermissionCreateChannel = "createChannel"
PermissionJoinChannel = "joinChannel"
PermissionDestroyChannel = "destroyChannel"
PermissionInviteMember = "inviteMember"
PermissionRemoveMember = "removeMember"
PermissionEditChannelName = "editChannelName"
PermissionEditChannelAttributes = "editChannelAttributes"
PermissionAddMember = "addMember"
PermissionEditAnyMessage = "editAnyMessage"
PermissionDeleteAnyMessage = "deleteAnyMessage"
PermissionSendMessage = "sendMessage"
PermissionLeaveChannel = "leaveChannel"
PermissionEditOwnMessage = "editOwnMessage"
PermissionDeleteOwnMessage = "deleteOwnMessage"
)
// NewIPRole creates a new IP Messaging Role.
// kind should be "channel" or "service".
// permissions should be a subset of the permissions consts above.
func NewIPRole(client *TwilioIPMessagingClient, serviceSid string, friendlyName string, kind string, permissions []string) (*IPRole, error) {
var role *IPRole
params := url.Values{}
params.Set("FriendlyName", friendlyName)
params.Set("Type", kind)
if permissions != nil {
for _, p := range permissions {
params.Add("Permission", p)
}
}
res, err := client.post(params, "/Services/"+serviceSid+"/Roles.json")
if err != nil {
return role, err
}
role = new(IPRole)
err = json.Unmarshal(res, role)
return role, err
}
// GetIPRole returns information on the specified role.
func GetIPRole(client *TwilioIPMessagingClient, serviceSid, sid string) (*IPRole, error) {
var role *IPRole
res, err := client.get(url.Values{}, "/Services/"+serviceSid+"/Roles/"+sid+".json")
if err != nil {
return nil, err
}
role = new(IPRole)
err = json.Unmarshal(res, role)
return role, err
}
// DeleteIPRole deletes the given IP Role.
func DeleteIPRole(client *TwilioIPMessagingClient, serviceSid, sid string) error {
return client.delete("/Services/" + serviceSid + "/Roles/" + sid)
}
// UpdateIPRole updates an existing IP Messaging Role.
func UpdateIPRole(client *TwilioIPMessagingClient, serviceSid string, sid string, friendlyName string, kind string, permissions []string) (*IPRole, error) {
var role *IPRole
params := url.Values{}
params.Set("FriendlyName", friendlyName)
params.Set("Type", kind)
if permissions != nil {
for _, p := range permissions {
params.Add("Permission", p)
}
}
res, err := client.post(params, "/Services/"+serviceSid+"/Roles/"+sid+".json")
if err != nil {
return role, err
}
role = new(IPRole)
err = json.Unmarshal(res, role)
return role, err
}
// ListIPRoles returns the first page of roles.
func ListIPRoles(client *TwilioIPMessagingClient, serviceSid string) (*IPRoleList, error) {
var roleList *IPRoleList
body, err := client.get(nil, "/Services/"+serviceSid+"/Roles.json")
if err != nil {
return roleList, err
}
roleList = new(IPRoleList)
roleList.Client = client
err = json.Unmarshal(body, roleList)
return roleList, err
}
// GetRoles returns the current page of roles.
func (s *IPRoleList) GetRoles() []IPRole {
return s.Roles
}
// GetAllRoles returns all of the roles from all of the pages (from here forward).
func (s *IPRoleList) GetAllRoles() ([]IPRole, error) {
roles := s.Roles
t := s
for t.HasNextPage() {
var err error
t, err = t.NextPage()
if err != nil {
return nil, err
}
roles = append(roles, t.Roles...)
}
return roles, nil
}
// HasNextPage returns whether or not there is a next page of roles.
func (s *IPRoleList) HasNextPage() bool {
return s.Meta.NextPageUri != ""
}
// NextPage returns the next page of roles.
func (s *IPRoleList) NextPage() (*IPRoleList, error) {
if !s.HasNextPage() {
return nil, Error{"No next page"}
}
return s.getPage(s.Meta.NextPageUri)
}
// HasPreviousPage indicates whether or not there is a previous page of results.
func (s *IPRoleList) HasPreviousPage() bool {
return s.Meta.PreviousPageUri != ""
}
// PreviousPage returns the previous page of roles.
func (s *IPRoleList) PreviousPage() (*IPRoleList, error) {
if !s.HasPreviousPage() {
return nil, Error{"No previous page"}
}
return s.getPage(s.Meta.NextPageUri)
}
// FirstPage returns the first page of roles.
func (s *IPRoleList) FirstPage() (*IPRoleList, error) {
return s.getPage(s.Meta.FirstPageUri)
}
// LastPage returns the last page of roles.
func (s *IPRoleList) LastPage() (*IPRoleList, error) {
return s.getPage(s.Meta.LastPageUri)
}
func (s *IPRoleList) getPage(uri string) (*IPRoleList, error) {
var roleList *IPRoleList
client := s.Client
body, err := client.get(nil, uri)
if err != nil {
return roleList, err
}
roleList = new(IPRoleList)
roleList.Client = client
err = json.Unmarshal(body, roleList)
return roleList, err
}