Esempio n. 1
0
    def post(self, audit_p):
        """Create a new audit.

        :param audit_p: an audit within the request body.
        """
        context = pecan.request.context
        policy.enforce(context, 'audit:create', action='audit:create')
        audit = audit_p.as_audit(context)

        if self.from_audits:
            raise exception.OperationNotPermitted

        if not audit._goal_uuid:
            raise exception.Invalid(
                message=_('A valid goal_id or audit_template_id '
                          'must be provided'))

        strategy_uuid = audit.strategy_uuid
        no_schema = True
        if strategy_uuid is not None:
            # validate parameter when predefined strategy in audit template
            strategy = objects.Strategy.get(pecan.request.context,
                                            strategy_uuid)
            schema = strategy.parameters_spec
            if schema:
                # validate input parameter with default value feedback
                no_schema = False
                utils.StrictDefaultValidatingDraft4Validator(schema).validate(
                    audit.parameters)

        if no_schema and audit.parameters:
            raise exception.Invalid(
                _('Specify parameters but no predefined '
                  'strategy for audit, or no '
                  'parameter spec in predefined strategy'))

        audit_dict = audit.as_dict()
        # convert local time to UTC time
        start_time_value = audit_dict.get('start_time')
        end_time_value = audit_dict.get('end_time')
        if start_time_value:
            audit_dict['start_time'] = start_time_value.replace(
                tzinfo=tz.tzlocal()).astimezone(
                    tz.tzutc()).replace(tzinfo=None)
        if end_time_value:
            audit_dict['end_time'] = end_time_value.replace(
                tzinfo=tz.tzlocal()).astimezone(
                    tz.tzutc()).replace(tzinfo=None)

        new_audit = objects.Audit(context, **audit_dict)
        new_audit.create()

        # Set the HTTP Location Header
        pecan.response.location = link.build_url('audits', new_audit.uuid)

        # trigger decision-engine to run the audit
        if new_audit.audit_type == objects.audit.AuditType.ONESHOT.value:
            self.dc_client.trigger_audit(context, new_audit.uuid)

        return Audit.convert_with_links(new_audit)
Esempio n. 2
0
    def do_execute_strategy(self, audit, request_context):
        osc = clients.OpenStackClients()
        # todo(jed) retrieve in audit parameters (threshold,...)
        # todo(jed) create ActionPlan

        goal = objects.Goal.get_by_id(request_context, audit.goal_id)

        # NOTE(jed56) In the audit object, the 'strategy_id' attribute
        # is optional. If the admin wants to force the trigger of a Strategy
        # it could specify the Strategy uuid in the Audit.
        strategy_name = None
        if audit.strategy_id:
            strategy = objects.Strategy.get_by_id(request_context,
                                                  audit.strategy_id)
            strategy_name = strategy.name

        strategy_selector = default.DefaultStrategySelector(
            goal_name=goal.name, strategy_name=strategy_name, osc=osc)

        selected_strategy = strategy_selector.select()

        selected_strategy.audit_scope = audit.scope

        schema = selected_strategy.get_schema()
        if not audit.parameters and schema:
            # Default value feedback if no predefined strategy
            utils.StrictDefaultValidatingDraft4Validator(schema).validate(
                audit.parameters)

        selected_strategy.input_parameters.update(
            {name: value
             for name, value in audit.parameters.items()})

        return selected_strategy.execute()
Esempio n. 3
0
    def do_execute_strategy(self, audit, request_context):
        selected_strategy = self.select_strategy(audit, request_context)
        selected_strategy.audit_scope = audit.scope

        schema = selected_strategy.get_schema()
        if not audit.parameters and schema:
            # Default value feedback if no predefined strategy
            utils.StrictDefaultValidatingDraft4Validator(schema).validate(
                audit.parameters)

        selected_strategy.input_parameters.update(
            {name: value
             for name, value in audit.parameters.items()})

        return selected_strategy.execute(audit=audit)