modified can_update variable
[plcapi.git] / PLC / Methods / AdmAddNodeGroup.py
1
2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Nodes import Node, Nodes
6 from PLC.NodeGroups import NodeGroup, NodeGroups
7 #from PLC.Sites import Site, Sites
8 from PLC.Auth import PasswordAuth
9
10 class AdmAddNodeGroup(Method):
11     """
12     Adds a new node group. Any values specified in optional_vals are used,
13     otherwise defaults are used.
14
15     Returns the new node_id (> 0) if successful, faults otherwise.
16     """
17
18     roles = ['admin']
19
20     can_update = lambda (field, value): field in \
21                  ['name', 'description', 'is_custom']
22     update_fields = dict(filter(can_update, NodeGroup.fields.items()))
23         
24     accepts = [
25         PasswordAuth(),
26         NodeGroup.fields['name'],
27         NodeGroup.fields['description'],
28         update_fields
29         ]
30
31     returns = Parameter(int, '1 if successful')
32
33     def call(self, auth, name, description, optional_vals = {}):
34         if filter(lambda field: field not in self.update_fields, optional_vals):
35             raise PLCInvalidArgument, "Invalid fields specified"
36
37         # Authenticated function
38         assert self.caller is not None
39
40         # make sure we are 'admin'
41         if 'admin' not in self.caller['roles']:
42                 raise PLCPermissionDenied, "Not allowed to add node groups"
43
44         #creat node group
45         node_group = NodeGroup(self.api, optional_vals)
46         node_group['name'] = name
47         node_group['description'] = description
48         node_group.flush()
49
50         return node_group['nodegroup_id']