This replaces the existing Legacy/* support with a new approach that
[plcapi.git] / PLC / v42LegacyNodes.py
1 # $Id: $
2
3 from PLC.Parameter import Parameter, Mixed, python_type
4 from PLC.Filter import Filter
5 from PLC.Nodes import Node, Nodes
6
7 def import_deep(name):
8     mod = __import__(name)
9     components = name.split('.')
10     for comp in components[1:]:
11         mod = getattr(mod, comp)
12     return mod
13
14 # GetNodes update
15 c = getattr(import_deep("PLC.Methods.GetNodes"),"GetNodes")
16 # rename call to __origcall so we can still invoke
17 original = getattr(c,"call")
18 setattr(c,"__origcall",original)
19
20 node_fields = {}
21 node_fields['nodenetwork_ids']=Parameter([int], "Legacy version of interface_ids")
22 for k,v in Node.fields.iteritems():
23     node_fields[k]=v
24
25 accepts = getattr(c,"accepts")
26 arg0=accepts[0]
27 arg1=Mixed([Mixed(Node.fields['node_id'],
28                   Node.fields['hostname'])],
29            Parameter(str,"hostname"),
30            Parameter(int,"node_id"),
31            Filter(node_fields))
32 arg2=accepts[2]
33 newaccepts = [arg0,arg1,arg2]
34 setattr(c,"accepts",newaccepts)
35 newreturns = [node_fields]
36 setattr(c,"returns",newreturns)
37
38 def GetNodesCall(self, auth, node_filter = None, return_fields = None):
39     # convert nodenetwork_ids -> interface_ids
40     if node_filter <> None and \
41            node_filter.has_key('nodenetwork_ids') and \
42            not node_filter.has_key('interface_ids'):
43         node_filter['interface_ids']=node_filter['nodenetwork_ids']
44         
45     nodes = self.__origcall(auth,node_filter,return_fields)
46
47     # add in a interface_ids -> nodenetwork_ids
48     for node in nodes:
49         if node.has_key('interface_ids'):
50             node['nodenetwork_ids']=node['interface_ids']
51
52     return nodes
53
54 setattr(c,"call",GetNodesCall)