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

        :param audit: a audit within the request body.
        """
        if self.from_audits:
            raise exception.OperationNotPermitted

        if not audit._audit_template_uuid:
            raise exception.Invalid(
                message=_('The audit template UUID or name specified is '
                          'invalid'))

        audit_dict = audit.as_dict()
        context = pecan.request.context
        new_audit = objects.Audit(context, **audit_dict)
        new_audit.create(context)

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

        # trigger decision-engine to run the audit

        dc_client = rpcapi.DecisionEngineAPI()
        dc_client.trigger_audit(context, new_audit.uuid)

        return Audit.convert_with_links(new_audit)
Exemplo n.º 2
0
class TestDecisionEngineAPI(base.TestCase):

    api = rpcapi.DecisionEngineAPI()

    def test_get_api_version(self):
        with mock.patch.object(om.RPCClient, 'call') as mock_call:
            expected_context = self.context
            self.api.check_api_version(expected_context)
            mock_call.assert_called_once_with(
                expected_context,
                'check_api_version',
                api_version=rpcapi.DecisionEngineAPI().api_version)

    def test_execute_audit_throw_exception(self):
        audit_uuid = "uuid"
        self.assertRaises(exception.InvalidUuidOrName, self.api.trigger_audit,
                          audit_uuid)

    def test_execute_audit_without_error(self):
        with mock.patch.object(om.RPCClient, 'cast') as mock_cast:
            audit_uuid = utils.generate_uuid()
            self.api.trigger_audit(self.context, audit_uuid)
            mock_cast.assert_called_once_with(self.context,
                                              'trigger_audit',
                                              audit_uuid=audit_uuid)
Exemplo n.º 3
0
    def get_all(self, data_model_type='compute', audit_uuid=None):
        """Retrieve information about the given data model.

        :param data_model_type: The type of data model user wants to list.
                                Supported values: compute.
                                Future support values: storage, baremetal.
                                The default value is compute.
        :param audit_uuid: The UUID of the audit,  used to filter data model
                           by the scope in audit.
        """
        if not utils.allow_list_datamodel():
            raise exception.NotAcceptable
        if self.from_data_model:
            raise exception.OperationNotPermitted
        allowed_data_model_type = [
            'compute',
            ]
        if data_model_type not in allowed_data_model_type:
            raise exception.DataModelTypeNotFound(
                data_model_type=data_model_type)
        context = pecan.request.context
        de_client = rpcapi.DecisionEngineAPI()
        policy.enforce(context, 'data_model:get_all',
                       action='data_model:get_all')
        rpc_all_data_model = de_client.get_data_model_info(
            context,
            data_model_type,
            audit_uuid)
        return rpc_all_data_model
Exemplo n.º 4
0
 def test_get_api_version(self):
     with mock.patch.object(om.RPCClient, 'call') as mock_call:
         expected_context = self.context
         self.api.check_api_version(expected_context)
         mock_call.assert_called_once_with(
             expected_context, 'check_api_version',
             api_version=rpcapi.DecisionEngineAPI().api_version)
Exemplo n.º 5
0
    def post(self, audit_p):
        """Create a new audit.

        :param audit_p: a 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 template, or no '
                  'parameter spec in predefined strategy'))

        audit_dict = audit.as_dict()

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

        # 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:
            dc_client = rpcapi.DecisionEngineAPI()
            dc_client.trigger_audit(context, new_audit.uuid)

        return Audit.convert_with_links(new_audit)
Exemplo n.º 6
0
    def state(self, strategy):
        """Retrieve an information about strategy requirements.

        :param strategy: name of the strategy.
        """
        context = pecan.request.context
        policy.enforce(context, 'strategy:state', action='strategy:state')
        parents = pecan.request.path.split('/')[:-1]
        if parents[-2] != "strategies":
            raise exception.HTTPNotFound
        rpc_strategy = api_utils.get_resource('Strategy', strategy)
        de_client = rpcapi.DecisionEngineAPI()
        strategy_state = de_client.get_strategy_info(context,
                                                     rpc_strategy.name)
        strategy_state.extend([{
            'type': 'Name', 'state': rpc_strategy.name,
            'mandatory': '', 'comment': ''}])
        return strategy_state
Exemplo n.º 7
0
 def __init__(self):
     super(WebhookController, self).__init__()
     self.dc_client = rpcapi.DecisionEngineAPI()