From 7cbd8820d7ee6237712aaa33ecd4af539fb952b8 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Wed, 10 May 2023 14:12:08 -0500 Subject: [PATCH] Address multiple v2 API issues. Fixes #1452 * Populate a context for use in v2 serializers * Add name to route to satisfy v2 get_latest_version href * Copy logic from UnpaginatedCollectionVersionSerializer.get_download_url to build a valid v2 download_url --- CHANGES/1452.bugfix | 1 + pulp_ansible/app/galaxy/serializers.py | 2 +- pulp_ansible/app/galaxy/views.py | 25 ++++++++++++++++--------- pulp_ansible/app/urls.py | 1 + 4 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 CHANGES/1452.bugfix diff --git a/CHANGES/1452.bugfix b/CHANGES/1452.bugfix new file mode 100644 index 000000000..83f9e7f0d --- /dev/null +++ b/CHANGES/1452.bugfix @@ -0,0 +1 @@ +Fixed several bugs in the galaxy v2 API related to generating URLs for various collection resources. diff --git a/pulp_ansible/app/galaxy/serializers.py b/pulp_ansible/app/galaxy/serializers.py index f07413c47..71818ee91 100644 --- a/pulp_ansible/app/galaxy/serializers.py +++ b/pulp_ansible/app/galaxy/serializers.py @@ -108,7 +108,7 @@ def get_latest_version(self, obj): """ rv = obj.versions.filter(is_highest=True).first() href = reverse( - "collection-versions-detail", + "v2-collection-versions-detail", kwargs={ "path": self.context["path"], "namespace": obj.namespace, diff --git a/pulp_ansible/app/galaxy/views.py b/pulp_ansible/app/galaxy/views.py index ef553c5e7..55a306e3d 100644 --- a/pulp_ansible/app/galaxy/views.py +++ b/pulp_ansible/app/galaxy/views.py @@ -4,6 +4,7 @@ from django.shortcuts import get_object_or_404, HttpResponse from drf_spectacular.utils import extend_schema, extend_schema_view from rest_framework import generics, pagination, response, views +from rest_framework.reverse import reverse from pulpcore.plugin.models import PulpTemporaryFile from pulpcore.plugin.viewsets import OperationPostponedResponse @@ -149,7 +150,8 @@ def get(self, request, path=None, namespace=None, name=None): """ # This seems wrong, no repository scoping occurring collection = get_object_or_404(Collection, namespace=namespace, name=name) - return response.Response(GalaxyCollectionSerializer(collection).data) + context = self.get_serializer_context() + return response.Response(GalaxyCollectionSerializer(collection, context=context).data) class GalaxyCollectionView(DistributionMixin, UploadGalaxyCollectionMixin, generics.ListAPIView): @@ -214,7 +216,7 @@ def get_queryset(self): return versions -class GalaxyCollectionVersionDetail(DistributionMixin, views.APIView): +class GalaxyCollectionVersionDetail(DistributionMixin, generics.GenericAPIView): """ APIView for Galaxy Collections Detail view. """ @@ -234,12 +236,17 @@ def get(self, request, path, namespace, name, version): ContentArtifact, content__in=self._distro_content, relative_path=version.relative_path ) - download_url = "{content_hostname}/{base_path}/{relative_path}".format( - content_hostname=settings.ANSIBLE_CONTENT_HOSTNAME, - base_path=self.kwargs["path"], - relative_path=version.relative_path, - ) - - data = GalaxyCollectionVersionSerializer(version).data + # Note: We're using ANSIBLE_API_HOSTNAME here instead of calling reverse with request= + # because using the request context to generate the full URL causes the download URL + # to be inaccessible when pulp is running behind a reverse proxy. + host = settings.ANSIBLE_API_HOSTNAME.strip("/") + path = reverse( + settings.ANSIBLE_URL_NAMESPACE + "collection-artifact-download", + kwargs={"distro_base_path": self.kwargs["path"], "filename": version.relative_path}, + ).strip("/") + download_url = f"{host}/{path}" + + context = self.get_serializer_context() + data = GalaxyCollectionVersionSerializer(version, context=context).data data["download_url"] = download_url return response.Response(data) diff --git a/pulp_ansible/app/urls.py b/pulp_ansible/app/urls.py index 2a4721872..ba569a7a4 100644 --- a/pulp_ansible/app/urls.py +++ b/pulp_ansible/app/urls.py @@ -33,6 +33,7 @@ path( "collections///versions//", GalaxyCollectionVersionDetail.as_view(), + name="v2-collection-versions-detail", ), path( "collection-imports//",