forked from device42/jamf_to_device42_sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device42.py
138 lines (110 loc) · 4.23 KB
/
device42.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
import requests
import base64
import json
class Device42Api:
def __init__(self, config, options):
self.username = config['username']
self.password = config['password']
self.host = config['host']
self.debug = options['debug']
self.dry_run = options['dry_run']
def _poster(self, data, url):
payload = data
headers = {
'Authorization': 'Basic ' + base64.b64encode(self.username + ':' + self.password)
}
r = requests.post(url, payload, headers=headers, verify=False)
if self.debug:
msg1 = payload
msg2 = 'Status code: %s' % str(r.status_code)
msg3 = str(r.text)
print('\n\t----------- POST FUNCTION -----------')
print('\t' + msg1)
print('\t' + msg2)
print('\t' + msg3)
print('\t------- END OF POST FUNCTION -------\n')
return r
def _put(self, data, url):
payload = data
headers = {
'Authorization': 'Basic ' + base64.b64encode(self.username + ':' + self.password)
}
r = requests.put(url=url, data=payload, headers=headers, verify=False)
if self.debug:
msg1 = payload
msg2 = 'Status code: %s' % str(r.status_code)
msg3 = str(r.text)
print('\n\t----------- POST FUNCTION -----------')
print('\t' + msg1)
print('\t' + msg2)
print('\t' + msg3)
print('\t------- END OF POST FUNCTION -------\n')
return r
def _getter(self, data, url):
params = data
headers = {
'Authorization': 'Basic ' + base64.b64encode(self.username + ':' + self.password),
'Content-Type': 'application/x-www-form-urlencoded'
}
r = requests.get(url, params=params, headers=headers, verify=False)
if self.debug:
msg1 = params
msg2 = 'Status code: %s' % str(r.status_code)
msg3 = str(r.text)
print('\n\t----------- POST FUNCTION -----------')
print('\t' + msg1)
print('\t' + msg2)
print('\t' + msg3)
print('\t------- END OF POST FUNCTION -------\n')
return r
def _deleter(self, url):
headers = {
'Authorization': 'Basic ' + base64.b64encode(self.username + ':' + self.password),
'Content-Type': 'application/x-www-form-urlencoded'
}
r = requests.delete(url, headers=headers, verify=False)
if self.debug:
msg1 = 'Status code: %s' % str(r.status_code)
msg2 = str(r.text)
print('\n\t----------- POST FUNCTION -----------')
print('\t' + msg1)
print('\t' + msg2)
print('\t------- END OF POST FUNCTION -------\n')
return r
# GET
def get_list(self, data, name):
url = 'https://%s/api/1.0/%s/' % (self.host, name)
msg = '\tGet request to %s ' % url
if not self.dry_run:
print(msg)
return self._getter(data, url).json()
# DELETE
def delete(self, identity, name):
url = 'https://%s/api/1.0/%s/%s' % (self.host, name, identity)
msg = '\tDelete request to %s ' % url
if not self.dry_run:
print(msg)
return self._deleter(url).json()
# BULK
def bulk(self, data):
url = 'https://%s/api/1.0/devices/bulk/' % self.host
msg = '\tBulk request to %s ' % url
if not self.dry_run:
print(msg)
return self._poster({'payload': json.dumps(data)}, url).json()
# POST - END USER
def post_enduser(self, data):
url ='https://%s/api/1.0/endusers/' %self.host
return self._poster(data, url).json()
# POST - BUILDING
def post_building(self, data):
url ='https://%s/api/1.0/buildings/' %self.host
return self._poster(data, url).json()
# PUT - END USER CUSTOM FIELDS
def put_enduserCF(self, data):
url ='https://%s/api/1.0/custom_fields/endusers/' %self.host
return self._put(data, url).json()
# PUT - DEVICE CUSTOM FIELDS
def put_deviceCF(self, data):
url ='https://%s/api/1.0/device/custom_field/' %self.host
return self._put(data, url).json()