def test_bind_with_agent_notification(self, mock_bind_manager): binding_config = {'binding': 'foo'} agent_options = {'bar': 'baz'} mock_bind_manager.consumer_agent_manager.return_value.bind.return_value = \ {'task_id': 'foo-request-id', 'other_task_detail': 'abc123'} result = consumer.bind('foo_consumer_id', 'foo_repo_id', 'foo_distributor_id', True, binding_config, agent_options) mock_bind_manager.consumer_agent_manager.return_value.bind.assert_called_once_with( 'foo_consumer_id', 'foo_repo_id', 'foo_distributor_id', agent_options ) self.assertTrue(isinstance(result, TaskResult)) self.assertEquals(result.return_value, mock_bind_manager.consumer_bind_manager.return_value.bind.return_value) self.assertEquals(result.spawned_tasks, [{'task_id': 'foo-request-id'}])
def update(repo_id, distributor_id, config, delta): """ Get the itinerary for updating a repository distributor. 1. Update the distributor on the server. 2. (re)bind any bound consumers. :param repo_id: A repository ID. :type repo_id: str :param distributor_id: A unique distributor id :type distributor_id: str :param config: A configuration dictionary for a distributor instance. The contents of this dict depends on the type of distributor. :type config: dict :param delta: A dictionary used to change other saved configuration values for a distributor instance. This currently only supports the 'auto_publish' keyword, which should have a value of type bool :type delta: dict or None :return: Any errors that may have occurred and the list of tasks spawned for each consumer :rtype: TaskResult """ manager = managers.repo_distributor_manager() # Retrieve configuration options from the delta auto_publish = None if delta is not None: auto_publish = delta.get('auto_publish') distributor = manager.update_distributor_config(repo_id, distributor_id, config, auto_publish) # Process each bound consumer bind_errors = [] additional_tasks = [] options = {} manager = managers.consumer_bind_manager() for bind in manager.find_by_distributor(repo_id, distributor_id): try: report = consumer.bind(bind['consumer_id'], bind['repo_id'], bind['distributor_id'], bind['notify_agent'], bind['binding_config'], options) if report.spawned_tasks: additional_tasks.extend(report.spawned_tasks) except Exception, e: bind_errors.append(e)
def test_bind_no_agent_notification(self, mock_bind_manager): binding_config = {'binding': 'foo'} agent_options = {'bar': 'baz'} result = consumer.bind('foo_consumer_id', 'foo_repo_id', 'foo_distributor_id', False, binding_config, agent_options) mock_bind_manager.consumer_bind_manager.return_value.bind.assert_called_once_with( 'foo_consumer_id', 'foo_repo_id', 'foo_distributor_id', False, binding_config) self.assertTrue(isinstance(result, TaskResult)) self.assertEquals(mock_bind_manager.consumer_bind_manager.return_value.bind.return_value, result.return_value) # Make sure we didn't process the agent self.assertEquals(result.spawned_tasks, []) self.assertFalse(mock_bind_manager.consumer_agent_manager.called)
def post(self, request, consumer_id): """ Create a bind association between the specified consumer by id included in the URL path and a repo-distributor specified in the POST body: {repo_id:<str>, distributor_id:<str>}. Designed to be idempotent so only MissingResource is expected to be raised by manager. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param consumer_id: consumer to bind. :type consumer_id: str :raises OperationPostponed: will dispatch a task if 'notify_agent' is set to True :raises InvalidValue: if binding_config is invalid :return: Response representing the binding(in case 'notify agent' is set to False) :rtype: django.http.HttpResponse """ # get other options and validate them body = request.body_as_json repo_id = body.get('repo_id') distributor_id = body.get('distributor_id') binding_config = body.get('binding_config', {}) options = body.get('options', {}) notify_agent = body.get('notify_agent', True) if not isinstance(binding_config, dict): raise InvalidValue(['binding_config']) call_report = consumer_controller.bind(consumer_id, repo_id, distributor_id, notify_agent, binding_config, options) if call_report.spawned_tasks: raise OperationPostponed(call_report) else: return generate_json_response_with_pulp_encoder( call_report.serialize())
def post(self, request, consumer_id): """ Create a bind association between the specified consumer by id included in the URL path and a repo-distributor specified in the POST body: {repo_id:<str>, distributor_id:<str>}. Designed to be idempotent so only MissingResource is expected to be raised by manager. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param consumer_id: consumer to bind. :type consumer_id: str :raises OperationPostponed: will dispatch a task if 'notify_agent' is set to True :raises InvalidValue: if binding_config is invalid :return: Response representing the binding(in case 'notify agent' is set to False) :rtype: django.http.HttpResponse """ # get other options and validate them body = request.body_as_json repo_id = body.get('repo_id') distributor_id = body.get('distributor_id') binding_config = body.get('binding_config', {}) options = body.get('options', {}) notify_agent = body.get('notify_agent', True) if not isinstance(binding_config, dict): raise InvalidValue(['binding_config']) call_report = consumer_controller.bind( consumer_id, repo_id, distributor_id, notify_agent, binding_config, options) if call_report.spawned_tasks: raise OperationPostponed(call_report) else: return generate_json_response_with_pulp_encoder(call_report.serialize())