- Looking for a snippet of how to build Python data structure needed to add a device. Also, when we are using this API, should be using the Python Response GET or POST method, or PUT verb. The documentation uses the term "PUT", but isn't clear about whether this really is.
To create an import job with this API call.
- Use the HTTP PUT method (as opposed to GET or POST). This is clearly stated in the Resource Information table of the API documentation.
- Add the Content-Type: application/json header.
- This is an example of data structure which is shown near the bottom of the API documentation.
{
"devicesImport" : {
"devices" : {
"device" : {
"networkMask": "255.255.255.0",
"udfs": [],
"ipAddress": "192.168.1.100"
}
}
}
}
Below is a minimal version of the python script that was required to import a single device.
#!/usr/bin/python
import requests
import json
from requests.auth import HTTPBasicAuth
basic = HTTPBasicAuth('<username>', '<password>')
host = 'https://<PI_DNS_name>'
uri = '/webacs/api/v1/op/devices/bulkImport.json'
url = host + uri
d = {
"ipAddress": "192.168.1.100",
"snmpCommunity": "public",
"snmpVersion": "2c",
}
device = {'device' : d }
devices = { 'devices' : device }
devicesImport = { 'devicesImport' : devices }
headers = {'Content-Type' : 'application/json; charset=utf8'}
response = requests.put(url, verify=False, auth=basic, headers=headers, data=json.dumps(devicesImport))
if response.status_code != 200:
print response.status_code
print response.text
else:
json_data = json.loads(response.text)
jobname = json_data['mgmtResponse']['bulkImportResult']['jobName']
print jobname
sys.exit(0)
As it says in the API doc, the device data structure can be a list - like this:
device = {'device' : [d1, d2] }
Comments
0 comments
Please sign in to leave a comment.