cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2351
Views
6
Helpful
5
Replies

PUT Bulk Import

smp
Level 4
Level 4

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.

1 Accepted Solution

Accepted Solutions

smp
Level 4
Level 4

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)

View solution in original post

5 Replies 5

smp
Level 4
Level 4

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?

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.

I was finally able to successfully create an import job with this API call. There were three important things for me to note:

  1. Use the HTTP PUT method (as opposed to GET or POST). This is clearly stated in the Resource Information table of the API documentation.
  2. I had to add the Content-Type: application/json header.
  3. My data structure looks like this, which is also explicitly shown near the bottom of the API documentation.

{

  "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.

smp
Level 4
Level 4

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)

smp
Level 4
Level 4

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] }

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: