- removed 'Adm' prefix
[plcapi.git] / PLC / Methods / GetNodeNetworks.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.NodeNetworks import NodeNetwork, NodeNetworks
5 from PLC.Nodes import Node, Nodes
6 from PLC.Auth import PasswordAuth
7
8 class GetNodeNetworks(Method):
9     """
10     Returns all the networks this node is connected to, as an array of
11     structs.
12     """
13
14     roles = ['admin', 'pi', 'user', 'tech']
15
16     accepts = [
17         PasswordAuth(),
18         Mixed(Node.fields['node_id'],
19               Node.fields['hostname'])
20         ]
21
22     returns = [NodeNetwork.fields]
23
24     def call(self, auth, node_id_or_hostname):
25         # Authenticated function
26         assert self.caller is not None
27
28         # Get node information
29         nodes = Nodes(self.api, [node_id_or_hostname]).values()
30         if not nodes:
31             raise PLCInvalidArgument, "No such node"
32         node = nodes[0]
33
34         # Get node networks for this node
35         if node['nodenetwork_ids']:
36             nodenetworks = NodeNetworks(self.api, node['nodenetwork_ids']).values()
37         else:
38             nodenetworks = []
39
40         # Filter out undesired or None fields (XML-RPC cannot marshal
41         # None) and turn each node into a real dict.
42         valid_return_fields_only = lambda (key, value): value is not None
43         nodenetworks = [dict(filter(valid_return_fields_only, nodenetwork.items())) \
44                         for nodenetwork in nodenetworks]
45         
46         return nodenetworks