-
Notifications
You must be signed in to change notification settings - Fork 24
/
role.go
58 lines (48 loc) · 1.48 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
package civogo
import (
"bytes"
"encoding/json"
"time"
)
// Role represents a set of permissions
type Role struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
Permissions string `json:"permissions,omitempty"`
BuiltIn bool `json:"built_in,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}
// ListRoles returns all roles (built-in and user defined)
func (c *Client) ListRoles() ([]Role, error) {
resp, err := c.SendGetRequest("/v2/roles")
if err != nil {
return nil, decodeError(err)
}
roles := make([]Role, 0)
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&roles); err != nil {
return nil, err
}
return roles, nil
}
// CreateRole creates a new role with a set of permissions for use within an organisation
func (c *Client) CreateRole(name, permissions string) (*Role, error) {
data := map[string]string{"name": name, "permissions": permissions}
resp, err := c.SendPostRequest("/v2/roles", data)
if err != nil {
return nil, decodeError(err)
}
role := &Role{}
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(role); err != nil {
return nil, err
}
return role, nil
}
// DeleteRole removes a non-built-in role from an organisation
func (c *Client) DeleteRole(id string) (*SimpleResponse, error) {
resp, err := c.SendDeleteRequest("/v2/roles/" + id)
if err != nil {
return nil, decodeError(err)
}
return c.DecodeSimpleResponse(resp)
}