- make Add() calling convention consistent among all functions that
[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 PasswordAuth
6
7 can_update = lambda (field, value): field in \
8              ['name', 'description']
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     for field in nodegroup_fields.values():
21         field.optional = True
22
23     accepts = [
24         PasswordAuth(),
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.values()[0]
40         
41         nodegroup.update(nodegroup_fields)
42         nodegroup.sync()
43
44         return 1