Merge "Add if_exists to lrp_set_options"

This commit is contained in:
Zuul 2024-05-16 17:29:08 +00:00 committed by Gerrit Code Review
commit fa4672b73f
4 changed files with 31 additions and 11 deletions

View File

@ -348,14 +348,20 @@ class BaseGetRowCommand(ReadOnlyCommand):
class BaseSetOptionsCommand(BaseCommand):
table = []
def __init__(self, api, entity, **options):
def __init__(self, api, entity, if_exists=False, **options):
super().__init__(api)
self.entity = entity
self.options = options
self.if_exists = if_exists
def run_idl(self, txn):
entity = self.api.lookup(self.table, self.entity)
entity.options = self.options
try:
entity = self.api.lookup(self.table, self.entity)
entity.options = self.options
except idlutils.RowNotFound:
if self.if_exists:
return
raise
class BaseGetOptionsCommand(ReadOnlyCommand):

View File

@ -650,14 +650,17 @@ class API(api.API, metaclass=abc.ABCMeta):
"""
@abc.abstractmethod
def lrp_set_options(self, port, **options):
def lrp_set_options(self, port, if_exists=False, **options):
"""Set options related to the type of 'port'
:param port: The name or uuid of the port
:type port: string or uuid.UUID
:param options: keys and values for the port 'options' dict
:type options: key: string, value: string
:returns: :class:`Command` with no result
:param port: The name or uuid of the port
:type port: string or uuid.UUID
:param options: keys and values for the port 'options' dict
:param if_exists: If True, don't fail if the logical router port
doesn't exist
:type if_exists: boolean
:type options: key: string, value: string
:returns: :class:`Command` with no result
"""
@abc.abstractmethod

View File

@ -212,8 +212,8 @@ class OvnNbApiIdlImpl(ovs_idl.Backend, api.API):
def lrp_get_enabled(self, port):
return cmd.LrpGetEnabledCommand(self, port)
def lrp_set_options(self, port, **options):
return cmd.LrpSetOptionsCommand(self, port, **options)
def lrp_set_options(self, port, if_exists=False, **options):
return cmd.LrpSetOptionsCommand(self, port, if_exists, **options)
def lrp_get_options(self, port):
return cmd.LrpGetOptionsCommand(self, port)

View File

@ -1642,6 +1642,17 @@ class TestLogicalRouterPortOps(OvnNorthboundTest):
self.assertEqual(options, self.api.lrp_get_options(lrp.uuid).execute(
check_error=True))
def test_lrp_set_options_if_exists(self):
options = {'one': 'two', 'three': 'four'}
self.api.lrp_set_options(utils.get_rand_device_name(),
if_exists=True,
**options).execute(check_error=True)
def test_lrp_set_options_no_exist(self):
options = {'one': 'two', 'three': 'four'}
cmd = self.api.lrp_set_options(utils.get_rand_device_name(), **options)
self.assertRaises(idlutils.RowNotFound, cmd.execute, check_error=True)
def test_lrp_get_set_gateway_chassis(self):
lrp = self._lrp_add(None)
c1_name = utils.get_rand_device_name()