Skip to content

Commit

Permalink
D42-20720 - GitHub JAMF script generates errors when using it with D4…
Browse files Browse the repository at this point in the history
…2 17.02.

Updated to use individual API endpoints to add data to Device42.
  • Loading branch information
cscaglioned42 committed Jul 20, 2021
1 parent 0bfd9c5 commit 537c161
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
17 changes: 7 additions & 10 deletions device42.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ def __init__(self, config, options):
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)
r = requests.post(url, data, headers=headers, verify=False)
if self.debug:
msg1 = unicode(payload)
msg1 = unicode(data)
msg2 = 'Status code: %s' % str(r.status_code)
msg3 = str(r.text)

Expand Down Expand Up @@ -86,12 +85,10 @@ def delete(self, identity, name):
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
# POST
def post(self, data, name):
url = 'https://%s/api/1.0/%s/' % (self.host, name)
msg = '\tPost request to %s ' % url
if not self.dry_run:
print msg
return self._poster({'payload': json.dumps(data)}, url).json()


return self._poster(data, url).json()
21 changes: 18 additions & 3 deletions starter.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,41 @@ def get_computers(self):
def get_device_network(general):
macs = []
ips = []
device_name = general['name']
if general['mac_address']:
macs.append({
'macaddress': general['mac_address'],
'device': device_name
})

if general['alt_mac_address']:
macs.append({
'macaddress': general['alt_mac_address'],
'device': device_name
})

if general['ip_address']:
ips.append({
'ipaddress': general['ip_address'],
'device': device_name
})

if general['last_reported_ip']:
ips.append({
'ipaddress': general['last_reported_ip'],
'device': device_name
})

return macs, ips

@staticmethod
def get_device_software(applications):
def get_device_software(applications, device_name):
software = []
for item in applications['applications']:
software.append({
'software': item['name'],
'version': item['version'],
'device': device_name
})

return software
Expand All @@ -121,7 +127,7 @@ def main():
}
for device in devices:
macs, ips = integration.get_device_network(device['general'])
software = integration.get_device_software(device['software'])
software = integration.get_device_software(device['software'], device['general']['name'])

if options['no_ips']:
ips = []
Expand All @@ -139,5 +145,14 @@ def main():
if __name__ == '__main__':
elements = main()
for element in elements['devices']:
print device42_api.bulk(element)
print device42_api.post(element['device'], 'devices')

for mac in element['macs']:
print device42_api.post(mac, 'macs')

for ip in element['ips']:
print device42_api.post(ip, 'ips')

for software in element['software']:
print device42_api.post(software, 'software_details')
print '\n Finished'

0 comments on commit 537c161

Please sign in to comment.