08-25-2015 06:49 AM
Anyone have a snippet of how to build Python data structure needed to add a device? Also, when we are using this API, should I 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.
Solved! Go to Solution.
09-01-2015 08:49 AM
One final note...since I'm learning Python at the same time I'm learning both PI and the API, it took me some work to figure out how to create the right Python data structure to actually import a device successfully. But, I did it. Below is a sanitized, minimal version of the python script I developed. I have some work to do to modify the data structure so I can import more than one with single call, but this was the minimum that that was required to import a single device. Having this to reference when I started would have saved me a /bunch/ of time.
I'm sure this is terrible python, but cut me some slack - I'm just starting out.
#!/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)
08-25-2015 01:03 PM
I noticed the Resource information table on the left side of the API documentation page states "HTTP Method: PUT", which clearly addresses my second point.
I'm still hoping someone can provide me with a snippet of python code that creates the JSON payload?
08-31-2015 05:32 AM
Bumping this question. I can't believe I am the only person who has ever attempted to add devices into PI with the API...I have not been able to figure out the right JSON data structure.
08-31-2015 01:31 PM
I was finally able to successfully create an import job with this API call. There were three important things for me to note:
{
"devicesImport" : {
"devices" : {
"device" : {
"networkMask": "255.255.255.0",
"udfs": [],
"ipAddress": "192.168.1.100"
}
}
}
}
I just guessed at the Content-Type header, but this changed the error message enough for me to figure out the data structure.
09-01-2015 08:49 AM
One final note...since I'm learning Python at the same time I'm learning both PI and the API, it took me some work to figure out how to create the right Python data structure to actually import a device successfully. But, I did it. Below is a sanitized, minimal version of the python script I developed. I have some work to do to modify the data structure so I can import more than one with single call, but this was the minimum that that was required to import a single device. Having this to reference when I started would have saved me a /bunch/ of time.
I'm sure this is terrible python, but cut me some slack - I'm just starting out.
#!/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)
09-01-2015 09:25 AM
Woohoo, I'm on a ROLL now...
As it says in the API doc, the device data structure can be a list - like this:
device = {'device' : [d1, d2] }
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide