Skip to content

Commit

Permalink
Move the publish endpoint to publications
Browse files Browse the repository at this point in the history
  • Loading branch information
David Davis committed Apr 29, 2019
1 parent 4bfaf75 commit 5c08855
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 34 deletions.
6 changes: 3 additions & 3 deletions README.rst
Expand Up @@ -186,10 +186,10 @@ Create a ``file`` Publisher
``$ export PUBLISHER_HREF=$(http :24817/pulp/api/v3/publishers/file/file/ | jq -r '.results[] | select(.name == "bar") | ._href')``


Use the ``bar`` Publisher to create a Publication
-------------------------------------------------
Create a Publication
--------------------

``$ http POST ':24817'$PUBLISHER_HREF'publish/' repository=$REPO_HREF``
``$ http POST http://localhost:24817/pulp/api/v3/publications/file/file/ repository=$REPO_HREF publisher=$PUBLISHER_HREF``

.. code:: json
Expand Down
9 changes: 8 additions & 1 deletion pulp_file/app/serializers.py
Expand Up @@ -3,6 +3,7 @@
from rest_framework import serializers

from pulpcore.plugin.serializers import (
DetailRelatedField,
PublicationSerializer,
PublisherSerializer,
RemoteSerializer,
Expand Down Expand Up @@ -79,6 +80,12 @@ class FilePublicationSerializer(PublicationSerializer):
Serializer for File Publications.
"""

publisher = DetailRelatedField(
help_text=_('The publisher that created this publication.'),
queryset=FilePublisher.objects.all(),
required=False,
)

class Meta:
fields = PublicationSerializer.Meta.fields
fields = PublicationSerializer.Meta.fields + ('publisher',)
model = FilePublication
14 changes: 11 additions & 3 deletions pulp_file/app/tasks/publishing.py
Expand Up @@ -27,18 +27,26 @@ def publish(publisher_pk, repository_version_pk):
repository_version_pk (str): Create a publication from this repository version.
"""
publisher = FilePublisher.objects.get(pk=publisher_pk)
if publisher_pk:
publisher = FilePublisher.objects.get(pk=publisher_pk)
manifest = publisher.manifest
publisher_name = publisher.name
else:
publisher = None
manifest = 'PULP_MANIFEST'
publisher_name = ''

repo_version = RepositoryVersion.objects.get(pk=repository_version_pk)

log.info(_('Publishing: repository={repo}, version={ver}, publisher={pub}').format(
repo=repo_version.repository.name,
ver=repo_version.number,
pub=publisher.name,
pub=publisher_name,
))

with WorkingDirectory():
with FilePublication.create(repo_version, publisher, pass_through=True) as publication:
manifest = Manifest(publisher.manifest)
manifest = Manifest(manifest)
manifest.write(populate(publication))
metadata = PublishedMetadata(
relative_path=os.path.basename(manifest.relative_path),
Expand Down
38 changes: 20 additions & 18 deletions pulp_file/app/viewsets.py
Expand Up @@ -5,7 +5,6 @@

from pulpcore.plugin.serializers import (
AsyncOperationResponseSerializer,
RepositoryPublishURLSerializer,
RepositorySyncURLSerializer,
)
from pulpcore.plugin.tasking import enqueue_with_reservation
Expand Down Expand Up @@ -98,40 +97,43 @@ class FilePublisherViewSet(PublisherViewSet):
queryset = FilePublisher.objects.all()
serializer_class = FilePublisherSerializer


class FilePublicationViewSet(PublicationViewSet):
"""
ViewSet for File Publications.
"""

endpoint_name = 'file'
queryset = FilePublication.objects.all()
serializer_class = FilePublicationSerializer

@swagger_auto_schema(
operation_description="Trigger an asynchronous task to publish file content.",
responses={202: AsyncOperationResponseSerializer}
)
@detail_route(methods=('post',), serializer_class=RepositoryPublishURLSerializer)
def publish(self, request, pk):
def create(self, request):
"""
Publishes a repository.
Either the ``repository`` or the ``repository_version`` fields can
be provided but not both at the same time.
"""
publisher = self.get_object()
serializer = RepositoryPublishURLSerializer(data=request.data,
context={'request': request})
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
repository_version = serializer.validated_data.get('repository_version')
publisher = serializer.validated_data.get('publisher')

if publisher:
publisher_pk = str(publisher.pk)
else:
publisher_pk = ''

result = enqueue_with_reservation(
tasks.publish,
[repository_version.repository, publisher],
[repository_version.repository, publisher_pk],
kwargs={
'publisher_pk': str(publisher.pk),
'publisher_pk': publisher_pk,
'repository_version_pk': str(repository_version.pk)
}
)
return OperationPostponedResponse(result, request)


class FilePublicationViewSet(PublicationViewSet):
"""
ViewSet for File Publications.
"""

endpoint_name = 'file'
queryset = FilePublication.objects.all()
serializer_class = FilePublicationSerializer
4 changes: 2 additions & 2 deletions pulp_file/tests/functional/api/test_download_content.py
Expand Up @@ -15,7 +15,6 @@
download_content_unit,
gen_distribution,
gen_repo,
publish,
sync,
)

Expand All @@ -25,6 +24,7 @@
FILE_REMOTE_PATH,
)
from pulp_file.tests.functional.utils import (
create_file_publication,
gen_file_publisher,
gen_file_remote,
get_file_content_paths,
Expand Down Expand Up @@ -99,7 +99,7 @@ def do_test(self, policy):
self.addCleanup(client.delete, publisher['_href'])

# Create a publication.
publication = publish(cfg, publisher, repo)
publication = create_file_publication(cfg, repo, publisher=publisher)
self.addCleanup(client.delete, publication['_href'])

# Create a distribution.
Expand Down
4 changes: 2 additions & 2 deletions pulp_file/tests/functional/api/test_download_policies.py
Expand Up @@ -14,7 +14,6 @@
gen_repo,
get_added_content_summary,
get_content_summary,
publish,
sync,
)

