forked from spaceapegames/go-wavefront
-
Notifications
You must be signed in to change notification settings - Fork 12
/
role.go
150 lines (130 loc) · 3.68 KB
/
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
package wavefront
import (
"fmt"
)
type Role struct {
ID string `json:"id"`
CreatedEpochMillis int `json:"createdEpochMillis,omitempty"`
LastUpdatedMs int `json:"lastUpdatedMs,omitempty"`
SampleLinkedGroups *[]UserGroup `json:"sampleLinkedGroups,omitempty"`
SampleLinkedAccounts *[]string `json:"sampleLinkedAccounts,omitempty"`
LinkedGroupsCount int `json:"linkedGroupsCount,omitempty"`
LinkedAccountsCount int `json:"linkedAccountsCount,omitempty"`
Customer string `json:"customer,omitempty"`
LastUpdatedAccountId string `json:"lastUpdatedAccountId,omitempty"`
Name string `json:"name"`
Permissions []string `json:"permissions,omitempty"`
Description string `json:"description,omitempty"`
}
const baseRoleUrlPath = "/api/v2/role"
type Roles struct {
client Wavefronter
}
func (c *Client) Roles() *Roles {
return &Roles{client: c}
}
func (r Roles) Find(filter []*SearchCondition) (roles []*Role, err error) {
err = doSearch(filter, "role", r.client, &roles)
return
}
func (r Roles) Create(role *Role) error {
if role.Name == "" {
return fmt.Errorf("name must be specified while creating a role")
}
return doRest(
"POST",
baseRoleUrlPath,
r.client,
doPayload(role),
doResponse(role))
}
// Get a specific Role for the given ID
// ID field must be specified
func (r Roles) Get(role *Role) error {
if role.ID == "" {
return fmt.Errorf("the ID field must be specified")
}
return doRest(
"GET",
fmt.Sprintf("%s/%s", baseRoleUrlPath, role.ID),
r.client,
doResponse(role))
}
// Update a specific Role for the given ID
// ID field must be specified
func (r Roles) Update(role *Role) error {
if role.ID == "" {
return fmt.Errorf("the ID field must be specified")
}
return doRest(
"PUT",
fmt.Sprintf("%s/%s", baseRoleUrlPath, role.ID),
r.client,
doPayload(role),
doResponse(role))
}
func (r Roles) Delete(role *Role) error {
if role.ID == "" {
return fmt.Errorf("the ID field must be specified")
}
return doRest(
"DELETE",
fmt.Sprintf("%s/%s", baseRoleUrlPath, role.ID),
r.client)
}
func (r Roles) AddAssignees(assignees []string, role *Role) error {
if role.ID == "" {
return fmt.Errorf("the ID field must be specified")
}
return doRest(
"POST",
fmt.Sprintf("%s/%s/addAssignees", baseRoleUrlPath, role.ID),
r.client,
doPayload(assignees),
doResponse(role))
}
func (r Roles) RemoveAssignees(assignees []string, role *Role) error {
if role.ID == "" {
return fmt.Errorf("the ID field must be specified")
}
return doRest(
"POST",
fmt.Sprintf("%s/%s/removeAssignees", baseRoleUrlPath, role.ID),
r.client,
doPayload(assignees),
doResponse(role))
}
func (r Roles) GrantPermission(permission string, roles []*Role) error {
if len(roles) == 0 {
return fmt.Errorf("must specify at least one role to modify")
}
roleIds := make([]string, 0, len(roles))
for _, role := range roles {
if role.ID == "" {
return fmt.Errorf("the ID field must be specified")
}
roleIds = append(roleIds, role.ID)
}
return doRest(
"POST",
fmt.Sprintf("%s/grant/%s", baseRoleUrlPath, permission),
r.client,
doPayload(roleIds))
}
func (r Roles) RevokePermission(permission string, roles []*Role) error {
if len(roles) == 0 {
return fmt.Errorf("must specify at least one role to modify")
}
roleIds := make([]string, 0, len(roles))
for _, role := range roles {
if role.ID == "" {
return fmt.Errorf("the ID field must be specified")
}
roleIds = append(roleIds, role.ID)
}
return doRest(
"POST",
fmt.Sprintf("%s/%s/%s", baseRoleUrlPath, "revoke", permission),
r.client,
doPayload(roleIds))
}