Understanding update and delete operations using Python client library

In the previous blog post, we discussed how to create and retrieve a NetApp® ONTAP® object of a type using the Python client library (PCL). We also saw how to use PCL methods like get_collection(), from_dict(), to_dict(), post(), and get(). This post, Part 3, discusses how to do update(PATCH) and delete(DELETE) operations on ONTAP objects using PCL methods. Also,  a few PCL sample scripts are available on GitHub for reference.

Update an ONTAP object using the Python client library

The following code snippet shows how to use PCL to do an update operation by delving into the code. This code snippet updates the name of an existing volume:

# STEP 1
new_volume_name= input("Enter the new name of the Volume: ")
try:
# STEP 2
    volume = Volume.find(name=volume_name)
# STEP 3
    volume.name = new_volume_name
# STEP 4
try:
    if volume.patch(poll=True):
        print("The Volume  has been updated/patched Successfully")
except NetAppRestError as error:
    print("Exception caught :" + str(error))

Step 1

# STEP 1
new_volume_name= input("Enter the new name of the Volume: ")

This step obtains the new name to update the name of an existing volume.

Step 2

# STEP 2
    volume = Volume.find(name=volume_name)

In this step, we use the PCL find() method to find an instance of an object on the host, given a query. The host is queried with the provided key/value pairs to find a matching resource. If 0 are found, None is returned. If more than 1 are found, an error is raised or returned. If there is exactly 1 matching record, it is returned.

The find() method takes the following as argument:*args, which represents a parent key that is used to build the  /api/foos/{foo.name}/bars; connection (storage connection); max_records (maximum records to be returned); **kwargs, any key/value pairs that can be sent as query parameters.

Here are a few examples:

  • The following example shows how to use the find method by passing a dictionary:

     Volume.find(**{'svm.name': svm_config['name'], 'comment': 'find me'})

  • The following example shows how to use the find method by passing a specific parameter as a key value:

     Svm.find(name=svmname)

  • The following example shows how to use the find method for objects like qtree and snapshots: /storage/volumes/{volume.uuid}/qtree/{uuid}.

     Qtree.find(vol_uuid,name=qtree_name) 

Step 3

# STEP 3
    volume.name = new_volume_name

This step sets the new volume name to the instance of the volume object that has been returned by the find method.

Step 4:  

# STEP 4
try:
if volume.patch(poll=True):
print("The Volume  has been updated/patched Successfully")
except NetAppRestError as error:
print("Exception caught :" + str(error))

Now we update the volume object with the new volume name. The patch method sends the difference in the object’s state to the host as a modification request, calculates the difference in the object’s state since the last time it interacted with the host, and sends that information in the request body.

There are quite a few arguments that can be used along with the patch method. If the hydrate argument is set to True, after the response is received from the call, a GET call is made to refresh all fields of the object. If the poll argument is set to True, the call is not returned until the asynchronous job on the host has completed. The poll_interval and poll_timeout arguments can be specified to set the frequency to query the jobs and how long the job status should be monitored. The **kwargs argument can be specified to send the query parameters to the host.

Here are a few examples.

  • This example shows how to do a patch operation by sending poll as True and hydrate as True: 

     aggr.post(poll=True, hydrate=True)        

  • This example shows how to do a patch operation to send the query:  

     response = cluster.post(**params)  

Batch updates on a set of ONTAP objects using the Python client library

The Python client library gives you the option of doing batch update operations on a set of ONTAP objects. For example, it’s possible to use a single method call to offline a set of volumes and online a set of volumes. The patch_collection method makes this feat possible.

This method patches all objects in a collection that match the given query. All records on the host that match the query are patched with the provided body.

There are quite a few arguments that can be used along with the patch_collection method. It accepts a dictionary of name/value pairs (body) to set on all matching members of the collection;  connection (storage connection); max_records (maximum records to be returned); **kwargs, any key/value pairs that can be sent as query parameters.

Here is an example:

  • This example offlines a set of volume that belongs to a particular SVM and whose name starts with “python-Vol”:

     print("offline volumes using patch_collection")
     volumes = Volume.patch_collection({'state': 'offline '}, svm=svm_config['name'], name='python_Vol*')

