API: use baymodel_ident to update a baymodel

Currently, we can not update a baymodel with baymodel name, this patch fixes
it by passing baymodel_ident instead of using baymodel_uuid.

Closes-Bug: #1516900
Change-Id: Ib372c06762fdb3918f391aefd25c43e6a2a5fca3
This commit is contained in:
Eli Qiao 2015-11-17 17:17:20 +08:00
parent cb3f5fe56e
commit 00b53e2637
4 changed files with 20 additions and 21 deletions

View File

@ -346,17 +346,17 @@ class BayModelsController(rest.RestController):
return BayModel.convert_with_links(new_baymodel)
@policy.enforce_wsgi("baymodel", "update")
@wsme.validate(types.uuid, [BayModelPatchType])
@expose.expose(BayModel, types.uuid, body=[BayModelPatchType])
@wsme.validate(types.uuid_or_name, [BayModelPatchType])
@expose.expose(BayModel, types.uuid_or_name, body=[BayModelPatchType])
@validation.enforce_network_driver_types_update()
def patch(self, baymodel_uuid, patch):
def patch(self, baymodel_ident, patch):
"""Update an existing baymodel.
:param baymodel_uuid: UUID of a baymodel.
:param baymodel_ident: UUID or logic name of a baymodel.
:param patch: a json PATCH document to apply to this baymodel.
"""
context = pecan.request.context
rpc_baymodel = objects.BayModel.get_by_uuid(context, baymodel_uuid)
rpc_baymodel = api_utils.get_rpc_resource('BayModel', baymodel_ident)
try:
baymodel_dict = rpc_baymodel.as_dict()
baymodel = BayModel(**api_utils.apply_jsonpatch(
@ -391,7 +391,7 @@ class BayModelsController(rest.RestController):
def delete(self, baymodel_ident):
"""Delete a baymodel.
:param baymodel_uuid: UUID or logical name of a baymodel.
:param baymodel_ident: UUID or logical name of a baymodel.
"""
rpc_baymodel = api_utils.get_rpc_resource('BayModel', baymodel_ident)
rpc_baymodel.destroy()

View File

@ -17,6 +17,7 @@ import decorator
from oslo_config import cfg
import pecan
from magnum.api.controllers.v1 import utils as api_utils
from magnum.common import exception
from magnum import objects
@ -85,8 +86,8 @@ def enforce_network_driver_types_create():
def enforce_network_driver_types_update():
@decorator.decorator
def wrapper(func, *args, **kwargs):
uuid = args[1]
baymodel = objects.BayModel.get_by_uuid(pecan.request.context, uuid)
baymodel_ident = args[1]
baymodel = api_utils.get_rpc_resource('BayModel', baymodel_ident)
_enforce_network_driver_types(baymodel)
return func(*args, **kwargs)

View File

@ -131,12 +131,12 @@ class BayModelTest(base.BaseMagnumTest):
bay_model_client.get_baymodel, 'fooo')
@testtools.testcase.attr('negative')
def test_update_baymodel_invalid_uuid(self):
def test_update_baymodel_name_not_found(self):
patch_model = datagen.random_baymodel_name_patch_data()
bay_model_client = cli.BayModelClient.as_user('default')
self.assertRaises(
exceptions.BadRequest,
exceptions.NotFound,
bay_model_client.patch_baymodel, 'fooo', patch_model)
@testtools.testcase.attr('negative')

View File

@ -16,7 +16,6 @@
import mock
from oslo_config import cfg
from magnum.api.controllers.v1 import baymodel as api_baymodel # noqa
from magnum.api import validation as v
from magnum.common import exception
from magnum.tests import base
@ -189,27 +188,26 @@ class TestValidation(base.BaseTestCase):
assert_raised=True)
@mock.patch('pecan.request')
@mock.patch('magnum.objects.BayModel.get_by_uuid')
@mock.patch('magnum.api.controllers.v1.utils.get_rpc_resource')
def _test_enforce_network_driver_types_update(
self,
mock_baymodel_get_by_uuid,
mock_get_rpc_resource,
mock_pecan_request,
network_driver_type,
network_driver_config_dict,
assert_raised=False):
@v.enforce_network_driver_types_update()
def test(self, baymodel_uuid):
def test(self, baymodel_ident):
pass
for key, val in network_driver_config_dict.items():
cfg.CONF.set_override(key, val, 'baymodel')
context = mock_pecan_request.context
baymodel_uuid = 'test_uuid'
baymodel_ident = 'test_uuid_or_name'
baymodel = mock.MagicMock()
baymodel.network_driver = network_driver_type
baymodel.coe = 'kubernetes'
mock_baymodel_get_by_uuid.return_value = baymodel
mock_get_rpc_resource.return_value = baymodel
# Reload the validator module so that baymodel configs are
# re-evaluated.
@ -219,11 +217,11 @@ class TestValidation(base.BaseTestCase):
if assert_raised:
self.assertRaises(exception.InvalidParameterValue,
test, self, baymodel_uuid)
test, self, baymodel_ident)
else:
test(self, baymodel_uuid)
mock_baymodel_get_by_uuid.assert_called_once_with(
context, baymodel_uuid)
test(self, baymodel_ident)
mock_get_rpc_resource.assert_called_once_with(
'BayModel', baymodel_ident)
def test_enforce_network_driver_types_one_allowed_update(self):
self._test_enforce_network_driver_types_update(