Exemplo n.º 1
0
    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(),
                )
Exemplo n.º 2
0
    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
        }
Exemplo n.º 3
0
    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(),
                    )
Exemplo n.º 4
0
 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