Delete an ONTAP object by using Python client library

 The following code example shows how to do a delete operation by using the Python client library:

# STEP 1
new_volume_name= input("Enter the name of the Volume that needs to be deleted: ")
# STEP 2
volume = Volume.find(name=volume_name)
# STEP 3
try:
if volume.delete(poll=True):
print("The Volume  has been deleted Ssccessfully")
except NetAppRestError as error:
print("Exception caught :" + str(error))

Step 1

# STEP 1
new_volume_name= input("Enter the name of the Volume that needs to be deleted: ")

This step obtains the name of the volume to be deleted.

Step 2

# STEP 2
volume = Volume.find(name=volume_name)

This step uses the PCL find method to find an instance of an object on the host, given a query. The host is queried with the provided key/value pairs to find a matching resource.  (This method is discussed in the previous section.)

Step 3  

# STEP 3
try:
if volume.delete(poll=True):
print("The Volume  has been deleted Ssccessfully")
except NetAppRestError as error:
print("Exception caught :" + str(error))

Now we delete the volume object instance that was returned by the  find method. The delete method sends a deletion request to the host for this object.

There are quite a few arguments that can be used with the delete method. It accepts a dictionary of name/value pairs (body) to set on all matching members of the collection.  If the poll argument is set to True, the call is not returned until the asynchronous job on the host has completed. The poll_interval and poll_timeout arguments can be specified to set the frequency to query the jobs and how long the job status should be monitored. The **kwargs argument can be specified to send the query parameters to the host.

Here are a few examples:

  • ·This example shows how to do a delete operation by sending query parameters to the host:  

     cifs_serv1.delete({'ad_domain':{'user':cifs_serv_info['ad_domain']\['user'],'password':cifs_serv_info['ad_domain']['password']}}) ·        

  • This example shows how to do a delete operation with a poll timeout and poll interval set:  

     svm1.delete(poll_timeout=60, poll_interval=5) 

Batch deletion on a set of ONTAP objects using Python client library

The Python client library gives you the option of doing batch delete operations on a set of ONTAP objects. For example, it’s possible to delete a set of volumes by using a single method call, the delete_collection method.

This method call deletes all objects in a collection that match the given query. All records on the host that  match the query are deleted with the provided body.

There are quite a few arguments that can be used with the delete_collection method. It accepts a dictionary of name/value pairs (body) to set on all matching members of the collection; a GET call is made to refresh all fields of the object connection (storage connection); max_records (maximum records to be returned); **kwargs, any key/value pairs that can be sent as query parameters.

Here are a few examples:

  • This example deletes a set of volumes that belongs to a particular SVM and whose name starts with “python-Vol”:

     print("Delete volumes using delete_collection without pagination")
     volumes = Volume.delete_collection({'state': 'Offline'}, svm=svm_config['name'], name='python_Vol*')

  • This example deletes a set of snapshot policies that was retrieved by using the get _collection() method using a specific query:

     Print("DELETE COLLECTION: Deleting snapshot policy {}".format(pol_info['name']))
     SnapshotPolicy.delete_collection(name=pol_info['name'])

Let us know…

Now that we’ve finished discussing ONTAP object operations like create, retrieve, update, and delete using the Python client library, you can go ahead start exploring. More PCL sample scripts are available for reference in GitHub. You can bring up a Lab on Demand instance to try out these scripts. Be sure to read the setup instructions (in the lod folder under the ontap-rest-python repository in Github) to start executing the scripts on the instance.

In this blog series, we haven’t covered every possible scenario, and we know that you’ll have questions and concerns, so please contact us at ng-ontap-restapi-queries@netapp.com, or connect with us through Slack. To report any issues, file them on the GitHub Issues page.

Part 1 > Part 2 > Part 3

 

Jacob Andathethu on EmailJacob Andathethu on Linkedin
Jacob Andathethu
Technical Marketing Engineer at NetApp
A dynamic professional with over 13 years of experience working in Data Storage Industry [NetApp and Dell-EMC]
Currently working as a Technical Marketing Engineer for Open Ecosystem Products in NetApp (Docker,Docker Swarm,Kubernetes, OpenShift).

Pin It on Pinterest