- added: check if caller is admin
[plcapi.git] / PLC / Methods / AdmAddNodeToNodeGroup.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.Nodes import Node, Nodes
6 from PLC.Auth import PasswordAuth
7
8 class AdmAddNodeToNodeGroup(Method):
9     """
10     Add a node to the specified node group. If the node is
11     already a member of the nodegroup, no errors are returned.
12
13     Returns 1 if successful, faults otherwise.
14     """
15
16     roles = ['admin']
17
18     accepts = [
19         PasswordAuth(),
20         Mixed(NodeGroup.fields['nodegroup_id'],
21               NodeGroup.fields['name']),
22         Mixed(Node.fields['node_id'],
23               Node.fields['hostname'])
24         ]
25
26     returns = Parameter(int, '1 if successful')
27
28     def call(self, auth, nodegroup_id_or_name, node_id_or_hostname):
29         # Get node info
30         nodes = Nodes(self.api, [node_id_or_hostname])
31         if not nodes:
32                 raise PLCInvalidArgument, "No such node"
33         node = nodes.values()[0]
34
35         # Get nodegroup info
36         nodegroups = NodeGroups(self.api, [nodegroup_id_or_name])
37         if not nodegroups:
38             raise PLCInvalidArgument, "No such nodegroup"
39
40         nodegroup = nodegroups.values()[0]
41         
42         # Authenticated function
43         assert self.caller is not None
44
45         # make sure we are 'admin'
46         if 'admin' not in self.caller['roles']:
47                 raise PLCPermissionDenied, "Not allowed to add node groups"
48
49         # add node to nodegroup
50         if node['node_id'] not in nodegroup['node_ids']:
51             nodegroup.add_node(node)
52
53         return 1