Sunday, 15 May 2022

[CI][REST API][Python] Adding Annotation Values via the REST API

Work In Progress

Some more NetApp Cloud Insights REST API with Python basics here.

Again using Python like a shell, no creating and running scripts.

We want to update some annotations via the REST API. We have already got a list of annotations not found in CI using the method here: Comparing OCI vs CI Annotations and Annotation Values.

0) Setup

Notes: 1) We're just running things directly in the Python Client, no creating scripts here. 2) Update the highlighted bits as per your CI tenant.

  • import requests
  • import urllib3
  • urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
  • apiKey = "YOUR_API_KEY"
  • yourTenant = ""
  • baseUrl = "https://" + yourTenant + "/rest/v1/"
  • headers = {'X-CloudInsights-ApiKey': '{key}'.format(key=apiKey)}
1) Get Annotation ID from the Annotation Name

This is written as a Python one liner.

a = "YOURANNOTATION";j = requests.get(baseUrl + "assets/annotations/" + a, headers=headers, verify=False).json();j['id']

Other useful stuff - 1) To get display all the values2) The count of all the values:

for v in j['enumValues']: print(v['name'])

Other useful stuff - 2) The count of all the values:


Other useful stuff - 3) Finding out if the value is already in enumValues:

values = []
for v in j['enumValues']: values += [v['name']]
if 'TEST_DATA' in values: print("EXISTS!")

2) Add a Value to the Annotation you've just got the Annotation ID for

2.1) Type FLEXIBLE_ENUM (List in the UI)

The REST API to add the annotation is:

PATCH /assets/annotations/{id}

The request body needs to include all the existing values (you could remove existing values that aren't in use and no longer required) and the new value(s).

For example, for a Country annotation where you already have Argentina and Brazil, and you want to add Chile, the request body is:

  "name": "Country",
  "description": "",
  "enumValues": [
      "name": "ARGENTINA"
      "label": "ARGENTINA"
      "name": "BRAZIL"
      "label": "BRAZIL"
      "name": "CHILE"
      "label": "CHILE"

In Python we can use (also see here):

requests.patch(url, data=json.dumps(payload), headers=headers, verify=False)

To be continued.

No comments:

Post a Comment

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