Python Discussions

How to Bypass or Continue netapp_ontap.error.NetAppRestError: Python

a4ashu
2,106 Views

Smart people of Netapp API I am trying to collect individual snapshot name and information to see whether a snapmirror was created on it or not

In the process I get volume information and use uuid to fetch snapshot for each volume somewhere it gets stuck and I see this 404 error  

Once I get the error for a particular volume/snapshot like below  it Breaks the  loop and exits. I understand it that there is some issue with some object  but I want to catch this and bypass the error to next volume or volume-snapshot

 

with HostConnection(hostname, username, password, verify=False):
vols = Volume.get_collection()

for volx in vols :
volx.get(fields='uuid,svm,name')
vservername = volx.svm.name
print(volx.svm.name)
snaps = Snapshot.get_collection(volx.uuid)
for snapx in snaps :
print(volx.name)
snapx.get(fields='volume,snapmirror_label,create_time,name')

 

 I get the following

 
netapp_ontap.error.NetAppRestError: Caused by HTTPError('404 Client Error: Not Found for url: https://192.168.xx.xx:443/api/storage/volumes/0090909090909yyhgg00hgv/snapshots0090909090909yyhgg00hgv?fields=volume%2Csnapmirror_label%2Ccreate_time%...😞 entry doesn't exist
 
how to catch this is error in a variable  .. its generated by snapx.get(fields='volume,snapmirror_label,create_time,name') iteration and I want to "continue" and not "break"
 
Hope I explained properly
1 ACCEPTED SOLUTION

RobertBlackhart
2,087 Views

The NetAppRestError object is the standard exception that might be raised from the library whenever the server provides an error response to the client. An error response is any API request that returns a status code of >= 400. If you want your application to continue, you should handle the exception like any other in Python with a try/except block wrapped around the block of code that might throw an exception you want to handle.

 

Here is your code with a try/except that reports the error but keeps going:

from netapp_ontap import HostConnection, NetAppRestError
from netapp_ontap.resources import Volume, Snapshot

with HostConnection(hostname, username, password, verify=False):
    vols = Volume.get_collection()

    for volx in vols :
        volx.get(fields='uuid,svm,name')
        vservername = volx.svm.name
        print(volx.svm.name)
        snaps = Snapshot.get_collection(volx.uuid)
            for snapx in snaps :
            print(volx.name)
            try:
                snapx.get(fields='volume,snapmirror_label,create_time,name')
            except NetAppRestError as e:
                print("Error fetching snapshot details: %s" % e)

On a side note, your code could be made a little more efficient. Right now, you are calling get_collection() and then for each result you are calling get() on it inside the loop to fetch additional fields. You could make this more efficient by passing the field list to get_collection() and then you don't need the get() inside. Here is the same code refactored:

from netapp_ontap import HostConnection, NetAppRestError
from netapp_ontap.resources import Volume, Snapshot

with HostConnection(hostname, username, password, verify=False):
    for volx in Volume.get_collection(fields="uuid,svm,name"):
        vservername = volx.svm.name
        print(volx.svm.name)
        for snapx in Snapshot.get_collection(volx.uuid, fields="volume,snapmirror_label,create_time,name")
            print(snapx.name)

View solution in original post

2 REPLIES 2

RobertBlackhart
2,088 Views

The NetAppRestError object is the standard exception that might be raised from the library whenever the server provides an error response to the client. An error response is any API request that returns a status code of >= 400. If you want your application to continue, you should handle the exception like any other in Python with a try/except block wrapped around the block of code that might throw an exception you want to handle.

 

Here is your code with a try/except that reports the error but keeps going:

from netapp_ontap import HostConnection, NetAppRestError
from netapp_ontap.resources import Volume, Snapshot

with HostConnection(hostname, username, password, verify=False):
    vols = Volume.get_collection()

    for volx in vols :
        volx.get(fields='uuid,svm,name')
        vservername = volx.svm.name
        print(volx.svm.name)
        snaps = Snapshot.get_collection(volx.uuid)
            for snapx in snaps :
            print(volx.name)
            try:
                snapx.get(fields='volume,snapmirror_label,create_time,name')
            except NetAppRestError as e:
                print("Error fetching snapshot details: %s" % e)

On a side note, your code could be made a little more efficient. Right now, you are calling get_collection() and then for each result you are calling get() on it inside the loop to fetch additional fields. You could make this more efficient by passing the field list to get_collection() and then you don't need the get() inside. Here is the same code refactored:

from netapp_ontap import HostConnection, NetAppRestError
from netapp_ontap.resources import Volume, Snapshot

with HostConnection(hostname, username, password, verify=False):
    for volx in Volume.get_collection(fields="uuid,svm,name"):
        vservername = volx.svm.name
        print(volx.svm.name)
        for snapx in Snapshot.get_collection(volx.uuid, fields="volume,snapmirror_label,create_time,name")
            print(snapx.name)

a4ashu
2,073 Views

Thank you Robert for your elaborate response I think this is what I was looking for . I realized quickly that I was little lazy coding ( first time ever in my life) there when I seperated the get() and get_collection. To any readers of this message please use the what Robert has asked it works .

The second thing I want to point out is how your blog on {getattr(snapx, 'snapmirror_label', 'not set')} helped , so just for everyone reading this blog there are cases where the variable is not set , leaving code in lurch. Robert gave a nice workaround of using getattr and setting it to some default specified value.

 

if any(str1 in snapx.name for str1 in snapmirrorsnapshot1) : #any(x in str for x in myList to identify )
print("\n")
print(snapx.volume.name,volx.uuid,snapx.name ,snapx.create_time,{getattr(snapx, 'snapmirror_label', 'snapmirror not set')} )

<— helped me bypass

 

As far as the exceptions are .. they do not always show up , in some cases sometimes and hence not replicable but I have added try and except to the code hopefully that might mitigate the break. I will keep testing till it breaks and hopefully this would be the final solution .

Public