-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdepartments.go
97 lines (77 loc) · 2.03 KB
/
departments.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
package jamf
import "fmt"
const uriDepartments = "/uapi/v1/departments"
type ResponseDepartments struct {
TotalCount *int `json:"totalCount,omitempty"`
Results []Department `json:"results,omitempty"`
}
type Department struct {
Id *string `json:"id,omitempty"` // The response type to be returned is a string
Name *string `json:"name,omitempty"`
Href *string `json:"href,omitempty"`
}
type DepartmentScope struct {
ID int `xml:"id,omitempty"`
Name string `xml:"name,omitempty"`
}
func (c *Client) GetDepartmentIdByName(name string) (string, error) {
var id string
d, err := c.GetDepartments()
if err != nil {
return "", err
}
for _, v := range d.Results {
if *v.Name == name {
id = *v.Id
break
}
}
return id, err
}
func (c *Client) GetDepartmentByName(name string) (*Department, error) {
id, err := c.GetDepartmentIdByName(name)
if err != nil {
return nil, err
}
return c.GetDepartment(id)
}
func (c *Client) GetDepartment(id string) (*Department, error) {
var out *Department
uri := fmt.Sprintf("%s/%s", uriDepartments, id)
err := c.DoRequest("GET", uri, nil, nil, &out)
return out, err
}
func (c *Client) GetDepartments() (*ResponseDepartments, error) {
var out *ResponseDepartments
err := c.DoRequest("GET", uriDepartments, nil, nil, &out)
return out, err
}
func (c *Client) CreateDepartment(name *string) (*Department, error) {
in := struct {
Name *string `json:"name"`
}{
Name: name,
}
var out *Department
err := c.DoRequest("POST", uriDepartments, in, nil, &out)
return out, err
}
func (c *Client) UpdateDepartment(d *Department) (*Department, error) {
var out *Department
uri := fmt.Sprintf("%s/%s", uriDepartments, *d.Id)
in := struct {
Name *string `json:"name"`
}{
Name: d.Name,
}
err := c.DoRequest("PUT", uri, in, nil, &out)
return out, err
}
func (c *Client) DeleteDepartment(name string) error {
id, err := c.GetDepartmentIdByName(name)
if err != nil {
return err
}
uri := fmt.Sprintf("%s/%s", uriDepartments, id)
return c.DoRequest("DELETE", uri, nil, nil, nil)
}