[OCI][Python] Collecting All NetApp OCI Annotation Rules Across Multiple OCI Servers and Export to CSV

Carrying on from the last post.

 A Python script to connect to multiple NetApp OnCommand Insight Servers in turn, and collect all the Annotation Rules and some useful information. Perhaps useful if you're doing a consolidation exercise (like  many OCI to one CI).

Save the script as say GetAnnoRules.py and run from your prompt as python GetAnnoRules.py.

Note: The script is very similar to the last post. The cycle through bit is a bit clear laid out.

The Script

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

## SETUP CREDENTIALS FOR THE OCI SERVERS (ASSUMING ALL THE SAME) ##
userAndPass = b64encode(b"USERNAME:PASSWORD").decode("ascii")
headers = {'Authorization':'Basic %s' % userAndPass}

## CREATE A LIST OF OCI SERVERS ##
OCIS=[]
OCIS.append("OCI_SERVER1_FQDN")
OCIS.append("OCI_SERVER2_FQDN")
OCIS.append("OCI_SERVER3_FQDN")

## INITIALIZE A FILE FOR THE CSV OUTPUT ##
output = open('annorules.csv',mode='w',newline='')
writer = csv.writer(output, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['OCI Server','Anno Rules Name','Evaluation Order','Query Name','Object Type','Anno Name','Anno Value'])

## CYCLE THROUGH THE OCIS COLLECTING THE ANNOTATION RULES ##
for OCI in OCIS:
  method = "GET"
  baseurl = "https://" + OCI + "/rest/v1/"
  api = "admin/rules"
  response = requests.request(method,baseurl + api, headers = headers, verify = False)
  json_data = json.loads(response.text)

  for annorule in json_data:
    row=[]
    row.append(OCI)
    row.append(annorule["name"])
    row.append(annorule["evaluationOrder"])
    row.append(annorule["query"]["name"])
    row.append(annorule["query"]["objectType"])
    row.append(annorule["annotation"]["name"])
    row.append(annorule["value"])
    writer.writerow(row)

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

Comments