Tuesday, 12 April 2022

[OCI][Python] Collecting NetApp OCI Annotations and Annotation Values Across Multiple OCI Servers and Export to CSV

A Python script to connect to multiple NetApp OnCommand Insight Servers in turn, and collect the annotations and annotation values, and then export to CSV.

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

The Script

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

## USER/ACQUISITION SPECIFIC VARIABLES ##
api       = 'assets/annotations'
username  = 'YOURUSERNAME'
password  = 'YOURPASSWORD'
ocis = []
ocis.append('OCISERVER1')
ocis.append('OCISERVER2')
ocis.append('OCISERVER3')

## SETUP CREDENTIALS (assume same on all OCIs) ##
s = username + ':' + password
userAndPass = b64encode(s.encode('ascii')).decode("ascii")
headers = {'Authorization':'Basic %s' % userAndPass}

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

## CYCLE THROUGH THE OCIS ##
for oci in ocis:

 ## ACQUIRE DATA OVER REST API ##
 url = "https://" + oci + "/rest/v1/" + api
 jsondata = requests.get(url, headers=headers, verify = False).json()

 ## 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('OciAnnosAndValues.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.