Friday, 11 September 2020

Python 3: OCI REST API - Set Annotation On Storage Array

In the NetApp OCI API Samples for Python (available from the OCI Operational Server WebUI > REST API documentation > API samples) there is a set_annotation.py which can be used to set the annotation on a specific volume. With a slight modification of the Python, we get a set_annotation_storage.py which can be used to set the annotation on a specific storage. An example of using it>

python set_annotation_storage.py --url https://YOUR_OCI_SERVER --user YOUR_REST_USER --password YOUR_PASSWORD --storage STORAGE_NAME -annotation SOME_ANNOTATION --value ANNOTATION_VALUE

Image: Insight REST API documentation > API samples

The Script

Save as say ‘set_annotation_storage.py’.

#!/usr/bin/env python 

"""

Set annotation for a specific storage

"""


from __future__ import print_function

import json

from oci_rest import OciRest, configure_command_line_parser


def find_storage(oci, storage_name):

  for storage in oci.get('assets/storages'):

    if storage['name'] == storage_name:

      return storage['self']

  return None


def annotate_storage(oci, storage_url, annotation_name, annotation_value):

  return oci.put('{}/annotations'.format(storage_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 storage annotation')

  

  # Add additional arguments to configure the search criteria

  parser.add_argument('--storage', required=True, help="Storage 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:

    storage_url = find_storage(oci, storage_name=options.storage)

    

    if storage_url is not None:

      new_annotation = annotate_storage(oci,

          storage_url,

          annotation_name=options.annotation,

          annotation_value=options.value)

      

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

      

    else:

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

APPENDIX: List of the 14 out-of-the-box ‘NetApp OCI API Samples for Python’

  • basic.py - print list of all storagePools with storage name and total allocatedCapacity
  • annotation_type.py - manipulate annotation types
  • business_entity.py - manipulate business entities
  • collection_report.py - Print status report for each datasource
  • create_user.py - Create OCI user
  • filter_storagepool_by_thresholds.py - print list of storage pools filtered by storage tier, and thresholds
  • filter_vms_by_storage.py - print virtual machines with dataStores on specific storage
  • license.py - Programmatically manipulate OCI license
  • import_patch.py - Import datasource patch
  • set_annotation.py - Set annotation for a specific volume
  • set_annotation_based_on_path.py - Set datacenter annotation for hosts based on their connected storage annotation
  • set_annotation_bulk.py - Set annotation for a group of volumes identified with prefix
  • set_ldap_config.py - set values for LDAP configuration
  • integrations.py - create, delete agents and integrations. Ingest integration data.

No comments:

Post a comment