Skip to content

Commit

Permalink
Adds upload support from mazer
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Bouterse committed May 2, 2019
1 parent 51bf29c commit 85a86a4
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 5 deletions.
20 changes: 19 additions & 1 deletion pulp_ansible/app/galaxy/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from gettext import gettext as _

from django.conf import settings
from rest_framework import serializers

from pulp_ansible.app.models import Role
from pulp_ansible.app.models import Collection, Role


class GalaxyRoleSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -51,3 +53,19 @@ def get_source(self, obj):
class Meta:
fields = ('name', 'source')
model = Role


class GalaxyCollectionUploadSerializer(serializers.Serializer):
"""
A serializer for Collection Uploads
"""
sha256 = serializers.CharField(
help_text=_('The sha256 checksum of the Collection Artifact.'),
required=True,
max_length=64,
min_length=64,
)
file = serializers.FileField(
help_text=_('The file containing the Artifact binary data.'),
required=True,
)
41 changes: 41 additions & 0 deletions pulp_ansible/app/galaxy/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from gettext import gettext as _
import json
import logging
import tarfile

from django.db import transaction

from pulpcore.plugin.models import Artifact, ContentArtifact, CreatedResource
from pulp_ansible.app.models import Collection


log = logging.getLogger(__name__)


def import_collection(artifact_pk):
"""
Create a collection from an uploaded artifact
Args:
artifact_pk (str): The pk or the Artifact to create the Collection from.
"""
artifact = Artifact.objects.get(pk=artifact_pk)
with tarfile.open(str(artifact.file.path), "r") as tar:
log.info(_('Reading MANIFEST.json from {path}').format(path=artifact.file.path))
file_obj = tar.extractfile('MANIFEST.json')
manifest_data = json.load(file_obj)
collection_info = manifest_data['collection_info']

collection = Collection(
namespace=collection_info['namespace'],
name=collection_info['name'],
version=collection_info['version']
)
with transaction.atomic():
collection.save()
ContentArtifact.objects.create(
artifact=artifact,
content=collection,
relative_path=collection.relative_path,
)
CreatedResource.objects.create(content_object=collection)
41 changes: 38 additions & 3 deletions pulp_ansible/app/galaxy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
from django.shortcuts import get_object_or_404
from rest_framework import generics, response, views

from pulpcore.app.models import Distribution
from pulpcore.app.models import Artifact, Distribution
from pulpcore.app.response import OperationPostponedResponse
from pulpcore.tasking.tasks import enqueue_with_reservation

from pulp_ansible.app.models import Role
from pulp_ansible.app.galaxy.tasks import import_collection
from pulp_ansible.app.models import Collection, Role

from .serializers import GalaxyRoleSerializer, GalaxyRoleVersionSerializer
from .serializers import (
GalaxyCollectionUploadSerializer, GalaxyRoleSerializer,
GalaxyRoleVersionSerializer,
)


class GalaxyVersionView(views.APIView):
Expand Down Expand Up @@ -79,3 +85,32 @@ def get_queryset(self):
for version in versions:
version.distro_path = distro.base_path
return versions


class GalaxyCollectionView(views.APIView):
"""
ViewSet for Collection models.
"""

authentication_classes = []
permission_classes = []

def post(self, request, *args, **kwargs):
"""
Queues a task that publishes a new Ansible Publication.
"""
serializer = GalaxyCollectionUploadSerializer(data=request.data,
context={'request': request})
serializer.is_valid(raise_exception=True)

expected_digests = {'sha256': serializer.validated_data['sha256']}
artifact = Artifact.init_and_validate(serializer.validated_data['file'],
expected_digests=expected_digests)
artifact.save()

async_result = enqueue_with_reservation(
import_collection, [str(artifact.pk)],
kwargs={
'artifact_pk': artifact.pk,
})
return OperationPostponedResponse(async_result, request)
5 changes: 4 additions & 1 deletion pulp_ansible/app/urls.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from django.conf.urls import url

from pulp_ansible.app.galaxy.views import (
GalaxyCollectionView,
GalaxyVersionView,
RoleList,
RoleVersionList
)


urlpatterns = [
url(r'pulp_ansible/galaxy/(?P<path>.+)/api/$', GalaxyVersionView.as_view()),
url(r'pulp_ansible/galaxy/(?P<path>.+)/api/v1/roles/$', RoleList.as_view()),
url(r'pulp_ansible/galaxy/(?P<path>.+)/api/v1/roles/(?P<role_pk>.+)/versions/$',
RoleVersionList.as_view())
RoleVersionList.as_view()),
url(r'pulp_ansible/galaxy/(?P<path>.+)/api/v2/collections/$', GalaxyCollectionView.as_view()),
]

0 comments on commit 85a86a4

Please sign in to comment.