Merge "Add db_create_row method"

This commit is contained in:
Zuul 2019-03-20 15:41:53 +00:00 committed by Gerrit Code Review
commit 745d94e0b1
4 changed files with 35 additions and 3 deletions

View File

@ -127,9 +127,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

View File

@ -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)

View File

@ -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):

View File

@ -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)