From 1e49f002caaeff67cec68f1f194f47e6deeba6dd Mon Sep 17 00:00:00 2001 From: David Davis Date: Sat, 29 Sep 2018 18:11:44 -0400 Subject: [PATCH] Remove AnsiblePublisher fixes #4048 https://pulp.plan.io/issues/4048 --- README.rst | 22 +---- pulp_ansible/app/models.py | 10 +- pulp_ansible/app/serializers.py | 14 +-- pulp_ansible/app/tasks/publishing.py | 57 +---------- pulp_ansible/app/viewsets.py | 43 ++++---- .../functional/api/test_crud_publishers.py | 99 ------------------- 6 files changed, 31 insertions(+), 214 deletions(-) delete mode 100644 pulp_ansible/tests/functional/api/test_crud_publishers.py diff --git a/README.rst b/README.rst index d6978e3b9..a7faa195c 100644 --- a/README.rst +++ b/README.rst @@ -162,26 +162,10 @@ Add content to repository ``foo`` ``$ http POST ':8000'$REPO_HREF'versions/' add_content_units:="[\"$CONTENT_HREF\"]"`` -Create an Ansible publisher ---------------------------- - -``$ http POST http://localhost:8000/pulp/api/v3/publishers/ansible/ name=bar`` - -.. code:: json - - { - "_href": "http://localhost:8000/pulp/api/v3/publishers/ansible/bar/", - ... - } - - -``$ export PUBLISHER_HREF=$(http :8000/pulp/api/v3/publishers/ansible/ | jq -r '.results[] | select(.name == "bar") | ._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/ansible/publications/ repository=$REPO_HREF`` .. code:: json @@ -189,7 +173,7 @@ Use the ``bar`` Publisher to create a Publication "task": "http://localhost:8000/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')`` Create a Distribution for the Publication diff --git a/pulp_ansible/app/models.py b/pulp_ansible/app/models.py index cd92cb8f8..17d70826b 100644 --- a/pulp_ansible/app/models.py +++ b/pulp_ansible/app/models.py @@ -2,7 +2,7 @@ from django.db import models -from pulpcore.plugin.models import Content, ContentArtifact, Remote, Publisher +from pulpcore.plugin.models import Content, ContentArtifact, Remote log = getLogger(__name__) @@ -73,14 +73,6 @@ class Meta: ) -class AnsiblePublisher(Publisher): - """ - A Publisher for Ansible content. - """ - - TYPE = 'ansible' - - class AnsibleRemote(Remote): """ A Remote for Ansible content. diff --git a/pulp_ansible/app/serializers.py b/pulp_ansible/app/serializers.py index b3649225f..75525590c 100644 --- a/pulp_ansible/app/serializers.py +++ b/pulp_ansible/app/serializers.py @@ -1,10 +1,10 @@ from rest_framework import serializers from pulpcore.plugin.serializers import ContentSerializer, IdentityField, NestedIdentityField, \ - RelatedField, RemoteSerializer, PublisherSerializer + RelatedField, RemoteSerializer from pulpcore.plugin.models import Artifact -from .models import AnsibleRemote, AnsiblePublisher, AnsibleRole, AnsibleRoleVersion +from .models import AnsibleRemote, AnsibleRole, AnsibleRoleVersion class AnsibleRoleSerializer(ContentSerializer): @@ -61,13 +61,3 @@ class AnsibleRemoteSerializer(RemoteSerializer): class Meta: fields = RemoteSerializer.Meta.fields model = AnsibleRemote - - -class AnsiblePublisherSerializer(PublisherSerializer): - """ - A serializer for Ansible Publishers. - """ - - class Meta: - fields = PublisherSerializer.Meta.fields - model = AnsiblePublisher diff --git a/pulp_ansible/app/tasks/publishing.py b/pulp_ansible/app/tasks/publishing.py index 75a723ae7..72a7025e3 100644 --- a/pulp_ansible/app/tasks/publishing.py +++ b/pulp_ansible/app/tasks/publishing.py @@ -2,72 +2,25 @@ from gettext import gettext as _ -from pulpcore.plugin.models import ( - RepositoryVersion, - Publication, - PublishedArtifact, - RemoteArtifact) -from pulpcore.plugin.tasking import WorkingDirectory - -from pulp_ansible.app.models import AnsiblePublisher +from pulpcore.plugin.models import RepositoryVersion, Publication 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 = AnsiblePublisher.objects.get(pk=publisher_pk) repository_version = RepositoryVersion.objects.get(pk=repository_version_pk) - - log.info( - _('Publishing: repository=%(repository)s, version=%(version)d, publisher=%(publisher)s'), - { - 'repository': repository_version.repository.name, - 'version': repository_version.number, - 'publisher': publisher.name, - }) - - with WorkingDirectory(): - with Publication.create(repository_version, publisher) as publication: - populate(publication) + with Publication.create(repository_version, pass_through=True) as publication: + pass log.info( _('Publication: %(publication)s created'), { 'publication': publication.pk }) - - -def populate(publication): - """ - Populate a publication. - - Create published artifacts and yield a Manifest Entry for each. - - Args: - publication (pulpcore.plugin.models.Publication): A Publication to populate. - - Yields: - Entry: Each manifest entry. - - """ - def find_artifact(): - _artifact = content_artifact.artifact - if not _artifact: - _artifact = RemoteArtifact.objects.filter(content_artifact=content_artifact).first() - return _artifact - - for content in publication.repository_version.content: - content_artifact = content.contentartifact_set.get() - published_artifact = PublishedArtifact( - relative_path=content_artifact.relative_path, - publication=publication, - content_artifact=content_artifact) - published_artifact.save() diff --git a/pulp_ansible/app/viewsets.py b/pulp_ansible/app/viewsets.py index d47a26417..8106467a2 100644 --- a/pulp_ansible/app/viewsets.py +++ b/pulp_ansible/app/viewsets.py @@ -1,10 +1,10 @@ from django.db import transaction from drf_yasg.utils import swagger_auto_schema from rest_framework.decorators import detail_route -from rest_framework import status +from rest_framework import mixins, status from rest_framework.response import Response -from pulpcore.plugin.models import Artifact, RepositoryVersion +from pulpcore.plugin.models import Artifact, RepositoryVersion, Publication from pulpcore.plugin.serializers import ( AsyncOperationResponseSerializer, RepositoryPublishURLSerializer, @@ -12,17 +12,17 @@ ) from pulpcore.plugin.tasking import enqueue_with_reservation from pulpcore.plugin.viewsets import ( + BaseFilterSet, ContentViewSet, - RemoteViewSet, + NamedModelViewSet, OperationPostponedResponse, - PublisherViewSet, - BaseFilterSet + RemoteViewSet ) from . import tasks -from .models import AnsibleRemote, AnsiblePublisher, AnsibleRole, AnsibleRoleVersion -from .serializers import (AnsibleRemoteSerializer, AnsiblePublisherSerializer, - AnsibleRoleSerializer, AnsibleRoleVersionSerializer) +from .models import AnsibleRemote, AnsibleRole, AnsibleRoleVersion +from .serializers import (AnsibleRemoteSerializer, AnsibleRoleSerializer, + AnsibleRoleVersionSerializer) class AnsibleRoleFilter(BaseFilterSet): @@ -153,25 +153,22 @@ def sync(self, request, pk): return OperationPostponedResponse(result, request) -class AnsiblePublisherViewSet(PublisherViewSet): +class AnsiblePublicationsViewSet(NamedModelViewSet, + mixins.CreateModelMixin): """ - ViewSet for Ansible Publishers. + ViewSet for Ansible Publications. """ - endpoint_name = 'ansible' - queryset = AnsiblePublisher.objects.all() - serializer_class = AnsiblePublisherSerializer + endpoint_name = 'ansible/publications' + queryset = Publication.objects.all() - @swagger_auto_schema( - operation_description="Trigger an asynchronous task to publish Ansible content.", - responses={202: AsyncOperationResponseSerializer} - ) - @detail_route(methods=('post',), serializer_class=RepositoryPublishURLSerializer) - def publish(self, request, pk): + @swagger_auto_schema(operation_description="Trigger an asynchronous task to create " + "a new Ansible content publication.", + responses={202: AsyncOperationResponseSerializer}) + def create(self, request): """ - Dispatches a publish task. + Queues a task that publishes a new Ansible Publication. """ - publisher = self.get_object() serializer = RepositoryPublishURLSerializer( data=request.data, context={'request': request} @@ -183,10 +180,10 @@ def publish(self, request, pk): 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], + tasks.publish, [repository_version.repository], kwargs={ - 'publisher_pk': str(publisher.pk), 'repository_version_pk': str(repository_version.pk) } ) diff --git a/pulp_ansible/tests/functional/api/test_crud_publishers.py b/pulp_ansible/tests/functional/api/test_crud_publishers.py deleted file mode 100644 index c5618b963..000000000 --- a/pulp_ansible/tests/functional/api/test_crud_publishers.py +++ /dev/null @@ -1,99 +0,0 @@ -# coding=utf-8 -"""Tests that CRUD publishers.""" -import unittest - -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_repo - -from pulp_ansible.tests.functional.constants import ANSIBLE_PUBLISHER_PATH -from pulp_ansible.tests.functional.utils import gen_ansible_publisher, skip_if -from pulp_ansible.tests.functional.utils import set_up_module as setUpModule # noqa:F401 - - -class CRUDPublishersTestCase(unittest.TestCase): - """CRUD publishers.""" - - @classmethod - def setUpClass(cls): - """Create class-wide variables. - - In order to create a publisher a repository has to be created first. - """ - cls.cfg = config.get_config() - cls.client = api.Client(cls.cfg, api.json_handler) - cls.publisher = {} - cls.repo = cls.client.post(REPO_PATH, gen_repo()) - - @classmethod - def tearDownClass(cls): - """Clean class-wide variable.""" - cls.client.delete(cls.repo['_href']) - - def test_01_create_publisher(self): - """Create a publisher.""" - body = gen_ansible_publisher() - type(self).publisher = self.client.post(ANSIBLE_PUBLISHER_PATH, body) - for key, val in body.items(): - with self.subTest(key=key): - self.assertEqual(self.publisher[key], val) - - @skip_if(bool, 'publisher', False) - def test_02_create_same_name(self): - """Try to create a second publisher with an identical name. - - See: `Pulp Smash #1055 - `_. - """ - body = gen_ansible_publisher() - body['name'] = self.publisher['name'] - with self.assertRaises(HTTPError): - self.client.post(ANSIBLE_PUBLISHER_PATH, body) - - @skip_if(bool, 'publisher', False) - def test_02_read_publisher(self): - """Read a publisher by its href.""" - publisher = self.client.get(self.publisher['_href']) - for key, val in self.publisher.items(): - with self.subTest(key=key): - self.assertEqual(publisher[key], val) - - @skip_if(bool, 'publisher', False) - def test_02_read_publishers(self): - """Read a publisher by its name.""" - page = self.client.get(ANSIBLE_PUBLISHER_PATH, params={ - 'name': self.publisher['name'] - }) - self.assertEqual(len(page['results']), 1) - for key, val in self.publisher.items(): - with self.subTest(key=key): - self.assertEqual(page['results'][0][key], val) - - @skip_if(bool, 'publisher', False) - def test_03_partially_update(self): - """Update a publisher using HTTP PATCH.""" - body = gen_ansible_publisher() - self.client.patch(self.publisher['_href'], body) - type(self).publisher = self.client.get(self.publisher['_href']) - for key, val in body.items(): - with self.subTest(key=key): - self.assertEqual(self.publisher[key], val) - - @skip_if(bool, 'publisher', False) - def test_04_fully_update(self): - """Update a publisher using HTTP PUT.""" - body = gen_ansible_publisher() - self.client.put(self.publisher['_href'], body) - type(self).publisher = self.client.get(self.publisher['_href']) - for key, val in body.items(): - with self.subTest(key=key): - self.assertEqual(self.publisher[key], val) - - @skip_if(bool, 'publisher', False) - def test_05_delete(self): - """Delete a publisher.""" - self.client.delete(self.publisher['_href']) - with self.assertRaises(HTTPError): - self.client.get(self.publisher['_href'])