class TestApplierAPI(base.TestCase): def setUp(self): super(TestApplierAPI, self).setUp() api = rpcapi.ApplierAPI() def test_get_version(self): expected_version = self.api.API_VERSION self.assertEqual(expected_version, self.api.get_version()) 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.to_dict(), 'check_api_version', api_version=rpcapi.ApplierAPI().API_VERSION) def test_execute_audit_without_error(self): with mock.patch.object(om.RPCClient, 'call') as mock_call: action_plan_uuid = utils.generate_uuid() self.api.launch_action_plan(self.context, action_plan_uuid) mock_call.assert_called_once_with( self.context.to_dict(), 'launch_action_plan', action_plan_uuid=action_plan_uuid) def test_execute_action_plan_throw_exception(self): action_plan_uuid = "uuid" self.assertRaises(exception.InvalidUuidOrName, self.api.launch_action_plan, action_plan_uuid)
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.to_dict(), 'check_api_version', api_version=rpcapi.ApplierAPI().API_VERSION)
def post_execute(self, audit, solution, request_context): action_plan = self.do_schedule(request_context, audit, solution) a_plan_filters = {'state': objects.action_plan.State.ONGOING} ongoing_action_plans = objects.ActionPlan.list(request_context, filters=a_plan_filters) if ongoing_action_plans: action_plan.state = objects.action_plan.State.SUPERSEDED action_plan.save() raise exception.ActionPlanIsOngoing( action_plan=ongoing_action_plans[0].uuid, new_action_plan=action_plan.uuid) elif audit.auto_trigger: applier_client = rpcapi.ApplierAPI() applier_client.launch_action_plan(request_context, action_plan.uuid)
def __init__(self): super(ActionPlansController, self).__init__() self.applier_client = rpcapi.ApplierAPI()
def patch(self, action_plan_uuid, patch): """Update an existing action plan. :param action_plan_uuid: UUID of a action plan. :param patch: a json PATCH document to apply to this action plan. """ launch_action_plan = True if self.from_actionsPlans: raise exception.OperationNotPermitted action_plan_to_update = objects.ActionPlan.get_by_uuid( pecan.request.context, action_plan_uuid) try: action_plan_dict = action_plan_to_update.as_dict() action_plan = ActionPlan( **api_utils.apply_jsonpatch(action_plan_dict, patch)) except api_utils.JSONPATCH_EXCEPTIONS as e: raise exception.PatchError(patch=patch, reason=e) launch_action_plan = False # transitions that are allowed via PATCH allowed_patch_transitions = [ (ap_objects.State.RECOMMENDED, ap_objects.State.PENDING), (ap_objects.State.RECOMMENDED, ap_objects.State.CANCELLED), (ap_objects.State.ONGOING, ap_objects.State.CANCELLED), (ap_objects.State.PENDING, ap_objects.State.CANCELLED), ] # todo: improve this in blueprint watcher-api-validation if hasattr(action_plan, 'state'): transition = (action_plan_to_update.state, action_plan.state) if transition not in allowed_patch_transitions: error_message = _("State transition not allowed: " "(%(initial_state)s -> %(new_state)s)") raise exception.PatchError( patch=patch, reason=error_message % dict(initial_state=action_plan_to_update.state, new_state=action_plan.state)) if action_plan.state == ap_objects.State.PENDING: launch_action_plan = True # Update only the fields that have changed for field in objects.ActionPlan.fields: try: patch_val = getattr(action_plan, field) except AttributeError: # Ignore fields that aren't exposed in the API continue if patch_val == wtypes.Unset: patch_val = None if action_plan_to_update[field] != patch_val: action_plan_to_update[field] = patch_val if (field == 'state' and patch_val == objects.action_plan.State.PENDING): launch_action_plan = True action_plan_to_update.save() if launch_action_plan: applier_client = rpcapi.ApplierAPI() applier_client.launch_action_plan(pecan.request.context, action_plan.uuid) action_plan_to_update = objects.ActionPlan.get_by_uuid( pecan.request.context, action_plan_uuid) return ActionPlan.convert_with_links(action_plan_to_update)
def post_execute(self, audit, solution, request_context): action_plan = self.do_schedule(request_context, audit, solution) if audit.auto_trigger: applier_client = rpcapi.ApplierAPI() applier_client.launch_action_plan(request_context, action_plan.uuid)
def __init__(self): super(AuditHandler, self).__init__() self._strategy_context = default_context.DefaultStrategyContext() self._planner_manager = planner_manager.PlannerManager() self._planner = None self.applier_client = rpcapi.ApplierAPI()
def __init__(self): super(AuditHandler, self).__init__() self._strategy_context = default_context.DefaultStrategyContext() self._planner_loader = loader.DefaultPlannerLoader() self.applier_client = rpcapi.ApplierAPI()