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
This commit is contained in:
parent
4fdf72b000
commit
f5c32ad8fd
45
heat/engine/constraint/heat_constraints.py
Normal file
45
heat/engine/constraint/heat_constraints.py
Normal file
@ -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
|
82
heat/tests/constraints/test_heat_constraints.py
Normal file
82
heat/tests/constraints/test_heat_constraints.py
Normal file
@ -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)
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user