def verify_template(self): if ExceptionCollector.exceptionsCaught(): if self.input_path: msg = (_('\nThe input "%(path)s" failed validation with ' 'the following error(s): \n\n\t') % { 'path': self.input_path }) + '\n\t'.join( ExceptionCollector.getExceptionsReport()) else: msg = _('\nThe pre-parsed input failed validation with ' 'the following error(s): \n\n\t') + '\n\t'.join( ExceptionCollector.getExceptionsReport()) else: if self.input_path: msg = (_('The input "%(path)s" successfully passed ' 'validation.') % { 'path': self.input_path }) else: msg = _('The pre-parsed input successfully passed validation.') log.info(msg) return msg
def __init__( self, toscaDef, spec=None, path=None, resolver=None, ): self.discovered = None if spec: inputs = spec.get("inputs") else: inputs = None if isinstance(toscaDef, ToscaTemplate): self.template = toscaDef else: topology_tpl = toscaDef.get("topology_template") if not topology_tpl: toscaDef["topology_template"] = dict(node_templates={}, relationship_templates={}) if spec: self.loadInstances(toscaDef, spec) logger.info("Validating TOSCA template at %s", path) self._parseTemplate(path, inputs, toscaDef, resolver) decorators = self.loadDecorators() if decorators: # copy errors before we clear them in _overlay errorsSoFar = ExceptionCollector.exceptions[:] self._overlay(decorators) if ExceptionCollector.exceptionsCaught(): # abort if overlay caused errors # report previously collected errors too ExceptionCollector.exceptions[:0] = errorsSoFar message = "\n".join( ExceptionCollector.getExceptionsReport( getLogLevel() < logging.INFO)) raise UnfurlValidationError( "TOSCA validation failed for %s: \n%s" % (path, message), ExceptionCollector.getExceptions(), ) # overlay modifies tosaDef in-place, try reparsing it self._parseTemplate(path, inputs, toscaDef, resolver) if ExceptionCollector.exceptionsCaught(): message = "\n".join( ExceptionCollector.getExceptionsReport( getLogLevel() < logging.INFO)) raise UnfurlValidationError( "TOSCA validation failed for %s: \n%s" % (path, message), ExceptionCollector.getExceptions(), )
def __init__(self, tosca_parser_template, provider, cluster_name): self.provider = provider self.provider_config = ProviderConfiguration(self.provider) self.cluster_name = cluster_name ExceptionCollector.start() for sec in self.REQUIRED_CONFIG_PARAMS: if not self.provider_config.config[self.provider_config.MAIN_SECTION].get(sec): ExceptionCollector.appendException(ProviderConfigurationParameterError( what=sec )) ExceptionCollector.stop() if ExceptionCollector.exceptionsCaught(): raise ValidationError( message='\nTranslating to provider failed: ' .join(ExceptionCollector.getExceptionsReport()) ) # toscaparser.tosca_template:ToscaTemplate self.tosca_parser_template = tosca_parser_template # toscaparser.topology_template:TopologyTemplate self.tosca_topology_template = self.full_type_definitions(self.tosca_parser_template.topology_template) import_definition_file = ImportsLoader([self.definition_file()], None, list(SERVICE_TEMPLATE_KEYS), self.tosca_topology_template.tpl) self.full_provider_defs = copy.copy(self.tosca_topology_template.custom_defs) self.provider_defs = import_definition_file.get_custom_defs() self.full_provider_defs.update(self.provider_defs) self.artifacts = [] self.used_conditions_set = set() self.extra_configuration_tool_params = dict() self.inputs = self.tosca_topology_template.inputs self.outputs = self.tosca_topology_template.outputs node_templates = self.resolve_get_property_functions(self.tosca_topology_template.nodetemplates) self.topology_template = self.translate_to_provider() self.node_templates = self.topology_template.nodetemplates self.relationship_templates = self.topology_template.relationship_templates self.template_dependencies = dict() # list of lists # After this step self.node_templates has requirements with node_filter parameter self.resolve_in_template_dependencies() self.resolve_in_template_get_functions() self.configuration_content = None self.configuration_ready = None # Create the list of ProviderResource instances self.software_component_names = [] for node in self.node_templates: if self._is_software_component(node): self.software_component_names.append(node.name) self.provider_nodes = self._provider_nodes() self.provider_nodes_by_name = self._provider_nodes_by_name() self.relationship_templates_by_name = self._relationship_templates_by_name() self.provider_node_names_by_priority = self._sort_nodes_by_priority() self.provider_nodes_queue = self.sort_nodes_by_dependency()
def __init__(self, toscaDef, inputs=None, instances=None, path=None, resolver=None): self.discovered = None if isinstance(toscaDef, ToscaTemplate): self.template = toscaDef else: topology_tpl = toscaDef.get("topology_template") if not topology_tpl: toscaDef["topology_template"] = dict(node_templates={}, relationship_templates={}) if instances: self.loadInstances(toscaDef, instances) logger.info("Validating TOSCA template at %s", path) try: # need to set a path for the import loader self.template = ToscaTemplate( path=path, parsed_params=inputs, yaml_dict_tpl=toscaDef, import_resolver=resolver, ) except ValidationError: message = "\n".join( ExceptionCollector.getExceptionsReport(False)) raise UnfurlValidationError( "TOSCA validation failed for %s: \n%s" % (path, message), ExceptionCollector.getExceptions(), ) self.nodeTemplates = {} self.installers = {} self.relationshipTemplates = {} for template in self.template.nodetemplates: nodeTemplate = NodeSpec(template, self) if template.is_derived_from(self.InstallerType): self.installers[template.name] = nodeTemplate self.nodeTemplates[template.name] = nodeTemplate if hasattr(self.template, "relationship_templates"): # user-declared RelationshipTemplates, source and target will be None for template in self.template.relationship_templates: relTemplate = RelationshipSpec(template, self) self.relationshipTemplates[template.name] = relTemplate self.topology = TopologySpec(self, inputs) self.load_workflows() self.groups = { g.name: GroupSpec(g, self) for g in self.template.topology_template.groups } self.policies = { p.name: PolicySpec(p, self) for p in self.template.topology_template.policies }
def verify_template(self): if ExceptionCollector.exceptionsCaught(): if self.input_path: msg=(_('\nThe input "%(path)s" failed validation with ' 'the following error(s): \n\n\t') % {'path': self.input_path}) + '\n\t'.join(ExceptionCollector.getExceptionsReport()) else: msg=_('\nThe pre-parsed input failed validation with ' 'the following error(s): \n\n\t') + '\n\t'.join(ExceptionCollector.getExceptionsReport()) else: if self.input_path: msg = (_('The input "%(path)s" successfully passed ' 'validation.') % {'path': self.input_path}) else: msg = _('The pre-parsed input successfully passed validation.') log.info(msg) return msg
def __init__(self, toscaDef, spec=None, path=None, resolver=None, skip_validation=False): self.discovered = None if spec: inputs = spec.get("inputs") else: inputs = None if isinstance(toscaDef, ToscaTemplate): self.template = toscaDef else: self.template = None topology_tpl = toscaDef.get("topology_template") if not topology_tpl: toscaDef["topology_template"] = dict(node_templates={}, relationship_templates={}) if spec: self.load_instances(toscaDef, spec) logger.info("Validating TOSCA template at %s", path) try: self._parse_template(path, inputs, toscaDef, resolver) except: if not ExceptionCollector.exceptionsCaught( ) or not self.template or not self.topology: raise # unexpected error # copy errors because self._patch() might clear them errorsSoFar = ExceptionCollector.exceptions[:] patched = self._patch(toscaDef, path, errorsSoFar) if patched: # overlay and evaluate_imports modifies tosaDef in-place, try reparsing it self._parse_template(path, inputs, toscaDef, resolver) else: # restore previously errors ExceptionCollector.exceptions[:0] = errorsSoFar if ExceptionCollector.exceptionsCaught(): message = "\n".join( ExceptionCollector.getExceptionsReport( full=(get_console_log_level() < logging.INFO))) if skip_validation: logger.warning("Found TOSCA validation failures: %s", message) else: raise UnfurlValidationError( f"TOSCA validation failed for {path}: \n{message}", ExceptionCollector.getExceptions(), )
def verify_template(self): if ExceptionCollector.exceptionsCaught(): raise ValidationError( message=(_('\nThe input "%(path)s" failed validation with the ' 'following error(s): \n\n\t') % {'path': self.input_path}) + '\n\t'.join(ExceptionCollector.getExceptionsReport())) else: msg = (_('The input "%(path)s" successfully passed validation.') % {'path': self.input_path}) log.info(msg) print(msg)
def verify_template(self): if self.no_required_paras_check: ExceptionCollector.removeException(MissingRequiredParameterError) ExceptionCollector.removeException(MissingRequiredInputError) ExceptionCollector.removeException(MissingRequiredOutputError) if ExceptionCollector.exceptionsCaught(): if self.input_path: exceptions = ValidationError( message=(_('\nThe input "%(path)s" failed validation with ' 'the following error(s): \n\n\t') % { 'path': self.input_path }) + '\n\t'.join(ExceptionCollector.getExceptionsReport())) else: exceptions = ValidationError( message=_('\nThe pre-parsed input failed validation with ' 'the following error(s): \n\n\t') + '\n\t'.join(ExceptionCollector.getExceptionsReport())) if not self.debug_mode: raise exceptions else: if self.sub_mapped_node_template: msg = _('======== nested service template ======== ') else: msg = _('======== main service template ======== ') print(msg) print(exceptions.message) log.error(msg) log.error(exceptions.message) else: if self.input_path: msg = (_('The input "%(path)s" successfully passed ' 'validation.') % { 'path': self.input_path }) else: msg = _('The pre-parsed input successfully passed validation.') log.info(msg)
def verify_template(self): if ExceptionCollector.exceptionsCaught(): raise ValidationError( message=(_('\nThe input "%(path)s" failed validation with the ' 'following error(s): \n\n\t') % { 'path': self.input_path }) + '\n\t'.join(ExceptionCollector.getExceptionsReport())) else: msg = (_('The input "%(path)s" successfully passed validation.') % { 'path': self.input_path }) log.info(msg) print(msg)
def translate_node_from_tosca(restructured_mapping, tpl_name, self): """ Translator from TOSCA definitions in provider definitions using rules from element_map_to_provider :param restructured_mapping: list of dicts(parameter, map, value) :param tpl_name: str :return: entity_tpl as dict """ resulted_structure = {} for item in restructured_mapping: ExceptionCollector.start() self[PARAMETER] = item[PARAMETER] self[VALUE] = item[VALUE] mapped_param = restructure_value(mapping_value=item[MAP_KEY], self=self) ExceptionCollector.stop() if ExceptionCollector.exceptionsCaught(): raise ValidationError( message='\nTranslating to provider failed: '.join( ExceptionCollector.getExceptionsReport())) structures, keyname = get_structure_of_mapped_param( mapped_param, item[VALUE]) for structure in structures: r = retrieve_node_templates(structure) for node_type, tpl in r.items(): if not keyname: (_, _, type_name) = tosca_type.parse(node_type) if not type_name: ExceptionCollector.appendException() keyname = self[KEYNAME] + "_" + snake_case.convert( type_name) node_tpl_with_name = {keyname: {node_type: tpl}} resulted_structure = deep_update_dict(resulted_structure, node_tpl_with_name) for keyname, node in resulted_structure.items(): for node_type, tpl in node.items(): if tpl.get(REQUIREMENTS): reqs = [] for req_name, req in tpl[REQUIREMENTS].items(): reqs.append({req_name: req}) resulted_structure[keyname][node_type][REQUIREMENTS] = reqs return resulted_structure
def _patch(self, toscaDef, path, errorsSoFar): matches = None decorators = self.load_decorators() if decorators: logger.debug("applying decorators %s", decorators) matches = self._overlay(decorators) # overlay uses ExceptionCollector if ExceptionCollector.exceptionsCaught(): # abort if overlay caused errors # report previously collected errors too ExceptionCollector.exceptions[:0] = errorsSoFar message = "\n".join( ExceptionCollector.getExceptionsReport( full=(get_console_log_level() < logging.INFO))) raise UnfurlValidationError( f"TOSCA validation failed for {path}: \n{message}", ExceptionCollector.getExceptions(), ) modified_imports = self.evaluate_imports(toscaDef) annotated = self.enforce_filters() return matches or modified_imports or annotated