Expand All @@ -26,6 +25,7 @@
FILE_REMOTE_PATH,
)
from pulp_file.tests.functional.utils import (
create_file_publication,
gen_file_publisher,
gen_file_remote,
)
Expand Down Expand Up @@ -126,7 +126,7 @@ def do_publish(self, download_policy):
publisher = self.client.post(FILE_PUBLISHER_PATH, gen_file_publisher())
self.addCleanup(self.client.delete, publisher['_href'])

publication = publish(self.cfg, publisher, repo)
publication = create_file_publication(self.cfg, repo, publisher=publisher)
self.assertIsNotNone(publication['repository_version'], publication)


Expand Down
10 changes: 5 additions & 5 deletions pulp_file/tests/functional/api/test_publish.py
Expand Up @@ -2,7 +2,6 @@
"""Tests that publish file plugin repositories."""
import unittest
from random import choice
from urllib.parse import urljoin

from requests.exceptions import HTTPError

Expand All @@ -12,16 +11,17 @@
gen_repo,
get_content,
get_versions,
publish,
sync,
)

from pulp_file.tests.functional.constants import (
FILE_CONTENT_NAME,
FILE_PUBLICATION_PATH,
FILE_PUBLISHER_PATH,
FILE_REMOTE_PATH,
)
from pulp_file.tests.functional.utils import (
create_file_publication,
gen_file_publisher,
gen_file_remote,
)
Expand Down Expand Up @@ -76,13 +76,13 @@ def test_all(self):
non_latest = choice(version_hrefs[:-1])

# Step 2
publication = publish(cfg, publisher, repo)
publication = create_file_publication(cfg, repo, publisher=publisher)

# Step 3
self.assertEqual(publication['repository_version'], version_hrefs[-1])

# Step 4
publication = publish(cfg, publisher, repo, non_latest)
publication = create_file_publication(cfg, repo, non_latest, publisher)

# Step 5
self.assertEqual(publication['repository_version'], non_latest)
Expand All @@ -93,4 +93,4 @@ def test_all(self):
'repository': repo['_href'],
'repository_version': non_latest,
}
client.post(urljoin(publisher['_href'], 'publish/'), body)
client.post(FILE_PUBLICATION_PATH, body)
3 changes: 3 additions & 0 deletions pulp_file/tests/functional/constants.py
Expand Up @@ -5,6 +5,7 @@
from pulp_smash.constants import PULP_FIXTURES_BASE_URL
from pulp_smash.pulp3.constants import (
API_DOCS_PATH,
BASE_PUBLICATION_PATH,
BASE_PUBLISHER_PATH,
BASE_REMOTE_PATH,
CONTENT_PATH,
Expand All @@ -18,6 +19,8 @@

FILE_REMOTE_PATH = urljoin(BASE_REMOTE_PATH, 'file/file/')

FILE_PUBLICATION_PATH = urljoin(BASE_PUBLICATION_PATH, 'file/file/')

FILE_PUBLISHER_PATH = urljoin(BASE_PUBLISHER_PATH, 'file/file/')

FILE_FIXTURE_URL = urljoin(PULP_FIXTURES_BASE_URL, 'file/')
Expand Down
26 changes: 26 additions & 0 deletions pulp_file/tests/functional/utils.py
Expand Up @@ -19,6 +19,7 @@
FILE_CONTENT_NAME,
FILE_CONTENT_PATH,
FILE_FIXTURE_MANIFEST_URL,
FILE_PUBLICATION_PATH,
FILE_REMOTE_PATH,
)

Expand Down Expand Up @@ -91,6 +92,31 @@ def populate_pulp(cfg, url=FILE_FIXTURE_MANIFEST_URL):
return client.get(FILE_CONTENT_PATH)['results']


def create_file_publication(cfg, repo, version_href=None, publisher=None):
"""Create a file publication.
:param pulp_smash.config.PulpSmashConfig cfg: Information about the Pulp
host.
:param repo: A dict of information about the repository.
:param version_href: A href for the repo version to be published.
:param publisher: A dict of publisher info to use to publish.
:returns: A publication. A dict of information about the just created
publication.
"""
if version_href:
body = {"repository_version": version_href}
else:
body = {"repository": repo["_href"]}

if publisher:
body['publisher'] = publisher['_href']

client = api.Client(cfg, api.json_handler)
call_report = client.post(FILE_PUBLICATION_PATH, body)
tasks = tuple(api.poll_spawned_tasks(cfg, call_report))
return client.get(tasks[-1]["created_resources"][0])


skip_if = partial(selectors.skip_if, exc=SkipTest) # pylint:disable=invalid-name
"""The ``@skip_if`` decorator, customized for unittest.
Expand Down

0 comments on commit 5c08855

Please sign in to comment.