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