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