-
Notifications
You must be signed in to change notification settings - Fork 0
/
JamfClassDelete.py
90 lines (74 loc) · 2.37 KB
/
JamfClassDelete.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
#
# JamfClassDelete.py
#
# Copyright (c) 2024 Doug Penny
# Licensed under MIT
#
# See LICENSE for license information
#
# SPDX-License-Identifier: MIT
#
import logging
import time
from typing import Dict, List
import tomllib
from pyjamfpro import jamfpro
##
## Read values from configuration file
##
with open("jamf_class_sync_config.toml", mode="rb") as config_file:
config = tomllib.load(config_file)
def main():
logging.basicConfig(
level=logging.INFO,
filename=config["log_file_path"],
format="%(asctime)s - %(levelname)s: %(message)s",
datefmt="%d-%b-%y %H:%M:%S",
)
logging.info("** Start log for JamfClassDelete.py **")
start = time.time()
#
# Instantiate the PyJamfPro client for communicating with the Jamf server
#
client = jamfpro.Client(
config["jamf_domain"], config["jamf_client_id"], config["jamf_client_secret"]
)
deleted: int = 0
delete_retires: List = []
delete_failed: List = []
#
# Fetch all existing classes from Jamf
#
existing_classes: List = client.classic_classes()
#
# Delete all existing classed from Jamf
#
jamf_class: Dict
for jamf_class in existing_classes:
class_id: int = jamf_class.get("id", 0)
success: bool = client.classic_delete_class_with_id(class_id)
if success:
deleted = deleted + 1
else:
delete_retires.append(class_id)
logging.info(
f"Tried to delete class with ID {class_id} from Jamf, but was unsuccessful. Will try again."
)
if len(delete_retires) > 0:
deleted_id: int
for delete_id in delete_retires:
success: bool = client.classic_delete_class_with_id(delete_id)
if success:
deleted = deleted + 1
else:
delete_failed.append(delete_id)
logging.warning(
f"Tried to delete class with ID {delete_id} from Jamf, but was unsuccessful. This class may need to be deleted manually."
)
end = time.time()
logging.info(
f"Deleted {deleted} class(es) found in Jamf in {(end - start):.4f} seconds."
)
logging.info("** End log for JamfClassDelete.py **\n")
if __name__ == "__main__":
main()