Sunday, 25 October 2020

Mini Tutorial: Using Python to Access the OCI REST API (without using OCI_REST utils)

Open your Python app:


The following example simply gets the number of storage arrays on the OCI server
import requests
from base64 import b64encode
import json
baseurl = ""
userAndPass = b64encode(b"yourRestApiUser:Password").decode("ascii")
headers = {'Authorization':'Basic %s' % userAndPass}
response = requests.request("GET",baseurl + "/assets/storages/count", headers = headers, verify = False)
NumberOfStorages = response.json()['value']
print("Number of Storages = " + str(NumberOfStorages))
A simple function that does the request and returns the json() output.
def easyOciGet(path):
  response = requests.request("GET",baseurl + path, headers = headers, verify = False)
  return response.json()
To get rid of the ‘InsecureRequestWarning’.
import urllib3
Listing storages.
storages = easyOciGet('/assets/storages')
for storage in storages: print(storage)
for storage in storages: print(storage['name'])
for storage in storages: print(storage['name'].ljust(25, ' ') + storage['model'].ljust(25, ' ') + storage['id'])
Listing a storage.
All pools on a single storage.
Storage nodes on a storage.
Performance on a storage.
Everything you can expand a storage by.
Other examples:

  • easyOciGet('/assets/storages/1234?expand=_expands,storagePools')
  • easyOciGet('/assets/storages/1234?expand=storagePools.volumes')
  • easyOciGet('/assets/storages/1234?expand=storagePools.volumes.performance')
  • easyOciGet('/assets/storages/1234?expand=performance.history')
Other things you can do with the NetApp OCI REST API request:
Can filter on times using fromTime and toTime (these are time stamps in milliseconds in the UNIX epoch - check out * 1000):
Expand, query, sort, fields, limit, offset:

Tuesday, 6 October 2020

[Python] Translating Curl into Python

I needed to Python-ize some code I had written for a Delphix Reporting integration with NetApp’s OCI. This KB for Delphix Reporting uses curl to login to the API and get data:

curl --data "password=YOURPASS&user=YOURUSER" http://YOURSERVER/api/login


Note: is a nice online curl to Python converter (do not enter real data; just use it to get the syntax right.)


Translated into Python, the above should be:


import requests


data = {

  'password': 'YOURPASS',

  'user': 'YOURUSER'



response ='https://YOURSERVER/api/login', data=data)


Unfortunately, I don’t have success when I try to connect to Delphix Reporting.


ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)


I had to put verify=False at the end, like so.


import requests


data = {

  'password': 'YOURPASS',

  'user': 'YOURUSER



response ='https://YOURSERVER/api/login', data=data, verify=False)


Great, we have our response but how do we access our required output (loginToken & userId)?


import json

json_data = response.json()

loginToken = json_data["loginToken"]

userId = json_data["userId"]


Great, so now we know how to get our loginToken and userId. How do we get Storage Summary data?


From the KB:


curl -H "X-Login-Token: LOGINTOKEN" -H "X-User-Id: USERNAME" "http://YOURSERVER/api/get_report?report=result_storage_summary"


This translates to:


headers = {

  'X-login-token': loginToken,

  'X-user-id': userId



params = (




response = requests.get('https://YOURSERVER/api/get_report', headers=headers, params=params, verify=False)


Note: Remember the False in verify=False is case sensitive – it must have a capital F.


And this worked. To get the output into JSON format, run:


json_data = response.json()


And to see the JSON format output, run: