OpenIxia / IxNetwork

A central location for IxNetwork sample scripts and utilities. Please also visit http://openixia.com
MIT License
50 stars 59 forks source link

RestPy:Any example to retreive object using href? #40

Closed muthvar1 closed 5 years ago

muthvar1 commented 5 years ago

Hi Team, Is there a way to get the object using the href of an object? For instance if I want to get the href of a session or an ixnetwork object etc?

Varghese

ajbalogh commented 5 years ago

"""Demonstrates how to get an object given an href

The TestPlatform.Sessions class has a helper method that assists in returning an object given a valid href

"""

from ixnetwork_restpy.testplatform.testplatform import TestPlatform

test_platform = TestPlatform('127.0.0.1', rest_port=11009) test_platform.Trace = 'request_response'

sessions = test_platform.Sessions.add()

ixnetwork = sessions.Ixnetwork ixnetwork.NewConfig()

vport = ixnetwork.Vport.add()

""" the following code is an attempt to get the object that the vport is connected to in this case the href that is returned from the .ConnectedTo property is null so the object returned is None if the vport was connected to an actual hardware port a valid /availableHardware/chassis/card/port object reference would be returned from the Vport.ConnectedTo property this reference is then used to get an actual object """ hardware_port = sessions.GetObjectFromHref(vport.ConnectedTo) assert(hardware_port is None)

muthvar1 commented 5 years ago

But what if the use case is to get the actual session handle itself? Here you have an object under sessions. But I want to get the session object itself.

ajbalogh commented 5 years ago

There is a property called .href on every object that is the 'handle' of the object. This is a valid href for the object - if you have the object why do you need to get the href to get the object - you have the object. Please post some code that outlines what you are trying to solve.

muthvar1 commented 5 years ago

There could me multiple reasons for a lookup by href One of them is below. Say I have multiple sessions open on the linux api server. I have the href (string) to the session that has certain configuration. I can get back into a session by just retrieving the object using the href. Note that this is not all done within one script, so I would not have the initial session object created in the first run where the session was added. This and many more use cases are there where you would need a lookup by href.

ajbalogh commented 5 years ago

There is no need to find a session by 'href', the actual way to do it is to use the 'id' which is part of the .find method. The entire library is based around the find method. Sessions.find(Id=x) is what you should be using.

muthvar1 commented 5 years ago

Thanks. This helps for the session use case. Will use this from now

whandjr commented 5 years ago

For sessions Id is a good way to find what you want. for other types of objects, other properties might be better to find, for example Topology.find(Name="Topo1") is also a good way to use the find method.

The documentation will show you all the properties that can be used to find, and i believe they can be used in combination to be more and more specific.

Topology.find(DescriptiveName=None, Errors=None, Name=None, Note=None, PortCount=None, Ports=None, PortsStateCount=None, Status=None, Vports=None)

muthvar1 commented 5 years ago

Not to extend this thread, but I think it would be good to add code for an href lookup across all Objects. This really helps because it seems like the href is the only fully resolved named property that accurately describes and object. i.e. an href is always unique across all objects across all classes.

muthvar1 commented 5 years ago

There is no need to find a session by 'href', the actual way to do it is to use the 'id' which is part of the .find method. The entire library is based around the find method. Sessions.find(Id=x) is what you should be using.

Has anyone tried this? I can't seem to get the ixnetwork object or any other object instance using this

session = testPlatform.Sessions.find(Id='17') 2019-03-05 00:04:34 [ixnetwork_restpy.connection] [DEBUG] GET https://10.23.239.233:443/api/v1/sessions None 2019-03-05 00:04:34 [ixnetwork_restpy.connection] [DEBUG] 200 OK ixNetwork = session.GetObjectFromHref('/api/v1/sessions/17/ixnetwork') ixNetwork ixNetwork = session.GetObjectFromHref(href = '/api/v1/sessions/17/ixnetwork') ixNetwork

r = session.GetObjectFromHref(href = '/api/v1/sessions/17/ixnetwork/topology') r