NetApp REST API Using Python Library Requests

NetApp allows performing automation using various methods. NetApp REST API is one of them. You can do basic or complex automation using the RESTAPI method. In this post, we will see how to connect NetApp RESTAPI using a Python library called requests.

You can also do NetApp automation using Paramiko library.

Python REST API Documents

Before you proceed further, you must understand to read the Python REST API documentation. It is available with the cluster itself. If you have admin credentials for your cluster, then type the below URL in the web browser.

https://<clustermanagementIP>/docs/api

You may have to type the cluster username and password to get into the documentation page. The official documentation site for NetApp REST API does not require any credentials.

Read Isilon Rest API using Python

You will be able to see various segments, and each of the segments consists of API endpoints. API endpoints are nothing but the HTTPS URL that gives information when we call them. Each API endpoints give specific information or do administrative tasks and you can call them via a get or post request.

Consider the below example of cluster endpoint. It has a GET endpoint that gives you cluster-related information.

https://<clustermanagementIP>/api/cluster

Before using any endpoint, it is best to go through the Doc section. It will give a fair idea of its usage.

NetApp REST API Documents

Python Code For NetApp REST API Connection

I assume, you already have installed Python in your system. Along with Python, you can have visual studio code, which autosuggestion feature helps developers to write code faster.

Below are a few lines of Python code, that connect to NetApp REST API and get cluster-level information.

import requests
def netappapi():
    url="https://clustermanagementIP/api/cluster"
    header={"Authorization": "Basic YWRtaW46bmV0YXBwMTIz"}
    output=requests.get(url,headers=header,verify=False)
    version=output.json().get("version").get("full")
    print(version)
netappapi()

The first line is for importing the requests library. It is a popular library that helps to make REST API call. The library does not come by default with Python packages, hence you need to install it from the command prompt. Just open the command prompt and execute the below command.

pip install requests

The second line defines a function, with the name netapapi. The function contains all Python code.

Authentication

The first line of the function holds the API endpoint, inside the variable “URL”. The second line creates a header variable that takes care of authentication by supplying the user name and password of the NetApp cluster. You must pass a username and password in base64 encoded format.

For example, you need to put a colon between the user name and password, and then use a base64 encoder to encode it. Visit the base64 encoder website that does the encoding part instantly.

Base64 Encode NetApp Credential

GET Call using Python Requests Library

Now, you need to make a get-call as shown in line number 5. The GET call needs three arguments, API endpoint, headers, and Verify. Setting the verify arguments to False, skips HTTPS validation, and allows us to use HTTP protocol.

If the GET call is successful, you get a 200 response code. In order to get the output in JSON format you need to use the .get() function. Line number six shows the usage of the get() function to retrieve the cluster’s full version.

Finally, a print statement, to show the result.

Conclusion

NetApp REST API is mostly used approach to perform NetApp automation. You can write simple and much more complex automation scripts. The post has covered just the basics. Hope it was helpful.