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