dc289facf86ea9ec0a5ef517ebd8762c4e8fe151
[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(Node.fields['node_id'],
20               Node.fields['hostname']),
21         Mixed(NodeGroup.fields['nodegroup_id'],
22               NodeGroup.fields['name']),
23         ]
24
25     returns = Parameter(int, '1 if successful')
26
27     event_type = 'DeleteFrom'
28     object_type = 'NodeGroup'
29
30     def call(self, auth, node_id_or_hostname, nodegroup_id_or_name):
31         # Get node info
32         nodes = Nodes(self.api, [node_id_or_hostname])
33         if not nodes:
34                 raise PLCInvalidArgument, "No such node"
35
36         node = nodes[0]
37
38         # Get nodegroup info
39         nodegroups = NodeGroups(self.api, [nodegroup_id_or_name])
40         if not nodegroups:
41             raise PLCInvalidArgument, "No such nodegroup"
42
43         nodegroup = nodegroups[0]
44
45         # Remove node from nodegroup
46         if node['node_id'] in nodegroup['node_ids']:
47             nodegroup.remove_node(node)
48         
49         self.object_ids = [nodegroup['nodegroup_id']]
50
51         return 1