-
Notifications
You must be signed in to change notification settings - Fork 6
/
apiRoles.go
131 lines (103 loc) · 2.83 KB
/
apiRoles.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
// apiRoles.go
// Jamf Pro Api
// Jamf Pro Api requires the structs to support JSON.
package jamfpro
import (
"fmt"
)
const uriAPIRoles = "/api/v1/api-roles"
type ResponseAPIRole struct {
TotalCount int `json:"totalCount"`
Results []APIRole `json:"results"`
}
type APIRole struct {
ID string `json:"id"`
DisplayName string `json:"displayName"`
Privileges []string `json:"privileges"`
}
type APIRoleUpdateRequest struct {
DisplayName string `json:"displayName"`
Privileges []string `json:"privileges"`
}
func (c *Client) GetApiRoleIdByName(name string) (string, error) {
var id string
roles, err := c.GetApiRoles()
if err != nil {
return "", err
}
for _, v := range roles.Results {
if v.DisplayName == name {
id = v.ID
break
}
}
return id, err
}
func (c *Client) GetApiRoleByName(name string) (*APIRole, error) {
allRolesResponse, err := c.GetApiRoles()
if err != nil {
return nil, err
}
for _, role := range allRolesResponse.Results {
if role.DisplayName == name {
return &role, nil
}
}
return nil, fmt.Errorf("API role with name '%s' not found", name)
}
func (c *Client) GetApiRoles() (*ResponseAPIRole, error) {
uri := fmt.Sprintf("%s?page=0&page-size=100&sort=id%%3Aasc", uriAPIRoles)
var out ResponseAPIRole
err := c.DoRequest("GET", uri, nil, nil, &out)
if err != nil {
return nil, fmt.Errorf("failed to get API roles: %v", err)
}
return &out, nil
}
func (c *Client) GetApiRoleByID(roleID int) (*APIRole, error) {
uri := fmt.Sprintf("%s/%d", uriAPIRoles, roleID)
var out APIRole
err := c.DoRequest("GET", uri, nil, nil, &out)
if err != nil {
return nil, fmt.Errorf("failed to get API role by ID: %v", err)
}
return &out, nil
}
func (c *Client) CreateApiRole(displayName *string, privileges *[]string) (*APIRole, error) {
in := struct {
DisplayName *string `json:"displayName"`
Privileges *[]string `json:"privileges"`
}{
DisplayName: displayName,
Privileges: privileges,
}
var out *APIRole
err := c.DoRequest("POST", uriAPIRoles, in, nil, &out)
if err != nil {
return nil, fmt.Errorf("failed to create API role: %v", err)
}
return out, nil
}
func (c *Client) UpdateApiRole(d *APIRole) (*APIRole, error) {
uri := fmt.Sprintf("%s/%s", uriAPIRoles, d.ID)
updateRequest := &APIRoleUpdateRequest{
DisplayName: d.DisplayName,
Privileges: d.Privileges,
}
updatedRole := &APIRole{}
// Perform the PUT request
err := c.DoRequest("PUT", uri, updateRequest, nil, updatedRole)
if err != nil {
return nil, fmt.Errorf("failed to update API role: %v", err)
}
return updatedRole, nil
}
func (c *Client) DeleteApiRole(roleID int) error {
uri := fmt.Sprintf("%s/%d", uriAPIRoles, roleID)
// Perform the DELETE request
err := c.DoRequest("DELETE", uri, nil, nil, nil)
if err != nil {
return fmt.Errorf("failed to delete API role: %v", err)
}
return nil
}