Skip to content

Commit

Permalink
Skip the publish and create directly the publication.
Browse files Browse the repository at this point in the history
  • Loading branch information
ipanova committed Apr 2, 2019
1 parent 34a7c62 commit ce0cc34
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 222 deletions.
20 changes: 3 additions & 17 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,33 +177,19 @@ Look at the new Repository Version created
"number": 1
}
Create a ``docker`` Publisher ``baz``
----------------------------------------------

``$ http POST http://localhost:8000/pulp/api/v3/publishers/docker/docker/ name=baz``

.. code:: json
{
"_href": "/pulp/api/v3/publishers/docker/8ce1b34c-56c3-4ced-81b8-81e83b174fbc/",
...
}
``$ export PUBLISHER_HREF=$(http :8000/pulp/api/v3/publishers/docker/docker/ | jq -r '.results[] | select(.name == "baz") | ._href')``

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

``$ http POST ':8000'$PUBLISHER_HREF'publish/' repository=$REPO_HREF``
``$ http POST :8000/pulp/api/v3/docker/publications/ repository=$REPO_HREF``

.. code:: json
{
"task": "/pulp/api/v3/tasks/fd4cbecd-6c6a-4197-9cbe-4e45b0516309/"
}
``$ export PUBLICATION_HREF=$(http :8000/pulp/api/v3/publications/ | jq -r --arg PUBLISHER_HREF "$PUBLISHER_HREF" '.results[] | select(.publisher==$PUBLISHER_HREF) | ._href')``
``$ export PUBLICATION_HREF=$(http :8000/pulp/api/v3/publications/ | jq -r '.results[0] | ._href')``

Add a Docker Distribution to serve your publication
---------------------------------------------------
Expand Down
12 changes: 1 addition & 11 deletions pulp_docker/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.db import models

from pulpcore.plugin.download import DownloaderFactory
from pulpcore.plugin.models import BaseDistribution, Content, Remote, Publisher
from pulpcore.plugin.models import BaseDistribution, Content, Remote

from . import downloaders

Expand Down Expand Up @@ -210,16 +210,6 @@ class Meta:
)


class DockerPublisher(Publisher):
"""
A Publisher for DockerContent.
Define any additional fields for your new publisher if needed.
"""

TYPE = 'docker'


class DockerRemote(Remote):
"""
A Remote for DockerContent.
Expand Down
20 changes: 0 additions & 20 deletions pulp_docker/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
BaseDistributionSerializer,
DetailRelatedField,
IdentityField,
PublisherSerializer,
RemoteSerializer,
SingleArtifactContentSerializer,
)
Expand Down Expand Up @@ -174,25 +173,6 @@ class Meta:
model = models.DockerRemote


class DockerPublisherSerializer(PublisherSerializer):
"""
A Serializer for DockerPublisher.
Add any new fields if defined on DockerPublisher.
Similar to the example above, in DockerContentSerializer.
Additional validators can be added to the parent validators list
For example::
class Meta:
validators = platform.PublisherSerializer.Meta.validators + [myValidator1, myValidator2]
"""

class Meta:
fields = PublisherSerializer.Meta.fields
model = models.DockerPublisher


class DockerDistributionSerializer(BaseDistributionSerializer):
"""
A serializer for DockerDistribution.
Expand Down
18 changes: 5 additions & 13 deletions pulp_docker/app/tasks/publishing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,20 @@
Publication
)

from pulp_docker.app.models import DockerPublisher


log = logging.getLogger(__name__)


def publish(publisher_pk, repository_version_pk):
def publish(repository_version_pk):
"""
Use provided publisher to create a Publication based on a RepositoryVersion.
Create a Publication based on a RepositoryVersion.
Args:
publisher_pk (str): Use the publish settings provided by this publisher.
repository_version_pk (str): Create a publication from this repository version.
"""
publisher = DockerPublisher.objects.get(pk=publisher_pk)
repository_version = RepositoryVersion.objects.get(pk=repository_version_pk)

log.info(_('Publishing: repository={repo}, version={ver}, publisher={pub}').format(
repo=repository_version.repository.name,
ver=repository_version.number,
pub=publisher.name
))
with Publication.create(repository_version, pass_through=True) as publication:
pass

with Publication.create(repository_version, publisher, pass_through=True) as publication:
log.info(_('Publication: {publication} created').format(publication=publication.pk))
log.info(_('Publication: {publication} created').format(publication=publication.pk))
35 changes: 17 additions & 18 deletions pulp_docker/app/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
RepositoryPublishURLSerializer,
RepositorySyncURLSerializer,
)

