svn keywords
[plcapi.git] / PLC / Methods / GetNodeGroups.py
1 # $Id$
2 # $URL$
3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Filter import Filter
7 from PLC.Auth import Auth
8 from PLC.NodeGroups import NodeGroup, NodeGroups
9
10 class v43GetNodeGroups(Method):
11     """
12     Returns an array of structs containing details about node groups.
13     If nodegroup_filter is specified and is an array of node group
14     identifiers or names, or a struct of node group attributes, only
15     node groups matching the filter will be returned. If return_fields
16     is specified, only the specified details will be returned.
17     """
18
19     roles = ['admin', 'pi', 'user', 'tech', 'node', 'anonymous']
20
21     accepts = [
22         Auth(),
23         Mixed([Mixed(NodeGroup.fields['nodegroup_id'],
24                      NodeGroup.fields['groupname'])],
25               Filter(NodeGroup.fields)),
26         Parameter([str], "List of fields to return", nullok = True)
27         ]
28
29     returns = [NodeGroup.fields]
30   
31     def call(self, auth, nodegroup_filter = None, return_fields = None):
32         return NodeGroups(self.api, nodegroup_filter, return_fields)
33
34
35 nodegroup_fields = NodeGroup.fields.copy()
36 nodegroup_fields['name'] = Parameter(str, "Legacy version of groupname", max = 50),
37
38 class v42GetNodeGroups(v43GetNodeGroups):
39     """
40     Legacy wrapper for v42GetNodeGroups.
41     """
42
43     accepts = [
44         Auth(),
45         Mixed([Mixed(NodeGroup.fields['nodegroup_id'],
46                      NodeGroup.fields['groupname'])],
47               Filter(nodegroup_fields)),
48         Parameter([str], "List of fields to return", nullok = True)
49         ]
50
51     returns = [nodegroup_fields]
52   
53     def call(self, auth, nodegroup_filter = None, return_fields = None):
54         # convert name -> groupname in both filters
55         if isinstance(nodegroup_filter, dict):
56             if nodegroup_filter.has_key('name'):
57                 groupname = nodegroup_filter.pop('name')
58                 if not nodegroup_filter.has_key('groupname'):
59                     nodegroup_filter['groupname']=groupname
60
61         if isinstance(return_fields, list):
62             if 'name' in return_fields:
63                 return_fields.remove('name')
64                 if 'groupname' not in return_fields:
65                     return_fields.append('groupname')
66
67         nodegroups = NodeGroups(self.api, nodegroup_filter, return_fields)
68         # if groupname is present, then create a name mapping
69         for nodegroup in nodegroups:
70             if nodegroup.has_key('groupname'):
71                 nodegroup['name']=nodegroup['groupname']
72         return nodegroups
73
74 class GetNodeGroups(v42GetNodeGroups):
75     """
76     Returns an array of structs containing details about node groups.
77     If nodegroup_filter is specified and is an array of node group
78     identifiers or names, or a struct of node group attributes, only
79     node groups matching the filter will be returned. If return_fields
80     is specified, only the specified details will be returned.
81     """
82
83     pass