def update_repo_and_plugins(repo, repo_delta, importer_config, distributor_configs): """ Update a reposiory and its related collections. All details do not need to be specified; if a piece is omitted it's configuration is not touched, nor is it removed from the repository. The same holds true for the distributor_configs dict, not every distributor must be represented. This call will attempt to update the repository object, then the importer, then the distributors. If an exception occurs during any of these steps, the updates stop and the exception is immediately raised. Any updates that have already taken place are not rolled back. Distributor updates are asynchronous as there could be a very large number of consumers to update. Repository and importer updates are done synchronously. :param repo: repository object :type repo: pulp.server.db.model.Repository :param repo_delta: list of attributes to change and their new values; if None, no attempt to update the repository object will be made :type repo_delta: dict, None :param importer_config: new configuration to use for the repo's importer; if None, no attempt will be made to update the importer :type importer_config: dict, None :param distributor_configs: mapping of distributor ID to the new configuration to set for it :type distributor_configs: dict, None :return: Task result that contains the updated repository object and additional spawned tasks :rtype: pulp.server.async.tasks.TaskResult :raises pulp_exceptions.InvalidValue: if repo_delta is not a dictionary """ if repo_delta: if isinstance(repo_delta, dict): repo.update_from_delta(repo_delta) repo.save() else: raise pulp_exceptions.PulpCodedValidationException( error_code=error_codes.PLP1010, field='delta', field_type='dict', value=repo_delta) if importer_config is not None: importer_controller.update_importer_config(repo.repo_id, importer_config) additional_tasks = [] if distributor_configs is not None: for dist_id, dist_config in distributor_configs.items(): task_tags = [ tags.resource_tag(tags.RESOURCE_REPOSITORY_TYPE, repo.repo_id), tags.resource_tag(tags.RESOURCE_REPOSITORY_DISTRIBUTOR_TYPE, dist_id), tags.action_tag(tags.ACTION_UPDATE_DISTRIBUTOR) ] async_result = dist_controller.update.apply_async_with_reservation( tags.RESOURCE_REPOSITORY_TYPE, repo.repo_id, [repo.repo_id, dist_id, dist_config, None], tags=task_tags) additional_tasks.append(async_result) return TaskResult(repo, None, additional_tasks)
def test_unset(self, mock_model, mock_plugin_api, mock_validate_config): """ Test that keys with value None are removed from the config. """ mock_repo = mock_model.Repository.objects.get_repo_or_missing_resource.return_value mock_importer = mock_model.Importer.objects.get_or_404.return_value mock_importer.config = {'keep': 'keep', 'dont_keep': 'dont_keep'} mock_imp_inst = mock.MagicMock() mock_plugin_config = mock.MagicMock() mock_plugin_api.get_importer_by_id.return_value = (mock_imp_inst, mock_plugin_config) mock_ser = mock_model.Importer.SERIALIZER mock_validate_config.return_value = (True, 'message') result = importer.update_importer_config('mrepo', { 'test': 'change', 'dont_keep': None, 'unknown': None }) mock_validate_config.assert_called_once_with( mock_repo, mock_importer.importer_type_id, { 'test': 'change', 'keep': 'keep' }) self.assertDictEqual(mock_importer.config, { 'test': 'change', 'keep': 'keep' }) mock_importer.save.assert_called_once_with() mock_ser.assert_called_once_with(mock_importer) self.assertTrue(result is mock_ser.return_value.data)
def test_minimal(self, mock_model, mock_plugin_api, mock_validate_config): """ Update an importer config in the minimal way. """ mock_importer = mock_model.Importer.objects.get_or_404.return_value mock_imp_inst = mock.MagicMock() mock_plugin_config = mock.MagicMock() mock_plugin_api.get_importer_by_id.return_value = (mock_imp_inst, mock_plugin_config) mock_ser = mock_model.Importer.SERIALIZER mock_validate_config.return_value = (True, 'message') result = importer.update_importer_config('mrepo', {'test': 'config'}) self.assertEqual(mock_validate_config.call_count, 2) self.assertEqual(mock_importer.save.call_count, 2) self.assertTrue(result is mock_ser.return_value.data)
def test_minimal(self, mock_model, mock_plugin_api, mock_validate_config): """ Update an importer config in the minimal way. """ mock_importer = mock_model.Importer.objects.get_or_404.return_value mock_imp_inst = mock.MagicMock() mock_plugin_config = mock.MagicMock() mock_plugin_api.get_importer_by_id.return_value = (mock_imp_inst, mock_plugin_config) mock_ser = mock_model.Importer.SERIALIZER mock_validate_config.return_value = (True, 'message') result = importer.update_importer_config('mrepo', {'test': 'config'}) mock_importer.config.update.assert_called_once_with({'test': 'config'}) mock_importer.save.assert_called_once_with() mock_ser.assert_called_once_with(mock_importer) self.assertTrue(result is mock_ser.return_value.data)
def test_unset(self, mock_model, mock_plugin_api, mock_validate_config): """ Test that keys with value None are removed from the config. """ mock_importer = mock_model.Importer.objects.get_or_404.return_value mock_importer.config = {'keep': 'keep', 'dont_keep': 'dont_keep'} mock_imp_inst = mock.MagicMock() mock_plugin_config = mock.MagicMock() mock_plugin_api.get_importer_by_id.return_value = (mock_imp_inst, mock_plugin_config) mock_ser = mock_model.Importer.SERIALIZER mock_validate_config.return_value = (True, 'message') result = importer.update_importer_config('mrepo', {'test': 'change', 'dont_keep': None}) self.assertDictEqual(mock_importer.config, {'test': 'change', 'keep': 'keep'}) mock_importer.save.assert_called_once_with() mock_ser.assert_called_once_with(mock_importer) self.assertTrue(result is mock_ser.return_value.data)
def test_minimal(self, mock_model, mock_plugin_api, mock_validate_config): """ Update an importer config in the minimal way. """ mock_importer = mock_model.Importer.objects.get_or_404.return_value mock_imp_inst = mock.MagicMock() mock_plugin_config = mock.MagicMock() mock_plugin_api.get_importer_by_id.return_value = (mock_imp_inst, mock_plugin_config) mock_ser = mock_model.Importer.serializer mock_validate_config.return_value = (True, 'message') result = importer.update_importer_config('mrepo', {'test': 'config'}) mock_importer.config.update.assert_called_once_with({'test': 'config'}) mock_importer.save.assert_called_once_with() mock_ser.assert_called_once_with(mock_importer) self.assertTrue(result is mock_ser.return_value.data)
def test_minimal(self, mock_model, mock_plugin_api, mock_validate_config): """ Update an importer config in the minimal way. """ mock_repo = mock_model.Repository.objects.get_repo_or_missing_resource.return_value mock_importer = mock_model.Importer.objects.get_or_404.return_value mock_imp_inst = mock.MagicMock() mock_plugin_config = mock.MagicMock() mock_plugin_api.get_importer_by_id.return_value = (mock_imp_inst, mock_plugin_config) mock_ser = mock_model.Importer.SERIALIZER mock_validate_config.return_value = (True, "message") result = importer.update_importer_config("mrepo", {"test": "config"}) mock_validate_config.assert_called_once_with(mock_repo, mock_importer.importer_type_id, mock_importer.config) mock_importer.save.assert_called_once_with() mock_ser.assert_called_once_with(mock_importer) self.assertTrue(result is mock_ser.return_value.data)
def test_unset(self, mock_model, mock_plugin_api, mock_validate_config): """ Test that keys with value None are removed from the config. """ mock_repo = mock_model.Repository.objects.get_repo_or_missing_resource.return_value mock_importer = mock_model.Importer.objects.get_or_404.return_value mock_importer.config = {"keep": "keep", "dont_keep": "dont_keep"} mock_imp_inst = mock.MagicMock() mock_plugin_config = mock.MagicMock() mock_plugin_api.get_importer_by_id.return_value = (mock_imp_inst, mock_plugin_config) mock_ser = mock_model.Importer.SERIALIZER mock_validate_config.return_value = (True, "message") result = importer.update_importer_config("mrepo", {"test": "change", "dont_keep": None}) mock_validate_config.assert_called_once_with( mock_repo, mock_importer.importer_type_id, {"test": "change", "keep": "keep"} ) self.assertDictEqual(mock_importer.config, {"test": "change", "keep": "keep"}) mock_importer.save.assert_called_once_with() mock_ser.assert_called_once_with(mock_importer) self.assertTrue(result is mock_ser.return_value.data)