Using the API with Python

This sample Python code performs authentication, issues three example GET requests, performs a POST operation (in this case, renaming a backup), and monitors the status of the operation using a task.

import requests
# Set the base URL for REST API requests.
url = 'https://[host]/api/'

# Set the username and password.
hms_username = 'HMS_USER'
hms_password = 'HMS_PASS'

# Authenticate user and generate access token.
response ='oauth/token', auth=('simplivity', ''), verify=False, data={
access_token = response.json()['access_token']

# Add the access_token to the header.
headers = {'Authorization':  'Bearer ' + access_token, 'Accept' : 'application/vnd.simplivity.v1+json'}

# Issue a GET request: GET /hosts.
response = requests.get(url+'hosts', verify=False, headers=headers)

# Issue a GET request: GET /datastores.
response = requests.get(url+'datastores', verify=False, headers=headers)

# Issue a GET request with sorting and filtering:
# GET first 100 backups of the MANUAL type
# sorted in ascending order by name
# and show only the name, id, and created_at fields.
response = requests.get(url+'backups?type=MANUAL&fields=name%2C%20id%2C%20created_at&limit=100&offset=0&sort=name&order=ascending', verify=False, headers=headers)

# Issue a POST request: Rename the first backup from the GET results.
# Define a container to hold the JSON response.
json = response.json()
# Find the ID of the first backup from the GET results.
backupid = json['backups'][0]['id']
# Create a JSON body for the rename action.
body = '{"backup_name":"newbackupname"}'
# Specify the correct MIME type for the body.
headers['Content-Type'] = 'application/vnd.simplivity.v1+json'
# Issue the POST operation and expect a task object in return.
response ='backups/'+backupid+'/rename', data=body, verify=False, headers=headers)
json = response.json()

# Monitor the status of the rename operation by using a loop to query
# the task while this task is IN_PROGRESS.
# The state field in the JSON response body indicates the status.
taskid = json['task']['id']
state = json['task']['state']
while (state == "IN_PROGRESS"):
    # Wait one second.
    response = requests.get(url+'tasks/'+taskid, verify=False, headers=headers)
    json = response.json()
    state = json['task']['state']

Related Blogs

Roy Atkins

HPE SimpliVity PowerShell Module

May 16, 2019
Ron Dharma

Using Postman with HPE SimpliVity

Nov 9, 2018

HPE Developer Newsletter

Stay in the loop.

Sign up for the HPE Developer Newsletter or visit the Newsletter Archive to see past content.

By clicking on “Subscribe Now”, I agree to HPE sending me personalized email communication about HPE and select HPE-Partner products, services, offers and events. I understand that my email address will be used in accordance with HPE Privacy Statement. You may unsubscribe from receiving HPE and HPE-Partner news and offers at any time by clicking on the Unsubscribe button at the bottom of the newsletter.

For more information on how HPE manages, uses, and protects your personal data please refer to HPE Privacy Statement.