-
Notifications
You must be signed in to change notification settings - Fork 8
/
userGroups.go
148 lines (118 loc) · 4.72 KB
/
userGroups.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
// userGroups.go
// Jamf Pro Classic Api
// Classic API requires the structs to support both XML and JSON.
package jamfpro
import (
"fmt"
)
const uriUserGroups = "/JSSResource/usergroups"
type ResponseUserGroup struct {
ID int `json:"id" xml:"id"`
Name string `json:"name" xml:"name"`
IsSmart bool `json:"is_smart" xml:"is_smart"`
IsNotifyOnChange bool `json:"is_notify_on_change" xml:"is_notify_on_change"`
Site Site `json:"site,omitempty" xml:"site,omitempty"`
Criteria []UserGroupCriteria `json:"criteria,omitempty" xml:"criteria,omitempty"`
Users []UserGroupUser `json:"users,omitempty" xml:"users,omitempty"`
}
type UserGroupCriteria struct {
Size int `json:"size" xml:"size"`
Criterion UserGroupCriterion `json:"criterion" xml:"criterion"`
}
type UserGroupCriterion struct {
Name string `json:"name" xml:"name"`
Priority int `json:"priority,omitempty" xml:"priority,omitempty"`
AndOr string `json:"and_or,omitempty" xml:"and_or,omitempty"`
SearchType string `json:"search_type,omitempty" xml:"search_type,omitempty"`
Value string `json:"value,omitempty" xml:"value,omitempty"`
OpeningParen bool `json:"opening_paren,omitempty" xml:"opening_paren,omitempty"`
ClosingParen bool `json:"closing_paren,omitempty" xml:"closing_paren,omitempty"`
}
type UserGroupUser struct {
Size int `json:"size" xml:"size"`
User User `json:"user" xml:"user"`
}
type User struct {
ID int `json:"id" xml:"id"`
Username string `json:"username,omitempty" xml:"username,omitempty"`
FullName string `json:"full_name,omitempty" xml:"full_name,omitempty"`
PhoneNumber string `json:"phone_number,omitempty" xml:"phone_number,omitempty"`
EmailAddress string `json:"email_address,omitempty" xml:"email_address,omitempty"`
}
type UserGroupScope struct {
Id int `xml:"id"`
Name string `xml:"name"`
}
// GetUserGroupByID retrieves the User Group by its ID
func (c *Client) GetUserGroupByID(id int) (*ResponseUserGroup, error) {
url := fmt.Sprintf("%s/id/%d", uriUserGroups, id)
var group ResponseUserGroup
if err := c.DoRequest("GET", url, nil, nil, &group); err != nil {
return nil, fmt.Errorf("failed to execute request: %v", err)
}
return &group, nil
}
// GetUserGroupByName retrieves the User Group by its Name
func (c *Client) GetUserGroupByName(name string) (*ResponseUserGroup, error) {
url := fmt.Sprintf("%s/name/%s", uriUserGroups, name)
var group ResponseUserGroup
if err := c.DoRequest("GET", url, nil, nil, &group); err != nil {
return nil, fmt.Errorf("failed to execute request: %v", err)
}
return &group, nil
}
// GetUserGroups retrieves all User Groups
func (c *Client) GetUserGroups() ([]ResponseUserGroup, error) {
url := uriUserGroups
var groups []ResponseUserGroup
if err := c.DoRequest("GET", url, nil, nil, &groups); err != nil {
return nil, fmt.Errorf("failed to fetch all User Groups: %v", err)
}
return groups, nil
}
// CreateSmartUserGroup creates a new Smart User Group
func (c *Client) CreateSmartUserGroup(group *ResponseUserGroup) (*ResponseUserGroup, error) {
url := fmt.Sprintf("%s/id/0", uriUserGroups)
// If Site ID is not set (or set to 0), set it to -1 to exclude from the request and set to unused
if group.Site.ID == 0 {
group.Site = Site{ID: -1, Name: "None"}
}
// Set the IsSmart field to true
group.IsSmart = true
// Construct a custom request body structure for proper XML serialization
reqBody := &struct {
XMLName struct{} `xml:"user_group"`
*ResponseUserGroup
}{
ResponseUserGroup: group,
}
// Execute the request
var responseGroup ResponseUserGroup
if err := c.DoRequest("POST", url, reqBody, nil, &responseGroup); err != nil {
return nil, fmt.Errorf("failed to create Smart User Group: %v", err)
}
return &responseGroup, nil
}
// CreateStaticUserGroup creates a new Static User Group
func (c *Client) CreateStaticUserGroup(group *ResponseUserGroup) (*ResponseUserGroup, error) {
url := fmt.Sprintf("%s/id/0", uriUserGroups)
// If Site ID is not set (or set to 0), set it to -1 to exclude from the request and set to unused
if group.Site.ID == 0 {
group.Site = Site{ID: -1, Name: "None"}
}
// Set the IsSmart field to false
group.IsSmart = false
// Construct a custom request body structure for proper XML serialization
reqBody := &struct {
XMLName struct{} `xml:"user_group"`
*ResponseUserGroup
}{
ResponseUserGroup: group,
}
// Execute the request
var responseGroup ResponseUserGroup
if err := c.DoRequest("POST", url, reqBody, nil, &responseGroup); err != nil {
return nil, fmt.Errorf("failed to create Static User Group: %v", err)
}
return &responseGroup, nil
}