Skip to content

Commit

Permalink
Address multiple v2 API issues. Fixes pulp#1452
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sivel committed May 16, 2023
1 parent 0939670 commit 7cbd882
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGES/1452.bugfix
@@ -0,0 +1 @@
Fixed several bugs in the galaxy v2 API related to generating URLs for various collection resources.
2 changes: 1 addition & 1 deletion pulp_ansible/app/galaxy/serializers.py
Expand Up @@ -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,
Expand Down
25 changes: 16 additions & 9 deletions pulp_ansible/app/galaxy/views.py
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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.
"""
Expand All @@ -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)
1 change: 1 addition & 0 deletions pulp_ansible/app/urls.py
Expand Up @@ -33,6 +33,7 @@
path(
"collections/<str:namespace>/<str:name>/versions/<str:version>/",
GalaxyCollectionVersionDetail.as_view(),
name="v2-collection-versions-detail",
),
path(
"collection-imports/<uuid:pk>/",
Expand Down

0 comments on commit 7cbd882

Please sign in to comment.