Merge "Add validation for an existence of a resource type"

This commit is contained in:
Jenkins 2013-12-13 08:58:13 +00:00 committed by Gerrit Code Review
commit fbdec8a7eb
3 changed files with 27 additions and 1 deletions

View File

@ -273,6 +273,14 @@ class ResourceRegistry(object):
return match
def get_class(self, resource_type, resource_name=None):
if resource_type == "":
msg = _('Resource "%s" has no type') % resource_name
raise exception.StackValidationFailed(message=msg)
elif resource_type is None:
msg = _('Non-empty resource type is required '
'for resource "%s"') % resource_name
raise exception.StackValidationFailed(message=msg)
info = self.get_resource_info(resource_type,
resource_name=resource_name)
if info is None:

View File

@ -159,7 +159,7 @@ class Resource(object):
return super(Resource, cls).__new__(cls)
# Select the correct subclass to instantiate
ResourceClass = stack.env.get_class(json['Type'],
ResourceClass = stack.env.get_class(json.get('Type'),
resource_name=name)
return ResourceClass(name, json, stack)

View File

@ -24,6 +24,7 @@ from heat.engine import resource
from heat.engine import scheduler
from heat.engine import template
from heat.engine import environment
from heat.openstack.common.gettextutils import _
import heat.db.api as db_api
from heat.tests import generic_resource as generic_rsrc
@ -67,6 +68,23 @@ class ResourceTest(HeatTestCase):
self.assertRaises(exception.StackValidationFailed,
resource.Resource, 'aresource', snippet, self.stack)
def test_resource_non_type(self):
snippet = {'Type': ''}
resource_name = 'aresource'
ex = self.assertRaises(exception.StackValidationFailed,
resource.Resource, resource_name,
snippet, self.stack)
self.assertIn(_('Resource "%s" has no type') % resource_name, str(ex))
def test_resource_missed_type(self):
snippet = {'not-a-Type': 'GenericResourceType'}
resource_name = 'aresource'
ex = self.assertRaises(exception.StackValidationFailed,
resource.Resource, resource_name,
snippet, self.stack)
self.assertIn(_('Non-empty resource type is required '
'for resource "%s"') % resource_name, str(ex))
def test_state_defaults(self):
tmpl = {'Type': 'Foo'}
res = generic_rsrc.GenericResource('test_res_def', tmpl, self.stack)