Fixes in the templates code

1. Remove the 'parameters' section after resolving the parameters
2. Give an error code in template validate if the template type was not specified

Story: 2004056
Task: 29576

Depends-On: I18af00599461ead7bf9084168aeed7321adfd4c0
Change-Id: If5d03278f6cc4ca9ae071783bf60520d95146eea
This commit is contained in:
Ifat Afek 2019-02-26 13:19:10 +00:00 committed by Eyal
parent ca66ef5855
commit 98e1fdf07b
3 changed files with 19 additions and 7 deletions

View File

@ -49,11 +49,15 @@ The following describes all the possible status code and their messages:
+------------------+---------------------------------------------------------+-------------------------------+ +------------------+---------------------------------------------------------+-------------------------------+
| 63 | Unsupported version. Version must be one of: {versions} | content | | 63 | Unsupported version. Version must be one of: {versions} | content |
+------------------+---------------------------------------------------------+-------------------------------+ +------------------+---------------------------------------------------------+-------------------------------+
| 64 | metadata section must contain a type field in version 2.| content | | 64 | metadata section must contain a type field starting | content |
| | Type must be one of: {standard, equivalence, definition}| content | | | from version 2. | |
| | Type must be one of: {standard, equivalence, definition}| |
+------------------+---------------------------------------------------------+-------------------------------+ +------------------+---------------------------------------------------------+-------------------------------+
| 65 | Invalid template type. Type must be one of: {standard, | content | | 65 | Invalid template type. Type must be one of: {standard, | content |
| | equivalence, definition} | content | | | equivalence, definition} | |
+------------------+---------------------------------------------------------+-------------------------------+
| 66 | Missing template type. Type must be one of: {standard, | content |
| | equivalence, definition} | |
+------------------+---------------------------------------------------------+-------------------------------+ +------------------+---------------------------------------------------------+-------------------------------+
| 80 | scenarios is a mandatory section | syntax | | 80 | scenarios is a mandatory section | syntax |
+------------------+---------------------------------------------------------+-------------------------------+ +------------------+---------------------------------------------------------+-------------------------------+

View File

@ -15,13 +15,15 @@ from oslo_log import log
from oslo_utils import uuidutils from oslo_utils import uuidutils
from vitrage.common.constants import TemplateStatus from vitrage.common.constants import TemplateStatus
from vitrage.common.constants import TemplateTopologyFields as TFields
from vitrage.common.constants import TemplateTypes as TType from vitrage.common.constants import TemplateTypes as TType
from vitrage.common.exception import VitrageError from vitrage.common.exception import VitrageError
from vitrage.evaluator.base import Template from vitrage.evaluator.base import Template
from vitrage.evaluator.template_fields import TemplateFields as TFields
from vitrage.evaluator.template_functions.v2 import resolve_parameters from vitrage.evaluator.template_functions.v2 import resolve_parameters
from vitrage.evaluator import template_validation from vitrage.evaluator import template_validation
from vitrage.evaluator.template_validation import base from vitrage.evaluator.template_validation import base
from vitrage.evaluator.template_validation.content.base import \
get_content_fault_result
from vitrage.storage.sqlalchemy import models from vitrage.storage.sqlalchemy import models
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
@ -42,6 +44,9 @@ def add_templates_to_db(db, templates, cli_type, params=None):
result = _validate_template(db, template, final_type, params) result = _validate_template(db, template, final_type, params)
if result.is_valid_config: if result.is_valid_config:
result = resolve_parameters(template, params) result = resolve_parameters(template, params)
if result.is_valid_config and TFields.PARAMETERS in template:
# remove the 'parameters' section, it is no longer needed
del template[TFields.PARAMETERS]
# validate again, with the resolved parameters # validate again, with the resolved parameters
if result.is_valid_config: if result.is_valid_config:
@ -66,7 +71,8 @@ def validate_templates(db, templates, cli_type, params):
for template in templates: for template in templates:
final_type = template[METADATA].get(TFields.TYPE, cli_type) final_type = template[METADATA].get(TFields.TYPE, cli_type)
if not final_type or (cli_type and cli_type != final_type): if not final_type or (cli_type and cli_type != final_type):
results.append(base.Result("", False, "", "Unknown template type")) results.append(
get_content_fault_result(66, "Unknown template type"))
else: else:
results.append( results.append(
_validate_template(db, template, final_type, params)) _validate_template(db, template, final_type, params))

View File

@ -45,10 +45,12 @@ status_msgs = {
62: 'metadata is a mandatory section.', 62: 'metadata is a mandatory section.',
63: 'Unsupported version. Version must be one of: {versions}' 63: 'Unsupported version. Version must be one of: {versions}'
.format(versions=TemplateSchemaFactory.supported_versions()), .format(versions=TemplateSchemaFactory.supported_versions()),
64: 'metadata section must contain a type field in version 2. Type must be' 64: 'metadata section must contain a type field starting from version 2. '
' one of: {types}'.format(types=TemplateTypes.types()), 'Type must be one of: {types}'.format(types=TemplateTypes.types()),
65: 'Invalid template type. Type must be one of: {types}' 65: 'Invalid template type. Type must be one of: {types}'
.format(types=TemplateTypes.types()), .format(types=TemplateTypes.types()),
66: 'Missing template type. Type must be one of: {types}'
.format(types=TemplateTypes.types()),
# scenarios section status messages 80-99 # scenarios section status messages 80-99
80: 'scenarios is a mandatory section.', 80: 'scenarios is a mandatory section.',