Skip to content

Commit

Permalink
Add template plugin and renaming script
Browse files Browse the repository at this point in the history
  • Loading branch information
goosemania committed Sep 26, 2017
1 parent d77c316 commit d1556da
Show file tree
Hide file tree
Showing 10 changed files with 469 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md

This file was deleted.

120 changes: 120 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
Tepmlate to create your own plugin
==================================

This is the ``plugin_template`` repository to help plugin writers
get started and write their own plugin for `Pulp Project
3.0+ <https://pypi.python.org/pypi/pulpcore/>`__.

Clone this repository and run the provided ``rename.py`` script to create
a skeleton for your plugin with the name of your choice. It will contain
``setup.py``, expected plugin layout and stubs for necessary classes and methods.

``$ git clone https://github.com/pulp/plugin_template.git``

``$ cd plugin_template``

``$ ./rename.py your_plugin_name``

Check `Plugin Writer's Guide <http://docs.pulpproject.org/en/3.0/nightly/plugins/plugin-writer/index.html>`__
for more details and suggestions on plugin implementaion.

Below are some ideas for how to document your plugin.


All REST API examples below use `httpie <https://httpie.org/doc>`__ to
perform the requests.

Install ``pulpcore``
--------------------

Follow the `installation
instructions <docs.pulpproject.org/en/3.0/nightly/installation/instructions.html>`__
provided with pulpcore.

Install plugin
--------------

From source
~~~~~~~~~~~

Define installation steps here.

Install from PyPI
~~~~~~~~~~~~~~~~~

Define installation steps here.


Create a repository ``foo``
---------------------------

``$ http POST http://localhost:8000/api/v3/repositories/ name=foo``

Add an Importer to repository ``foo``
-------------------------------------

Add important details about your Importer and provide examples.

``$ http POST http://localhost:8000/api/v3/repositories/foo/importers/plugin-template/ some=params``

.. code:: json
{
"_href": "http://localhost:8000/api/v3/repositories/foo/importers/plugin-template/bar/",
...
}
Add a Publisher to repository ``foo``
-------------------------------------

``$ http POST http://localhost:8000/api/v3/repositories/foo/publishers/plugin-template/ name=bar``

.. code:: json
{
"_href": "http://localhost:8000/api/v3/repositories/foo/publishers/plugin-template/bar/",
...
}
Add a Distribution to Publisher ``bar``
---------------------------------------

``$ http POST http://localhost:8000/api/v3/repositories/foo/publishers/plugin-template/bar/distributions/ some=params``

Sync repository ``foo`` using Importer ``bar``
----------------------------------------------

Use ``plugin-template`` Importer:

``http POST http://localhost:8000/api/v3/repositories/foo/importers/plugin-template/bar/sync/``

Add content to repository ``foo``
---------------------------------

``$ http POST http://localhost:8000/api/v3/repositorycontents/ repository='http://localhost:8000/api/v3/repositories/foo/' content='http://localhost:8000/api/v3/content/plugin-template/a9578a5f-c59f-4920-9497-8d1699c112ff/'``

Create a Publication using Publisher ``bar``
--------------------------------------------

Dispatch the Publish task

``$ http POST http://localhost:8000/api/v3/repositories/foo/publishers/plugin-template/bar/publish/``

.. code:: json
[
{
"_href": "http://localhost:8000/api/v3/tasks/fd4cbecd-6c6a-4197-9cbe-4e45b0516309/",
"task_id": "fd4cbecd-6c6a-4197-9cbe-4e45b0516309"
}
]
Check status of a task
----------------------

``$ http GET http://localhost:8000/api/v3/tasks/82e64412-47f8-4dd4-aa55-9de89a6c549b/``

Download ``foo.tar.gz`` from Pulp
---------------------------------

