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

Comments