from pulpcore.plugin.models import RepositoryVersion, Publication
from pulpcore.plugin.tasking import enqueue_with_reservation
from pulpcore.plugin.viewsets import (
ContentViewSet,
NamedModelViewSet,
RemoteViewSet,
OperationPostponedResponse,
PublisherViewSet)
OperationPostponedResponse,)
from rest_framework.decorators import detail_route
from rest_framework import mixins

Expand Down Expand Up @@ -148,42 +149,40 @@ def sync(self, request, pk):
return OperationPostponedResponse(result, request)


class DockerPublisherViewSet(PublisherViewSet):
class DockerPublicationViewSet(NamedModelViewSet, mixins.CreateModelMixin):
"""
A ViewSet for DockerPublisher.
A ViewSet for Docker Publication.
"""

endpoint_name = 'docker'
queryset = models.DockerPublisher.objects.all()
serializer_class = serializers.DockerPublisherSerializer
endpoint_name = 'docker/publications'
queryset = Publication.objects.all()
serializer_class = RepositoryPublishURLSerializer

# This decorator is necessary since a publish operation is asyncrounous and returns
# the id and href of the publish task.
@swagger_auto_schema(
operation_description="Trigger an asynchronous task to publish content",
operation_description="Trigger an asynchronous task to create a docker publication",
responses={202: AsyncOperationResponseSerializer}
)
@detail_route(methods=('post',), serializer_class=RepositoryPublishURLSerializer)
def publish(self, request, pk):
def create(self, request):
"""
Publishes a repository.
Queues a task that publishes a new Docker Publication.
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.is_valid(raise_exception=True)
repository_version = serializer.validated_data.get('repository_version')

# Safe because version OR repository is enforced by serializer.
if not repository_version:
repository = serializer.validated_data.get('repository')
repository_version = RepositoryVersion.latest(repository)

result = enqueue_with_reservation(
tasks.publish,
[repository_version.repository, publisher],
[repository_version.repository],
kwargs={
'publisher_pk': str(publisher.pk),
'repository_version_pk': str(repository_version.pk)
}
)
Expand Down
99 changes: 0 additions & 99 deletions pulp_docker/tests/functional/api/test_crud_publishers.py

This file was deleted.

29 changes: 6 additions & 23 deletions pulp_docker/tests/functional/api/test_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@
"""Tests that publish docker plugin repositories."""
import unittest
from random import choice
from urllib.parse import urljoin

from requests.exceptions import HTTPError

from pulp_smash import api, config
from pulp_smash.pulp3.constants import REPO_PATH
from pulp_smash.pulp3.utils import (
gen_publisher,
gen_repo,
get_content,
get_versions,
publish,
publication,
sync,
)

from pulp_docker.tests.functional.utils import gen_docker_remote
from pulp_docker.tests.functional.constants import (
DOCKER_CONTENT_NAME,
DOCKER_REMOTE_PATH,
DOCKER_PUBLISHER_PATH,
DOCKER_PUBLICATION_PATH,
)
from pulp_docker.tests.functional.utils import set_up_module as setUpModule # noqa:F401

Expand Down Expand Up @@ -51,8 +47,6 @@ def test_all(self):
4. Create a publication by supplying the non-latest ``repository_version``.
5. Assert that the publication ``repository_version`` attribute points
to the supplied repository version.
6. Assert that an exception is raised when providing two different
repository versions to be published at same time.
"""
body = gen_docker_remote()
remote = self.client.post(DOCKER_REMOTE_PATH, body)
Expand All @@ -63,9 +57,6 @@ def test_all(self):

sync(self.cfg, remote, repo)

publisher = self.client.post(DOCKER_PUBLISHER_PATH, gen_publisher())
self.addCleanup(self.client.delete, publisher['_href'])

# Step 1
repo = self.client.get(repo['_href'])
for docker_content in get_content(repo)[DOCKER_CONTENT_NAME]:
Expand All @@ -77,21 +68,13 @@ def test_all(self):
non_latest = choice(version_hrefs[:-1])

# Step 2
publication = publish(self.cfg, publisher, repo)
publication1 = publication(self.cfg, DOCKER_PUBLICATION_PATH, repo)

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

# Step 4
publication = publish(self.cfg, publisher, repo, non_latest)
publication2 = publication(self.cfg, DOCKER_PUBLICATION_PATH, repo, non_latest)

# Step 5
self.assertEqual(publication['repository_version'], non_latest)

# Step 6
with self.assertRaises(HTTPError):
body = {
'repository': repo['_href'],
'repository_version': non_latest
}
self.client.post(urljoin(publisher['_href'], 'publish/'), body)
self.assertEqual(publication2['repository_version'], non_latest)
Loading

0 comments on commit ce0cc34

Please sign in to comment.