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