- BootGetNodeDetails implementation
[plcapi.git] / PLC / Methods / AdmQueryNode.py
1 import socket
2
3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Nodes import Node, Nodes
7 from PLC.NodeNetworks import NodeNetwork, NodeNetworks, valid_ip
8 from PLC.Auth import PasswordAuth
9
10 class AdmQueryNode(Method):
11     """
12     Deprecated. Functionality can be implemented with GetNodes and
13     GetNodeNetworks.
14     """
15
16     status = "deprecated"
17
18     roles = ['admin', 'pi', 'user', 'tech']
19
20     accepts = [
21         PasswordAuth(),
22         {'node_hostname': Node.fields['hostname'],
23          'nodenetwork_ip': NodeNetwork.fields['ip'],
24          'nodenetwork_mac': NodeNetwork.fields['mac'],
25          'nodenetwork_method': NodeNetwork.fields['method']}
26         ]
27
28     returns = [Node.fields['node_id']]
29
30     def call(self, auth, search_vals):
31         # Get possible nodenetworks
32         if 'node_hostname' in search_vals:
33             nodes = Nodes(self.api, [search_vals['node_hostname']]).values()
34             if not nodes:
35                 return []
36
37             # No network interface filters specified
38             if 'nodenetwork_ip' not in search_vals and \
39                'nodenetwork_mac' not in search_vals and \
40                'nodenetwork_method' not in search_vals:
41                 return [nodes[0]['node_id']]
42
43             if nodes[0]['nodenetwork_ids']:
44                 nodenetworks = NodeNetworks(self.api, nodes[0]['nodenetwork_ids']).values()
45             else:
46                 nodenetworks = []
47         else:
48             nodenetworks = NodeNetworks(self.api).values()
49
50         if 'nodenetwork_ip' in search_vals:
51             if not valid_ip(search_vals['nodenetwork_ip']):
52                 raise PLCInvalidArgument, "Invalid IP address"
53             nodenetworks = filter(lambda nodenetwork: \
54                                   socket.inet_aton(nodenetwork['ip']) == socket.inet_aton(search_vals['nodenetwork_ip']),
55                                   nodenetworks)
56
57         if 'nodenetwork_mac' in search_vals:
58             nodenetworks = filter(lambda nodenetwork: \
59                                  nodenetwork['mac'].lower() == search_vals['nodenetwork_mac'].lower(),
60                                  nodenetworks)
61
62         if 'nodenetwork_method' in search_vals:
63             nodenetworks = filter(lambda nodenetwork: \
64                                   nodenetwork['method'].lower() == search_vals['nodenetwork_method'].lower(),
65                                   nodenetworks)
66
67         return [nodenetwork['node_id'] for nodenetwork in nodenetworks]