nuagenetworks / vspk-python

A Python library for managing Nuage through its API
http://www.nuagenetworks.net
BSD 3-Clause "New" or "Revised" License
17 stars 19 forks source link

vspk get and fetch methods doesn’t work for nume.shared_network_resources object #12

Open agorbachev opened 7 years ago

agorbachev commented 7 years ago

get and fetch methods doesn’t work for nusharednetworkresource.NUSharedNetworkResource called as method of nume.shared_network_resources

nuage_user
<vspk.v4_0.nume.NUMe object at 0x0000000003F1DEF0>
for net in nuage_user.shared_network_resources.get(): print net.id
432d1f95-72de-46c5-8a8d-1a8ce3d15d8d
2e2d2920-686c-466e-9e2f-4522e3f46241
81abdf80-4740-4a88-b72c-7d17f7726b5e
9cbdefea-7b49-49ca-97be-de2e3c00b55d
59d348a5-1cf8-476a-af70-2a002c20bab1
7c210462-8552-4b43-a462-8cbec1b80171
7cca76b8-253e-47f4-a5e0-eb5cc3c2d63a
86f7181f-d780-4cbd-adce-5985a4cffedd
e23d530d-67dc-43e7-ad3f-703e371efef2
e0acb8b0-1355-46c7-9c52-e909b4f53180
a7e81462-bc84-406f-9467-fca1df8f0173
fbd2817f-4419-4176-b627-7b551cdf97fe
e562146a-4602-4bc4-89e0-2218a4a9fcdb
e00b8fca-18f7-40e3-8c8a-ae7a90eef76e
nuage_user.shared_network_resources.get(filter='ID=="432d1f95-72de-46c5-8a8d-1a8ce3d15d8d"')
[]
nuage_user.shared_network_resources.fetch(filter='ID=="432d1f95-72de-46c5-8a8d-1a8ce3d15d8d"')[2]
[]
vsdk.NUSharedNetworkResource(id="432d1f95-72de-46c5-8a8d-1a8ce3d15d8d").fetch()[0]
<vspk.v4_0.nusharednetworkresource.NUSharedNetworkResource object at 0x0000000006118160>

but this approach works for nusubnet object

for net in nuage_user.subnets.get()[0:10]: print net.id
bb960d58-e61f-48e9-9ea6-2aceaef577f4
c5746714-5dd9-4494-bcff-43e86be64f51
eabbe212-358c-477f-95dd-579910a01951
73963fef-3cf1-41ba-a6f5-84b764cf49db
a46aba38-6f78-4ac9-9d94-00625055140b
432d1f95-72de-46c5-8a8d-1a8ce3d15d8d
be2de3df-b3db-4934-8d10-b2d3bb720239
5659d983-f5a8-4ca2-b49f-e00fd87d1230
09fa4f79-ef2c-4809-8988-a8c474563be1
13956992-bb7f-4f5a-a2d8-3e61780ad863
nuage_user.subnets.get(filter='ID=="bb960d58-e61f-48e9-9ea6-2aceaef577f4"')
[<vspk.v4_0.nusubnet.NUSubnet object at 0x0000000007249278>]
nuage_user.subnets.fetch(filter='ID=="bb960d58-e61f-48e9-9ea6-2aceaef577f4"')[2]
[<vspk.v4_0.nusubnet.NUSubnet object at 0x000000000661CB00>]
vsdk.NUSubnet(id='bb960d58-e61f-48e9-9ea6-2aceaef577f4').fetch()[0]
<vspk.v4_0.nusubnet.NUSubnet object at 0x000000000642BD68>
pdellaert commented 7 years ago

@agorbachev The only issue i see is the get with a filter on the ID not working for shared resources, correct. This does work (you don't have to do the array, btw):

shared_resource = vsdk.NUSharedNetworkResource(id="432d1f95-72de-46c5-8a8d-1a8ce3d15d8d")
shared_resource.fetch()

When you have the ID of an object, please don't do a get with a filter, this is highly inefficient. Instead create an object of the type, configure the id with the ID you have and do a fetch on that object. That will update the object with all the information from the VSD DB.

agorbachev commented 7 years ago

@pdellaert Yes, you are correct, the issue is with not working get and fetch methods by id for shared networks. At certain situation (for example, when searching parent obj) there is no way to get object type name. You have only parent_type and parent_id. in such situation I use

parrent_obj = nuage_user.fetcher_for_rest_name(child.parent_type).get_first(child.parent_id)

btw child.parent_obj returns None object

hellt commented 7 years ago

I second it, as this bothers me as well. In many situations when you want to streamline the code and apply recursion it is needed to know the parent in camelCase to fetch and object by its ID.

Why do we do this? If we can't fetch an object by its ID without specifying the type of the object (performance optimization?) then having parent object camelCase name available to its child suggests itself. Otherwise it leaves no options but using fetchers+filters

For example the following code makes sense:

from vspk import v4_0 as vspk
# child_obj = 'NUSubnet'
parent_camelCase = child_obj.parent_camelCase
parent_obj = getattr(vspk, parent_camelCase)
pdellaert commented 7 years ago

Can you give me an exact use case, cause i seem to be confused... child.parent_type gives you the camelCase class name of the parent.

parent_obj is set if you do a parent.childs.get(), not necessarily when you do child = class(id='...'); child.fetch()

agorbachev commented 7 years ago

@pdellaert parent_type does not give camelCase. Example:

from vspk import v4_0 as vsdk
nc = vsdk.NUVSDSession(username='csproot',
                       password='csproot',
                       enterprise='csp',
                       api_url="https://10.166.72.100:8443")
nc.start()
nuage_user = nc.user
subnet = nuage_user.subnets.get_first()
print subnet.parent_type

print will return

u'zone'
pdellaert commented 7 years ago

aha, seems i was mistaken :), sorry

Well, still not sure what the use case is, but if you really need the parent_type to go into the python camelCase class name, not sure if we have something for that :s

hellt commented 7 years ago

@pdellaert i have updated my comment, showing the code that seems reasonable to me, but for it to work we need to have

pdellaert commented 7 years ago

having vspk.zone() be treated as vspk.NUZone() is an impossibility. That would mean we need to create two classes with the same exact code. That is just bad and against PEP8

Best case is to have a method, in the me object, that does a translation from zone to NUZone. Using that, you can use getattr() to do: new_zone = getattr(vspk, session.rest_name_to_class_name('zone')) or something like that

No promise, and if you want to look into this, feel free to investigate and do a PR on the vspk generator. (Which will need to be very heavily tested and reviewed of course)