From 7955e94b79a9df3f6f2dad2a5fd31a5338393e9d Mon Sep 17 00:00:00 2001 From: David Davis Date: Wed, 18 Apr 2018 16:18:47 -0400 Subject: [PATCH] Allow users to upload role versions fixes #3529 https://pulp.plan.io/issues/3529 --- README.rst | 67 ++++++++++++++++++++++++++++++++---- pulp_ansible/app/models.py | 2 +- pulp_ansible/app/viewsets.py | 16 +++++++-- 3 files changed, 76 insertions(+), 9 deletions(-) diff --git a/README.rst b/README.rst index 2f4f8d3c6..ac45ddc26 100644 --- a/README.rst +++ b/README.rst @@ -94,6 +94,63 @@ Look at the new Repository Version created } +Upload ``pulp-0.0.1.tar`` to Pulp +----------------------------- + +Create an Artifact by uploading the role version tar to Pulp. + +``$ http --form POST http://localhost:8000/api/v3/artifacts/ file@pulp-0.0.1.tar`` + +.. code:: json + + { + "_href": "http://localhost:8000/api/v3/artifacts/7d39e3f6-535a-4b6e-81e9-c83aa56aa19e/", + ... + } + + +Create a Role content unit +-------------------------- + +Create an Ansible role in Pulp. + +``$ http http://localhost:8000/api/v3/content/ansible/roles/ namespace=pulp name=pulp`` + +.. code:: json + + { + "_href": "http://localhost:8000/api/v3/content/ansible/roles/3965b515-53a0-4667-a540-a76714a903d4/", + "_versions_href": "http://localhost:8000/api/v3/content/ansible/roles/3965b515-53a0-4667-a540-a76714a903d4/versions/", + "name": "pulp", + "namespace": "pulp" + } + + +Create a ``role version`` from the Role and Artifact +----------------------------------------------------- + +Create a content unit and point it to your Artifact and Role + +``$ http POST http://localhost:8000/api/v3/content/ansible/roles/3965b515-53a0-4667-a540-a76714a903d4/versions/ version=0.0.1 artifact="http://localhost:8000/api/v3/artifacts/7d39e3f6-535a-4b6e-81e9-c83aa56aa19e/"`` + +.. code:: json + + { + "_href": "http://localhost:8000/api/v3/content/ansible/roles/3965b515-53a0-4667-a540-a76714a903d4/versions/183d532c-37e8-4c0a-a00e-5a80b2de4162/", + "artifact": "http://localhost:8000/api/v3/artifacts/7d39e3f6-535a-4b6e-81e9-c83aa56aa19e/", + "version": "0.0.1", + "type": "ansible-role-version" + } + +``$ export CONTENT_HREF=$(http :8000/api/v3/content/ansible/roles/3965b515-53a0-4667-a540-a76714a903d4/versions/ | jq -r '.results[] | select(.version == "0.0.1") | ._href')`` + + +Add content to repository ``foo`` +--------------------------------- + +``$ http POST $REPO_HREF'versions/' add_content_units:="[\"$CONTENT_HREF\"]"`` + + Create an Ansible publisher --------------------------- @@ -117,12 +174,10 @@ Use the ``bar`` Publisher to create a Publication .. code:: json - [ - { - "_href": "http://localhost:8000/api/v3/tasks/fd4cbecd-6c6a-4197-9cbe-4e45b0516309/", - "task_id": "fd4cbecd-6c6a-4197-9cbe-4e45b0516309" - } - ] + { + "_href": "http://localhost:8000/api/v3/tasks/fd4cbecd-6c6a-4197-9cbe-4e45b0516309/", + "task_id": "fd4cbecd-6c6a-4197-9cbe-4e45b0516309" + } ``$ export PUBLICATION_HREF=$(http :8000/api/v3/publications/ | jq -r --arg PUBLISHER_HREF "$PUBLISHER_HREF" '.results[] | select(.publisher==$PUBLISHER_HREF) | ._href')`` diff --git a/pulp_ansible/app/models.py b/pulp_ansible/app/models.py index 288207a59..3fda48e01 100644 --- a/pulp_ansible/app/models.py +++ b/pulp_ansible/app/models.py @@ -44,7 +44,7 @@ def artifact(self, artifact): if self.pk: ca = ContentArtifact(artifact=artifact, content=self, - relative_path=self.relative_path) + relative_path="{}.tar".format(self.version)) ca.save() class Meta: diff --git a/pulp_ansible/app/viewsets.py b/pulp_ansible/app/viewsets.py index 297e99bc5..ee70ed20c 100644 --- a/pulp_ansible/app/viewsets.py +++ b/pulp_ansible/app/viewsets.py @@ -6,7 +6,7 @@ from rest_framework import serializers, status from rest_framework.response import Response -from pulpcore.plugin.models import Repository, RepositoryVersion +from pulpcore.plugin.models import Artifact, Repository, RepositoryVersion from pulpcore.plugin.viewsets import ( ContentViewSet, RemoteViewSet, @@ -67,8 +67,20 @@ class AnsibleRoleVersionViewSet(ContentViewSet): def endpoint_pieces(cls): return (cls.endpoint_name,) + @transaction.atomic def create(self, request, role_pk): - raise NotImplementedError + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + validated_data = serializer.validated_data + + role_version = AnsibleRoleVersion(**validated_data) + role_version.role = AnsibleRole.objects.get(pk=role_pk) + role_version.save() + role_version.artifact = self.get_resource(request.data['artifact'], Artifact) + + headers = self.get_success_headers(request.data) + return Response(self.get_serializer(role_version).data, status=status.HTTP_201_CREATED, + headers=headers) class AnsibleRemoteViewSet(RemoteViewSet):