(most) all functions now take SessionAuth in addition to PasswordAuth
[plcapi.git] / PLC / Methods / DeleteNodeFromNodeGroup.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 Auth
7
8 class DeleteNodeFromNodeGroup(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         Auth(),
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
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         # Remove node from nodegroup
43         if node['node_id'] in nodegroup['node_ids']:
44             nodegroup.remove_node(node)
45
46         return 1