Raise InvalidTemplateReference for missing resources

Previously this code raised InvalidTemplateAttribute when a resource
doesn't exist. But our existing code (in the dependency check, which
executes first) raises InvalidTemplateReference in this case and reserves
InvalidTemplateAttribute for when the resource exists but the attribute is
invalid. This patch makes the functions consistent with that behaviour.

It also raises InvalidTemplateReference, instead of KeyError, in the same
case in get_resource. I assumed that this could never fail, but that is
only true for Ref, not get_resource.

Change-Id: Ib7dcd3333bc2fc7778d3a013abae10c5795086dd
This commit is contained in:
Zane Bitter 2014-04-29 16:39:07 -04:00
parent 40860d6c82
commit 78265babaf
3 changed files with 14 additions and 10 deletions

View File

@ -85,8 +85,9 @@ class ParamRef(function.Function):
try:
return self.parameters[param_name]
except (KeyError, ValueError):
raise exception.UserParameterMissing(key=param_name)
except KeyError:
raise exception.InvalidTemplateReference(resource=param_name,
key='unknown')
class ResourceRef(function.Function):
@ -98,10 +99,14 @@ class ResourceRef(function.Function):
{ "Ref" : "<resource_name>" }
'''
def _resource(self):
def _resource(self, path='unknown'):
resource_name = function.resolve(self.args)
return self.stack[resource_name]
try:
return self.stack[resource_name]
except KeyError:
raise exception.InvalidTemplateReference(resource=resource_name,
key=path)
def result(self):
return self._resource().FnGetRefId()
@ -150,15 +155,14 @@ class GetAtt(function.Function):
return resource_name, attribute
def _resource(self):
def _resource(self, path='unknown'):
resource_name = function.resolve(self._resource_name)
try:
return self.stack[resource_name]
except KeyError:
raise exception.InvalidTemplateAttribute(
resource=resource_name,
key=function.resolve(self._attribute))
raise exception.InvalidTemplateReference(resource=resource_name,
key=path)
def result(self):
attribute = function.resolve(self._attribute)

View File

@ -220,7 +220,7 @@ class Parameter(object):
if self.has_default():
return self.default()
raise KeyError(_('Missing parameter %s') % self.name)
raise exception.UserParameterMissing(key=self.name)
def hidden(self):
'''

View File

@ -649,7 +649,7 @@ class StackTest(test_parser.StackTest):
self.stack.state)
snippet = {'Value': {'get_attr': ['resource2', 'who_cares']}}
self.assertRaises(exception.InvalidTemplateAttribute,
self.assertRaises(exception.InvalidTemplateReference,
self.resolve, snippet)
@utils.stack_delete_after