Python Discussions
Python Discussions
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
Solved! See The Solution
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)
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)
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 .