Exemple #1
0
    def validate(audit_template):
        available_goals = objects.Goal.list(AuditTemplatePostType._ctx)
        available_goal_uuids_map = {g.uuid: g for g in available_goals}
        available_goal_names_map = {g.name: g for g in available_goals}
        if audit_template.goal in available_goal_uuids_map:
            goal = available_goal_uuids_map[audit_template.goal]
        elif audit_template.goal in available_goal_names_map:
            goal = available_goal_names_map[audit_template.goal]
        else:
            raise exception.InvalidGoal(goal=audit_template.goal)

        common_utils.Draft4Validator(
            default.DefaultScope.DEFAULT_SCHEMA).validate(audit_template.scope)

        include_host_aggregates = False
        exclude_host_aggregates = False
        for rule in audit_template.scope:
            if 'host_aggregates' in rule:
                include_host_aggregates = True
            elif 'exclude' in rule:
                for resource in rule['exclude']:
                    if 'host_aggregates' in resource:
                        exclude_host_aggregates = True
        if include_host_aggregates and exclude_host_aggregates:
            raise exception.Invalid(
                message=_("host_aggregates can't be "
                          "included and excluded together"))

        if audit_template.strategy:
            available_strategies = objects.Strategy.list(
                AuditTemplatePostType._ctx)
            available_strategies_map = {
                s.uuid: s
                for s in available_strategies
            }
            if audit_template.strategy not in available_strategies_map:
                raise exception.InvalidStrategy(
                    strategy=audit_template.strategy)

            strategy = available_strategies_map[audit_template.strategy]
            # Check that the strategy we indicate is actually related to the
            # specified goal
            if strategy.goal_id != goal.id:
                choices = [
                    "'%s' (%s)" % (s.uuid, s.name)
                    for s in available_strategies
                ]
                raise exception.InvalidStrategy(
                    message=_("'%(strategy)s' strategy does relate to the "
                              "'%(goal)s' goal. Possible choices: %(choices)s")
                    % dict(strategy=strategy.name,
                           goal=goal.name,
                           choices=", ".join(choices)))
            audit_template.strategy = strategy.uuid

        # We force the UUID so that we do not need to query the DB with the
        # name afterwards
        audit_template.goal = goal.uuid

        return audit_template
Exemple #2
0
 def _validate_strategy(patch):
     patch.path = "/strategy_id"
     strategy = patch.value
     if strategy:
         available_strategies = objects.Strategy.list(
             AuditTemplatePatchType._ctx)
         available_strategy_uuids_map = {
             s.uuid: s for s in available_strategies}
         available_strategy_names_map = {
             s.name: s for s in available_strategies}
         if strategy in available_strategy_uuids_map:
             patch.value = available_strategy_uuids_map[strategy].id
         elif strategy in available_strategy_names_map:
             patch.value = available_strategy_names_map[strategy].id
         else:
             raise exception.InvalidStrategy(strategy=strategy)