Pure Storage REST Client and Python Library

The Pure Storage FlashArray REST Client is a python module that simplifies integration with the Pure Storage FlashArray REST interface. The blog talks about how to install the python libraries required to get the Pure Storage FlashArray REST client working. The assumption is that you have already installed python 2.6 or higher package on your system. The example I show below is based on installation on an Apple OS X, the procedure is quite similar on Linux distros.

Step 1:

Go to https://pypi.python.org/pypi/purestorage and click on the download icon.

python-download-icon

 

 

Alternatively one can go to – https://pypi.python.org/pypi and search for Pure Storage

Step 2:

Extract all the files using tar(1) command

ravi-mbp:t1 ravi$ tar zxvf purestorage-1.4.0.tar.gz

Step 3:

Install the third-party library – requests, one could do pip install to get that. Note this is a mandatory step as the Pure Storage REST client requires requests library.

If pip is not installed on your system then you could get that by doing easy_install.

Step 3a:

Install pip, if its not already installed

ravi-mbp:~ ravi$ sudo easy_install pip

Step 3b:

Install “requests” library

ravi-mbp:purestorage ravi$ sudo pip install requests

Step 4:

Install purestorage REST client package downloaded in step 2.

ravi-mbp:~ ravi$ cd purestorage-1.4.0

ravi-mbp:purestorage-1.4.0 ravi$ sudo python setup.py install

<All done installing the package>

Additional notes:

There may be SSL errors and to eliminate that, install SSL (if not already installed) and certifi.

  1. ravi-mbp:purestorage-1.4.0 ravi$ pip install ssl
  1. ravi-mbp:purestorage-1.4.0 ravi$ sudo pip install certifi

Once installed import both ssl and certifi into your python script.

To further eliminate the warning try installing the request security package.

$ sudo pip install requests[security]

This should get you going and you can start programming with the Pure Storage python APIs. One can do every operation that can be done with the Pure Storage GUI and CLI using python API. For a list of all the API calls please refer to the API index (reference [2]). A full manual documenting our REST API is also available on our GUI help section (right hand top corner).

Now that we have installed the packages, let us try a simple script to create a host group, add host to the host group, create and connect a volume to the host group. Please check reference [3] for more details on the API examples.

Example Script:

# first import the purestorage module

import purestorage

#get array info populated either with clear text username/password or use the API token (See appendix A on how to get API token)

array = purestorage.FlashArray(“ip-address-of-array”, “username”, “userpassword”)

# or

array = purestorage.FlashArray(“ip-address-of-array”, api_token=”6e1b80a1-cd63-de90-b74a-7fc16d034016″)

array_info = array.get()

print “FlashArray {} (version {}) REST session established!”.format(array_info[‘array_name’], array_info[‘version’])

# Create two volumes vol1 and vol2

array.create_volume(“vol1”, “2T”)

array.create_volume(“vol2”, “5T”)

# Create hosts : host1 and host2

array.create_host(“host1”, iqnlist=[“iqn1”, “iqn2”])

array.create_host(“host2”)

array.set_host(“host2”, wwnlist=[“1234567812345678”])

# Create host group and add host1, host2

array.create_hgroup(“hgroup1”, hostlist=[“host1”, “host2”])

# Now connect the volumes vol1, and vol2 to the host group

array.connect_hgroup(“hgroup1”, “vol1”)

array.connect_hgroup(“hgroup1”, “vol2”)

# List the host group connections

array.list_hgroup_connections(“hgroup1”)

# now end the session and invalidate the cookie

array.invalidate_cookie()

Lets save this file and call it my_first_python_code.py

ravi-mbp:SCRIPTS ravi$ chmod 755 my_first_python_code.py

ravi-mbp:SCRIPTS ravi$ ./my_first_python_code.py

We can verify using the Pure GUI or the CLI if the desired result is seen. That’s it, quite simple, right!

In my case, the end results looks as follows (which is the desired results):

my-first-python-script

 

 

 

 

Reference

  1. Main page for Pure Storage Python documentation – http://pythonhosted.org//purestorage/index.html
  2. API Glossary page – http://pythonhosted.org//purestorage/api.html#module-purestorage
  3. Quick start guide – http://pythonhosted.org//purestorage/quick_start.html

 

APPENDIX A

How to obtain an API token from the Pure Storage GUI

obtain-api-token