Merge "Validate TOSCA template version"

This commit is contained in:
Jenkins
2015-02-05 08:29:37 +00:00
committed by Gerrit Code Review
4 changed files with 40 additions and 1 deletions

View File

@@ -89,3 +89,8 @@ class UnknownInputError(TOSCAException):
class InvalidPropertyValueError(TOSCAException):
msg_fmt = _('Value of property "%(what)s" is invalid.')
class InvalidTemplateVersion(TOSCAException):
msg_fmt = _('The template version "%(what)s" is invalid. '
'The valid versions are: "%(valid_versions)s"')

View File

@@ -0,0 +1,11 @@
tosca_definitions_version: tosca_xyz
description: >
Test template with an invalid template version.
node_templates:
server:
type: tosca.nodes.Compute
properties:
os_type: Linux
num_cpus: 2

View File

@@ -13,6 +13,7 @@
import os
import six
from translator.toscalib.common.exception import InvalidTemplateVersion
from translator.toscalib.common.exception import InvalidTypeError
from translator.toscalib.common.exception import MissingRequiredFieldError
from translator.toscalib.common.exception import TypeMismatchError
@@ -430,3 +431,14 @@ class ToscaTemplateValidationTest(TestCase):
rel_template = RelationshipTemplate(rel_template[name], name)
err = self.assertRaises(expectederror, rel_template.validate)
self.assertEqual(expectedmessage, six.text_type(err))
def test_invalid_template_version(self):
tosca_tpl = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/test_invalid_template_version.yaml")
err = self.assertRaises(InvalidTemplateVersion, ToscaTemplate,
tosca_tpl)
valid_versions = ', '.join(ToscaTemplate.VALID_TEMPLATE_VERSIONS)
ex_err_msg = ('The template version "tosca_xyz" is invalid. '
'The valid versions are: "%s"' % valid_versions)
self.assertEqual(six.text_type(err), ex_err_msg)

View File

@@ -14,6 +14,7 @@
import logging
import os
from translator.toscalib.common.exception import InvalidTemplateVersion
from translator.toscalib.common.exception import MissingRequiredFieldError
from translator.toscalib.common.exception import UnknownFieldError
from translator.toscalib import functions
@@ -44,6 +45,9 @@ YAML_LOADER = translator.toscalib.utils.yamlparser.load_yaml
class ToscaTemplate(object):
VALID_TEMPLATE_VERSIONS = ['tosca_simple_yaml_1_0_0']
'''Load the template data.'''
def __init__(self, path):
self.tpl = YAML_LOADER(path)
@@ -151,7 +155,8 @@ class ToscaTemplate(object):
def _validate_field(self):
try:
self._tpl_version()
version = self._tpl_version()
self._validate_version(version)
except KeyError:
raise MissingRequiredFieldError(what='Template',
required=DEFINITION_VERSION)
@@ -190,3 +195,9 @@ class ToscaTemplate(object):
func = functions.get_function(self, self.outputs, output.value)
if isinstance(func, functions.GetAttribute):
output.attrs[output.VALUE] = func
def _validate_version(self, version):
if version not in self.VALID_TEMPLATE_VERSIONS:
raise InvalidTemplateVersion(
what=version,
valid_versions=', '. join(self.VALID_TEMPLATE_VERSIONS))