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