ich bin gerade dabei zu versuchen einige Daten aus der CMDB zu bekommen. Mit dem ersten Teil hat das auch soweit ganz gut funktioniert.
Ich habe die Informationen (name, aliases und customer) erfolgreich durch die Dokumentation https://api.device42.com/#resource_Devices in eine JSON laden können.
Da ich nun auch noch die Informationen aus dem Feld cost_center benötige, stehe ich nun vor einem Rätsel, da die Abfrage nach dem Vorbild der hier stehenden Version nicht durch weitere Abfrage
erweitert werden kann. Ich schätze das ich den Path irgendwie noch einbinden müsste um auch die Felder unter Purchasing abfragen zu können.
https://api.device42.com/#resource_Purchasing
Kann hier jemand weiterhelfen ?
Code: Alles auswählen
#! /bin/python
import requests
import base64
import json
#filepath = "/tmp/"
filepath = ""
filename = "data.json"
def getAllDevFromCMDB():
D42_URL = your_d42_fqdn_or_ip #no / in the end
D42_USERNAME = your_d42_username_here
D42_PASSWORD = your_d42_password_her
path = '/api/1.0/devices/all?include_cols=name,customer'
headers = {
'Authorization': 'Basic ' + base64.b64encode((D42_USERNAME + ':' + D42_PASSWORD).encode()).decode(),
'Content-Type': 'application/x-www-form-urlencoded'
}
res = requests.get(D42_URL + path, headers=headers, verify=False)
res = res.json()
i = res['total_count'] / 1000
i = int(round(i+1))
offset = 0
allD = []
for e in range(0, i):
#All VMS
path = '/api/1.0/devices/all?include_cols=name,customer,aliases&offset='
path += str(offset)
offset += 1000
test = requests.get(D42_URL + path, headers=headers, verify=False)
test = test.json()
print(test)
allD += test['Devices']
return allD
res = []
for device in getAllDevFromCMDB():
tempJSON = {}
tempJSON.update({'name': device['name']})
tempJSON.update({'aliases': device['aliases']})
tempJSON.update({'customer': device['customer']})
res.append(tempJSON)
with open(filepath + filename, 'w') as outfile:
json.dump(res, outfile)