diff --git a/heat/engine/clients/os/magnum.py b/heat/engine/clients/os/magnum.py index b14da83e6..ee3e536c9 100644 --- a/heat/engine/clients/os/magnum.py +++ b/heat/engine/clients/os/magnum.py @@ -11,10 +11,12 @@ # License for the specific language governing permissions and limitations # under the License. -from magnumclient.openstack.common.apiclient import exceptions +from magnumclient.openstack.common.apiclient import exceptions as mc_exc from magnumclient.v1 import client as magnum_client +from heat.common import exception from heat.engine.clients import client_plugin +from heat.engine import constraints class MagnumClientPlugin(client_plugin.ClientPlugin): @@ -34,10 +36,25 @@ class MagnumClientPlugin(client_plugin.ClientPlugin): return client def is_not_found(self, ex): - return isinstance(ex, exceptions.NotFound) + return isinstance(ex, mc_exc.NotFound) def is_over_limit(self, ex): - return isinstance(ex, exceptions.RequestEntityTooLarge) + return isinstance(ex, mc_exc.RequestEntityTooLarge) def is_conflict(self, ex): - return isinstance(ex, exceptions.Conflict) + return isinstance(ex, mc_exc.Conflict) + + def get_baymodel(self, value): + try: + self.client().baymodels.get(value) + except mc_exc.NotFound: + raise exception.EntityNotFound(entity='BayModel', + name=value) + + +class BaymodelConstraint(constraints.BaseCustomConstraint): + + expected_exceptions = (exception.EntityNotFound,) + + def validate_with_client(self, client, value): + client.client_plugin('magnum').get_baymodel(value) diff --git a/heat/engine/resources/openstack/magnum/bay.py b/heat/engine/resources/openstack/magnum/bay.py index e34398f79..831f7866f 100644 --- a/heat/engine/resources/openstack/magnum/bay.py +++ b/heat/engine/resources/openstack/magnum/bay.py @@ -45,7 +45,10 @@ class Bay(resource.Resource): ), BAYMODEL: properties.Schema( properties.Schema.STRING, - _('The ID of the bay model.'), + _('The name or ID of the bay model.'), + constraints=[ + constraints.CustomConstraint('magnum.baymodel') + ], required=True ), NODE_COUNT: properties.Schema( @@ -62,7 +65,7 @@ class Bay(resource.Resource): ), DISCOVERY_URL: properties.Schema( properties.Schema.STRING, - _('Url used for bay node discovery.') + _('Specifies a custom discovery url for node discovery.') ), BAY_CREATE_TIMEOUT: properties.Schema( properties.Schema.INTEGER, diff --git a/heat/tests/magnum/test_magnum_bay.py b/heat/tests/magnum/test_magnum_bay.py index 7fab3c043..f337f6867 100644 --- a/heat/tests/magnum/test_magnum_bay.py +++ b/heat/tests/magnum/test_magnum_bay.py @@ -16,8 +16,11 @@ import mock from oslo_config import cfg import six +from magnumclient.openstack.common.apiclient import exceptions as mc_exc + from heat.common import exception from heat.common import template_format +from heat.engine.clients.os import magnum as mc from heat.engine import resource from heat.engine.resources.openstack.magnum import bay from heat.engine import scheduler @@ -54,6 +57,7 @@ class TestMagnumBay(common.HeatTestCase): self.rsrc_defn = resource_defns['test_bay'] self.client = mock.Mock() self.patchobject(bay.Bay, 'client', return_value=self.client) + self.patchobject(mc.MagnumClientPlugin, 'get_baymodel') def _create_resource(self, name, snippet, stack, stat='CREATE_COMPLETE'): self.resource_id = '12345' @@ -144,3 +148,21 @@ class TestMagnumBay(common.HeatTestCase): mapping = bay.resource_mapping() self.assertEqual(1, len(mapping)) self.assertEqual(mapping[RESOURCE_TYPE], bay.Bay) + + +class BaymodelConstraintTest(common.HeatTestCase): + def setUp(self): + super(BaymodelConstraintTest, self).setUp() + self.ctx = utils.dummy_context() + self.mock_baymodel_get = mock.Mock() + self.ctx.clients.client_plugin( + 'magnum').client().baymodels.get = self.mock_baymodel_get + self.constraint = mc.BaymodelConstraint() + + def test_validate(self): + self.mock_baymodel_get.return_value = None + self.assertTrue(self.constraint.validate("mybaymodel", self.ctx)) + + def test_validate_fail(self): + self.mock_baymodel_get.side_effect = mc_exc.NotFound() + self.assertFalse(self.constraint.validate("badbaymodel", self.ctx)) diff --git a/setup.cfg b/setup.cfg index 31ad27f35..cc3ba8ae0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -102,6 +102,7 @@ heat.constraints = keystone.group = heat.engine.clients.os.keystone:KeystoneGroupConstraint keystone.service = heat.engine.clients.os.keystone:KeystoneServiceConstraint keystone.user = heat.engine.clients.os.keystone:KeystoneUserConstraint + magnum.baymodel = heat.engine.clients.os.magnum:BaymodelConstraint manila.share_snapshot = heat.engine.clients.os.manila:ManilaShareSnapshotConstraint manila.share_network = heat.engine.clients.os.manila:ManilaShareNetworkConstraint manila.share_type = heat.engine.clients.os.manila:ManilaShareTypeConstraint