- value returned is now xml-rpc safe
[plcapi.git] / PLC / Methods / AdmGetNodeGroups.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.Sites import Site, Sites
7 from PLC.Nodes import Node, Nodes
8 from PLC.Auth import PasswordAuth
9 from PLC.NodeGroups import NodeGroup, NodeGroups
10 class AdmGetNodeGroups(Method):
11     """
12     Returns a list of structs containing the details about the node groups 
13     specified. 
14
15     """
16
17     roles = ['admin', 'pi', 'user', 'tech']
18
19     accepts = [
20         PasswordAuth(),
21         [Mixed(NodeGroup.fields['nodegroup_id'],
22                NodeGroup.fields['name'])]
23         ]
24
25     returns = [NodeGroup.all_fields]
26   
27     def __init__(self, *args, **kwds):
28         Method.__init__(self, *args, **kwds)
29         # Update documentation with list of default fields returned
30         self.__doc__ += os.linesep.join(Site.default_fields.keys())
31
32     def call(self, auth, nodegroup_id_or_name_list=None):
33         # Authenticated function
34         assert self.caller is not None
35
36         # Get nodes in this nodegroup
37         nodegroups = NodeGroups(self.api, nodegroup_id_or_name_list).values()   
38
39         # make sure sites are found
40         if not nodegroups:
41                 raise PLCInvalidArgument, "No such site"
42         elif nodegroup_id_or_name_list is None:
43                 pass
44         elif not len(nodegroups) == len(nodegroup_id_or_name_list):
45                 raise PLCInvalidArgument, "at least one node group id is invalid"
46         
47         # Filter out undesired or None fields (XML-RPC cannot marshal
48         # None) and turn each node into a real dict.
49         valid_return_fields_only = lambda (key, value): \
50                                    key in NodeGroup.all_fields and value is not None
51         nodegroups = [dict(filter(valid_return_fields_only, nodegroup.items())) \
52                  for nodegroup in nodegroups]
53
54         return nodegroups