HyperFlex API Python Scripts

With API calls you can configure and monitor HyperFlex. Here is a post about the API Explorer. Things changed. Since HyperFlex 3.5 there is a timeout for the token.

Via the HyperFlex API Explorer, you can get your token and what is a nice script to get your token ?

Get Access Token Script

First you have to do is to get an Access Token.Via the HyperFlex API Explorer you can get a token, but this token changes every x hours. (HyperFlex DP 3.5 and higher)

Here is the script into small blocks :

Import libraries :

import requests
import urllib3
import json
import sys
import getpass

 

In the lab I don’t have a CA server, so I am going to disable the certificate error :

urllib3.disable_warnings()

 

We need the username and password. If it’s not given as a parameter (unsafe), the script is going to ask for it :

if len(sys.argv)<2 :

username = input(“Username : “)
password = getpass.getpass(“Password : “)

if len(sys.argv)>2 :
username = sys.argv[1]
password = sys.argv[2]

 

Now we can ask for a token via the API :

url = ‘https://10.61.125.51/aaa/v1/auth?grant_type=password’

payload = {‘username’: username,’password’: password,’client_id’: ‘HxGuiClient’, ‘client_secret’: ‘Sunnyvale’, ‘redirect_uri’: ‘http://localhost:8080/aaa/redirect/’}

headers = {
“Content-type”: “application/json”,
“Accept”: “application/json”}

response = requests.post(url, data=json.dumps(payload), headers=headers, verify=False)

 

When the result is a code 201 (Request has been fulfilled), show the token :

if response.status_code == 201 :
item = response.json()
print (“Token Type:”)
print (item[‘token_type’])
print ()
print (“Access Token: “)
print (item[‘access_token’])
print ()
print (“Refresh Token”)
print (item[‘refresh_token’])
print ()

 

Let’s print an error if there was an error.

else:
print (“Failed to get an Access Code.”)

 

And this is the script for getting an Access Token from the HyperFlex Cluster.

Please be aware that this script is in python and that you need the 4 spaces at some lines to indent.

List the datastores of the HyperFlex Cluster

Here is a script to show the datastores of a certain HyperFlex cluster. In this example I am using a fixed ip address in the script, but you can do it with a parameter if you want.

import requests
import urllib3
import json
import sys

urllib3.disable_warnings()

 

Let’s do the request. you will see the token in the Authorization field below.

url = ‘https://10.61.125.51/rest/datastores’
headers = {
“Content-type”: “application/json”,
“Authorization”: “Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2Vycy9hZG1pbiIsImV4cCI6MTU3Nzc3ODExOCwidXNlciI6ImFkbWluIiwidG9rZW4iOiI4Iiwic2NvcGUiOiJSRUFELE1PRElGWSIsImlzc3VlZEF0IjoxNTc2MjIyOTE4NTg0LCJ0b2tlbkxpZmVUaW1lIjoxNTU1MjAwMDAwLCJpZGxlVGltZW91dCI6MTgwMDAwMCwid2FybklkbGVUaW1lb3V0IjoxMDAwMCwiaHlwZXJ2aXNvciI6Imh5cGVydiJ9.equUI2oX_AXXaBRqukfds7EXLtZeRzaQGVdQY4RKLxQ”}

response = requests.get(url, headers=headers, verify=False)

 

Instead of having the whole json file print out, we just want to have the name and the size :

print (“Datastores on HX Cluster :”)

for item in response.json():
capacity = item[‘stPlatDatastore’][‘totalCapacity’]
capacity = capacity / 1073741824
print (item[‘entityRef’][‘name’] + ‘ : ‘, capacity,’ Gb’)

 

Delete a datastore of the HyperFlex Cluster

For deleting a datastore, you will need to know the datastore id (DSID). To know this you first have to list all datastores. Search for the same name as the script is asking to the user and then delete the DSID.

Start with importing from some files:

import requests
import urllib3
import json
import sys

 

Are there any argument in the script ?

if len(sys.argv)>1 :
print (“Datastore name: “,sys.argv[1])
else:
print (“Usage : Command “)

 

Disable the certificate warnings :


urllib3.disable_warnings()

 

Let’s list the datastore :

url = ‘https://10.61.125.51/rest/datastores’
headers = {
“Content-type”: “application/json”,
“Authorization”: “Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2Vycy9qdmRtYWRlQHZzcGhlcmUubG9jYWwiLCJleHAiOjE1NDUwMzkwOTQsInVzZXIiOiJqdmRtYWRlQHZzcGhlcmUubG9jYWwiLCJ0b2tlbiI6ImFkODYwOTNjLTlhZWItNDYwZS1hODkwLTIyM2U1MDRkMWFhOSIsInNzb1VybCI6Imh0dHBzOi8vMTAuNjEuMTI1LjExMi9zdHMvU1RTU2VydmljZS92c3BoZXJlLmxvY2FsIiwic2Vzc2lvbiI6InZtd2FyZV9zb2FwX3Nlc3Npb249XCI5NTFiYzllYzNlNTU4N2QwMzUyYzQ5MDA2ZDkyM2FjMTkxOTI3NGVkXCI7IFBhdGg9LzsgSHR0cE9ubHk7IFNlY3VyZTsiLCJzY29wZSI6IlJFQUQsTU9ESUZZIiwiaXNzdWVkQXQiOjE1NDM0ODM4OTQ2OTMsInRva2VuTGlmZVRpbWUiOjE1NTUyMDAwMDAsImlkbGVUaW1lb3V0IjoxODAwMDAwLCJ3YXJuSWRsZVRpbWVvdXQiOjEwMDAwLCJoeXBlcnZpc29yIjoiaHlwZXJ2In0.n9Xa6O6yz_xiURGNI7GU9t3Wv9rOwznJJWd4L-RpsM8”}

response = requests.get(url, headers=headers, verify=False)

 

See if the script can find the datastore with the name and remember the DSID.

stop = True
for item in response.json():
if item[‘entityRef’][‘name’] == sys.argv[1] :
dsid = item[‘entityRef’][‘id’]
stop = False

print dsid

 

if stop == False:

 

Delelete the Datastore:

url = “https://10.61.125.51/rest/datastores/” + dsid

response = requests.delete(url,headers=headers, verify=False)

 

If the status is 200 (Successful), show this to the user.

if response.status_code == 200 :
print (“Datastore is deleted.”)
else:
print (“Failed to delete Datastore.”)
else:
print (“Failed to find the Datastore”)

 

This blog is just an example of how to List and Delete a Datastore. Creating a Datastore is with the same principles.

Use this as an example and create some awesome nice HyperFlex Python Scripts !

Leave a Comment