-
Notifications
You must be signed in to change notification settings - Fork 5
/
computers.py
executable file
·104 lines (78 loc) · 3.01 KB
/
computers.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
import requests
import math
from auth.creds import instance_id
from auth.bearer_auth import get_token
headers = {
'Accept': 'application/json',
'Authorization': f'Bearer {get_token()}',
'Content-Type': 'application/json',
}
def get_computer_count():
"""
Returns the number of computers in Jamf Pro
"""
try:
response = requests.get(
url=f'https://{instance_id}/api/v1/computers-inventory?section=HARDWARE&page=0&page-size=1&sort=id%3Aasc',
headers=headers
)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
raise SystemExit(err)
count = response.json()['totalCount']
return count
computers_per_page = 1000
number_of_pages = math.ceil(get_computer_count() / computers_per_page)
def get_arm64(filter = None):
"""
Returns Jamf IDs of all arm64 type computers
Parameters:
filter - (e.g. 'filter=general.name=="jdoe-mbp"'). If empty, returns all computers.
Computer name in filter is not case sensitive
"""
computers_id = []
for pageIndex in range(number_of_pages):
try:
response = requests.get(
url=f'https://{instance_id}/api/v1/computers-inventory?section=HARDWARE&page={pageIndex}&page-size={computers_per_page}&sort=id%3Aasc&{filter}',
headers=headers
)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
raise SystemExit(err)
computers = response.json()['results']
for computer in computers:
if computer['hardware']['processorArchitecture'] == 'arm64':
computers_id.append(computer['id'])
return computers_id
def get_mgmt_id(computers_id):
"""
Returns Jamf computers management id
Parameters:
computers_id - (e.g. ['10', '12']]). List of Jamf computers id
"""
computers_mgmt_id = []
for pageIndex in range(number_of_pages):
try:
response = requests.get(
url = f'https://{instance_id}/api/preview/computers?page={pageIndex}&page-size={computers_per_page}&sort=name%3Aasc',
headers=headers
)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
raise SystemExit(err)
computers = response.json()['results']
for computer_id in computers_id:
for computer in computers:
# Find computers that given computer id in list of computers
if computer['id'] == computer_id:
computer_mgmt_id = computer['managementId']
computer_name = computer['name']
# Add computer to list
computers_mgmt_id.append({
'id': computer_id,
'name': computer_name,
'mgmt_id': computer_mgmt_id
})
break
return computers_mgmt_id