Free utility

Importing CSV files into device42 using the API helper utility

Device42 RESTful APIs make it simple to import data into the d42 appliance. In this post, we will discuss how you can use the API helper script to import csv files into device42 using the APIs.

Pre-requisite

You will need Python 2.7 installed on the machine from which the csv script will be run. Most linux OS’s have python installed (you can verify this by typing python at the shell). For windows machines, you may need to download python 2.7 and install it. (Note: We haven’t tested the script with python 3 yet)

About API helper python script

About_API_helper_python_script1.png

The API helper python script source is available on the device42 github site. If you head over to the API helpers repository and click on csv2d42api.py, you will see the source code for the csv application.

We will be modifying four sections in the script as highlighted above. We will talk about each of these in following steps. First, lets talk about the sample csv file that we will use in this post.

CSV file

CSV_file1.png

For this post, we will be importing device information. We will use a simple csv file with 4 columns – device name, asset number, serial number and mac address. Column A will be index 0 in the code in our script.

API url and method

We will be using the APIs documented in the device42 API Overview. As outlined in the Overview, we will need the API string, the API method, and a list of required and optional parameters. To find documentation on these, you would go to the List of Add/Update API’s in the Overview and choose the Adding/Updating Devices link.

That link will tell that the API string is /api/device/.

Since we are adding a new resource, the method we will use is POST.

The parameters we will use are name (for device name — this is required), asset_no, serial_no, and macaddress.

Now lets talk about the sections that need to be changed in the sample script to match the csv file and API url.

d42 url and credentials

d42_url_and_credentials1.png

First, you will need to change the portion of the script shown above to add your d42 appliance url and credentials (the urluser must of course have permission to edit devices).

Matching csv rows to API arguments

Matching_csv_rows_to_API_arguments1.png

Next, we will change the portion of the code that creates the parameters in the api call…

If you recall from above, the first column of the CSV file will be read into the 0th element of the row_values array and associated with the ‘name’ parameter. Similarly, the 2nd column of the CSV file will be read into row_values[1] and associated with the ‘asset_no’ parameter.

The documentation will have told you that the ‘name’ parameter is required and the others are optional. So, you would change the code above to the following:

    if not row_values[0]: raise Exception('Required Parameter Missing')
    #create a dictionary from values, that can be passed to api call.
    args = {'name': row_values[0]}
    if row_values[1]: args.update({'asset_no': row_values[1]})
    if row_values[2]: args.update({'serial_no': row_values[2]})
    if row_values[3]: args.update({'macaddress': row_values[3]})
    return args

API url and method

API_url_and_method1.png

As discussed above, the API url is /api/device/ and method is POST. So, you would change the code above to:

    if d42url[:-1] == '/':
        API_URL = d42url + 'api/device/'
    else:
        API_URL = d42url + '/api/device/'
    API_METHOD = 'post' #whether you are doing a put or post call.

CSV file name

CSV_file_name1.png

Last, replace “file-name.csv” with the name of the csv file you will be using. If the file is located in a different folder than the script, you will need to add the whole path for the file as well.

Now you can save the script and run it as discussed below.

Running the script

Running_the_script1.png

To run the script, just open a command line and, if python is in your environment path, you can run it from anywhere.

If it’s not on the path (or you’re not sure), put the CSV file and python script in the folder in which python is installed.

Call the script via: python <script-name.py>.

This will execute the script and upload the csv file contents into d42 appliance.

That is it. Now you can login and see the data show up in the appliance.

Please post any follow up questions in the comments, or open a support ticket from the left tab.

Share this post

About the author

Steve has spent the last 30 years as an entrepreneur and CTO. He created the Esperant business intelligence product in the early 1990's and has been CTO of Tangoe, Computershare, Transcentive, and Cognitive Systems.