step2 : basic functions for handling nodetags and nodegroups - still highly volatile
[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 NodeGroup.fields.keys() and field != NodeGroup.primary_field
8
9 class AddNodeGroup(Method):
10     """
11     Adds a new node group. Any values specified in nodegroup_fields
12     are used, otherwise defaults are used.
13
14     Returns the new nodegroup_id (> 0) if successful, faults otherwise.
15     """
16
17     roles = ['admin']
18
19     nodegroup_fields = dict(filter(can_update, NodeGroup.fields.items()))
20
21     accepts = [
22         Auth(),
23         nodegroup_fields
24         ]
25
26     returns = Parameter(int, 'New nodegroup_id (> 0) if successful')
27
28
29     def call(self, auth, nodegroup_fields):
30         nodegroup_fields = dict([f for f in nodegroup_fields.items() if can_update(f)])
31         nodegroup = NodeGroup(self.api, nodegroup_fields)
32         nodegroup.sync()
33
34         # Logging variables
35         self.event_objects = {'NodeGroup': [nodegroup['nodegroup_id']]}
36         self.message = 'Node group %d created' % nodegroup['nodegroup_id']
37  
38         return nodegroup['nodegroup_id']