forked from grahampugh/jamf-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjamf_category_upload.py
executable file
·179 lines (149 loc) · 5.7 KB
/
jamf_category_upload.py
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
"""
** Jamf Category Upload Script
by G Pugh
Credentials can be supplied from the command line as arguments, or inputted, or
from an existing PLIST containing values for JSS_URL, API_USERNAME and API_PASSWORD,
for example an AutoPkg preferences file which has been configured for use with
JSSImporter: ~/Library/Preferences/com.github.autopkg
For usage, run jamf_category_upload.py --help
"""
import argparse
import os
from time import sleep
from jamf_upload_lib import api_connect, api_get, curl
def upload_category(jamf_url, category_name, priority, verbosity, token, obj_id=0):
"""Update category metadata."""
# build the object
category_data = {"priority": priority, "name": category_name}
if obj_id:
url = "{}/uapi/v1/categories/{}".format(jamf_url, obj_id)
category_data["name"] = category_name
else:
url = "{}/uapi/v1/categories".format(jamf_url)
if verbosity > 2:
print("Category data:")
print(category_data)
print("Uploading category..")
count = 0
# we cannot PUT a category of the same name due to a bug in Jamf Pro (PI-008157).
# so we have to do a first pass with a temporary different name, then change it back...
if obj_id:
category_name_temp = category_name + "_TEMP"
category_data_temp = {"priority": priority, "name": category_name_temp}
category_json_temp = curl.write_json_file(category_data_temp)
while True:
count += 1
if verbosity > 1:
print("Category upload attempt {}".format(count))
r = curl.request("PUT", url, token, verbosity, category_json_temp)
# check HTTP response
if curl.status_check(r, "Category", category_name_temp) == "break":
break
if count > 5:
print(
"ERROR: Temporary category update did not succeed after 5 attempts"
)
print("\nHTTP POST Response Code: {}".format(r.status_code))
break
sleep(10)
# write the category. If updating an existing category, this reverts the name to its original.
category_json = curl.write_json_file(category_data)
while True:
count += 1
if verbosity > 1:
print("Category upload attempt {}".format(count))
method = "PUT" if obj_id else "POST"
r = curl.request(method, url, token, verbosity, category_json)
# check HTTP response
if curl.status_check(r, "Category", category_name) == "break":
break
if count > 5:
print("ERROR: Category creation did not succeed after 5 attempts")
print("\nHTTP POST Response Code: {}".format(r.status_code))
break
sleep(10)
if verbosity > 1:
api_get.get_headers(r)
# clean up temp files
for file in category_json, category_json_temp:
if os.path.exists(file):
os.remove(file)
return r
def get_args():
"""Parse any command line arguments"""
parser = argparse.ArgumentParser()
parser.add_argument(
"category", nargs="+", help="Category to create",
)
parser.add_argument(
"--replace", help="overwrite an existing category", action="store_true",
)
parser.add_argument(
"--priority", default="10", help="Category priority. Default value is 10",
)
parser.add_argument(
"--url", default="", help="the Jamf Pro Server URL",
)
parser.add_argument(
"--user",
default="",
help="a user with the rights to create and update a category",
)
parser.add_argument(
"--password",
default="",
help="password of the user with the rights to create and update a category",
)
parser.add_argument(
"--prefs",
default="",
help=(
"full path to an AutoPkg prefs file containing "
"JSS URL, API_USERNAME and API_PASSWORD, "
"for example an AutoPkg preferences file which has been configured "
"for use with JSSImporter (~/Library/Preferences/com.github.autopkg.plist) "
"or a separate plist anywhere (e.g. ~/.com.company.jcds_upload.plist)"
),
)
parser.add_argument(
"-v",
"--verbose",
action="count",
default=0,
help="print verbose output headers",
)
args = parser.parse_args()
return args
def main():
"""Do the main thing here"""
print("\n** Jamf category upload script")
print("** Creates a category in Jamf Pro.")
# parse the command line arguments
args = get_args()
verbosity = args.verbose
# grab values from a prefs file if supplied
jamf_url, _, _, _, enc_creds = api_connect.get_creds_from_args(args)
# now get the session token
token = api_connect.get_uapi_token(jamf_url, enc_creds, verbosity)
# now process the list of categories
for category_name in args.category:
# check for existing category
print("\nChecking '{}' on {}".format(category_name, jamf_url))
obj_id = api_get.get_uapi_obj_id_from_name(
jamf_url, "categories", category_name, token, verbosity
)
if obj_id:
print("Category '{}' already exists: ID {}".format(category_name, obj_id))
if args.replace:
upload_category(
jamf_url, category_name, args.priority, verbosity, token, obj_id
)
else:
print("Not replacing existing category. Use --replace to enforce.")
else:
# post the category
upload_category(jamf_url, category_name, args.priority, verbosity, token)
print()
if __name__ == "__main__":
main()