- change calling convention for Add slightly; if the Add function for
[plcapi.git] / PLC / Methods / AddNodeGroup.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 AddNodeGroup(Method):
11     """
12     Adds a new node group. Any values specified in nodegroup_fields
13     are used, otherwise defaults are used.
14
15     Returns the new nodegroup_id (> 0) if successful, faults otherwise.
16     """
17
18     roles = ['admin']
19
20     nodegroup_fields = dict(filter(can_update, NodeGroup.fields.items()))
21
22     accepts = [
23         PasswordAuth(),
24         nodegroup_fields
25         ]
26
27     returns = Parameter(int, 'New nodegroup_id (> 0) if successful')
28
29     event_type = 'Add'
30     object_type = 'NodeGroup'
31     object_ids = []
32
33     def call(self, auth, nodegroup_fields):
34         nodegroup_fields = dict(filter(can_update, nodegroup_fields.items()))
35         nodegroup = NodeGroup(self.api, nodegroup_fields)
36         nodegroup.sync()
37
38         self.object_ids = [nodegroup['nodegroup_id']]
39
40         return nodegroup['nodegroup_id']