34f5f972ac640a72c15ff4857ff10f5cb50399a3
[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 Auth
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         Auth(),
24         nodegroup_fields
25         ]
26
27     returns = Parameter(int, 'New nodegroup_id (> 0) if successful')
28
29
30     def call(self, auth, nodegroup_fields):
31         nodegroup_fields = dict(filter(can_update, nodegroup_fields.items()))
32         nodegroup = NodeGroup(self.api, nodegroup_fields)
33         nodegroup.sync()
34
35         # Logging variables
36         self.event_objects = {'NodeGroup': [nodegroup['nodegroup_id']]}
37         self.message = 'Node group %d created' % nodegroup['nodegroup_id']
38  
39         return nodegroup['nodegroup_id']