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, 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 __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 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 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 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 validate(self): """Validate the provided CSAR file.""" self.is_validated = True # validate that the file or URL exists missing_err_msg = (_('"%s" does not exist.') % self.path) if self.a_file: if not os.path.isfile(self.path): ExceptionCollector.appendException( ValidationError(message=missing_err_msg)) return False else: self.csar = self.path else: # a URL if not UrlUtils.validate_url(self.path): ExceptionCollector.appendException( ValidationError(message=missing_err_msg)) return False else: response = requests.get(self.path) self.csar = BytesIO(response.content) # validate that it is a valid zip file if not zipfile.is_zipfile(self.csar): err_msg = (_('"%s" is not a valid zip file.') % self.path) ExceptionCollector.appendException( ValidationError(message=err_msg)) return False # validate that it contains the metadata file in the correct location self.zfile = zipfile.ZipFile(self.csar, 'r') filelist = self.zfile.namelist() if TOSCA_META in filelist: self.is_tosca_metadata = True # validate that 'Entry-Definitions' property exists in TOSCA.meta is_validated = self._validate_tosca_meta(filelist) else: self.is_tosca_metadata = False is_validated = self._validate_root_level_yaml(filelist) if is_validated: # validate that external references and imports in the main # template actually exist and are accessible main_tpl = self._read_template_yaml(self.main_template_file_name) self._validate_external_references(main_tpl) return not ExceptionCollector.exceptionsCaught()
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 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 _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
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)