def _process_repos(repo_objs, details, importers, distributors): """ Serialize repository objects and add related importers and distributors if requested. Apply standard processing to a collection of repositories being returned to a client. Adds the object link and optionally adds related importers and distributors. :param repo_objs: collection of repository objects :type repo_objs: list or tuple of pulp.server.db.model.Repository objects :param details: if True, include importers and distributors, overrides other values :type details: bool :param importers: if True, adds related importers under the attribute "importers". :type importers: bool :param distributors: if True, adds related distributors under the attribute "distributors" :type distributors: bool :return: a list of serialized repositories with importer and distributor data optionally added :rtype: list of dicts """ repos = serializers.Repository(repo_objs, multiple=True).data if importers or details: _merge_related_objects('importers', model.Importer, repos) if distributors or details: _merge_related_objects('distributors', model.Distributor, repos) return repos
def put(self, request, repo_id): """ Update a repository. This call will return synchronously unless a distributor is updated. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param repo_id: id of repository to be updated :type repo_id: str :return: Response containing a serialized dict for the updated repo. :rtype : django.http.HttpResponse :raises exceptions.OperationPostponed: if a task has been dispatched to update a distributor """ delta = request.body_as_json.get('delta', None) importer_config = request.body_as_json.get('importer_config', None) distributor_configs = request.body_as_json.get('distributor_configs', None) repo = model.Repository.objects.get_repo_or_missing_resource(repo_id) task_result = repo_controller.update_repo_and_plugins(repo, delta, importer_config, distributor_configs) # Tasks are spawned if a distributor is updated, raise that as a result if task_result.spawned_tasks: raise exceptions.OperationPostponed(task_result) call_report = task_result.serialize() call_report['result'] = serializers.Repository(call_report['result']).data return generate_json_response_with_pulp_encoder(call_report)
def get(self, request, repo_id): """ Looks for query parameters 'importers' and 'distributors', and will add the corresponding fields to the repository returned. Query parameter 'details' is equivalent to passing both 'importers' and 'distributors'. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param repo_id: id of requested repository :type repo_id: str :return: Response containing a serialized dict for the requested repo. :rtype : django.http.HttpResponse :raises exceptions.MissingResource: if repo cannot be found """ repo_obj = model.Repository.objects.get_repo_or_missing_resource(repo_id) repo = serializers.Repository(repo_obj).data # Add importers and distributors to the dicts if requested. details = request.GET.get('details', 'false').lower() == 'true' if request.GET.get('importers', 'false').lower() == 'true' or details: _merge_related_objects('importers', model.Importer, (repo,)) if request.GET.get('distributors', 'false').lower() == 'true' or details: _merge_related_objects('distributors', model.Distributor, (repo,)) if details: repo['total_repository_units'] = sum(repo['content_unit_counts'].itervalues()) total_missing = repo_controller.missing_unit_count(repo_obj.repo_id) repo['locally_stored_units'] = repo['total_repository_units'] - total_missing return generate_json_response_with_pulp_encoder(repo)
def post(self, request): """ Create a new repo. `id` field in body is required. `display_name` will default to `id`. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :return: Response containing a serialized dict for the created repo. :rtype : django.http.HttpResponse """ repo_data = request.body_as_json repo_id = repo_data.get('id') repo_obj = repo_controller.create_repo( repo_id, display_name=repo_data.get('display_name', repo_id), description=repo_data.get('description'), notes=repo_data.get('notes'), importer_type_id=repo_data.get('importer_type_id'), importer_repo_plugin_config=repo_data.get('importer_config'), distributor_list=repo_data.get('distributors') ) repo = serializers.Repository(repo_obj).data response = generate_json_response_with_pulp_encoder(repo) return generate_redirect_response(response, repo['_href'])
def test_get_href(self, mock_rev): """ Test the generation of a href for a repository. """ repo = mock.MagicMock() test_serializer = serializers.Repository() result = test_serializer.get_href(repo) self.assertEquals(result, mock_rev.return_value) mock_rev.assert_called_once_with('repo_resource', kwargs={'repo_id': repo.repo_id})
def find_with_importer_type(importer_type_id): """ This originally lived in the RepoQueryManager. This code is now used in a pulp_rpm migration, which is done after the `id` to `repo_id` migration. """ results = [] repo_importers = list(RepoImporter.get_collection().find( {'importer_type_id': importer_type_id})) for ri in repo_importers: repo_obj = model.Repository.objects.get(repo_id=ri['repo_id']) repo = serializers.Repository(repo_obj).data repo['importers'] = [ri] results.append(repo) return results
def test_to_representation(self): """ This is not really necessary, but this test ensures that the ModelSerializer works as expected with a repository object. Though this is not a true unit test, I felt that it was crucial enough to warrant a little extra testing here. Specifically, make sure that repo_id in the database is converted to id for backwards compatability and _id refers to what used to be stored in id. """ repo = mock.MagicMock() repo._fields = ['best', 'still_good', 'id', 'repo_id'] repo.best = 'joule' repo.id = 'test_id' repo.repo_id = 'test_repo_id' repo.still_good = 'morning_times' test_serializer = serializers.Repository() result = test_serializer.to_representation(repo) self.assertDictEqual(result, {'best': 'joule', 'still_good': 'morning_times', '_id': 'test_id', 'id': 'test_repo_id'})