Rest API Setup through Python

Purity//FA REST API GuidePurity//FA REST API

Audience
Public
Product
FlashArray
FlashArray > Purity//FA
Content Type
API Reference
Source Type
Documentation

Any time you use a REST API, you'll need to be able (for good ones) authenticate and make GET and POST requests. If you're not familiar with GET and POST requests, you may want to freshen up at W3Schools. You'll use your GET and POST methods to send and receive information to and from the array. In this demonstration, we're going to be using the python requests library to make our GET and POST requests to the array.

We're going to use the following information. But first, you need a REST API Token for this demonstration. Here are the steps:

  1. Navigate to your FlashArray IP: https://10.204.112.109
  2. Navigate to the Users section: System -> Users -> API Tokens.
  3. If you don't already have one, create an API token.

Here's the information for reference from our documentation. These IPs and tokens have been changed for security.

Hostname

slc-coz

IP

https://10.204.112.109

Token

899fdd4f-50ce-c5bf-f253-29be01c51920

FlashStache

http://10.204.119.55/

The next cell is just our imports and some housekeeping. If you're not familiar with python, "import" is a command that imports a set of commands/scripts you can use that someone else wrote.

In [1]:

import json

import pandas

import requests

import time

import urllib3

# We're using a version that warns us about insecure requests, and we don't want to see that noise.

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

%matplotlib inline

Establishing a session with the array

The first thing we need to do is establish a session with the array. This article demonstrates a nifty Python object that manages that session for us. While there are other languages that can manage the sessions for us, our API wrappers are written in Python.

In [2]:

We'll use a requests Session object.

session = requests.Session()

# We're going to use the full URL without any conventional ".format()" type stuff for the demo.

rest_api_url = 'https://slc-m10.slc.purestorage.com/...2-3d634318235d'

# Now we'll submit a POST request to the URL, and don't verify our SSL because it's not set up.

result = session.post(rest_api_url, verify=False)

In [3]:

result.text

Out[3]:

'{"username": "pureuser"}'

Now that you have a session, you can make some API calls. Begin with a simple REST API call to get all the volumes on our array. While there's usually a lot more information, we're using a shortcut in Python to get just the first volume. To get all of them, the command is as follows:

session.get('https://slc-coz.slc.purestorage.com/api/1.12/volume').json()

Adding a [1] at the end to get just the first volume

session.get('https://slc-coz.slc.purestorage.com/api/1.12/volume').json()

In [10]:


session.get('https://slc-coz.slc.purestorage.com/api/1.12/volume').json()[1]

Out [10]:


{'created': '2014-01-23T00:11:00Z',
 'name': 'ISO-Repository',
 'serial': '4966C24FCE8A51E90001007E',
 'size': 5497558138880,
 'source': None}

Getting information about a specific volume

Now that you have a volume name, you can get information about a specific volume. Use a rest API call from the documentation to get that information. See the following:

In [12]:


session.get('https://slc-coz.slc.purestorage.com/api/1.12/volume/ISO-Repository').text

Out[12]:


'{"source": null, "serial": "4966C24FCE8A51E90001007E", "created": "2014-01-23T00:11:00Z", "name": "ISO-Repository", "size": 5497558138880}'

Tracking volume growth over time

You can save these outputs and track volume growth over time.

In [13]:


                  # Run a query to the rest API every 60 seconds.
volume_over_time = []
for i in range(10):
    timestamp = pandas.Timestamp.now()
    size = session.get('https://slc-coz.slc.purestorage.com/api/1.12/volume/ISO-Repository').json()['size']
    volume_over_time.append({'time': timestamp, 'size': size})
    time.sleep(1)

In [14]:


                  # Convert our list of dictionaries to a dataframe for easy plotting
df = pandas.DataFrame(volume_over_time)
# Set our axes to time for simple plotting.
df.set_index(['time'], inplace=True)

In [15]:


df.head()

Out[15]:

 

size

time

 

2018-02-21 11:19:14.594602

5497558138880

2018-02-21 11:19:15.632903

5497558138880

2018-02-21 11:19:16.667310

5497558138880

2018-02-21 11:19:17.689911

5497558138880

2018-02-21 11:19:18.718040

5497558138880

In [16]:


ax = df.plot(title='ISO-Repository size over time')
ax.set_xlabel('Time')
ax.set_ylabel('Size (bytes)')

Out[16]:


Text(0,0.5,'Size (bytes)')

Getting information about multiple volumes

We could do this for every volume over time instead of individual ones.

In [17]:


                  # Run a query to the rest API every 60 seconds.
volumes_over_time = []
for i in range(10):
    size_data = session.get('https://slc-coz.slc.purestorage.com/api/1.12/volume').json()
    timestamp = pandas.Timestamp.now()
    for size_datum in size_data:
        size_datum['time'] = timestamp
    volumes_over_time.extend(size_data)
    time.sleep(1)

In [19]:


volumes_over_time[0:6]

Out[19]:


[{'created': '2014-04-18T11:45:12Z',
  'name': '(null)',
  'serial': '4966C24FCE8A51E90000FFFE',
  'size': 322122547200,
  'source': None,
  'time': Timestamp('2018-02-21 11:21:37.257984')},
 {'created': '2014-01-23T00:11:00Z',
  'name': 'ISO-Repository',
  'serial': '4966C24FCE8A51E90001007E',
  'size': 5497558138880,
  'source': None,
  'time': Timestamp('2018-02-21 11:21:37.257984')},
 {'created': '2014-04-12T04:32:24Z',
  'name': 'JhopFC1',
  'serial': '4966C24FCE8A51E9000100F3',
  'size': 4398046511104,
  'source': None,
  'time': Timestamp('2018-02-21 11:21:37.257984')},
 {'created': '2014-04-12T04:32:34Z',
  'name': 'JhopFC2',
  'serial': '4966C24FCE8A51E9000100F4',
  'size': 4398046511104,
  'source': None,
  'time': Timestamp('2018-02-21 11:21:37.257984')},
 {'created': '2014-04-17T20:52:46Z',
  'name': 'Jhop_RDM',
  'serial': '4966C24FCE8A51E9000100F8',
  'size': 1099511627776,
  'source': None,
  'time': Timestamp('2018-02-21 11:21:37.257984')},
 {'created': '2014-11-24T22:11:54Z',
  'name': 'ESXi-VDI-LUN10',
  'serial': '4966C24FCE8A51E900011210',
  'size': 21990232555520,
  'source': None,
  'time': Timestamp('2018-02-21 11:21:37.257984')}]