>> Important note: This def is designed to for APIs that support limit and offset, but it should work with other GETs too if the size of the first batch of output is less than the limit. <<
The following is a completely standalone Python Def (Definition/Function) that you can use to get data from the NetApp Cloud Insights REST API. It allows you to run a basic get REST API starting from default offset=0 with default batch size of 10000. It could be expanded to support other CI REST API features (like expands) by allowing additional arguments.
The only thing you might need to do (from the command prompt if you're running Windows) - if you've not already done it - is:
python -m pip install requests
The other imports are included with Python as standard (I think.)
You can essentially just copy and paste the whole def into Python CLI and then use it from the CLI (don't need to write and run scripts.)
Examples of usage:
## FIRST CREATE VARIABLES FOR YOUR API KEY AND TENANT ##
apiKey = "YOUR-VERY-LONG-API-KEY"
yourTenant = "https://YOUR-TENANT.cloudinsights.netapp.com"
## GET THE HOSTS ##
api = "/rest/v1/assets/hosts"
hosts = ciGet(yourTenant,apiKey,api)
## GET THE VMs ##
api = "/rest/v1/assets/virtualmachines"
vms = ciGet(yourTenant,apiKey,api)
Note: This is for the CI REST API and not the CI DWH ODATA REST API.
Def ciGet
def ciGet(*args):
# Usage: ciGet(tenant,apiKey,api,limit,offset)
# Mandatory: tenant,apiKey,api
import json
import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Define pwd (print with date):
def pwd(string):
import datetime
print(str(datetime.datetime.now()).split('.',1)[0] + ": " + string)
# Validate we have arguments:
if len(args) == 0:
pwd("Syntax error!")
return
# Process arguments:
tenant = args[0]
apiKey = args[1]
api = args[2]
limit = 10000 if len(args) < 4 else args[3]
offset = 0 if len(args) < 5 else args[4]
# Get the data
headers = {'X-CloudInsights-ApiKey': '{key}'.format(key=apiKey)}
output = []
try:
while True:
a = "?limit={0}&offset={1}".format(limit,offset)
url = tenant + api + a
r = requests.get(url,headers=headers,verify=False)
if r.status_code == 200 or r.status_code == 201:
if not json.loads(r.text): break
for s in json.loads(r.text): output.append(s)
pwd("Offset: {0}, data: {1}, overall: {2}".format(offset, len(json.loads(r.text)), len(output)))
if len(json.loads(r.text)) < limit: return output
else:
pwd("Error on offset: {0}. HTTP ret code: {1}. HTTP error: {2}".format(offset, r.status_code, r.text))
return
offset += limit
except Exception as e:
pwd(str(e), offset)
return output
Note: I've been very sparing with my indents, just one space for an indent.
Comments
Post a Comment