X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FPCUs.py;h=d628677bb71dac5f754bcad69b786716d3f58862;hb=19d4a01ccf66af9e00914351b3eacd5fc880f988;hp=57ec44cfd5717a1cb1edbfd005b6d1c0aa9ed007;hpb=f6f0164afcbada9498622842964024f6fb3e5dfa;p=plcapi.git diff --git a/PLC/PCUs.py b/PLC/PCUs.py index 57ec44cf..d628677b 100644 --- a/PLC/PCUs.py +++ b/PLC/PCUs.py @@ -4,14 +4,13 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id: PCUs.py,v 1.5 2006/10/24 20:02:22 mlhuang Exp $ -# from PLC.Faults import * from PLC.Parameter import Parameter +from PLC.Filter import Filter from PLC.Debug import profile from PLC.Table import Row, Table -from PLC.NodeNetworks import valid_ip, NodeNetwork, NodeNetworks +from PLC.Interfaces import valid_ip, Interface, Interfaces from PLC.Nodes import Node, Nodes class PCU(Row): @@ -28,13 +27,14 @@ class PCU(Row): 'site_id': Parameter(int, "Identifier of site where PCU is located"), 'hostname': Parameter(str, "PCU hostname", max = 254), 'ip': Parameter(str, "PCU IP address", max = 254), - 'protocol': Parameter(str, "PCU protocol, e.g. ssh, https, telnet", max = 16), - 'username': Parameter(str, "PCU username", max = 254), - 'password': Parameter(str, "PCU username", max = 254), - 'notes': Parameter(str, "Miscellaneous notes", max = 254), - 'model': Parameter(str, "PCU model string", max = 32), - 'node_ids': Parameter([int], "List of nodes that this PCU controls", ro = True), - 'ports': Parameter([int], "List of the port numbers that each node is connected to", ro = True), + 'protocol': Parameter(str, "PCU protocol, e.g. ssh, https, telnet", max = 16, nullok = True), + 'username': Parameter(str, "PCU username", max = 254, nullok = True), + 'password': Parameter(str, "PCU username", max = 254, nullok = True), + 'notes': Parameter(str, "Miscellaneous notes", max = 254, nullok = True), + 'model': Parameter(str, "PCU model string", max = 32, nullok = True), + 'node_ids': Parameter([int], "List of nodes that this PCU controls"), + 'ports': Parameter([int], "List of the port numbers that each node is connected to"), + 'last_updated': Parameter(int, "Date and time when node entry was created", ro = True), } def validate_ip(self, ip): @@ -42,6 +42,23 @@ class PCU(Row): raise PLCInvalidArgument, "Invalid IP address " + ip return ip + validate_last_updated = Row.validate_timestamp + + def update_timestamp(self, col_name, commit = True): + """ + Update col_name field with current time + """ + + assert 'pcu_id' in self + assert self.table_name + + self.api.db.do("UPDATE %s SET %s = CURRENT_TIMESTAMP " % (self.table_name, col_name) + \ + " where pcu_id = %d" % (self['pcu_id']) ) + self.sync(commit) + + def update_last_updated(self, commit = True): + self.update_timestamp('last_updated', commit) + def add_node(self, node, port, commit = True): """ Add node to existing PCU. @@ -99,22 +116,19 @@ class PCUs(Table): database. """ - def __init__(self, api, pcu_ids = None): - self.api = api - - # N.B.: Node IDs returned may be deleted. - sql = "SELECT %s FROM view_pcus" % \ - ", ".join(PCU.fields) + def __init__(self, api, pcu_filter = None, columns = None): + Table.__init__(self, api, PCU, columns) - if pcu_ids: - sql += " WHERE pcu_id IN (%s)" % ", ".join(map(str, pcu_ids)) + sql = "SELECT %s FROM view_pcus WHERE True" % \ + ", ".join(self.columns) - rows = self.api.db.selectall(sql) + if pcu_filter is not None: + if isinstance(pcu_filter, (list, tuple, set, int, long)): + pcu_filter = Filter(PCU.fields, {'pcu_id': pcu_filter}) + elif isinstance(pcu_filter, dict): + pcu_filter = Filter(PCU.fields, pcu_filter) + else: + raise PLCInvalidArgument, "Wrong pcu filter %r"%pcu_filter + sql += " AND (%s) %s" % pcu_filter.sql(api) - for row in rows: - self[row['pcu_id']] = pcu = PCU(api, row) - for aggregate in ['node_ids', 'ports']: - if not pcu.has_key(aggregate) or pcu[aggregate] is None: - pcu[aggregate] = [] - else: - pcu[aggregate] = map(int, pcu[aggregate].split(',')) + self.selectall(sql)