svn keywords
[plcapi.git] / PLC / Methods / UpdateNodeGroup.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.NodeGroups import NodeGroup, NodeGroups
7 from PLC.Auth import Auth
8
9 can_update = lambda (field, value): field in ['groupname','value'] 
10
11 class UpdateNodeGroup(Method):
12     """
13     Updates a custom node group.
14      
15     Returns 1 if successful, faults otherwise.
16     """
17
18     roles = ['admin']
19
20     nodegroup_fields = dict(filter(can_update, NodeGroup.fields.items()))
21
22     accepts = [
23         Auth(),
24         Mixed(NodeGroup.fields['nodegroup_id'],
25               NodeGroup.fields['groupname']),
26         nodegroup_fields
27         ]
28
29     returns = Parameter(int, '1 if successful')
30
31     def call(self, auth, nodegroup_id_or_name, nodegroup_fields):
32         nodegroup_fields = dict(filter(can_update, nodegroup_fields.items()))
33
34         # Get nodegroup information
35         nodegroups = NodeGroups(self.api, [nodegroup_id_or_name])
36         if not nodegroups:
37             raise PLCInvalidArgument, "No such nodegroup %r"%nodegroup_id_or_name
38         nodegroup = nodegroups[0]
39
40         nodegroup.update(nodegroup_fields)
41         nodegroup.sync()
42         
43         # Logging variables
44         self.event_objects = {'NodeGroup': [nodegroup['nodegroup_id']]}
45         self.message = 'Node group %d updated: %s' % \
46                 (nodegroup['nodegroup_id'], ", ".join(nodegroup_fields.keys()))  
47         return 1