From 8d8ab85485f8b4171d80675849bddadf7cbd0f6e Mon Sep 17 00:00:00 2001 From: Terry Wilson Date: Thu, 28 Feb 2019 16:23:23 -0600 Subject: [PATCH] Add db_create_row method Since there are row-returning versions of db_find/list, and db_create currently returns a uuid, it makes sense to have a version that returns a RowView as well. I've seen some usages where people do a db_create() followed by a db_find() to get the row, and one shouldn't have to do that. Change-Id: I5250e37a54411297a64568a3e8135855f06de807 --- ovsdbapp/api.py | 11 ++++++++++- ovsdbapp/backend/ovs_idl/__init__.py | 3 +++ ovsdbapp/backend/ovs_idl/command.py | 9 +++++++-- .../schema/open_vswitch/test_common_db.py | 15 +++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/ovsdbapp/api.py b/ovsdbapp/api.py index dd0b9243..2652bd3d 100644 --- a/ovsdbapp/api.py +++ b/ovsdbapp/api.py @@ -120,9 +120,18 @@ class API(object): :param col_values: The columns and their associated values to be set after create :type col_values: Dictionary of columns id's and values - :returns: :class:`Command` with no result + :returns: :class:`Command` with uuid result """ + def db_create_row(self, table, **col_values): + """Create a command to create new record + + Identical to db_create, but returns a RowView result + :returns: :class:`Command` with RowView result + """ + # vif_plug_ovs has a copy of impl_vsctl that doesn't implement this + raise NotImplementedError + @abc.abstractmethod def db_destroy(self, table, record): """Create a command to destroy a record diff --git a/ovsdbapp/backend/ovs_idl/__init__.py b/ovsdbapp/backend/ovs_idl/__init__.py index 9413c3d3..b346f4f2 100644 --- a/ovsdbapp/backend/ovs_idl/__init__.py +++ b/ovsdbapp/backend/ovs_idl/__init__.py @@ -66,6 +66,9 @@ class Backend(object): def db_create(self, table, **col_values): return cmd.DbCreateCommand(self, table, **col_values) + def db_create_row(self, table, **col_values): + return cmd.DbCreateCommand(self, table, _as_row=True, **col_values) + def db_destroy(self, table, record): return cmd.DbDestroyCommand(self, table, record) diff --git a/ovsdbapp/backend/ovs_idl/command.py b/ovsdbapp/backend/ovs_idl/command.py index aa9b40e2..be71fbe4 100644 --- a/ovsdbapp/backend/ovs_idl/command.py +++ b/ovsdbapp/backend/ovs_idl/command.py @@ -81,10 +81,11 @@ class AddCommand(BaseCommand): class DbCreateCommand(BaseCommand): - def __init__(self, api, table, **columns): + def __init__(self, api, table, _as_row=False, **columns): super(DbCreateCommand, self).__init__(api) self.table = table self.columns = columns + self.row = _as_row def run_idl(self, txn): row = txn.insert(self.api._tables[self.table]) @@ -94,7 +95,11 @@ class DbCreateCommand(BaseCommand): def post_commit(self, txn): # Replace the temporary row with the post-commit UUID to match vsctl - self.result = txn.get_insert_uuid(self.result.uuid) + u = txn.get_insert_uuid(self.result.uuid) + if self.row: + self.result = rowview.RowView(self.api.tables[self.table].rows[u]) + else: + self.result = u class DbDestroyCommand(BaseCommand): diff --git a/ovsdbapp/tests/functional/schema/open_vswitch/test_common_db.py b/ovsdbapp/tests/functional/schema/open_vswitch/test_common_db.py index 66ceb48c..bf26d1d7 100644 --- a/ovsdbapp/tests/functional/schema/open_vswitch/test_common_db.py +++ b/ovsdbapp/tests/functional/schema/open_vswitch/test_common_db.py @@ -12,7 +12,10 @@ # License for the specific language governing permissions and limitations # under the License. +import uuid + from ovsdbapp.backend.ovs_idl import idlutils +from ovsdbapp.backend.ovs_idl import rowview from ovsdbapp.schema.open_vswitch import impl_idl from ovsdbapp.tests.functional import base from ovsdbapp.tests.functional.schema.open_vswitch import fixtures @@ -98,3 +101,15 @@ class TestBackendDb(base.FunctionalTestCase): self.assertTrue( set(b['name'] for b in self.bridges).issubset( set(b.name for b in res))) + + def test_db_create(self): + _uuid = self.api.db_create( + 'Queue', external_ids={'x': 'x'}).execute(check_error=True) + self.assertIsInstance(_uuid, uuid.UUID) + self.api.db_destroy('Queue', _uuid).execute(check_error=True) + + def test_db_create_row(self): + row = self.api.db_create_row( + 'Queue', external_ids={'x': 'x'}).execute(check_error=True) + self.assertIsInstance(row, rowview.RowView) + self.api.db_destroy('Queue', row.uuid).execute(check_error=True)