75d4d9122b26b48dcde520f24ec9f0b7d6ad618e
[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              ['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     update_fields = dict(filter(can_update, NodeGroup.fields.items()))
21
22     accepts = [
23         PasswordAuth(),
24         NodeGroup.fields['name'],
25         update_fields
26         ]
27
28     returns = Parameter(int, 'New nodegroup_id (> 0) if successful')
29
30     event_type = 'Add'
31     object_type = 'NodeGroup'
32     object_ids = []
33
34     def call(self, auth, name, nodegroup_fields = {}):
35         nodegroup_fields = dict(filter(can_update, nodegroup_fields.items()))
36         nodegroup = NodeGroup(self.api, nodegroup_fields)
37         nodegroup['name'] = name
38         nodegroup.sync()
39         self.object_ids = [nodegroup['nodegroup_id']]
40
41         return nodegroup['nodegroup_id']