Tuesday, 12 April 2022

[CI][Python] Collecting NetApp Cloud Insights Annotations and Annotation Values and Export to CSV

A Python script to connect to your NetApp Cloud Insights tenant, and collect the annotations and annotation values, and then export to CSV.

Save the script as say GetCiAnnosAndValues.py and run from your command prompt as> python GetCiAnnosAndValues.py (remembering to set the highlighted bits a per your environment.)

Note: If you're running from a corporate machine, you may need to set you command prompt proxy as per this post.

The Script

## IMPORT THE ESSENTIALS ##
import csv
import json
import urllib3
import requests
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

## USER/ACQUISITION SPECIFIC VARIABLES ##
api = 'assets/annotations'
api_key = 'YOURAPIKEY'
yourTenant = 'YOURTENANT' # This is the bit that comes before .cloudinsights.netapp.com

## REST API GET ##
url = 'https://' + yourTenant + '.cloudinsights.netapp.com/rest/v1/' + api
headers = {'X-CloudInsights-ApiKey': '{key}'.format(key=api_key)}
jsondata = requests.get(url, headers=headers, verify = False).json()

## INITIALIZE 1 LIST AND 2 DICTS TO STORE THE DATA ##
annoNames = []
annoTypes = {}
annoValues = {}

## STORING THE DATA ##
for a in jsondata:
 if a['isUserDefined'] == True:
  if a['name'] not in annoNames:
   annoNames += [a['name']]
   annoTypes[a['name']] = a['type']
   annoValues[a['name']] = []
   for v in a['enumValues']:
    annoValues[a['name']] += [v['name']]
  else:
   for v in a['enumValues']:
    if v['name'] not in annoValues[a['name']]:
     annoValues[a['name']] += [v['name']]

## INITIALIZE A FILE FOR THE CSV OUTPUT ##
output = open('CiAnnosAndValues.csv',mode='w',newline='')
writer = csv.writer(output, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
headings = ['Anno Name','Anno Type','Anno Value']
writer.writerow(headings)

## WRITE THE DATA TO CSV ##
for a in annoNames:
 for v in annoValues[a]:
  row=[]
  row.append(a)
  row.append(annoTypes[a])
  row.append(v)
  writer.writerow(row)

## CLOSE THE CSV ##
output.close()

No comments:

Post a Comment

Note: only a member of this blog may post a comment.