Use appropriate exception in StackResource.get_output()

Don't raise InvalidTemplateAttribute in StackResource.get_output() when an
output does not exist - it's not the case that get_output() is only used
for fetching attributes. Instead, raise NotFound from get_output(), and
translate that to InvalidTemplateAttribute in the caller when we are
actually fetching an attribute.

Change-Id: I4f883e4b583a965785d0a595c8c33b47dc94118c
Related-Bug: #1719333
This commit is contained in:
Zane Bitter 2017-12-19 16:36:43 -05:00
parent 622af9952b
commit 2e4a6e237c
5 changed files with 27 additions and 13 deletions

View File

@ -85,7 +85,11 @@ class NestedStack(stack_resource.StackResource):
if key and not key.startswith('Outputs.'): if key and not key.startswith('Outputs.'):
raise exception.InvalidTemplateAttribute(resource=self.name, raise exception.InvalidTemplateAttribute(resource=self.name,
key=key) key=key)
attribute = self.get_output(key.partition('.')[-1]) try:
attribute = self.get_output(key.partition('.')[-1])
except exception.NotFound:
raise exception.InvalidTemplateAttribute(resource=self.name,
key=key)
return attributes.select_from_attribute(attribute, path) return attributes.select_from_attribute(attribute, path)
def get_reference_id(self): def get_reference_id(self):

View File

@ -628,7 +628,11 @@ backend servers
def _resolve_attribute(self, name): def _resolve_attribute(self, name):
"""We don't really support any of these yet.""" """We don't really support any of these yet."""
if name == self.DNS_NAME: if name == self.DNS_NAME:
return self.get_output('PublicIp') try:
return self.get_output('PublicIp')
except exception.NotFound:
raise exception.InvalidTemplateAttribute(resource=self.name,
key=name)
elif name in self.attributes_schema: elif name in self.attributes_schema:
# Not sure if we should return anything for the other attribs # Not sure if we should return anything for the other attribs
# since they aren't really supported in any meaningful way # since they aren't really supported in any meaningful way

View File

@ -606,10 +606,7 @@ class StackResource(resource.Resource):
def get_output(self, op): def get_output(self, op):
"""Return the specified Output value from the nested stack. """Return the specified Output value from the nested stack.
If the output key does not exist, raise an InvalidTemplateAttribute If the output key does not exist, raise a NotFound exception.
exception. (Note that TemplateResource.get_attribute() relies on this
particular exception, not KeyError, being raised if the key does not
exist.)
""" """
if (self._outputs is None or if (self._outputs is None or
(op in self._outputs and (op in self._outputs and
@ -626,8 +623,8 @@ class StackResource(resource.Resource):
self._outputs = {o[rpc_api.OUTPUT_KEY]: o for o in outputs} self._outputs = {o[rpc_api.OUTPUT_KEY]: o for o in outputs}
if op not in self._outputs: if op not in self._outputs:
raise exception.InvalidTemplateAttribute(resource=self.name, raise exception.NotFound(_('Specified output key %s not '
key=op) 'found.') % op)
output_data = self._outputs[op] output_data = self._outputs[op]
if rpc_api.OUTPUT_ERROR in output_data: if rpc_api.OUTPUT_ERROR in output_data:
@ -639,4 +636,8 @@ class StackResource(resource.Resource):
return output_data[rpc_api.OUTPUT_VALUE] return output_data[rpc_api.OUTPUT_VALUE]
def _resolve_attribute(self, name): def _resolve_attribute(self, name):
return self.get_output(name) try:
return self.get_output(name)
except exception.NotFound:
raise exception.InvalidTemplateAttribute(resource=self.name,
key=name)

View File

@ -330,7 +330,7 @@ class TemplateResource(stack_resource.StackResource):
message=output[rpc_api.OUTPUT_ERROR]) message=output[rpc_api.OUTPUT_ERROR])
except exception.TemplateOutputError as err: except exception.TemplateOutputError as err:
LOG.info('%s', err) LOG.info('%s', err)
except (exception.InvalidTemplateAttribute, exception.NotFound): except exception.NotFound:
pass pass
else: else:
self._reference_id = output[rpc_api.OUTPUT_VALUE] self._reference_id = output[rpc_api.OUTPUT_VALUE]
@ -347,4 +347,9 @@ class TemplateResource(stack_resource.StackResource):
return grouputils.get_nested_attrs(self, key, False, *path) return grouputils.get_nested_attrs(self, key, False, *path)
# then look for normal outputs # then look for normal outputs
return attributes.select_from_attribute(self.get_output(key), path) try:
return attributes.select_from_attribute(self.get_output(key),
path)
except exception.NotFound:
raise exception.InvalidTemplateAttribute(resource=self.name,
key=key)

View File

@ -689,7 +689,7 @@ class StackResourceAttrTest(StackResourceBaseTest):
output = {'outputs': []} output = {'outputs': []}
self.parent_resource._rpc_client.show_stack.return_value = [output] self.parent_resource._rpc_client.show_stack.return_value = [output]
self.assertRaises(exception.InvalidTemplateAttribute, self.assertRaises(exception.NotFound,
self.parent_resource.get_output, self.parent_resource.get_output,
"key") "key")
@ -701,7 +701,7 @@ class StackResourceAttrTest(StackResourceBaseTest):
output = {} output = {}
self.parent_resource._rpc_client.show_stack.return_value = [output] self.parent_resource._rpc_client.show_stack.return_value = [output]
self.assertRaises(exception.InvalidTemplateAttribute, self.assertRaises(exception.NotFound,
self.parent_resource.get_output, self.parent_resource.get_output,
"key") "key")