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