2 # Functions for interacting with the pcus table in the database
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
7 # $Id: PCUs.py,v 1.1 2006/09/06 15:36:07 mlhuang Exp $
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Debug import profile
13 from PLC.Table import Row, Table
14 from PLC.NodeNetworks import valid_ip, NodeNetwork, NodeNetworks
18 Representation of a row in the pcus table. To use,
19 instantiate with a dict of values.
23 primary_key = 'pcu_id'
25 'pcu_id': Parameter(int, "PCU identifier"),
26 'site_id': Parameter(int, "Identifier of site where PCU is located"),
27 'hostname': Parameter(str, "PCU hostname", max = 254),
28 'ip': Parameter(str, "PCU IP address", max = 254),
29 'protocol': Parameter(str, "PCU protocol, e.g. ssh, https, telnet", max = 16),
30 'username': Parameter(str, "PCU username", max = 254),
31 'password': Parameter(str, "PCU username", max = 254),
32 'notes': Parameter(str, "Miscellaneous notes", max = 254),
33 'model': Parameter(str, "PCU model string", max = 32),
34 'node_ids': Parameter([int], "List of nodes that this PCU controls", ro = True),
35 'ports': Parameter([int], "List of the port numbers that each node is connected to", ro = True),
38 def __init__(self, api, fields = {}):
39 Row.__init__(self, fields)
42 def validate_ip(self, ip):
44 raise PLCInvalidArgument, "Invalid IP address " + ip
47 def delete(self, commit = True):
52 assert 'pcu_id' in self
54 # Clean up various join tables
55 for table in ['pcu_node', 'pcus']:
56 self.api.db.do("DELETE FROM " + table +
57 " WHERE pcu_id = %(pcu_id)d",
65 Representation of row(s) from the pcus table in the
69 def __init__(self, api, pcu_ids = None):
72 # N.B.: Node IDs returned may be deleted.
73 sql = "SELECT %s FROM view_pcus" % \
77 sql += " WHERE pcu_id IN (%s)" % ", ".join(map(str, pcu_ids))
79 rows = self.api.db.selectall(sql)
82 self[row['pcu_id']] = pcu = PCU(api, row)
83 for aggregate in ['pcu_ids', 'ports']:
84 if not pcu.has_key(aggregate) or pcu[aggregate] is None:
87 pcu[aggregate] = map(int, pcu[aggregate].split(','))