``$ http GET http://localhost:8000/content/foo/foo.tar.gz``
5 changes: 5 additions & 0 deletions flake8.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
exclude = */migrations/*
# E401: multiple imports on one line
ignore = E401
max-line-length = 100
1 change: 1 addition & 0 deletions pulp_plugin_template/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default_app_config = 'pulp_plugin_template.app.PulpPluginTemplatePluginAppConfig'
6 changes: 6 additions & 0 deletions pulp_plugin_template/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pulpcore.plugin import PulpPluginAppConfig


class PulpPluginTemplatePluginAppConfig(PulpPluginAppConfig):
name = 'pulp_plugin_template.app'
label = 'pulp_plugin_template'
84 changes: 84 additions & 0 deletions pulp_plugin_template/app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""
Check `Plugin Writer's Guide`_ and `pulp_example`_ plugin
implementation for more details.
.. _Plugin Writer's Guide:
http://docs.pulpproject.org/en/3.0/nightly/plugins/plugin-writer/index.html
.. _pulp_example:
https://github.com/pulp/pulp_example/
"""

from gettext import gettext as _
from logging import getLogger

from django.db import models

from pulpcore.plugin.models import (Artifact, Content, ContentArtifact, RemoteArtifact, Importer,
ProgressBar, Publisher, RepositoryContent, PublishedArtifact,
PublishedMetadata)
from pulpcore.plugin.tasking import Task


log = getLogger(__name__)


class PluginTemplateContent(Content):
"""
The "plugin-template" content type.
Define fields you need for your new content type and
specify uniqueness constraint to identify unit of this type.
For example::
field1 = models.TextField()
field2 = models.IntegerField()
field3 = models.CharField()
class Meta:
unique_together = (field1, field2)
"""
TYPE = 'plugin-template'

@classmethod
def natural_key_fields(cls):
for unique in cls._meta.unique_together:
for field in unique:
yield field


class PluginTemplatePublisher(Publisher):
"""
A Publisher for PluginTemplateContent.
Define any additional fields for your new publisher if needed.
A ``publish`` method should be defined.
It is responsible for publishing metadata and artifacts
which belongs to a specific repository.
"""
TYPE = 'plugin-template'

def publish(self):
"""
Publish the repository.
"""
raise NotImplementedError


class PluginTemplateImporter(Importer):
"""
An Importer for PluginTemplateContent.
Define any additional fields for your new importer if needed.
A ``sync`` method should be defined.
It is responsible for parsing metadata of the content,
downloading of the content and saving it to Pulp.
"""
TYPE = 'plugin-template'

def sync(self):
"""
Synchronize the repository with the remote repository.
"""
raise NotImplementedError
61 changes: 61 additions & 0 deletions pulp_plugin_template/app/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
Check `Plugin Writer's Guide`_ and `pulp_example`_ plugin
implementation for more details.
.. _Plugin Writer's Guide:
http://docs.pulpproject.org/en/3.0/nightly/plugins/plugin-writer/index.html
.. _pulp_example:
https://github.com/pulp/pulp_example/
"""

from rest_framework import serializers
from pulpcore.plugin import serializers as platform

from . import models


class PluginTemplateContentSerializer(platform.ContentSerializer):
"""
A Serializer for PluginTemplateContent.
Add serializers for the new fields defined in PluginTemplateContent and
add those fields to the Meta class keeping fields from the parent class as well.
For example::
field1 = serializers.TextField()
field2 = serializers.IntegerField()
field3 = serializers.CharField()
class Meta:
fields = platform.ContentSerializer.Meta.fields + ('field1', 'field2', 'field3')
model = models.PluginTemplateContent
"""
class Meta:
fields = platform.ContentSerializer.Meta.fields
model = models.PluginTemplateContent


class PluginTemplateImporterSerializer(platform.ImporterSerializer):
"""
A Serializer for PluginTemplateImporter.
Add any new fields if defined on PluginTemplateImporter.
Similar to the example above, in PluginTemplateContentSerializer.
"""
class Meta:
fields = platform.ImporterSerializer.Meta.fields
model = models.PluginTemplateImporter


class PluginTemplatePublisherSerializer(platform.PublisherSerializer):
"""
A Serializer for PluginTemplatePublisher.
Add any new fields if defined on PluginTemplatePublisher.
Similar to the example above, in PluginTemplateContentSerializer.
"""
class Meta:
fields = platform.PublisherSerializer.Meta.fields
model = models.PluginTemplatePublisher
53 changes: 53 additions & 0 deletions pulp_plugin_template/app/viewsets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Check `Plugin Writer's Guide`_ and `pulp_example`_ plugin
implementation for more details.
.. _Plugin Writer's Guide:
http://docs.pulpproject.org/en/3.0/nightly/plugins/plugin-writer/index.html
.. _pulp_example:
https://github.com/pulp/pulp_example/
"""

from pulpcore.plugin import viewsets as platform

from . import models, serializers


class PluginTemplateContentViewSet(platform.ContentViewSet):
"""
A ViewSet for PluginTemplateContent.
Define endpoint name which will appear in the API endpoint for this content type.
For example::
http://pulp.example.com/api/v3/content/plugin-template/
Also specify queryset and serializer for PluginTemplateContent.
"""
endpoint_name = 'plugin-template'
queryset = models.PluginTemplateContent.objects.all()
serializer_class = serializers.PluginTemplateContentSerializer


class PluginTemplateImporterViewSet(platform.ImporterViewSet):
"""
A ViewSet for PluginTemplateImporter.
Similar to the PluginTemplateContentViewSet above, define endpoint_name,
queryset and serializer, at a minimum.
"""
endpoint_name = 'plugin-template'
queryset = models.PluginTemplateImporter.objects.all()
serializer_class = serializers.PluginTemplateImporterSerializer


class PluginTemplatePublisherViewSet(platform.PublisherViewSet):
"""
A ViewSet for PluginTemplatePublisher.
Similar to the PluginTemplateContentViewSet above, define endpoint_name,
queryset and serializer, at a minimum.
"""
endpoint_name = 'plugin-template'
queryset = models.PluginTemplatePublisher.objects.all()
serializer_class = serializers.PluginTemplatePublisherSerializer
Loading

0 comments on commit d1556da

Please sign in to comment.