Python Discussions

Basic question around accessing single result

__mattb__
3,378 Views

Hello, I've seen several examples around accessing results from get-iter but I'm struggling to access a single result and the examples in the 9.4 API seem to rely on deprecated api calls.

 

<results status="passed">
	<attributes>
		<snapmirror-info>
			<destination-location>vsm01:vsm01_my_volume</destination-location>
			<destination-volume>vsm01_my_volume</destination-volume>
			<destination-vserver>vsm01</destination-vserver>
			<lag-time>1143</lag-time>
			<source-location>vsm01:my_volume</source-location>
			<source-volume>my_volume</source-volume>
			<source-vserver>vsm01</source-vserver>
		</snapmirror-info>
	</attributes>
</results>

My code snippet is as follows, my functions returns the ZExplore generated 'xo' object as 'XMLObject', how can I access the lat-time key/value please, thanks.

 

def getLagTime(strCluster, strUser, strPassword, strDestVserver, strDestVolume):
    s = NaServer(strCluster, 1 , ONTAPI_VERSION)
    s.set_server_type("FILER")
    s.set_transport_type("HTTPS")
    s.set_port(443)
    s.set_style("LOGIN")
    s.set_admin_user(strUser, strPassword)

    api = NaElement("snapmirror-get")

    xi = NaElement("desired-attributes")
    api.child_add(xi)

    xi1 = NaElement("snapmirror-info")
    xi.child_add(xi1)

    xi1.child_add_string("lag-time","<lag-time>")
    api.child_add_string("destination-volume",strDestVolume)
    api.child_add_string("destination-vserver",strDestVserver)

    xo = s.invoke_elem(api)
    if (xo.results_status() != "passed") :
        print ("Error:\n")
        print (xo.sprintf())
        # If the check fails, we exit with 'UNKNOWN' according to the nagios spec.
        sys.exit (UNKNOWN)

    #print ("Received:\n")
    #print (xo.sprintf())
    
    return xo

# Call the function
XMLObject = getLagTime(strCluster, strUser, strPassword, strDestVserver, strDestVolume)

print ("Received SnapMirror Lag Time Data:")
print (XMLObject.sprintf())

 

1 ACCEPTED SOLUTION

__mattb__
3,362 Views

Ah, I finally managed to answer my own question - posting here in case anyone else has the same query

 

# Call the function
XMLObject = getLagTime(strCluster, strUser, strPassword, strDestVserver, strDestVolume)

# Debug info:
print ("Received SnapMirror Lag Time Data:")
print (XMLObject.sprintf())
print ("\n")

# Walk the tree and pull out the lag-time value.
mynewlagtime = XMLObject.child_get("attributes").child_get("snapmirror-info").child_get_int("lag-time")

print (mynewlagtime)

View solution in original post

2 REPLIES 2

__mattb__
3,363 Views

Ah, I finally managed to answer my own question - posting here in case anyone else has the same query

 

# Call the function
XMLObject = getLagTime(strCluster, strUser, strPassword, strDestVserver, strDestVolume)

# Debug info:
print ("Received SnapMirror Lag Time Data:")
print (XMLObject.sprintf())
print ("\n")

# Walk the tree and pull out the lag-time value.
mynewlagtime = XMLObject.child_get("attributes").child_get("snapmirror-info").child_get_int("lag-time")

print (mynewlagtime)

gaurav_verma
3,294 Views

You can also use xmltodict also to make it simple for these kind of tasks. 

Here is volume API example. 

 

import xmltodict
.
.
.
xo = s.invoke_elem(api)
obj = xmltodict.parse(xo.sprintf())
jd = json.dumps(obj)
jl = json.loads(jd)

if xo.results_status() == "failed":
	reason = xo.results_reason()
	print ("Failure: %s" %reason)
else:
	for h in jl["results"]["attributes-list"]["volume-attributes"]:
		print (h["volume-id-attributes"]["owning-vserver-name"], h["volume-id-attributes"]["name"], h["volume-space-attributes"]["percentage-size-used"])					
		

 

 

Public