Exemple #1
0
def node_templates_or_groups_validator(field, presentation, context):
    """
    Makes sure that the field's elements refer to either node templates or groups.

    Used with the :func:`field_validator` decorator for the "targets" field in :class:`GroupTemplate`.
    """

    field.default_validate(presentation, context)

    values = getattr(presentation, field.name)
    if values is not None:
        for value in values:
            node_templates = context.presentation.get('service_template',
                                                      'node_templates') or {}
            groups = context.presentation.get('service_template',
                                              'groups') or {}
            if (value not in node_templates) and (value not in groups):
                report_issue_for_unknown_type(context, presentation,
                                              'node template or group',
                                              field.name)
    def validator_fn(field, presentation, context):
        field.default_validate(presentation, context)

        # Make sure type exists
        value = getattr(presentation, field.name)
        if value is not None:
            copy = context.presentation.get_from_dict('service_template',
                                                      'topology_template',
                                                      templates_dict_name,
                                                      value)
            if copy is None:
                report_issue_for_unknown_type(context, presentation,
                                              template_type_name, field.name)
            else:
                if copy.copy is not None:
                    context.validation.report(
                        '"copy" field refers to a %s that itself is a copy in "%s": %s'
                        % (template_type_name, presentation._fullname,
                           safe_repr(value)),
                        locator=presentation._locator,
                        level=Issue.BETWEEN_TYPES)
def node_template_or_type_validator(field, presentation, context):
    """
    Makes sure that the field refers to either a node template or a node type.

    Used with the :func:`field_validator` decorator for the :code:`node` field in
    :class:`RequirementAssignment`.
    """

    field.default_validate(presentation, context)

    value = getattr(presentation, field.name)
    if value is not None:
        node_templates = \
            context.presentation.get('service_template', 'topology_template', 'node_templates') \
            or {}
        node_types = context.presentation.get('service_template',
                                              'node_types') or {}
        if (value not in node_templates) and (value not in node_types):
            report_issue_for_unknown_type(context, presentation,
                                          'node template or node type',
                                          field.name)