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 ociserver.corp.com:
 
import requests
from base64 import b64encode
import json
 
baseurl = "https://ociserver.corp.com/rest/v1"
 
userAndPass = b64encode(b"yourRestApiUser:Password").decode("ascii")
headers = {'Authorization':'Basic %s' % userAndPass}
 
response = requests.request("GET",baseurl + "/assets/storages/count", headers = headers, verify = False)
 
response
response.text
response.json()
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()
 
easyOciGet('/assets/storages/count')
 
To get rid of the ‘InsecureRequestWarning’.
  
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
 
easyOciGet('/assets/storages/count')
 
Listing storages.
  
storages = easyOciGet('/assets/storages')
 
storages
print(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.
 
easyOciGet('/assets/storages/1234')
 
All pools on a single storage.
 
easyOciGet('/assets/storages/1234/storagePools')
 
Storage nodes on a storage.
 
easyOciGet('/assets/storages/1234?expand=storageNodes')
 
Performance on a storage.
 
easyOciGet('/assets/storages/1234?expand=performance')
 
Everything you can expand a storage by.
 
easyOciGet('/assets/storages/1234?expand=_expands')
 
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 https://www.unixtimestamp.com/ * 1000):
 
?expand=performance.history&fromTime=XX&toTime=YY
 
Expand, query, sort, fields, limit, offset:
  
/rest/v1/assets/volumes/{id}?expand=masks.host,masks.storagePort
/rest/v1/query?objectType=Volume&sort=storage.name&fields=masks.host,masks.storagePort&limit=5&offset=0

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:

https://support.delphix.com/Delphix_Reporting/KBA1734_Delphix_Reporting_(Formerly_Mission_Control)_Web_Service_API_Guide

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

 

Note: https://curl.trillwords.com 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 = requests.post('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 = requests.post('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 = (

  ('report','result_storage_summary'),

)

 

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:

 

json_data