Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / trunk / PLC / Methods / AdmQueryPowerControlUnit.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.PCUs import PCU, PCUs
7 from PLC.Nodes import Node, Nodes
8 from PLC.NodeNetworks import NodeNetwork, NodeNetworks, valid_ip
9 from PLC.Auth import Auth
10
11 class AdmQueryPowerControlUnit(Method):
12     """
13     Deprecated. Functionality can be implemented with GetPCUs or
14     GetNodes.
15     """
16
17     status = "deprecated"
18
19     roles = ['admin', 'pi', 'user', 'tech']
20
21     accepts = [
22         Auth(),
23         {'pcu_hostname': PCU.fields['hostname'],
24          'pcu_ip': PCU.fields['ip'],
25          'node_hostname': Node.fields['hostname'],
26          'node_id': Node.fields['node_id']}
27         ]
28
29     returns = [PCU.fields['pcu_id']]
30
31     def call(self, auth, search_vals):
32         # Get all PCUs. This is a stupid function. The API should not
33         # be used for DB mining.
34         pcus = PCUs(self.api)
35
36         if 'pcu_hostname' in search_vals:
37             pcus = filter(lambda pcu: \
38                           pcu['hostname'].lower() == search_vals['pcu_hostname'].lower(),
39                           pcus)
40
41         if 'pcu_ip' in search_vals:
42             if not valid_ip(search_vals['pcu_ip']):
43                 raise PLCInvalidArgument, "Invalid IP address"
44             pcus = filter(lambda pcu: \
45                           socket.inet_aton(pcu['ip']) == socket.inet_aton(search_vals['pcu_ip']),
46                           pcus)
47
48         if 'node_id' in search_vals:
49             pcus = filter(lambda pcu: \
50                           search_vals['node_id'] in pcu['node_ids'],
51                           pcus)
52
53         if 'node_hostname' in search_vals:
54             pcus = filter(lambda pcu: \
55                           search_vals['node_hostname'] in \
56                           [node['hostname'] for node in Nodes(self.api, pcu['node_ids'])],
57                           pcus)
58
59         return [pcu['pcu_id'] for pcu in pcus]