def run_idl(self, txn): table_schema = self.api._tables[self.table] columns = self.columns or list(table_schema.columns.keys()) + ['_uuid'] if self.records: row_uuids = [] for record in self.records: try: row_uuids.append(idlutils.row_by_record( self.api.idl, self.table, record).uuid) except idlutils.RowNotFound: if self.if_exists: continue # NOTE(kevinbenton): this is converted to a RuntimeError # for compat with the vsctl version. It might make more # sense to change this to a RowNotFoundError in the future. raise RuntimeError(_( "Row doesn't exist in the DB. Request info: " "Table=%(table)s. Columns=%(columns)s. " "Records=%(records)s.") % { "table": self.table, "columns": self.columns, "records": self.records, }) else: row_uuids = table_schema.rows.keys() self.result = [ { c: idlutils.get_column_value(table_schema.rows[uuid], c) for c in columns } for uuid in row_uuids ]
def run_idl(self, txn): table_schema = self.api._tables[self.table] idx = idlutils.get_index_column(table_schema) columns = self.columns or list(table_schema.columns.keys()) + ['_uuid'] # If there's an index for this table, we'll fetch all columns and # remove the unwanted ones based on self.records. Otherwise, let's try # to get the uuid of the wanted ones which is an O(n^2) operation. if not idx and self.records: rows = [] for record in self.records: try: rows.append( idlutils.row_by_record(self.api.idl, self.table, record)) except idlutils.RowNotFound: if self.if_exists: continue self._raise_notfound() else: rows = table_schema.rows.values() if idx and self.records: match = lambda row: getattr(row, idx) in self.records else: match = lambda row: True self.result = [{c: idlutils.get_column_value(row, c) for c in columns} for row in rows if match(row)] if (not self.if_exists and idx and self.records and len(self.result) < len(self.records)): self._raise_notfound()
def run_idl(self, txn): table_schema = self.api._tables[self.table] columns = self.columns or list(table_schema.columns.keys()) + ['_uuid'] if self.records: row_uuids = [] for record in self.records: try: row_uuids.append( idlutils.row_by_record(self.api.idl, self.table, record).uuid) except idlutils.RowNotFound: if self.if_exists: continue # NOTE(kevinbenton): this is converted to a RuntimeError # for compat with the vsctl version. It might make more # sense to change this to a RowNotFoundError in the future. raise RuntimeError( _("Row doesn't exist in the DB. Request info: " "Table=%(table)s. Columns=%(columns)s. " "Records=%(records)s.") % { "table": self.table, "columns": self.columns, "records": self.records, }) else: row_uuids = table_schema.rows.keys() self.result = [{ c: idlutils.get_column_value(table_schema.rows[uuid], c) for c in columns } for uuid in row_uuids]
def run_idl(self, txn): record = idlutils.row_by_record(self.api.idl, self.table, self.record) for col, val in self.col_values: # TODO(twilson) Ugh, the OVS library doesn't like OrderedDict # We're only using it to make a unit test work, so we should fix # this soon. if isinstance(val, collections.OrderedDict): val = dict(val) setattr(record, col, val)
def test_row_by_record(self): FAKE_RECORD = 'fake_record' mock_idl_ = mock.MagicMock() mock_table = mock.MagicMock( rows={mock.sentinel.row: mock.sentinel.row_value}) mock_idl_.tables = {mock.sentinel.table_name: mock_table} res = idlutils.row_by_record(mock_idl_, mock.sentinel.table_name, FAKE_RECORD) self.assertEqual(mock.sentinel.row_value, res)
def run_idl(self, txn): record = idlutils.row_by_record(self.api.idl, self.table, self.record) # TODO(twilson) This feels wrong, but ovs-vsctl returns single results # on set types without the list. The IDL is returning them as lists, # even if the set has the maximum number of items set to 1. Might be # able to inspect the Schema and just do this conversion for that case. result = idlutils.get_column_value(record, self.column) if isinstance(result, list) and len(result) == 1: self.result = result[0] else: self.result = result
def __init__(self, api, table, records, columns, if_exists): super(DbListCommand, self).__init__(api) self.table = self.api._tables[table] self.columns = columns or self.table.columns.keys() + ['_uuid'] self.if_exists = if_exists if records: self.records = [ idlutils.row_by_record(self.api.idl, table, record).uuid for record in records] else: self.records = self.table.rows.keys()
def __init__(self, api, table, records, columns, if_exists): super(DbListCommand, self).__init__(api) self.table = self.api._tables[table] self.columns = columns or self.table.columns.keys() + ['_uuid'] self.if_exists = if_exists if records: self.records = [ idlutils.row_by_record(self.api.idl, table, record).uuid for record in records ] else: self.records = self.table.rows.keys()
def run_idl(self, txn): record = idlutils.row_by_record(self.api.idl, self.table, self.record) for col, val in self.col_values: # TODO(twilson) Ugh, the OVS library doesn't like OrderedDict # We're only using it to make a unit test work, so we should fix # this soon. if isinstance(val, collections.OrderedDict): val = dict(val) if isinstance(val, dict): # NOTE(twilson) OVS 2.6's Python IDL has mutate methods that # would make this cleaner, but it's too early to rely on them. existing = getattr(record, col, {}) existing.update(val) val = existing setattr(record, col, idlutils.db_replace_record(val))
def run_idl(self, txn): record = idlutils.row_by_record(self.api.idl, self.table, self.record) for value in self.values: field = getattr(record, self.column) if isinstance(value, collections.Mapping): # We should be doing an add on a 'map' column. If the key is # already set, do nothing, otherwise set the key to the value for k, v in six.iteritems(value): if k in field: continue field[k] = v else: # We should be appending to a 'set' column. field.append(value) record.verify(self.column) setattr(record, self.column, idlutils.db_replace_record(field))
def __init__(self, api, table, records, columns, if_exists): super(DbListCommand, self).__init__(api) self.requested_info = {'records': records, 'columns': columns, 'table': table} self.table = self.api._tables[table] self.columns = columns or self.table.columns.keys() + ['_uuid'] self.if_exists = if_exists if records: self.records = [] for record in records: try: self.records.append(idlutils.row_by_record( self.api.idl, table, record).uuid) except idlutils.RowNotFound: if self.if_exists: continue raise else: self.records = self.table.rows.keys()
def run_idl(self, txn): record = idlutils.row_by_record(self.api.idl, self.table, self.record) for value in self.values: if isinstance(value, collections.Mapping): # We should be doing an add on a 'map' column. If the key is # already set, do nothing, otherwise set the key to the value # Since this operation depends on the previous value, verify() # must be called. field = getattr(record, self.column, {}) for k, v in six.iteritems(value): if k in field: continue field[k] = v else: # We should be appending to a 'set' column. try: record.addvalue(self.column, idlutils.db_replace_record(value)) continue except AttributeError: # OVS < 2.6 field = getattr(record, self.column, []) field.append(value) record.verify(self.column) setattr(record, self.column, idlutils.db_replace_record(field))
def run_idl(self, txn): record = idlutils.row_by_record(self.api.idl, self.table, self.record) # Create an empty value of the column type value = type(getattr(record, self.column))() setattr(record, self.column, value)
def run_idl(self, txn): record = idlutils.row_by_record(self.api.idl, self.table, self.record) record.delete()