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
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Debug import profile
13 from PLC.Table import Row, Table
17 Representation of a row in the pcu table. To use,
18 instantiate with a dict of values.
22 'pcu_id': Parameter(int, "Node group identifier"),
23 'hostname': Parameter(str, "Fully qualified hostname"),
26 # These fields are derived from join tables and are not
27 # actually in the pcu table.
29 'node_ids': Parameter([int], "List of nodes that this PCU controls"),
32 def __init__(self, api, fields):
33 Row.__init__(self, fields)
36 def flush(self, commit = True):
38 Commit changes back to the database.
43 # Fetch a new pcu_id if necessary
44 if 'pcu_id' not in self:
45 rows = self.api.db.selectall("SELECT NEXTVAL('pcu_pcu_id_seq') AS pcu_id")
47 raise PLCDBError, "Unable to fetch new pcu_id"
48 self['pcu_id'] = rows[0]['pcu_id']
53 # Filter out unknown fields
54 fields = dict(filter(lambda (key, value): key in self.fields,
57 # Parameterize for safety
59 values = [self.api.db.param(key, value) for (key, value) in fields.items()]
62 # Insert new row in pcu table
63 sql = "INSERT INTO pcu (%s) VALUES (%s)" % \
64 (", ".join(keys), ", ".join(values))
66 # Update existing row in sites table
67 columns = ["%s = %s" % (key, value) for (key, value) in zip(keys, values)]
68 sql = "UPDATE pcu SET " + \
69 ", ".join(columns) + \
70 " WHERE pcu_id = %(pcu_id)d"
72 self.api.db.do(sql, fields)
77 def delete(self, commit = True):
82 assert 'pcu_id' in self
85 for table in ['pcu_ports', 'pcu']:
86 self.api.db.do("DELETE FROM %s" \
87 " WHERE nodenetwork_id = %(pcu_id)" % \
95 Representation of row(s) from the pcu table in the
99 def __init__(self, api, pcu_id_or_hostname_list = None):
102 # N.B.: Node IDs returned may be deleted.
103 sql = "SELECT pcu.*, pcu_ports.node_id" \
105 " LEFT JOIN pcu_ports USING (pcu_id)"
107 if pcu_id_or_hostname_list:
108 # Separate the list into integers and strings
109 pcu_ids = filter(lambda pcu_id: isinstance(pcu_id, (int, long)),
110 pcu_id_or_hostname_list)
111 hostnames = filter(lambda hostname: isinstance(hostname, StringTypes),
112 pcu_id_or_hostname_list)
115 sql += " OR pcu_id IN (%s)" % ", ".join(map(str, pcu_ids))
117 sql += " OR hostname IN (%s)" % ", ".join(api.db.quote(hostnames)).lower()
120 rows = self.api.db.selectall(sql, locals())
122 if self.has_key(row['pcu_id']):
123 pcu = self[row['pcu_id']]
126 self[row['pcu_id']] = PCU(api, row)