Ejemplo n.º 1
0
    def validator(field, presentation, context):
        field.default_validate(presentation, context)

        value = getattr(presentation, field.name)
        if value is not None:
            # Test for circular definitions
            container_data_type = get_container_data_type(presentation)
            if (container_data_type is not None) and (container_data_type._name
                                                      == value):
                context.validation.report(
                    'type of property "%s" creates a circular value hierarchy: %s'
                    % (presentation._fullname, safe_repr(value)),
                    locator=presentation._get_child_locator('type'),
                    level=Issue.BETWEEN_TYPES)

            # Can be a complex data type
            if get_type_by_name(context, value, 'data_types') is not None:
                return True

            # Can be a primitive data type
            if get_primitive_data_type(value) is None:
                report_issue_for_unknown_type(context, presentation, type_name,
                                              field.name)

        return False
Ejemplo n.º 2
0
def policy_targets_validator(field, presentation, context):
    """
    Makes sure that the field's elements refer to either node templates or groups, and that
    they match the node types and group types declared in the policy type.

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

    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', 'topology_template',
                                         'node_templates') \
                or {}
            groups = context.presentation.get('service_template', 'topology_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, value)

            policy_type = presentation._get_type(context)
            if policy_type is None:
                break

            node_types, group_types = policy_type._get_targets(context)

            is_valid = False

            if value in node_templates:
                our_node_type = node_templates[value]._get_type(context)
                for node_type in node_types:
                    if node_type._is_descendant(context, our_node_type):
                        is_valid = True
                        break

            elif value in groups:
                our_group_type = groups[value]._get_type(context)
                for group_type in group_types:
                    if group_type._is_descendant(context, our_group_type):
                        is_valid = True
                        break

            if not is_valid:
                context.validation.report(
                    'policy definition target does not match either a node type or a group type'
                    ' declared in the policy type in "%s": %s' %
                    (presentation._name, safe_repr(value)),
                    locator=presentation._locator,
                    level=Issue.BETWEEN_TYPES)
Ejemplo n.º 3
0
def list_node_type_or_group_type_validator(field, presentation, context):
    """
    Makes sure that the field's elements refer to either node types or a group types.

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

    field.default_validate(presentation, context)

    values = getattr(presentation, field.name)
    if values is not None:
        for value in values:
            if (get_type_by_name(context, value, 'node_types') is None) and \
                    (get_type_by_name(context, value, 'group_types') is None):
                report_issue_for_unknown_type(context, presentation,
                                              'node type or group type',
                                              field.name, value)
Ejemplo n.º 4
0
def group_members_validator(field, presentation, context):
    """
    Makes sure that the field's elements refer to node templates  and that they match the node types
    declared in the group type.

    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:
        node_templates = \
            context.presentation.get('service_template', 'topology_template', 'node_templates') \
                or {}
        for value in values:
            if value not in node_templates:
                report_issue_for_unknown_type(context, presentation,
                                              'node template', field.name,
                                              value)

            group_type = presentation._get_type(context)
            if group_type is None:
                break

            node_types = group_type._get_members(context)

            is_valid = False

            if value in node_templates:
                our_node_type = node_templates[value]._get_type(context)
                for node_type in node_types:
                    if node_type._is_descendant(context, our_node_type):
                        is_valid = True
                        break

            if not is_valid:
                context.validation.report(
                    u'group definition target does not match a node type'
                    u' declared in the group type in "{0}": {1}'.format(
                        presentation._name, safe_repr(value)),
                    locator=presentation._locator,
                    level=Issue.BETWEEN_TYPES)
Ejemplo n.º 5
0
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 ``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 {}
        if (value not in node_templates) and \
            (get_type_by_name(context, value, 'node_types') is None):
            report_issue_for_unknown_type(context, presentation,
                                          'node template or node type',
                                          field.name)
Ejemplo n.º 6
0
    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 relationship_template_or_type_validator(field, presentation, context):
    """
    Makes sure that the field refers to either a relationship template or a relationship type.

    Used with the :func:`field_validator` decorator for the :code:`type` field in
    :class:`RelationshipAssignment`.
    """

    field.default_validate(presentation, context)

    value = getattr(presentation, field.name)
    if value is not None:
        relationship_templates = \
            context.presentation.get('service_template', 'topology_template',
                                     'relationship_templates') \
            or {}
        if (value not in relationship_templates) and \
            (get_type_by_full_or_shorthand_name(context, value, 'relationship_types') is None):
            report_issue_for_unknown_type(
                context, presentation,
                'relationship template or relationship type', field.name)
Ejemplo n.º 8
0
def list_node_type_or_group_type_validator(field, presentation, context):
    """
    Makes sure that the field's elements refer to either node types or a group types.

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

    field.default_validate(presentation, context)

    values = getattr(presentation, field.name)
    if values is not None:
        for value in values:
            node_types = context.presentation.get('service_template',
                                                  'node_types') or {}
            group_types = context.presentation.get('service_template',
                                                   'group_types') or {}
            if (value not in node_types) and (value not in group_types):
                report_issue_for_unknown_type(context, presentation,
                                              'node type or group type',
                                              field.name, value)