From f5c32ad8fda3d7dcee6e4f9447621cde6d0f8a01 Mon Sep 17 00:00:00 2001 From: Jay Dobies Date: Thu, 25 Feb 2016 17:03:01 -0500 Subject: [PATCH] Added heat.resource_type custom constraint This constraint will validate that a parameter value is a valid resource type within the context of the template (that is, taking into account the environment file(s) passed in as well). Change-Id: I82b18d52982c2731370df9ea4ea0e398bf7702f2 Closes-Bug: #1545857 --- heat/engine/constraint/heat_constraints.py | 45 ++++++++++ .../constraints/test_heat_constraints.py | 82 +++++++++++++++++++ setup.cfg | 1 + 3 files changed, 128 insertions(+) create mode 100644 heat/engine/constraint/heat_constraints.py create mode 100644 heat/tests/constraints/test_heat_constraints.py diff --git a/heat/engine/constraint/heat_constraints.py b/heat/engine/constraint/heat_constraints.py new file mode 100644 index 0000000000..c2bc4bcd90 --- /dev/null +++ b/heat/engine/constraint/heat_constraints.py @@ -0,0 +1,45 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import collections + +import six + +from heat.common.i18n import _ +from heat.engine import constraints + + +class ResourceTypeConstraint(constraints.BaseCustomConstraint): + + def validate(self, value, context, template): + + if not isinstance(value, collections.Sequence): + return False + + if isinstance(value, six.string_types): + value = [value] + + invalid_types = [] + for t in value: + try: + template.env.get_class(t) + except Exception: + invalid_types.append(t) + + if invalid_types: + msg = _('The following resource types could not be found: %s') + types = ','.join(invalid_types) + self._error_message = msg % types + return False + + return True diff --git a/heat/tests/constraints/test_heat_constraints.py b/heat/tests/constraints/test_heat_constraints.py new file mode 100644 index 0000000000..e962c68266 --- /dev/null +++ b/heat/tests/constraints/test_heat_constraints.py @@ -0,0 +1,82 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock + +from heat.engine.constraint import heat_constraints as hc +from heat.tests import common + + +class ResourceTypeConstraintTest(common.HeatTestCase): + + def setUp(self): + super(ResourceTypeConstraintTest, self).setUp() + self.constraint = hc.ResourceTypeConstraint() + + self.mock_template = mock.MagicMock() + self.mock_env = mock.MagicMock() + self.mock_template.env = self.mock_env + + def test_validate(self): + # Setup + value = ['OS::Heat::None'] + + # Test + result = self.constraint.validate(value, None, self.mock_template) + + # Verify + self.assertTrue(result) + self.mock_env.get_class.assert_called_once_with(value[0]) + + def test_validate_failure(self): + # Setup + value = ['OS::Heat::None'] + self.mock_env.get_class.side_effect = Exception() + + # Test + result = self.constraint.validate(value, None, self.mock_template) + + # Verify + self.assertFalse(result) + self.assertTrue('OS::Heat::None' in self.constraint._error_message) + self.mock_env.get_class.assert_called_once_with(value[0]) + + def test_validate_multiple_failures(self): + # Setup + value = ['OS::Heat::None', 'OS::Heat::RandomString'] + self.mock_env.get_class.side_effect = [Exception(), Exception()] + + # Test + result = self.constraint.validate(value, None, self.mock_template) + + # Verify + self.assertFalse(result) + self.assertTrue('OS::Heat::None,OS::Heat::RandomString' + in self.constraint._error_message) + self.mock_env.get_class.assert_has_calls([mock.call(value[0]), + mock.call(value[1])]) + + def test_validate_single_item(self): + # Setup + value = 'OS::Heat::None' + + # Test + result = self.constraint.validate(value, None, self.mock_template) + + # Verify + self.assertTrue(result) + self.mock_env.get_class.assert_called_once_with(value) + + def test_validate_non_string(self): + result = self.constraint.validate(dict(), None, self.mock_template) + self.assertFalse(result) diff --git a/setup.cfg b/setup.cfg index 311437f6f5..c66d5baa8e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -94,6 +94,7 @@ heat.constraints = cinder.vtype = heat.engine.clients.os.cinder:VolumeTypeConstraint designate.domain = heat.engine.clients.os.designate:DesignateDomainConstraint glance.image = heat.engine.clients.os.glance:ImageConstraint + heat.resource_type = heat.engine.constraint.heat_constraints:ResourceTypeConstraint keystone.domain = heat.engine.clients.os.keystone.keystone_constraints:KeystoneDomainConstraint keystone.group = heat.engine.clients.os.keystone.keystone_constraints:KeystoneGroupConstraint keystone.project = heat.engine.clients.os.keystone.keystone_constraints:KeystoneProjectConstraint