Python 3: OCI REST API - Set Annotation On Switch

 A very similar post to this September 2020 post:

Cosonok's IT Blog: Python 3: OCI REST API - Set Annotation On Storage Array

- which just goes to show that if you have a good framework (not mine, these were from the out-the-box OCI Python examples) then it's very easy to adapt that framework for other use cases (in this case switch and not storage.) To use the python script below:

python --url https://YOUR_OCI_SERVER --user YOUR_REST_USER --password YOUR_PASSWORD --switch SWITCH_NAME --annotation SOME_ANNOTATION --value ANNOTATION_VALUE

The Script

Save as '':

#!/usr/bin/env python 

Set annotation for a specific switch

from __future__ import print_function
import json
from oci_rest import OciRest, configure_command_line_parser

def find_switch(oci, switch_name):
  for switch in oci.get('assets/switches'):
    if switch['name'] == switch_name:
      return switch['self']
  return None

def annotate_switch(oci, switch_url, annotation_name, annotation_value):
  return oci.put('{}/annotations'.format(switch_url), data=json.dumps(
        "rawValue": annotation_value,
        "definition": {"name": annotation_name}

if __name__ == "__main__":

  # Get the default command-line arguments (url, user, password)
  parser = configure_command_line_parser(usage='Set switch annotation')

  # Add additional arguments to configure the search criteria
  parser.add_argument('--switch', required=True, help="Switch name")
  parser.add_argument('--annotation', required=True, help="Annotation name (do NOT use label!)")
  parser.add_argument('--value', required=True, help="Annotation value to set")

  options = parser.parse_args()
  url, user, password = options.url, options.user, options.password

  with OciRest(url, user, password) as oci:
    switch_url = find_switch(oci, switch_name=options.switch)

    if switch_url is not None:
      new_annotation = annotate_switch(oci,

      print('Created new annotation:\n', json.dumps(new_annotation))

      print("Could not find switch {}".format(options.switch))

Image: OCI REST API documentation > API samples