-
Notifications
You must be signed in to change notification settings - Fork 2
/
packages.go
132 lines (107 loc) · 2.9 KB
/
packages.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
package jamf
import (
"fmt"
)
const uriPackages = "/JSSResource/packages"
type Package struct {
ID int `xml:"id"`
Name string `xml:"name"`
CategoryName string `xml:"category"`
Filename string `xml:"filename"`
Info string `xml:"info"`
Notes string `xml:"notes"`
RebootRequired bool `xml:"reboot_required"`
Priority int `xml:"priority"`
FillExistingUsers bool `xml:"fill_existing_users"`
BootVolumeRequired bool `xml:"boot_volume_required"`
AllowUninstalled bool `xml:"allow_uninstalled"`
OsRequirements string `xml:"os_requirements"`
RequiredProcessor string `xml:"required_processor"`
HashType string `xml:"hash_type,omitempty"`
HashValue string `xml:"hash_value,omitempty"`
}
type PackageListResponse struct {
Size int `xml:"size"`
Results []PackageListItem `xml:"package"`
}
type PackageListItem struct {
ID int `xml:"id"`
Name string `xml:"name"`
}
func (c *Client) GetPackageIdByName(name string) (int, error) {
var id int
d, err := c.GetPackages()
if err != nil {
return -1, err
}
for _, v := range d.Results {
if v.Name == name {
id = v.ID
break
}
}
return id, err
}
func (c *Client) GetPackageByName(name string) (*Package, error) {
id, err := c.GetPackageIdByName(name)
if err != nil {
return nil, err
}
return c.GetPackage(id)
}
func (c *Client) GetPackage(id int) (*Package, error) {
var out *Package
uri := fmt.Sprintf("%s/id/%v", uriPackages, id)
err := c.doRequest("GET", uri, nil, &out)
return out, err
}
func (c *Client) GetPackages() (*PackageListResponse, error) {
out := &PackageListResponse{}
err := c.doRequest("GET", uriPackages, nil, out)
return out, err
}
func (c *Client) CreatePackage(d *Package) (int, error) {
// Forcing these to empty strings to not allow setting
d.HashType = ""
d.HashValue = ""
// Setting defaults, per jamf unwritten requirements :/
if d.Priority == 0 {
d.Priority = 10
}
if d.RequiredProcessor == "" {
d.RequiredProcessor = "None"
}
if d.Filename == "" {
d.Filename = "ThisPackageDoesNotExist.pkg"
}
res := &Package{}
reqBody := &struct {
*Package
XMLName struct{} `xml:"package"`
}{
Package: d,
}
err := c.doRequest("POST", fmt.Sprintf("%s/id/0", uriPackages), reqBody, res)
return res.ID, err
}
func (c *Client) UpdatePackage(d *Package) (int, error) {
// Forcing these to empty strings to not allow updates
d.HashType = ""
d.HashValue = ""
res := &Package{}
uri := fmt.Sprintf("%s/id/%v", uriPackages, d.ID)
reqBody := &struct {
*Package
XMLName struct{} `xml:"package"`
}{
Package: d,
}
err := c.doRequest("PUT", uri, reqBody, res)
return res.ID, err
}
func (c *Client) DeletePackage(id int) (int, error) {
group := &Package{}
uri := fmt.Sprintf("%s/id/%v", uriPackages, id)
err := c.doRequest("DELETE", uri, nil, group)
return group.ID, err
}