# Plugin Development This documentation covers the development of custom plugins for NetBox. Plugins are essentially self-contained [Django apps](https://docs.djangoproject.com/en/stable/) which integrate with NetBox to provide custom functionality. Since the development of Django apps is already very well-documented, we'll only be covering the aspects that are specific to NetBox. Plugins can do a lot, including: * Create Django models to store data in the database * Provide their own "pages" (views) in the web user interface * Inject template content and navigation links * Establish their own REST API endpoints * Add custom request/response middleware However, keep in mind that each piece of functionality is entirely optional. For example, if your plugin merely adds a piece of middleware or an API endpoint for existing data, there's no need to define any new models. !!! warning While very powerful, the NetBox plugins API is necessarily limited in its scope. The plugins API is discussed here in its entirety: Any part of the NetBox code base not documented here is _not_ part of the supported plugins API, and should not be employed by a plugin. Internal elements of NetBox are subject to change at any time and without warning. Plugin authors are **strongly** encouraged to develop plugins using only the officially supported components discussed here and those provided by the underlying Django framework so as to avoid breaking changes in future releases. ## Initial Setup ### Plugin Structure Although the specific structure of a plugin is largely left to the discretion of its authors, a typical NetBox plugin looks something like this: ```no-highlight project-name/ - plugin_name/ - templates/ - plugin_name/ - *.html - __init__.py - middleware.py - navigation.py - signals.py - template_content.py - urls.py - views.py - README - setup.py ``` The top level is the project root, which can have any name that you like. Immediately within the root should exist several items: * `setup.py` - This is a standard installation script used to install the plugin package within the Python environment. * `README` - A brief introduction to your plugin, how to install and configure it, where to find help, and any other pertinent information. It is recommended to write README files using a markup language such as Markdown. * The plugin source directory, with the same name as your plugin. This must be a valid Python package name (e.g. no spaces or hyphens). The plugin source directory contains all the actual Python code and other resources used by your plugin. Its structure is left to the author's discretion, however it is recommended to follow best practices as outlined in the [Django documentation](https://docs.djangoproject.com/en/stable/intro/reusable-apps/). At a minimum, this directory **must** contain an `__init__.py` file containing an instance of NetBox's `PluginConfig` class. ### Create setup.py `setup.py` is the [setup script](https://docs.python.org/3.7/distutils/setupscript.html) we'll use to install our plugin once it's finished. The primary function of this script is to call the setuptools library's `setup()` function to create a Python distribution package. We can pass a number of keyword arguments to inform the package creation as well as to provide metadata about the plugin. An example `setup.py` is below: ```python from setuptools import find_packages, setup setup( name='netbox-animal-sounds', version='0.1', description='An example NetBox plugin', url='https://github.com/netbox-community/netbox-animal-sounds', author='Jeremy Stretch', license='Apache 2.0', install_requires=[], packages=find_packages(), include_package_data=True, zip_safe=False, ) ``` Many of these are self-explanatory, but for more information, see the [setuptools documentation](https://setuptools.readthedocs.io/en/latest/setuptools.html). !!! note `zip_safe=False` is **required** as the current plugin iteration is not zip safe due to upstream python issue [issue19699](https://bugs.python.org/issue19699) ### Define a PluginConfig The `PluginConfig` class is a NetBox-specific wrapper around Django's built-in [`AppConfig`](https://docs.djangoproject.com/en/stable/ref/applications/) class. It is used to declare NetBox plugin functionality within a Python package. Each plugin should provide its own subclass, defining its name, metadata, and default and required configuration parameters. An example is below: ```python from extras.plugins import PluginConfig class AnimalSoundsConfig(PluginConfig): name = 'netbox_animal_sounds' verbose_name = 'Animal Sounds' description = 'An example plugin for development purposes' version = '0.1' author = 'Jeremy Stretch' author_email = 'author@example.com' base_url = 'animal-sounds' required_settings = [] default_settings = { 'loud': False } config = AnimalSoundsConfig ``` NetBox looks for the `config` variable within a plugin's `__init__.py` to load its configuration. Typically, this will be set to the PluginConfig subclass, but you may wish to dynamically generate a PluginConfig class based on environment variables or other factors. #### PluginConfig Attributes | Name | Description | | ---- | ----------- | | `name` | Raw plugin name; same as the plugin's source directory | | `verbose_name` | Human-friendly name for the plugin | | `version` | Current release ([semantic versioning](https://semver.org/) is encouraged) | | `description` | Brief description of the plugin's purpose | | `author` | Name of plugin's author | | `author_email` | Author's public email address | | `base_url` | Base path to use for plugin URLs (optional). If not specified, the project's `name` will be used. | | `required_settings` | A list of any configuration parameters that **must** be defined by the user | | `default_settings` | A dictionary of configuration parameters and their default values | | `min_version` | Minimum version of NetBox with which the plugin is compatible | | `max_version` | Maximum version of NetBox with which the plugin is compatible | | `middleware` | A list of middleware classes to append after NetBox's build-in middleware | | `template_extensions` | The dotted path to the list of template extension classes (default: `template_content.template_extensions`) | | `menu_items` | The dotted path to the list of menu items provided by the plugin (default: `navigation.menu_items`) | All required settings must be configured by the user. If a configuration parameter is listed in both `required_settings` and `default_settings`, the default setting will be ignored. ### Create a Virtual Environment It is strongly recommended to create a Python [virtual environment](https://docs.python.org/3/tutorial/venv.html) specific to your plugin. This will afford you complete control over the installed versions of all dependencies and avoid conflicting with any system packages. This environment can live wherever you'd like, however it should be excluded from revision control. (A popular convention is to keep all virtual environments in the user's home directory, e.g. `~/.virtualenvs/`.) ```shell python3 -m venv /path/to/my/venv ``` You can make NetBox available within this environment by creating a path file pointing to its location. This will add NetBox to the Python path upon activation. (Be sure to adjust the command below to specify your actual virtual environment path, Python version, and NetBox installation.) ```shell cd $VENV/lib/python3.7/site-packages/ echo /opt/netbox/netbox > netbox.pth ``` ### Install the Plugin for Development To ease development, it is recommended to go ahead and install the plugin at this point using setuptools' `develop` mode. This will create symbolic links within your Python environment to the plugin development directory. Call `setup.py` from the plugin's root directory with the `develop` argument (instead of `install`): ```no-highlight $ python setup.py develop ``` ## Database Models If your plugin introduces a new type of object in NetBox, you'll probably want to create a [Django model](https://docs.djangoproject.com/en/stable/topics/db/models/) for it. A model is essentially a Python representation of a database table, with attributes that represent individual columns. Model instances can be created, manipulated, and deleted using [queries](https://docs.djangoproject.com/en/stable/topics/db/queries/). Models must be defined within a file named `models.py`. Below is an example `models.py` file containing a model with two character fields: ```python from django.db import models class Animal(models.Model): name = models.CharField(max_length=50) sound = models.CharField(max_length=50) def __str__(self): return self.name ``` Once you have defined the model(s) for your plugin, you'll need to create the database schema migrations. A migration file is essentially a set of instructions for manipulating the PostgreSQL database to support your new model, or to alter existing models. Creating migrations can usually be done automatically using Django's `makemigrations` management command. !!! note A plugin must be installed before it can be used with Django management commands. If you skipped this step above, run `python setup.py develop` from the plugin's root directory. ```no-highlight $ ./manage.py makemigrations netbox_animal_sounds Migrations for 'netbox_animal_sounds': /home/jstretch/animal_sounds/netbox_animal_sounds/migrations/0001_initial.py - Create model Animal ``` Next, we can apply the migration to the database with the `migrate` command: ```no-highlight $ ./manage.py migrate netbox_animal_sounds Operations to perform: Apply all migrations: netbox_animal_sounds Running migrations: Applying netbox_animal_sounds.0001_initial... OK ``` For more background on schema migrations, see the [Django documentation](https://docs.djangoproject.com/en/stable/topics/migrations/). ### Using the Django Admin Interface Plugins can optionally expose their models via Django's built-in [administrative interface](https://docs.djangoproject.com/en/stable/ref/contrib/admin/). This can greatly improve troubleshooting ability, particularly during development. To expose a model, simply register it using Django's `admin.register()` function. An example `admin.py` file for the above model is shown below: ```python from django.contrib import admin from .models import Animal @admin.register(Animal) class AnimalAdmin(admin.ModelAdmin): list_display = ('name', 'sound') ``` This will display the plugin and its model in the admin UI. Staff users can create, change, and delete model instances via the admin UI without needing to create a custom view. ![NetBox plugin in the admin UI](../media/plugins/plugin_admin_ui.png) ## Views If your plugin needs its own page or pages in the NetBox web UI, you'll need to define views. A view is a particular page tied to a URL within NetBox, which renders content using a template. Views are typically defined in `views.py`, and URL patterns in `urls.py`. As an example, let's write a view which displays a random animal and the sound it makes. First, we'll create the view in `views.py`: ```python from django.shortcuts import render from django.views.generic import View from .models import Animal class RandomAnimalView(View): """ Display a randomly-selected animal. """ def get(self, request): animal = Animal.objects.order_by('?').first() return render(request, 'netbox_animal_sounds/animal.html', { 'animal': animal, }) ``` This view retrieves a random animal from the database and and passes it as a context variable when rendering a template named `animal.html`, which doesn't exist yet. To create this template, first create a directory named `templates/netbox_animal_sounds/` within the plugin source directory. (We use the plugin's name as a subdirectory to guard against naming collisions with other plugins.) Then, create a template named `animal.html` as described below. ### Extending the Base Template NetBox provides a base template to ensure a consistent user experience, which plugins can extend with their own content. This template includes four content blocks: * `title` - The page title * `header` - The upper portion of the page * `content` - The main page body * `javascript` - A section at the end of the page for including Javascript code For more information on how template blocks work, consult the [Django documentation](https://docs.djangoproject.com/en/stable/ref/templates/builtins/#block). ```jinja2 {% extends 'base/layout.html' %} {% block content %} {% with config=settings.PLUGINS_CONFIG.netbox_animal_sounds %}