Просмотр исходного кода

Merge pull request #4473 from netbox-community/develop-2.8

Merge v2.8 work into develop
Jeremy Stretch 5 лет назад
Родитель
Сommit
ee51dae73f
100 измененных файлов с 2768 добавлено и 860 удалено
  1. 10 9
      .github/ISSUE_TEMPLATE/bug_report.md
  2. 1 2
      .travis.yml
  3. 1 2
      base_requirements.txt
  4. 1 1
      docs/additional-features/custom-scripts.md
  5. 2 2
      docs/administration/netbox-shell.md
  6. 25 51
      docs/api/overview.md
  7. 89 0
      docs/configuration/optional-settings.md
  8. 55 0
      docs/development/application-registry.md
  9. 1 1
      docs/index.md
  10. BIN
      docs/media/plugins/plugin_admin_ui.png
  11. BIN
      docs/media/plugins/plugin_rest_api_endpoint.png
  12. 392 0
      docs/plugins/development.md
  13. 82 0
      docs/plugins/index.md
  14. 58 0
      docs/release-notes/version-2.8.md
  15. 5 0
      mkdocs.yml
  16. 0 3
      netbox/circuits/api/urls.py
  17. 1 12
      netbox/circuits/api/views.py
  18. 1 9
      netbox/circuits/filters.py
  19. 28 0
      netbox/circuits/migrations/0008_standardize_description.py
  20. 3 3
      netbox/circuits/models.py
  21. 1 14
      netbox/circuits/tests/test_api.py
  22. 2 11
      netbox/circuits/tests/test_filters.py
  23. 7 4
      netbox/dcim/api/serializers.py
  24. 0 3
      netbox/dcim/api/urls.py
  25. 1 57
      netbox/dcim/api/views.py
  26. 37 48
      netbox/dcim/filters.py
  27. 33 7
      netbox/dcim/forms.py
  28. 28 0
      netbox/dcim/migrations/0100_mptt_remove_indexes.py
  29. 43 0
      netbox/dcim/migrations/0101_nested_rackgroups.py
  30. 21 0
      netbox/dcim/migrations/0102_nested_rackgroups_rebuild.py
  31. 98 0
      netbox/dcim/migrations/0103_standardize_description.py
  32. 56 9
      netbox/dcim/models/__init__.py
  33. 1 1
      netbox/dcim/models/device_components.py
  34. 15 8
      netbox/dcim/tables.py
  35. 20 92
      netbox/dcim/tests/test_api.py
  36. 72 56
      netbox/dcim/tests/test_filters.py
  37. 28 20
      netbox/dcim/tests/test_views.py
  38. 7 1
      netbox/dcim/views.py
  39. 4 4
      netbox/extras/api/customfields.py
  40. 1 1
      netbox/extras/api/serializers.py
  41. 0 3
      netbox/extras/api/urls.py
  42. 1 13
      netbox/extras/api/views.py
  43. 8 4
      netbox/extras/forms.py
  44. 23 0
      netbox/extras/migrations/0040_standardize_description.py
  45. 23 0
      netbox/extras/migrations/0041_tag_description.py
  46. 4 6
      netbox/extras/models.py
  47. 250 0
      netbox/extras/plugins/__init__.py
  48. 42 0
      netbox/extras/plugins/urls.py
  49. 93 0
      netbox/extras/plugins/views.py
  50. 15 0
      netbox/extras/reports.py
  51. 13 0
      netbox/extras/scripts.py
  52. 1 1
      netbox/extras/tables.py
  53. 73 0
      netbox/extras/templatetags/plugins.py
  54. 17 0
      netbox/extras/tests/dummy_plugin/__init__.py
  55. 9 0
      netbox/extras/tests/dummy_plugin/admin.py
  56. 9 0
      netbox/extras/tests/dummy_plugin/api/serializers.py
  57. 6 0
      netbox/extras/tests/dummy_plugin/api/urls.py
  58. 8 0
      netbox/extras/tests/dummy_plugin/api/views.py
  59. 7 0
      netbox/extras/tests/dummy_plugin/middleware.py
  60. 23 0
      netbox/extras/tests/dummy_plugin/migrations/0001_initial.py
  61. 0 0
      netbox/extras/tests/dummy_plugin/migrations/__init__.py
  62. 13 0
      netbox/extras/tests/dummy_plugin/models.py
  63. 25 0
      netbox/extras/tests/dummy_plugin/navigation.py
  64. 20 0
      netbox/extras/tests/dummy_plugin/template_content.py
  65. 8 0
      netbox/extras/tests/dummy_plugin/urls.py
  66. 11 0
      netbox/extras/tests/dummy_plugin/views.py
  67. 5 25
      netbox/extras/tests/test_api.py
  68. 2 2
      netbox/extras/tests/test_filters.py
  69. 136 0
      netbox/extras/tests/test_plugins.py
  70. 1 1
      netbox/extras/views.py
  71. 2 2
      netbox/ipam/api/serializers.py
  72. 0 3
      netbox/ipam/api/urls.py
  73. 1 15
      netbox/ipam/api/views.py
  74. 2 0
      netbox/ipam/fields.py
  75. 19 31
      netbox/ipam/filters.py
  76. 2 2
      netbox/ipam/forms.py
  77. 8 3
      netbox/ipam/lookups.py
  78. 1 1
      netbox/ipam/managers.py
  79. 38 0
      netbox/ipam/migrations/0035_drop_ip_family.py
  80. 58 0
      netbox/ipam/migrations/0036_standardize_description.py
  81. 45 41
      netbox/ipam/models.py
  82. 2 2
      netbox/ipam/tables.py
  83. 1 26
      netbox/ipam/tests/test_api.py
  84. 51 65
      netbox/ipam/tests/test_filters.py
  85. 47 48
      netbox/ipam/tests/test_models.py
  86. 81 81
      netbox/ipam/tests/test_ordering.py
  87. 19 18
      netbox/ipam/tests/test_views.py
  88. 1 1
      netbox/ipam/views.py
  89. 1 0
      netbox/netbox/admin.py
  90. 26 0
      netbox/netbox/configuration.example.py
  91. 40 0
      netbox/netbox/configuration.testing.py
  92. 62 14
      netbox/netbox/settings.py
  93. 159 0
      netbox/netbox/tests/test_authentication.py
  94. 6 0
      netbox/netbox/urls.py
  95. 1 0
      netbox/netbox/views.py
  96. 7 1
      netbox/project-static/css/base.css
  97. 11 0
      netbox/secrets/__init__.py
  98. 0 8
      netbox/secrets/api/serializers.py
  99. 0 3
      netbox/secrets/api/urls.py
  100. 1 9
      netbox/secrets/api/views.py

+ 10 - 9
.github/ISSUE_TEMPLATE/bug_report.md

@@ -15,22 +15,23 @@ about: Report a reproducible bug in the current release of NetBox
 
 
     Please describe the environment in which you are running NetBox. Be sure
     Please describe the environment in which you are running NetBox. Be sure
     that you are running an unmodified instance of the latest stable release
     that you are running an unmodified instance of the latest stable release
-    before submitting a bug report.
+    before submitting a bug report, and that any plugins have been disabled.
 -->
 -->
 ### Environment
 ### Environment
-* Python version:  <!-- Example: 3.6.9 -->
-* NetBox version:  <!-- Example: 2.7.3 -->
+* Python version: 
+* NetBox version: 
 
 
 <!--
 <!--
     Describe in detail the exact steps that someone else can take to reproduce
     Describe in detail the exact steps that someone else can take to reproduce
-    this bug using the current stable release of NetBox (or the current beta
-    release where applicable). Begin with the creation of any necessary
-    database objects and call out every operation being performed explicitly.
-    If reporting a bug in the REST API, be sure to reconstruct the raw HTTP
-    request(s) being made: Don't rely on a wrapper like pynetbox.
+    this bug using the current stable release of NetBox. Begin with the
+    creation of any necessary database objects and call out every operation
+    being performed explicitly. If reporting a bug in the REST API, be sure to
+    reconstruct the raw HTTP request(s) being made: Don't rely on a client
+    library such as pynetbox.
 -->
 -->
 ### Steps to Reproduce
 ### Steps to Reproduce
-1.
+1. Disable any installed plugins by commenting out the `PLUGINS` setting in
+   `configuration.py`.
 2.
 2.
 3.
 3.
 
 

+ 1 - 2
.travis.yml

@@ -3,10 +3,9 @@ services:
   - postgresql
   - postgresql
   - redis-server
   - redis-server
 addons:
 addons:
-  postgresql: "9.4"
+  postgresql: "9.6"
 language: python
 language: python
 python:
 python:
-  - "3.5"
   - "3.6"
   - "3.6"
   - "3.7"
   - "3.7"
 install:
 install:

+ 1 - 2
base_requirements.txt

@@ -68,8 +68,7 @@ Jinja2
 
 
 # Simple markup language for rendering HTML
 # Simple markup language for rendering HTML
 # https://github.com/Python-Markdown/markdown
 # https://github.com/Python-Markdown/markdown
-# py-gfm requires Markdown<3.0
-Markdown<3.0
+Markdown
 
 
 # Library for manipulating IP prefixes and addresses
 # Library for manipulating IP prefixes and addresses
 # https://github.com/drkjam/netaddr
 # https://github.com/drkjam/netaddr

+ 1 - 1
docs/additional-features/custom-scripts.md

@@ -63,7 +63,7 @@ A human-friendly description of what your script does.
 
 
 ### `field_order`
 ### `field_order`
 
 
-A list of field names indicating the order in which the form fields should appear. This is optional, however on Python 3.5 and earlier the fields will appear in random order. (Declarative ordering is preserved on Python 3.6 and above.) For example:
+A list of field names indicating the order in which the form fields should appear. This is optional, and should not be required on Python 3.6 and above. For example:
 
 
 ```
 ```
 field_order = ['var1', 'var2', 'var3']
 field_order = ['var1', 'var2', 'var3']

+ 2 - 2
docs/administration/netbox-shell.md

@@ -10,8 +10,8 @@ This will launch a customized version of [the built-in Django shell](https://doc
 
 
 ```
 ```
 $ ./manage.py nbshell
 $ ./manage.py nbshell
-### NetBox interactive shell (jstretch-laptop)
-### Python 3.5.2 | Django 2.0.8 | NetBox 2.4.3
+### NetBox interactive shell (localhost)
+### Python 3.6.9 | Django 2.2.11 | NetBox 2.7.10
 ### lsmodels() will show available models. Use help(<model>) for more info.
 ### lsmodels() will show available models. Use help(<model>) for more info.
 ```
 ```
 
 

+ 25 - 51
docs/api/overview.md

@@ -187,37 +187,6 @@ GET /api/ipam/prefixes/13980/?brief=1
 
 
 The brief format is supported for both lists and individual objects.
 The brief format is supported for both lists and individual objects.
 
 
-### Static Choice Fields
-
-Some model fields, such as the `status` field in the above example, utilize static integers corresponding to static choices. The available choices can be retrieved from the read-only `_choices` endpoint within each app. A specific `model:field` tuple may optionally be specified in the URL.
-
-Each choice includes a human-friendly label and its corresponding numeric value. For example, `GET /api/ipam/_choices/prefix:status/` will return:
-
-```
-[
-    {
-        "value": 0,
-        "label": "Container"
-    },
-    {
-        "value": 1,
-        "label": "Active"
-    },
-    {
-        "value": 2,
-        "label": "Reserved"
-    },
-    {
-        "value": 3,
-        "label": "Deprecated"
-    }
-]
-```
-
-Thus, to set a prefix's status to "Reserved," it would be assigned the integer `2`.
-
-A request for `GET /api/ipam/_choices/` will return choices for _all_ fields belonging to models within the IPAM app.
-
 ## Pagination
 ## Pagination
 
 
 API responses which contain a list of objects (for example, a request to `/api/dcim/devices/`) will be paginated to avoid unnecessary overhead. The root JSON object will contain the following attributes:
 API responses which contain a list of objects (for example, a request to `/api/dcim/devices/`) will be paginated to avoid unnecessary overhead. The root JSON object will contain the following attributes:
@@ -280,27 +249,32 @@ A list of objects retrieved via the API can be filtered by passing one or more q
 GET /api/ipam/prefixes/?status=1
 GET /api/ipam/prefixes/?status=1
 ```
 ```
 
 
-The choices available for fixed choice fields such as `status` are exposed in the API under a special `_choices` endpoint for each NetBox app. For example, the available choices for `Prefix.status` are listed at `/api/ipam/_choices/` under the key `prefix:status`:
+The choices available for fixed choice fields such as `status` can be retrieved by sending an `OPTIONS` API request for the desired endpoint:
+
+```no-highlight
+$ curl -s -X OPTIONS \
+-H "Content-Type: application/json" \
+-H "Accept: application/json; indent=4" \
+http://localhost:8000/api/ipam/prefixes/ | jq ".actions.POST.status.choices"
+[
+  {
+    "value": "container",
+    "display_name": "Container"
+  },
+  {
+    "value": "active",
+    "display_name": "Active"
+  },
+  {
+    "value": "reserved",
+    "display_name": "Reserved"
+  },
+  {
+    "value": "deprecated",
+    "display_name": "Deprecated"
+  }
+]
 
 
-```
-"prefix:status": [
-    {
-        "label": "Container",
-        "value": 0
-    },
-    {
-        "label": "Active",
-        "value": 1
-    },
-    {
-        "label": "Reserved",
-        "value": 2
-    },
-    {
-        "label": "Deprecated",
-        "value": 3
-    }
-],
 ```
 ```
 
 
 For most fields, when a filter is passed multiple times, objects matching _any_ of the provided values will be returned. For example, `GET /api/dcim/sites/?name=Foo&name=Bar` will return all sites named "Foo" _or_ "Bar". The exception to this rule is ManyToManyFields which may have multiple values assigned. Tags are the most common example of a ManyToManyField. For example, `GET /api/dcim/sites/?tag=foo&tag=bar` will return only sites tagged with both "foo" _and_ "bar".
 For most fields, when a filter is passed multiple times, objects matching _any_ of the provided values will be returned. For example, `GET /api/dcim/sites/?name=Foo&name=Bar` will return all sites named "Foo" _or_ "Bar". The exception to this rule is ManyToManyFields which may have multiple values assigned. Tags are the most common example of a ManyToManyField. For example, `GET /api/dcim/sites/?tag=foo&tag=bar` will return only sites tagged with both "foo" _and_ "bar".

+ 89 - 0
docs/configuration/optional-settings.md

@@ -191,6 +191,14 @@ LOGGING = {
 }
 }
 ```
 ```
 
 
+### Available Loggers
+
+* `netbox.auth.*` - Authentication events
+* `netbox.api.views.*` - Views which handle business logic for the REST API
+* `netbox.reports.*` - Report execution (`module.name`)
+* `netbox.scripts.*` - Custom script execution (`module.name`)
+* `netbox.views.*` - Views which handle business logic for the web UI
+
 ---
 ---
 
 
 ## LOGIN_REQUIRED
 ## LOGIN_REQUIRED
@@ -291,6 +299,39 @@ Determine how many objects to display per page within each list of objects.
 
 
 ---
 ---
 
 
+## PLUGINS
+
+Default: Empty
+
+A list of installed [NetBox plugins](../../plugins/) to enable. Plugins will not take effect unless they are listed here.
+
+!!! warning
+    Plugins extend NetBox by allowing external code to run with the same access and privileges as NetBox itself. Only install plugins from trusted sources. The NetBox maintainers make absolutely no guarantees about the integrity or security of your installation with plugins enabled.
+
+---
+
+## PLUGINS_CONFIG
+
+Default: Empty
+
+This parameter holds configuration settings for individual NetBox plugins. It is defined as a dictionary, with each key using the name of an installed plugin. The specific parameters supported are unique to each plugin: Reference the plugin's documentation to determine the supported parameters. An example configuration is shown below:
+
+```python
+PLUGINS_CONFIG = {
+    'plugin1': {
+        'foo': 123,
+        'bar': True
+    },
+    'plugin2': {
+        'foo': 456,
+    },
+}
+```
+
+Note that a plugin must be listed in `PLUGINS` for its configuration to take effect.
+
+---
+
 ## PREFER_IPV4
 ## PREFER_IPV4
 
 
 Default: False
 Default: False
@@ -299,6 +340,54 @@ When determining the primary IP address for a device, IPv6 is preferred over IPv
 
 
 ---
 ---
 
 
+## REMOTE_AUTH_ENABLED
+
+Default: `False`
+
+NetBox can be configured to support remote user authentication by inferring user authentication from an HTTP header set by the HTTP reverse proxy (e.g. nginx or Apache). Set this to `True` to enable this functionality. (Local authenitcation will still take effect as a fallback.)
+
+---
+
+## REMOTE_AUTH_BACKEND
+
+Default: `'utilities.auth_backends.RemoteUserBackend'`
+
+Python path to the custom [Django authentication backend](https://docs.djangoproject.com/en/stable/topics/auth/customizing/) to use for external user authentication, if not using NetBox's built-in backend. (Requires `REMOTE_AUTH_ENABLED`.)
+
+---
+
+## REMOTE_AUTH_HEADER
+
+Default: `'HTTP_REMOTE_USER'`
+
+When remote user authentication is in use, this is the name of the HTTP header which informs NetBox of the currently authenticated user. (Requires `REMOTE_AUTH_ENABLED`.)
+
+---
+
+## REMOTE_AUTH_AUTO_CREATE_USER
+
+Default: `True`
+
+If true, NetBox will automatically create local accounts for users authenticated via a remote service. (Requires `REMOTE_AUTH_ENABLED`.)
+
+---
+
+## REMOTE_AUTH_DEFAULT_GROUPS
+
+Default: `[]` (Empty list)
+
+The list of groups to assign a new user account when created using remote authentication. (Requires `REMOTE_AUTH_ENABLED`.)
+
+---
+
+## REMOTE_AUTH_DEFAULT_PERMISSIONS
+
+Default: `[]` (Empty list)
+
+The list of permissions to assign a new user account when created using remote authentication. (Requires `REMOTE_AUTH_ENABLED`.)
+
+---
+
 ## RELEASE_CHECK_TIMEOUT
 ## RELEASE_CHECK_TIMEOUT
 
 
 Default: 86,400 (24 hours)
 Default: 86,400 (24 hours)

+ 55 - 0
docs/development/application-registry.md

@@ -0,0 +1,55 @@
+# Application Registry
+
+The registry is an in-memory data structure which houses various miscellaneous application-wide parameters, such as installed plugins. It is not exposed to the user and is not intended to be modified by any code outside of NetBox core.
+
+The registry behaves essentially like a Python dictionary, with the notable exception that once a store (key) has been declared, it cannot be deleted or overwritten. The value of a store can, however, me modified; e.g. by appending a value to a list. Store values generally do not change once the application has been initialized.
+
+## Stores
+
+### `model_features`
+
+A dictionary of particular features (e.g. custom fields) mapped to the NetBox models which support them, arranged by app. For example:
+
+```python
+{
+    'custom_fields': {
+        'circuits': ['provider', 'circuit'],
+        'dcim': ['site', 'rack', 'devicetype', ...],
+        ...
+    },
+    'webhooks': {
+        ...
+    },
+    ...
+}
+```
+
+### `plugin_menu_items`
+
+Navigation menu items provided by NetBox plugins. Each plugin is registered as a key with the list of menu items it provides. An example:
+
+```python
+{
+    'Plugin A': (
+        <MenuItem>, <MenuItem>, <MenuItem>,
+    ),
+    'Plugin B': (
+        <MenuItem>, <MenuItem>, <MenuItem>,
+    ),
+}
+```
+
+### `plugin_template_extensions`
+
+Plugin content that gets embedded into core NetBox templates. The store comprises NetBox models registered as dictionary keys, each pointing to a list of applicable template extension classes that exist. An example:
+
+```python
+{
+    'dcim.site': [
+        <TemplateExtension>, <TemplateExtension>, <TemplateExtension>,
+    ],
+    'dcim.rack': [
+        <TemplateExtension>, <TemplateExtension>,
+    ],
+}
+```

+ 1 - 1
docs/index.md

@@ -55,7 +55,7 @@ NetBox is built on the [Django](https://djangoproject.com/) Python framework and
 
 
 ## Supported Python Versions
 ## Supported Python Versions
 
 
-NetBox supports Python 3.5, 3.6, and 3.7 environments currently. Python 3.5 is scheduled to be unsupported in NetBox v2.8.
+NetBox supports Python 3.6 and 3.7 environments currently. (Support for Python 3.5 was removed in NetBox v2.8.)
 
 
 ## Getting Started
 ## Getting Started
 
 

BIN
docs/media/plugins/plugin_admin_ui.png


BIN
docs/media/plugins/plugin_rest_api_endpoint.png


+ 392 - 0
docs/plugins/development.md

@@ -0,0 +1,392 @@
+# 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.
+
+## 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
+plugin_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. 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.
+
+The plugin source directory contains all of 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.6/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,
+)
+```
+
+Many of these are self-explanatory, but for more information, see the [setuptools documentation](https://setuptools.readthedocs.io/en/latest/setuptools.html).
+
+### 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 |
+| `caching_config` | Plugin-specific cache configuration
+| `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`) |
+
+### 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 `animal.html`:
+
+```jinja2
+{% extends 'base.html' %}
+
+{% block content %}
+{% with config=settings.PLUGINS_CONFIG.netbox_animal_sounds %}
+<h2 class="text-center" style="margin-top: 200px">
+    {% if animal %}
+        The {{ animal.name|lower }} says
+        {% if config.loud %}
+            {{ animal.sound|upper }}!
+        {% else %}
+            {{ animal.sound }}
+        {% endif %}
+    {% else %}
+        No animals have been created yet!
+    {% endif %}
+</h2>
+{% endwith %}
+{% endblock %}
+
+```
+
+The first line of the template instructs Django to extend the NetBox base template and inject our custom content within its `content` block.
+
+!!! note
+    Django renders templates with its own custom [template language](https://docs.djangoproject.com/en/stable/topics/templates/#the-django-template-language). This is very similar to Jinja2, however there are some important differences to be aware of.
+
+Finally, to make the view accessible to users, we need to register a URL for it. We do this in `urls.py` by defining a `urlpatterns` variable containing a list of paths.
+
+```python
+from django.urls import path
+from . import views
+
+urlpatterns = [
+    path('random/', views.RandomAnimalView.as_view(), name='random_animal'),
+]
+```
+
+A URL pattern has three components:
+
+* `route` - The unique portion of the URL dedicated to this view
+* `view` - The view itself
+* `name` - A short name used to identify the URL path internally
+
+This makes our view accessible at the URL `/plugins/animal-sounds/random/`. (Remember, our `AnimalSoundsConfig` class sets our plugin's base URL to `animal-sounds`.) Viewing this URL should show the base NetBox template with our custom content inside it.
+
+## REST API Endpoints
+
+Plugins can declare custom endpoints on NetBox's REST API to retrieve or manipulate models or other data. These behave very similarly to views, except that instead of rendering arbitrary content using a template, data is returned in JSON format using a serializer. NetBox uses the [Django REST Framework](https://www.django-rest-framework.org/), which makes writing API serializers and views very simple.
+
+First, we'll create a serializer for our `Animal` model, in `api/serializers.py`:
+
+```python
+from rest_framework.serializers import ModelSerializer
+from netbox_animal_sounds.models import Animal
+
+class AnimalSerializer(ModelSerializer):
+
+    class Meta:
+        model = Animal
+        fields = ('id', 'name', 'sound')
+```
+
+Next, we'll create a generic API view set that allows basic CRUD (create, read, update, and delete) operations for Animal instances. This is defined in `api/views.py`:
+
+```python
+from rest_framework.viewsets import ModelViewSet
+from netbox_animal_sounds.models import Animal
+from .serializers import AnimalSerializer
+
+class AnimalViewSet(ModelViewSet):
+    queryset = Animal.objects.all()
+    serializer_class = AnimalSerializer
+```
+
+Finally, we'll register a URL for our endpoint in `api/urls.py`. This file **must** define a variable named `urlpatterns`.
+
+```python
+from rest_framework import routers
+from .views import AnimalViewSet
+
+router = routers.DefaultRouter()
+router.register('animals', AnimalViewSet)
+urlpatterns = router.urls
+```
+
+With these three components in place, we can request `/api/plugins/animal-sounds/animals/` to retrieve a list of all Animal objects defined.
+
+![NetBox REST API plugin endpoint](../media/plugins/plugin_rest_api_endpoint.png)
+
+!!! warning
+    This example is provided as a minimal reference implementation only. It does not address authentication, performance, or myriad other concerns that plugin authors should have.
+
+## Navigation Menu Items
+
+To make its views easily accessible to users, a plugin can inject items in NetBox's navigation menu under the "Plugins" header. Menu items are added by defining a list of PluginMenuItem instances. By default, this should be a variable named `menu_items` in the file `navigation.py`. An example is shown below.
+
+```python
+from extras.plugins import PluginMenuButton, PluginMenuItem
+from utilities.choices import ButtonColorChoices
+
+menu_items = (
+    PluginMenuItem(
+        link='plugins:netbox_animal_sounds:random_animal',
+        link_text='Random sound',
+        buttons=(
+            PluginMenuButton('home', 'Button A', 'fa fa-info', ButtonColorChoices.BLUE),
+            PluginMenuButton('home', 'Button B', 'fa fa-warning', ButtonColorChoices.GREEN),
+        )
+    ),
+)
+```
+
+A `PluginMenuItem` has the following attributes:
+
+* `link` - The name of the URL path to which this menu item links
+* `link_text` - The text presented to the user
+* `permissions` - A list of permissions required to display this link (optional)
+* `buttons` - An iterable of PluginMenuButton instances to display (optional)
+
+A `PluginMenuButton` has the following attributes:
+
+* `link` - The name of the URL path to which this button links
+* `title` - The tooltip text (displayed when the mouse hovers over the button)
+* `icon_class` - Button icon CSS class (NetBox currently supports [Font Awesome 4.7](https://fontawesome.com/v4.7.0/icons/))
+* `color` - One of the choices provided by `ButtonColorChoices` (optional)
+* `permissions` - A list of permissions required to display this button (optional)
+
+## Extending Core Templates
+
+Plugins can inject custom content into certain areas of the detail views of applicable models. This is accomplished by subclassing `PluginTemplateExtension`, designating a particular NetBox model, and defining the desired methods to render custom content. Four methods are available:
+
+* `left_page()` - Inject content on the left side of the page
+* `right_page()` - Inject content on the right side of the page
+* `full_width_page()` - Inject content across the entire bottom of the page
+* `buttons()` - Add buttons to the top of the page
+
+Additionally, a `render()` method is available for convenience. This method accepts the name of a template to render, and any additional context data you want to pass. Its use is optional, however.
+
+When a PluginTemplateExtension is instantiated, context data is assigned to `self.context`. Available data include:
+
+* `object` - The object being viewed
+* `request` - The current request
+* `settings` - Global NetBox settings
+* `config` - Plugin-specific configuration parameters
+
+For example, accessing `{{ request.user }}` within a template will return the current user.
+
+Declared subclasses should be gathered into a list or tuple for integration with NetBox. By default, NetBox looks for an iterable named `template_extensions` within a `template_content.py` file. (This can be overridden by setting `template_extensions` to a custom value on the plugin's PluginConfig.) An example is below.
+
+```python
+from extras.plugins import PluginTemplateExtension
+from .models import Animal
+
+class SiteAnimalCount(PluginTemplateExtension):
+    model = 'dcim.site'
+
+    def right_page(self):
+        return self.render('netbox_animal_sounds/inc/animal_count.html', extra_context={
+            'animal_count': Animal.objects.count(),
+        })
+
+template_extensions = [SiteAnimalCount]
+```
+
+## Caching Configuration
+
+By default, all query operations within a plugin are cached. To change this, define a caching configuration under the PluginConfig class' `caching_config` attribute. All configuration keys will be applied within the context of the plugin; there is no need to include the plugin name. An example configuration is below:
+
+```python
+class MyPluginConfig(PluginConfig):
+    ...
+    caching_config = {
+        'foo': {
+            'ops': 'get',
+            'timeout': 60 * 15,
+        },
+        '*': {
+            'ops': 'all',
+        }
+    }
+```
+
+To disable caching for your plugin entirely, set:
+
+```python
+caching_config = {
+    '*': None
+}
+```
+
+See the [django-cacheops](https://github.com/Suor/django-cacheops) documentation for more detail on configuring caching.

+ 82 - 0
docs/plugins/index.md

@@ -0,0 +1,82 @@
+# Plugins
+
+Plugins are packaged [Django](https://docs.djangoproject.com/) apps that can be installed alongside NetBox to provide custom functionality not present in the core application. Plugins can introduce their own models and views, but cannot interfere with existing components. A NetBox user may opt to install plugins provided by the community or build his or her own.
+
+Plugins are supported on NetBox v2.8 and later.
+
+## Capabilities
+
+The NetBox plugin architecture allows for the following:
+
+* **Add new data models.** A plugin can introduce one or more models to hold data. (A model is essentially a table in the SQL database.)
+* **Add new URLs and views.** Plugins can register URLs under the `/plugins` root path to provide browsable views for users.
+* **Add content to existing model templates.** A template content class can be used to inject custom HTML content within the view of a core NetBox model. This content can appear in the left side, right side, or bottom of the page.
+* **Add navigation menu items.** Each plugin can register new links in the navigation menu. Each link may have a set of buttons for specific actions, similar to the built-in navigation items.
+* **Add custom middleware.** Custom Django middleware can be registered by each plugin.
+* **Declare configuration parameters.** Each plugin can define required, optional, and default configuration parameters within its unique namespace. Plug configuration parameter are defined by the user under `PLUGINS_CONFIG` in `configuration.py`.
+* **Limit installation by NetBox version.** A plugin can specify a minimum and/or maximum NetBox version with which it is compatible.
+
+## Limitations
+
+Either by policy or by technical limitation, the interaction of plugins with NetBox core is restricted in certain ways. A plugin may not:
+
+* **Modify core models.** Plugins may not alter, remove, or override core NetBox models in any way. This rule is in place to ensure the integrity of the core data model.
+* **Register URLs outside the `/plugins` root.** All plugin URLs are restricted to this path to prevent path collisions with core or other plugins.
+* **Override core templates.** Plugins can inject additional content where supported, but may not manipulate or remove core content.
+* **Modify core settings.** A configuration registry is provided for plugins, however they cannot alter or delete the core configuration.
+* **Disable core components.** Plugins are not permitted to disable or hide core NetBox components.
+
+## Installing Plugins
+
+The instructions below detail the process for installing and enabling a NetBox plugin.
+
+### Install Package
+
+Download and install the plugin package per its installation instructions. Plugins published via PyPI are typically installed using pip. Be sure to install the plugin within NetBox's virtual environment.
+
+```no-highlight
+$ source /opt/netbox/venv/bin/activate
+(venv) $ pip install <package>
+```
+
+Alternatively, you may wish to install the plugin manually by running `python setup.py install`. If you are developing a plugin and want to install it only temporarily, run `python setup.py develop` instead.
+
+### Enable the Plugin
+
+In `configuration.py`, add the plugin's name to the `PLUGINS` list:
+
+```python
+PLUGINS = [
+    'plugin_name',
+]
+```
+
+### Configure Plugin
+
+If the plugin requires any configuration, define it in `configuration.py` under the `PLUGINS_CONFIG` parameter. The available configuration parameters should be detailed in the plugin's README file.
+
+```no-highlight
+PLUGINS_CONFIG = {
+    'plugin_name': {
+        'foo': 'bar',
+        'buzz': 'bazz'
+    }
+}
+```
+
+### Collect Static Files
+
+Plugins may package static files to be served directly by the HTTP front end. Ensure that these are copied to the static root directory with the `collectstatic` management command:
+
+```no-highlight
+(venv) $ cd /opt/netbox/netbox/
+(venv) $ python3 manage.py collectstatic
+```
+
+### Restart WSGI Service
+
+Restart the WSGI service to load the new plugin:
+
+```no-highlight
+# sudo systemctl restart netbox
+```

+ 58 - 0
docs/release-notes/version-2.8.md

@@ -0,0 +1,58 @@
+# NetBox v2.8
+
+## v2.8.0 (FUTURE)
+
+### New Features (Beta)
+
+This releases introduces two new features in beta status. While they are expected to be functional, their precise implementation is subject to change during the v2.8 release cycle. It is recommended to wait until NetBox v2.9 to deploy them in production.
+
+#### Remote Authentication Support ([#2328](https://github.com/netbox-community/netbox/issues/2328))
+
+Several new configuration parameters provide support for authenticating an incoming request based on the value of a specific HTTP header. This can be leveraged to employ remote authentication via an nginx or Apache plugin, directing NetBox to create and configure a local user account as needed. The configuration parameters are:
+
+* `REMOTE_AUTH_ENABLED` - Enables remote authentication (disabled by default)
+* `REMOTE_AUTH_HEADER` - The name of the HTTP header which conveys the username
+* `REMOTE_AUTH_AUTO_CREATE_USER` - Enables the automatic creation of new users (disabled by default)
+* `REMOTE_AUTH_DEFAULT_GROUPS` - A list of groups to assign newly created users
+* `REMOTE_AUTH_DEFAULT_PERMISSIONS` - A list of permissions to assign newly created users
+
+If further customization of remote authentication is desired (for instance, if you want to pass group/permission information via HTTP headers as well), NetBox allows you to inject a custom [Django authentication backend](https://docs.djangoproject.com/en/stable/topics/auth/customizing/) to retain full control over the authentication and configuration of remote users.
+
+#### Plugins ([#3351](https://github.com/netbox-community/netbox/issues/3351))
+
+This release introduces support for custom plugins, which can be used to extend NetBox's functionality beyond what the core product provides. For example, plugins can be used to:
+
+* Add new Django models
+* Provide new views with custom templates
+* Inject custom template into object views
+* Introduce new API endpoints
+* Add custom request/response middleware
+
+For NetBox plugins to be recognized, they must be installed and added by name to the `PLUGINS` configuration parameter. (Plugin support is disabled by default.) Plugins can be configured under the `PLUGINS_CONFIG` parameter. More information can be found the in the [plugins documentation](https://netbox.readthedocs.io/en/stable/plugins/).
+
+### Enhancements
+
+* [#1754](https://github.com/netbox-community/netbox/issues/1754) - Added support for nested rack groups
+* [#3939](https://github.com/netbox-community/netbox/issues/3939) - Added support for nested tenant groups
+* [#4078](https://github.com/netbox-community/netbox/issues/4078) - Standardized description fields across all models
+* [#4195](https://github.com/netbox-community/netbox/issues/4195) - Enabled application logging (see [logging configuration](../configuration/optional-settings.md#logging))
+
+### API Changes
+
+* The `_choices` API endpoints have been removed. Instead, use an `OPTIONS` request to a model's endpoint to view the available values for all fields. ([#3416](https://github.com/netbox-community/netbox/issues/3416))
+* The `id__in` filter has been removed. Use the format `?id=1&id=2` instead. ([#4313](https://github.com/netbox-community/netbox/issues/4313))
+* dcim.Manufacturer: Added a `description` field
+* dcim.Platform: Added a `description` field
+* dcim.Rack: The `/api/dcim/racks/<pk>/units/` endpoint has been replaced with `/api/dcim/racks/<pk>/elevation/`.
+* dcim.RackGroup: Added a `description` field
+* dcim.Region: Added a `description` field
+* extras.Tag: Renamed `comments` to `description`; truncated length to 200 characters; removed Markdown rendering
+* ipam.RIR: Added a `description` field
+* ipam.VLANGroup: Added a `description` field
+* tenancy.TenantGroup: Added a `description` field
+* virtualization.ClusterGroup: Added a `description` field
+* virtualization.ClusterType: Added a `description` field
+
+### Other Changes
+
+* [#4081](https://github.com/netbox-community/netbox/issues/4081) - The `family` field has been removed from the Aggregate, Prefix, and IPAddress models

+ 5 - 0
mkdocs.yml

@@ -54,6 +54,9 @@ nav:
         - Reports: 'additional-features/reports.md'
         - Reports: 'additional-features/reports.md'
         - Tags: 'additional-features/tags.md'
         - Tags: 'additional-features/tags.md'
         - Webhooks: 'additional-features/webhooks.md'
         - Webhooks: 'additional-features/webhooks.md'
+    - Plugins:
+        - Using Plugins: 'plugins/index.md'
+        - Developing Plugins: 'plugins/development.md'
     - Administration:
     - Administration:
         - Replicating NetBox: 'administration/replicating-netbox.md'
         - Replicating NetBox: 'administration/replicating-netbox.md'
         - NetBox Shell: 'administration/netbox-shell.md'
         - NetBox Shell: 'administration/netbox-shell.md'
@@ -68,9 +71,11 @@ nav:
         - Style Guide: 'development/style-guide.md'
         - Style Guide: 'development/style-guide.md'
         - Utility Views: 'development/utility-views.md'
         - Utility Views: 'development/utility-views.md'
         - Extending Models: 'development/extending-models.md'
         - Extending Models: 'development/extending-models.md'
+        - Application Registry: 'development/application-registry.md'
         - Release Checklist: 'development/release-checklist.md'
         - Release Checklist: 'development/release-checklist.md'
         - Squashing Migrations: 'development/squashing-migrations.md'
         - Squashing Migrations: 'development/squashing-migrations.md'
     - Release Notes:
     - Release Notes:
+        - Version 2.8: 'release-notes/version-2.8.md'
         - Version 2.7: 'release-notes/version-2.7.md'
         - Version 2.7: 'release-notes/version-2.7.md'
         - Version 2.6: 'release-notes/version-2.6.md'
         - Version 2.6: 'release-notes/version-2.6.md'
         - Version 2.5: 'release-notes/version-2.5.md'
         - Version 2.5: 'release-notes/version-2.5.md'

+ 0 - 3
netbox/circuits/api/urls.py

@@ -14,9 +14,6 @@ class CircuitsRootView(routers.APIRootView):
 router = routers.DefaultRouter()
 router = routers.DefaultRouter()
 router.APIRootView = CircuitsRootView
 router.APIRootView = CircuitsRootView
 
 
-# Field choices
-router.register('_choices', views.CircuitsFieldChoicesViewSet, basename='field-choice')
-
 # Providers
 # Providers
 router.register('providers', views.ProviderViewSet)
 router.register('providers', views.ProviderViewSet)
 
 

+ 1 - 12
netbox/circuits/api/views.py

@@ -8,21 +8,10 @@ from circuits.models import Provider, CircuitTermination, CircuitType, Circuit
 from extras.api.serializers import RenderedGraphSerializer
 from extras.api.serializers import RenderedGraphSerializer
 from extras.api.views import CustomFieldModelViewSet
 from extras.api.views import CustomFieldModelViewSet
 from extras.models import Graph
 from extras.models import Graph
-from utilities.api import FieldChoicesViewSet, ModelViewSet
+from utilities.api import ModelViewSet
 from . import serializers
 from . import serializers
 
 
 
 
-#
-# Field choices
-#
-
-class CircuitsFieldChoicesViewSet(FieldChoicesViewSet):
-    fields = (
-        (serializers.CircuitSerializer, ['status']),
-        (serializers.CircuitTerminationSerializer, ['term_side']),
-    )
-
-
 #
 #
 # Providers
 # Providers
 #
 #

+ 1 - 9
netbox/circuits/filters.py

@@ -5,7 +5,7 @@ from dcim.models import Region, Site
 from extras.filters import CustomFieldFilterSet, CreatedUpdatedFilterSet
 from extras.filters import CustomFieldFilterSet, CreatedUpdatedFilterSet
 from tenancy.filters import TenancyFilterSet
 from tenancy.filters import TenancyFilterSet
 from utilities.filters import (
 from utilities.filters import (
-    BaseFilterSet, NameSlugSearchFilterSet, NumericInFilter, TagFilter, TreeNodeMultipleChoiceFilter
+    BaseFilterSet, NameSlugSearchFilterSet, TagFilter, TreeNodeMultipleChoiceFilter
 )
 )
 from .choices import *
 from .choices import *
 from .models import Circuit, CircuitTermination, CircuitType, Provider
 from .models import Circuit, CircuitTermination, CircuitType, Provider
@@ -19,10 +19,6 @@ __all__ = (
 
 
 
 
 class ProviderFilterSet(BaseFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
 class ProviderFilterSet(BaseFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
-    id__in = NumericInFilter(
-        field_name='id',
-        lookup_expr='in'
-    )
     q = django_filters.CharFilter(
     q = django_filters.CharFilter(
         method='search',
         method='search',
         label='Search',
         label='Search',
@@ -77,10 +73,6 @@ class CircuitTypeFilterSet(BaseFilterSet, NameSlugSearchFilterSet):
 
 
 
 
 class CircuitFilterSet(BaseFilterSet, CustomFieldFilterSet, TenancyFilterSet, CreatedUpdatedFilterSet):
 class CircuitFilterSet(BaseFilterSet, CustomFieldFilterSet, TenancyFilterSet, CreatedUpdatedFilterSet):
-    id__in = NumericInFilter(
-        field_name='id',
-        lookup_expr='in'
-    )
     q = django_filters.CharFilter(
     q = django_filters.CharFilter(
         method='search',
         method='search',
         label='Search',
         label='Search',

+ 28 - 0
netbox/circuits/migrations/0008_standardize_description.py

@@ -0,0 +1,28 @@
+# Generated by Django 3.0.3 on 2020-03-13 20:27
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('circuits', '0007_circuit_add_description_squashed_0017_circuittype_description'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='circuit',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='circuittermination',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='circuittype',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+    ]

+ 3 - 3
netbox/circuits/models.py

@@ -110,7 +110,7 @@ class CircuitType(ChangeLoggedModel):
         unique=True
         unique=True
     )
     )
     description = models.CharField(
     description = models.CharField(
-        max_length=100,
+        max_length=200,
         blank=True,
         blank=True,
     )
     )
 
 
@@ -176,7 +176,7 @@ class Circuit(ChangeLoggedModel, CustomFieldModel):
         null=True,
         null=True,
         verbose_name='Commit rate (Kbps)')
         verbose_name='Commit rate (Kbps)')
     description = models.CharField(
     description = models.CharField(
-        max_length=100,
+        max_length=200,
         blank=True
         blank=True
     )
     )
     comments = models.TextField(
     comments = models.TextField(
@@ -295,7 +295,7 @@ class CircuitTermination(CableTermination):
         verbose_name='Patch panel/port(s)'
         verbose_name='Patch panel/port(s)'
     )
     )
     description = models.CharField(
     description = models.CharField(
-        max_length=100,
+        max_length=200,
         blank=True
         blank=True
     )
     )
 
 

+ 1 - 14
netbox/circuits/tests/test_api.py

@@ -6,7 +6,7 @@ from circuits.choices import *
 from circuits.models import Circuit, CircuitTermination, CircuitType, Provider
 from circuits.models import Circuit, CircuitTermination, CircuitType, Provider
 from dcim.models import Site
 from dcim.models import Site
 from extras.models import Graph
 from extras.models import Graph
-from utilities.testing import APITestCase, choices_to_dict
+from utilities.testing import APITestCase
 
 
 
 
 class AppTest(APITestCase):
 class AppTest(APITestCase):
@@ -18,19 +18,6 @@ class AppTest(APITestCase):
 
 
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response.status_code, 200)
 
 
-    def test_choices(self):
-
-        url = reverse('circuits-api:field-choice-list')
-        response = self.client.get(url, **self.header)
-
-        self.assertEqual(response.status_code, 200)
-
-        # Circuit
-        self.assertEqual(choices_to_dict(response.data.get('circuit:status')), CircuitStatusChoices.as_dict())
-
-        # CircuitTermination
-        self.assertEqual(choices_to_dict(response.data.get('circuit-termination:term_side')), CircuitTerminationSideChoices.as_dict())
-
 
 
 class ProviderTest(APITestCase):
 class ProviderTest(APITestCase):
 
 

+ 2 - 11
netbox/circuits/tests/test_filters.py

@@ -70,11 +70,6 @@ class ProviderTestCase(TestCase):
         params = {'account': ['1234', '2345']}
         params = {'account': ['1234', '2345']}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
 
-    def test_id__in(self):
-        id_list = self.queryset.values_list('id', flat=True)[:3]
-        params = {'id__in': ','.join([str(id) for id in id_list])}
-        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3)
-
     def test_site(self):
     def test_site(self):
         sites = Site.objects.all()[:2]
         sites = Site.objects.all()[:2]
         params = {'site_id': [sites[0].pk, sites[1].pk]}
         params = {'site_id': [sites[0].pk, sites[1].pk]}
@@ -144,7 +139,8 @@ class CircuitTestCase(TestCase):
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
         )
         )
-        TenantGroup.objects.bulk_create(tenant_groups)
+        for tenantgroup in tenant_groups:
+            tenantgroup.save()
 
 
         tenants = (
         tenants = (
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
@@ -194,11 +190,6 @@ class CircuitTestCase(TestCase):
         params = {'commit_rate': ['1000', '2000']}
         params = {'commit_rate': ['1000', '2000']}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
 
-    def test_id__in(self):
-        id_list = self.queryset.values_list('id', flat=True)[:3]
-        params = {'id__in': ','.join([str(id) for id in id_list])}
-        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3)
-
     def test_provider(self):
     def test_provider(self):
         provider = Provider.objects.first()
         provider = Provider.objects.first()
         params = {'provider_id': [provider.pk]}
         params = {'provider_id': [provider.pk]}

+ 7 - 4
netbox/dcim/api/serializers.py

@@ -64,7 +64,7 @@ class RegionSerializer(serializers.ModelSerializer):
 
 
     class Meta:
     class Meta:
         model = Region
         model = Region
-        fields = ['id', 'name', 'slug', 'parent', 'site_count']
+        fields = ['id', 'name', 'slug', 'parent', 'description', 'site_count']
 
 
 
 
 class SiteSerializer(TaggitSerializer, CustomFieldModelSerializer):
 class SiteSerializer(TaggitSerializer, CustomFieldModelSerializer):
@@ -96,11 +96,12 @@ class SiteSerializer(TaggitSerializer, CustomFieldModelSerializer):
 
 
 class RackGroupSerializer(ValidatedModelSerializer):
 class RackGroupSerializer(ValidatedModelSerializer):
     site = NestedSiteSerializer()
     site = NestedSiteSerializer()
+    parent = NestedRackGroupSerializer(required=False, allow_null=True)
     rack_count = serializers.IntegerField(read_only=True)
     rack_count = serializers.IntegerField(read_only=True)
 
 
     class Meta:
     class Meta:
         model = RackGroup
         model = RackGroup
-        fields = ['id', 'name', 'slug', 'site', 'rack_count']
+        fields = ['id', 'name', 'slug', 'site', 'parent', 'description', 'rack_count']
 
 
 
 
 class RackRoleSerializer(ValidatedModelSerializer):
 class RackRoleSerializer(ValidatedModelSerializer):
@@ -218,7 +219,9 @@ class ManufacturerSerializer(ValidatedModelSerializer):
 
 
     class Meta:
     class Meta:
         model = Manufacturer
         model = Manufacturer
-        fields = ['id', 'name', 'slug', 'devicetype_count', 'inventoryitem_count', 'platform_count']
+        fields = [
+            'id', 'name', 'slug', 'description', 'devicetype_count', 'inventoryitem_count', 'platform_count',
+        ]
 
 
 
 
 class DeviceTypeSerializer(TaggitSerializer, CustomFieldModelSerializer):
 class DeviceTypeSerializer(TaggitSerializer, CustomFieldModelSerializer):
@@ -355,7 +358,7 @@ class PlatformSerializer(ValidatedModelSerializer):
     class Meta:
     class Meta:
         model = Platform
         model = Platform
         fields = [
         fields = [
-            'id', 'name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'device_count',
+            'id', 'name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'description', 'device_count',
             'virtualmachine_count',
             'virtualmachine_count',
         ]
         ]
 
 

+ 0 - 3
netbox/dcim/api/urls.py

@@ -14,9 +14,6 @@ class DCIMRootView(routers.APIRootView):
 router = routers.DefaultRouter()
 router = routers.DefaultRouter()
 router.APIRootView = DCIMRootView
 router.APIRootView = DCIMRootView
 
 
-# Field choices
-router.register('_choices', views.DCIMFieldChoicesViewSet, basename='field-choice')
-
 # Sites
 # Sites
 router.register('regions', views.RegionViewSet)
 router.register('regions', views.RegionViewSet)
 router.register('sites', views.SiteViewSet)
 router.register('sites', views.SiteViewSet)

+ 1 - 57
netbox/dcim/api/views.py

@@ -26,7 +26,7 @@ from extras.api.views import CustomFieldModelViewSet
 from extras.models import Graph
 from extras.models import Graph
 from ipam.models import Prefix, VLAN
 from ipam.models import Prefix, VLAN
 from utilities.api import (
 from utilities.api import (
-    get_serializer_for_model, IsAuthenticatedOrLoginNotRequired, FieldChoicesViewSet, ModelViewSet, ServiceUnavailable,
+    get_serializer_for_model, IsAuthenticatedOrLoginNotRequired, ModelViewSet, ServiceUnavailable,
 )
 )
 from utilities.utils import get_subquery
 from utilities.utils import get_subquery
 from virtualization.models import VirtualMachine
 from virtualization.models import VirtualMachine
@@ -34,35 +34,6 @@ from . import serializers
 from .exceptions import MissingFilterException
 from .exceptions import MissingFilterException
 
 
 
 
-#
-# Field choices
-#
-
-class DCIMFieldChoicesViewSet(FieldChoicesViewSet):
-    fields = (
-        (serializers.CableSerializer, ['length_unit', 'status', 'termination_a_type', 'termination_b_type', 'type']),
-        (serializers.ConsolePortSerializer, ['type', 'connection_status']),
-        (serializers.ConsolePortTemplateSerializer, ['type']),
-        (serializers.ConsoleServerPortSerializer, ['type']),
-        (serializers.ConsoleServerPortTemplateSerializer, ['type']),
-        (serializers.DeviceSerializer, ['face', 'status']),
-        (serializers.DeviceTypeSerializer, ['subdevice_role']),
-        (serializers.FrontPortSerializer, ['type']),
-        (serializers.FrontPortTemplateSerializer, ['type']),
-        (serializers.InterfaceSerializer, ['type', 'mode']),
-        (serializers.InterfaceTemplateSerializer, ['type']),
-        (serializers.PowerFeedSerializer, ['phase', 'status', 'supply', 'type']),
-        (serializers.PowerOutletSerializer, ['type', 'feed_leg']),
-        (serializers.PowerOutletTemplateSerializer, ['type', 'feed_leg']),
-        (serializers.PowerPortSerializer, ['type', 'connection_status']),
-        (serializers.PowerPortTemplateSerializer, ['type']),
-        (serializers.RackSerializer, ['outer_unit', 'status', 'type', 'width']),
-        (serializers.RearPortSerializer, ['type']),
-        (serializers.RearPortTemplateSerializer, ['type']),
-        (serializers.SiteSerializer, ['status']),
-    )
-
-
 # Mixins
 # Mixins
 
 
 class CableTraceMixin(object):
 class CableTraceMixin(object):
@@ -176,33 +147,6 @@ class RackViewSet(CustomFieldModelViewSet):
     serializer_class = serializers.RackSerializer
     serializer_class = serializers.RackSerializer
     filterset_class = filters.RackFilterSet
     filterset_class = filters.RackFilterSet
 
 
-    @swagger_auto_schema(deprecated=True)
-    @action(detail=True)
-    def units(self, request, pk=None):
-        """
-        List rack units (by rack)
-        """
-        # TODO: Remove this action detail route in v2.8
-        rack = get_object_or_404(Rack, pk=pk)
-        face = request.GET.get('face', 'front')
-        exclude_pk = request.GET.get('exclude', None)
-        if exclude_pk is not None:
-            try:
-                exclude_pk = int(exclude_pk)
-            except ValueError:
-                exclude_pk = None
-        elevation = rack.get_rack_units(face, exclude_pk)
-
-        # Enable filtering rack units by ID
-        q = request.GET.get('q', None)
-        if q:
-            elevation = [u for u in elevation if q in str(u['id'])]
-
-        page = self.paginate_queryset(elevation)
-        if page is not None:
-            rack_units = serializers.RackUnitSerializer(page, many=True, context={'request': request})
-            return self.get_paginated_response(rack_units.data)
-
     @swagger_auto_schema(
     @swagger_auto_schema(
         responses={200: serializers.RackUnitSerializer(many=True)},
         responses={200: serializers.RackUnitSerializer(many=True)},
         query_serializer=serializers.RackElevationDetailFilterSerializer
         query_serializer=serializers.RackElevationDetailFilterSerializer

+ 37 - 48
netbox/dcim/filters.py

@@ -7,7 +7,7 @@ from tenancy.models import Tenant
 from utilities.constants import COLOR_CHOICES
 from utilities.constants import COLOR_CHOICES
 from utilities.filters import (
 from utilities.filters import (
     BaseFilterSet, MultiValueCharFilter, MultiValueMACAddressFilter, MultiValueNumberFilter,
     BaseFilterSet, MultiValueCharFilter, MultiValueMACAddressFilter, MultiValueNumberFilter,
-    NameSlugSearchFilterSet, NumericInFilter, TagFilter, TreeNodeMultipleChoiceFilter,
+    NameSlugSearchFilterSet, TagFilter, TreeNodeMultipleChoiceFilter,
 )
 )
 from virtualization.models import Cluster
 from virtualization.models import Cluster
 from .choices import *
 from .choices import *
@@ -74,14 +74,10 @@ class RegionFilterSet(BaseFilterSet, NameSlugSearchFilterSet):
 
 
     class Meta:
     class Meta:
         model = Region
         model = Region
-        fields = ['id', 'name', 'slug']
+        fields = ['id', 'name', 'slug', 'description']
 
 
 
 
 class SiteFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
 class SiteFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
-    id__in = NumericInFilter(
-        field_name='id',
-        lookup_expr='in'
-    )
     q = django_filters.CharFilter(
     q = django_filters.CharFilter(
         method='search',
         method='search',
         label='Search',
         label='Search',
@@ -157,10 +153,20 @@ class RackGroupFilterSet(BaseFilterSet, NameSlugSearchFilterSet):
         to_field_name='slug',
         to_field_name='slug',
         label='Site (slug)',
         label='Site (slug)',
     )
     )
+    parent_id = django_filters.ModelMultipleChoiceFilter(
+        queryset=RackGroup.objects.all(),
+        label='Rack group (ID)',
+    )
+    parent = django_filters.ModelMultipleChoiceFilter(
+        field_name='parent__slug',
+        queryset=RackGroup.objects.all(),
+        to_field_name='slug',
+        label='Rack group (slug)',
+    )
 
 
     class Meta:
     class Meta:
         model = RackGroup
         model = RackGroup
-        fields = ['id', 'name', 'slug']
+        fields = ['id', 'name', 'slug', 'description']
 
 
 
 
 class RackRoleFilterSet(BaseFilterSet, NameSlugSearchFilterSet):
 class RackRoleFilterSet(BaseFilterSet, NameSlugSearchFilterSet):
@@ -171,10 +177,6 @@ class RackRoleFilterSet(BaseFilterSet, NameSlugSearchFilterSet):
 
 
 
 
 class RackFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
 class RackFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
-    id__in = NumericInFilter(
-        field_name='id',
-        lookup_expr='in'
-    )
     q = django_filters.CharFilter(
     q = django_filters.CharFilter(
         method='search',
         method='search',
         label='Search',
         label='Search',
@@ -202,15 +204,18 @@ class RackFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, Creat
         to_field_name='slug',
         to_field_name='slug',
         label='Site (slug)',
         label='Site (slug)',
     )
     )
-    group_id = django_filters.ModelMultipleChoiceFilter(
+    group_id = TreeNodeMultipleChoiceFilter(
         queryset=RackGroup.objects.all(),
         queryset=RackGroup.objects.all(),
-        label='Group (ID)',
+        field_name='group',
+        lookup_expr='in',
+        label='Rack group (ID)',
     )
     )
-    group = django_filters.ModelMultipleChoiceFilter(
-        field_name='group__slug',
+    group = TreeNodeMultipleChoiceFilter(
         queryset=RackGroup.objects.all(),
         queryset=RackGroup.objects.all(),
+        field_name='group',
+        lookup_expr='in',
         to_field_name='slug',
         to_field_name='slug',
-        label='Group',
+        label='Rack group (slug)',
     )
     )
     status = django_filters.MultipleChoiceFilter(
     status = django_filters.MultipleChoiceFilter(
         choices=RackStatusChoices,
         choices=RackStatusChoices,
@@ -251,10 +256,6 @@ class RackFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, Creat
 
 
 
 
 class RackReservationFilterSet(BaseFilterSet, TenancyFilterSet):
 class RackReservationFilterSet(BaseFilterSet, TenancyFilterSet):
-    id__in = NumericInFilter(
-        field_name='id',
-        lookup_expr='in'
-    )
     q = django_filters.CharFilter(
     q = django_filters.CharFilter(
         method='search',
         method='search',
         label='Search',
         label='Search',
@@ -274,16 +275,18 @@ class RackReservationFilterSet(BaseFilterSet, TenancyFilterSet):
         to_field_name='slug',
         to_field_name='slug',
         label='Site (slug)',
         label='Site (slug)',
     )
     )
-    group_id = django_filters.ModelMultipleChoiceFilter(
-        field_name='rack__group',
+    group_id = TreeNodeMultipleChoiceFilter(
         queryset=RackGroup.objects.all(),
         queryset=RackGroup.objects.all(),
-        label='Group (ID)',
+        field_name='rack__group',
+        lookup_expr='in',
+        label='Rack group (ID)',
     )
     )
-    group = django_filters.ModelMultipleChoiceFilter(
-        field_name='rack__group__slug',
+    group = TreeNodeMultipleChoiceFilter(
         queryset=RackGroup.objects.all(),
         queryset=RackGroup.objects.all(),
+        field_name='rack__group',
+        lookup_expr='in',
         to_field_name='slug',
         to_field_name='slug',
-        label='Group',
+        label='Rack group (slug)',
     )
     )
     user_id = django_filters.ModelMultipleChoiceFilter(
     user_id = django_filters.ModelMultipleChoiceFilter(
         queryset=User.objects.all(),
         queryset=User.objects.all(),
@@ -315,14 +318,10 @@ class ManufacturerFilterSet(BaseFilterSet, NameSlugSearchFilterSet):
 
 
     class Meta:
     class Meta:
         model = Manufacturer
         model = Manufacturer
-        fields = ['id', 'name', 'slug']
+        fields = ['id', 'name', 'slug', 'description']
 
 
 
 
 class DeviceTypeFilterSet(BaseFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
 class DeviceTypeFilterSet(BaseFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
-    id__in = NumericInFilter(
-        field_name='id',
-        lookup_expr='in'
-    )
     q = django_filters.CharFilter(
     q = django_filters.CharFilter(
         method='search',
         method='search',
         label='Search',
         label='Search',
@@ -494,7 +493,7 @@ class PlatformFilterSet(BaseFilterSet, NameSlugSearchFilterSet):
 
 
     class Meta:
     class Meta:
         model = Platform
         model = Platform
-        fields = ['id', 'name', 'slug', 'napalm_driver']
+        fields = ['id', 'name', 'slug', 'napalm_driver', 'description']
 
 
 
 
 class DeviceFilterSet(
 class DeviceFilterSet(
@@ -504,10 +503,6 @@ class DeviceFilterSet(
     CustomFieldFilterSet,
     CustomFieldFilterSet,
     CreatedUpdatedFilterSet
     CreatedUpdatedFilterSet
 ):
 ):
-    id__in = NumericInFilter(
-        field_name='id',
-        lookup_expr='in'
-    )
     q = django_filters.CharFilter(
     q = django_filters.CharFilter(
         method='search',
         method='search',
         label='Search',
         label='Search',
@@ -571,9 +566,10 @@ class DeviceFilterSet(
         to_field_name='slug',
         to_field_name='slug',
         label='Site name (slug)',
         label='Site name (slug)',
     )
     )
-    rack_group_id = django_filters.ModelMultipleChoiceFilter(
-        field_name='rack__group',
+    rack_group_id = TreeNodeMultipleChoiceFilter(
         queryset=RackGroup.objects.all(),
         queryset=RackGroup.objects.all(),
+        field_name='rack__group',
+        lookup_expr='in',
         label='Rack group (ID)',
         label='Rack group (ID)',
     )
     )
     rack_id = django_filters.ModelMultipleChoiceFilter(
     rack_id = django_filters.ModelMultipleChoiceFilter(
@@ -1236,10 +1232,6 @@ class InterfaceConnectionFilterSet(BaseFilterSet):
 
 
 
 
 class PowerPanelFilterSet(BaseFilterSet):
 class PowerPanelFilterSet(BaseFilterSet):
-    id__in = NumericInFilter(
-        field_name='id',
-        lookup_expr='in'
-    )
     q = django_filters.CharFilter(
     q = django_filters.CharFilter(
         method='search',
         method='search',
         label='Search',
         label='Search',
@@ -1267,9 +1259,10 @@ class PowerPanelFilterSet(BaseFilterSet):
         to_field_name='slug',
         to_field_name='slug',
         label='Site name (slug)',
         label='Site name (slug)',
     )
     )
-    rack_group_id = django_filters.ModelMultipleChoiceFilter(
-        field_name='rack_group',
+    rack_group_id = TreeNodeMultipleChoiceFilter(
         queryset=RackGroup.objects.all(),
         queryset=RackGroup.objects.all(),
+        field_name='rack_group',
+        lookup_expr='in',
         label='Rack group (ID)',
         label='Rack group (ID)',
     )
     )
 
 
@@ -1287,10 +1280,6 @@ class PowerPanelFilterSet(BaseFilterSet):
 
 
 
 
 class PowerFeedFilterSet(BaseFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
 class PowerFeedFilterSet(BaseFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
-    id__in = NumericInFilter(
-        field_name='id',
-        lookup_expr='in'
-    )
     q = django_filters.CharFilter(
     q = django_filters.CharFilter(
         method='search',
         method='search',
         label='Search',
         label='Search',

+ 33 - 7
netbox/dcim/forms.py

@@ -187,7 +187,7 @@ class RegionForm(BootstrapMixin, forms.ModelForm):
     class Meta:
     class Meta:
         model = Region
         model = Region
         fields = (
         fields = (
-            'parent', 'name', 'slug',
+            'parent', 'name', 'slug', 'description',
         )
         )
 
 
 
 
@@ -377,12 +377,16 @@ class RackGroupForm(BootstrapMixin, forms.ModelForm):
     site = DynamicModelChoiceField(
     site = DynamicModelChoiceField(
         queryset=Site.objects.all()
         queryset=Site.objects.all()
     )
     )
+    parent = DynamicModelChoiceField(
+        queryset=RackGroup.objects.all(),
+        required=False
+    )
     slug = SlugField()
     slug = SlugField()
 
 
     class Meta:
     class Meta:
         model = RackGroup
         model = RackGroup
         fields = (
         fields = (
-            'site', 'name', 'slug',
+            'site', 'parent', 'name', 'slug', 'description',
         )
         )
 
 
 
 
@@ -395,6 +399,15 @@ class RackGroupCSVForm(forms.ModelForm):
             'invalid_choice': 'Site not found.',
             'invalid_choice': 'Site not found.',
         }
         }
     )
     )
+    parent = forms.ModelChoiceField(
+        queryset=RackGroup.objects.all(),
+        required=False,
+        to_field_name='name',
+        help_text='Name of parent rack group',
+        error_messages={
+            'invalid_choice': 'Rack group not found.',
+        }
+    )
 
 
     class Meta:
     class Meta:
         model = RackGroup
         model = RackGroup
@@ -413,7 +426,8 @@ class RackGroupFilterForm(BootstrapMixin, forms.Form):
         widget=APISelectMultiple(
         widget=APISelectMultiple(
             value_field="slug",
             value_field="slug",
             filter_for={
             filter_for={
-                'site': 'region'
+                'site': 'region',
+                'parent': 'region',
             }
             }
         )
         )
     )
     )
@@ -423,6 +437,18 @@ class RackGroupFilterForm(BootstrapMixin, forms.Form):
         required=False,
         required=False,
         widget=APISelectMultiple(
         widget=APISelectMultiple(
             value_field="slug",
             value_field="slug",
+            filter_for={
+                'parent': 'site',
+            }
+        )
+    )
+    parent = DynamicModelMultipleChoiceField(
+        queryset=RackGroup.objects.all(),
+        to_field_name='slug',
+        required=False,
+        widget=APISelectMultiple(
+            api_url="/api/dcim/rack-groups/",
+            value_field="slug",
         )
         )
     )
     )
 
 
@@ -918,7 +944,7 @@ class ManufacturerForm(BootstrapMixin, forms.ModelForm):
     class Meta:
     class Meta:
         model = Manufacturer
         model = Manufacturer
         fields = [
         fields = [
-            'name', 'slug',
+            'name', 'slug', 'description',
         ]
         ]
 
 
 
 
@@ -1669,7 +1695,7 @@ class PlatformForm(BootstrapMixin, forms.ModelForm):
     class Meta:
     class Meta:
         model = Platform
         model = Platform
         fields = [
         fields = [
-            'name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args',
+            'name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'description',
         ]
         ]
         widgets = {
         widgets = {
             'napalm_args': SmallTextarea(),
             'napalm_args': SmallTextarea(),
@@ -1835,14 +1861,14 @@ class DeviceForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
 
 
                 # Collect interface IPs
                 # Collect interface IPs
                 interface_ips = IPAddress.objects.prefetch_related('interface').filter(
                 interface_ips = IPAddress.objects.prefetch_related('interface').filter(
-                    family=family, interface_id__in=interface_ids
+                    address__family=family, interface_id__in=interface_ids
                 )
                 )
                 if interface_ips:
                 if interface_ips:
                     ip_list = [(ip.id, '{} ({})'.format(ip.address, ip.interface)) for ip in interface_ips]
                     ip_list = [(ip.id, '{} ({})'.format(ip.address, ip.interface)) for ip in interface_ips]
                     ip_choices.append(('Interface IPs', ip_list))
                     ip_choices.append(('Interface IPs', ip_list))
                 # Collect NAT IPs
                 # Collect NAT IPs
                 nat_ips = IPAddress.objects.prefetch_related('nat_inside').filter(
                 nat_ips = IPAddress.objects.prefetch_related('nat_inside').filter(
-                    family=family, nat_inside__interface__in=interface_ids
+                    address__family=family, nat_inside__interface__in=interface_ids
                 )
                 )
                 if nat_ips:
                 if nat_ips:
                     ip_list = [(ip.id, '{} ({})'.format(ip.address, ip.nat_inside.address)) for ip in nat_ips]
                     ip_list = [(ip.id, '{} ({})'.format(ip.address, ip.nat_inside.address)) for ip in nat_ips]

+ 28 - 0
netbox/dcim/migrations/0100_mptt_remove_indexes.py

@@ -0,0 +1,28 @@
+# Generated by Django 3.0.3 on 2020-02-18 21:02
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('dcim', '0099_powerfeed_negative_voltage'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='region',
+            name='level',
+            field=models.PositiveIntegerField(editable=False),
+        ),
+        migrations.AlterField(
+            model_name='region',
+            name='lft',
+            field=models.PositiveIntegerField(editable=False),
+        ),
+        migrations.AlterField(
+            model_name='region',
+            name='rght',
+            field=models.PositiveIntegerField(editable=False),
+        ),
+    ]

+ 43 - 0
netbox/dcim/migrations/0101_nested_rackgroups.py

@@ -0,0 +1,43 @@
+from django.db import migrations, models
+import django.db.models.deletion
+import mptt.fields
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('dcim', '0100_mptt_remove_indexes'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='rackgroup',
+            name='parent',
+            field=mptt.fields.TreeForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='children', to='dcim.RackGroup'),
+        ),
+        migrations.AddField(
+            model_name='rackgroup',
+            name='level',
+            field=models.PositiveIntegerField(default=0, editable=False),
+            preserve_default=False,
+        ),
+        migrations.AddField(
+            model_name='rackgroup',
+            name='lft',
+            field=models.PositiveIntegerField(default=1, editable=False),
+            preserve_default=False,
+        ),
+        migrations.AddField(
+            model_name='rackgroup',
+            name='rght',
+            field=models.PositiveIntegerField(default=2, editable=False),
+            preserve_default=False,
+        ),
+        # tree_id will be set to a valid value during the following migration (which needs to be a separate migration)
+        migrations.AddField(
+            model_name='rackgroup',
+            name='tree_id',
+            field=models.PositiveIntegerField(db_index=True, default=0, editable=False),
+            preserve_default=False,
+        ),
+    ]

+ 21 - 0
netbox/dcim/migrations/0102_nested_rackgroups_rebuild.py

@@ -0,0 +1,21 @@
+from django.db import migrations
+
+
+def rebuild_mptt(apps, schema_editor):
+    RackGroup = apps.get_model('dcim', 'RackGroup')
+    for i, rackgroup in enumerate(RackGroup.objects.all(), start=1):
+        RackGroup.objects.filter(pk=rackgroup.pk).update(tree_id=i)
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('dcim', '0101_nested_rackgroups'),
+    ]
+
+    operations = [
+        migrations.RunPython(
+            code=rebuild_mptt,
+            reverse_code=migrations.RunPython.noop
+        ),
+    ]

+ 98 - 0
netbox/dcim/migrations/0103_standardize_description.py

@@ -0,0 +1,98 @@
+# Generated by Django 3.0.3 on 2020-03-13 20:27
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('dcim', '0102_nested_rackgroups_rebuild'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='manufacturer',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AddField(
+            model_name='platform',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AddField(
+            model_name='rackgroup',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AddField(
+            model_name='region',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='consoleport',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='consoleserverport',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='devicebay',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='devicerole',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='frontport',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='interface',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='inventoryitem',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='poweroutlet',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='powerport',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='rackreservation',
+            name='description',
+            field=models.CharField(max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='rackrole',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='rearport',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='site',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+    ]

+ 56 - 9
netbox/dcim/models/__init__.py

@@ -96,8 +96,12 @@ class Region(MPTTModel, ChangeLoggedModel):
     slug = models.SlugField(
     slug = models.SlugField(
         unique=True
         unique=True
     )
     )
+    description = models.CharField(
+        max_length=200,
+        blank=True
+    )
 
 
-    csv_headers = ['name', 'slug', 'parent']
+    csv_headers = ['name', 'slug', 'parent', 'description']
 
 
     class MPTTMeta:
     class MPTTMeta:
         order_insertion_by = ['name']
         order_insertion_by = ['name']
@@ -113,6 +117,7 @@ class Region(MPTTModel, ChangeLoggedModel):
             self.name,
             self.name,
             self.slug,
             self.slug,
             self.parent.name if self.parent else None,
             self.parent.name if self.parent else None,
+            self.description,
         )
         )
 
 
     def get_site_count(self):
     def get_site_count(self):
@@ -185,7 +190,7 @@ class Site(ChangeLoggedModel, CustomFieldModel):
         blank=True
         blank=True
     )
     )
     description = models.CharField(
     description = models.CharField(
-        max_length=100,
+        max_length=200,
         blank=True
         blank=True
     )
     )
     physical_address = models.CharField(
     physical_address = models.CharField(
@@ -287,7 +292,7 @@ class Site(ChangeLoggedModel, CustomFieldModel):
 #
 #
 
 
 @extras_features('export_templates')
 @extras_features('export_templates')
-class RackGroup(ChangeLoggedModel):
+class RackGroup(MPTTModel, ChangeLoggedModel):
     """
     """
     Racks can be grouped as subsets within a Site. The scope of a group will depend on how Sites are defined. For
     Racks can be grouped as subsets within a Site. The scope of a group will depend on how Sites are defined. For
     example, if a Site spans a corporate campus, a RackGroup might be defined to represent each building within that
     example, if a Site spans a corporate campus, a RackGroup might be defined to represent each building within that
@@ -302,8 +307,20 @@ class RackGroup(ChangeLoggedModel):
         on_delete=models.CASCADE,
         on_delete=models.CASCADE,
         related_name='rack_groups'
         related_name='rack_groups'
     )
     )
+    parent = TreeForeignKey(
+        to='self',
+        on_delete=models.CASCADE,
+        related_name='children',
+        blank=True,
+        null=True,
+        db_index=True
+    )
+    description = models.CharField(
+        max_length=200,
+        blank=True
+    )
 
 
-    csv_headers = ['site', 'name', 'slug']
+    csv_headers = ['site', 'parent', 'name', 'slug', 'description']
 
 
     class Meta:
     class Meta:
         ordering = ['site', 'name']
         ordering = ['site', 'name']
@@ -312,6 +329,9 @@ class RackGroup(ChangeLoggedModel):
             ['site', 'slug'],
             ['site', 'slug'],
         ]
         ]
 
 
+    class MPTTMeta:
+        order_insertion_by = ['name']
+
     def __str__(self):
     def __str__(self):
         return self.name
         return self.name
 
 
@@ -321,10 +341,27 @@ class RackGroup(ChangeLoggedModel):
     def to_csv(self):
     def to_csv(self):
         return (
         return (
             self.site,
             self.site,
+            self.parent.name if self.parent else '',
             self.name,
             self.name,
             self.slug,
             self.slug,
+            self.description,
+        )
+
+    def to_objectchange(self, action):
+        # Remove MPTT-internal fields
+        return ObjectChange(
+            changed_object=self,
+            object_repr=str(self),
+            action=action,
+            object_data=serialize_object(self, exclude=['level', 'lft', 'rght', 'tree_id'])
         )
         )
 
 
+    def clean(self):
+
+        # Parent RackGroup (if any) must belong to the same Site
+        if self.parent and self.parent.site != self.site:
+            raise ValidationError(f"Parent rack group ({self.parent}) must belong to the same site ({self.site})")
+
 
 
 class RackRole(ChangeLoggedModel):
 class RackRole(ChangeLoggedModel):
     """
     """
@@ -339,7 +376,7 @@ class RackRole(ChangeLoggedModel):
     )
     )
     color = ColorField()
     color = ColorField()
     description = models.CharField(
     description = models.CharField(
-        max_length=100,
+        max_length=200,
         blank=True,
         blank=True,
     )
     )
 
 
@@ -766,7 +803,7 @@ class RackReservation(ChangeLoggedModel):
         on_delete=models.PROTECT
         on_delete=models.PROTECT
     )
     )
     description = models.CharField(
     description = models.CharField(
-        max_length=100
+        max_length=200
     )
     )
 
 
     csv_headers = ['site', 'rack_group', 'rack', 'units', 'tenant', 'user', 'description']
     csv_headers = ['site', 'rack_group', 'rack', 'units', 'tenant', 'user', 'description']
@@ -843,8 +880,12 @@ class Manufacturer(ChangeLoggedModel):
     slug = models.SlugField(
     slug = models.SlugField(
         unique=True
         unique=True
     )
     )
+    description = models.CharField(
+        max_length=200,
+        blank=True
+    )
 
 
-    csv_headers = ['name', 'slug']
+    csv_headers = ['name', 'slug', 'description']
 
 
     class Meta:
     class Meta:
         ordering = ['name']
         ordering = ['name']
@@ -859,6 +900,7 @@ class Manufacturer(ChangeLoggedModel):
         return (
         return (
             self.name,
             self.name,
             self.slug,
             self.slug,
+            self.description
         )
         )
 
 
 
 
@@ -1128,7 +1170,7 @@ class DeviceRole(ChangeLoggedModel):
         help_text='Virtual machines may be assigned to this role'
         help_text='Virtual machines may be assigned to this role'
     )
     )
     description = models.CharField(
     description = models.CharField(
-        max_length=100,
+        max_length=200,
         blank=True,
         blank=True,
     )
     )
 
 
@@ -1184,8 +1226,12 @@ class Platform(ChangeLoggedModel):
         verbose_name='NAPALM arguments',
         verbose_name='NAPALM arguments',
         help_text='Additional arguments to pass when initiating the NAPALM driver (JSON format)'
         help_text='Additional arguments to pass when initiating the NAPALM driver (JSON format)'
     )
     )
+    description = models.CharField(
+        max_length=200,
+        blank=True
+    )
 
 
-    csv_headers = ['name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args']
+    csv_headers = ['name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'description']
 
 
     class Meta:
     class Meta:
         ordering = ['name']
         ordering = ['name']
@@ -1203,6 +1249,7 @@ class Platform(ChangeLoggedModel):
             self.manufacturer.name if self.manufacturer else None,
             self.manufacturer.name if self.manufacturer else None,
             self.napalm_driver,
             self.napalm_driver,
             self.napalm_args,
             self.napalm_args,
+            self.description,
         )
         )
 
 
 
 

+ 1 - 1
netbox/dcim/models/device_components.py

@@ -35,7 +35,7 @@ __all__ = (
 
 
 class ComponentModel(models.Model):
 class ComponentModel(models.Model):
     description = models.CharField(
     description = models.CharField(
-        max_length=100,
+        max_length=200,
         blank=True
         blank=True
     )
     )
 
 

+ 15 - 8
netbox/dcim/tables.py

@@ -11,13 +11,13 @@ from .models import (
     VirtualChassis,
     VirtualChassis,
 )
 )
 
 
-REGION_LINK = """
+MPTT_LINK = """
 {% if record.get_children %}
 {% if record.get_children %}
     <span style="padding-left: {{ record.get_ancestors|length }}0px "><i class="fa fa-caret-right"></i>
     <span style="padding-left: {{ record.get_ancestors|length }}0px "><i class="fa fa-caret-right"></i>
 {% else %}
 {% else %}
     <span style="padding-left: {{ record.get_ancestors|length }}9px">
     <span style="padding-left: {{ record.get_ancestors|length }}9px">
 {% endif %}
 {% endif %}
-    <a href="{% url 'dcim:site_list' %}?region={{ record.slug }}">{{ record.name }}</a>
+    <a href="{{ record.get_absolute_url }}">{{ record.name }}</a>
 </span>
 </span>
 """
 """
 
 
@@ -214,7 +214,7 @@ def get_component_template_actions(model_name):
 
 
 class RegionTable(BaseTable):
 class RegionTable(BaseTable):
     pk = ToggleColumn()
     pk = ToggleColumn()
-    name = tables.TemplateColumn(template_code=REGION_LINK, orderable=False)
+    name = tables.TemplateColumn(template_code=MPTT_LINK, orderable=False)
     site_count = tables.Column(verbose_name='Sites')
     site_count = tables.Column(verbose_name='Sites')
     slug = tables.Column(verbose_name='Slug')
     slug = tables.Column(verbose_name='Slug')
     actions = tables.TemplateColumn(
     actions = tables.TemplateColumn(
@@ -225,7 +225,7 @@ class RegionTable(BaseTable):
 
 
     class Meta(BaseTable.Meta):
     class Meta(BaseTable.Meta):
         model = Region
         model = Region
-        fields = ('pk', 'name', 'site_count', 'slug', 'actions')
+        fields = ('pk', 'name', 'site_count', 'description', 'slug', 'actions')
 
 
 
 
 #
 #
@@ -250,7 +250,10 @@ class SiteTable(BaseTable):
 
 
 class RackGroupTable(BaseTable):
 class RackGroupTable(BaseTable):
     pk = ToggleColumn()
     pk = ToggleColumn()
-    name = tables.LinkColumn()
+    name = tables.TemplateColumn(
+        template_code=MPTT_LINK,
+        orderable=False
+    )
     site = tables.LinkColumn(
     site = tables.LinkColumn(
         viewname='dcim:site',
         viewname='dcim:site',
         args=[Accessor('site.slug')],
         args=[Accessor('site.slug')],
@@ -268,7 +271,7 @@ class RackGroupTable(BaseTable):
 
 
     class Meta(BaseTable.Meta):
     class Meta(BaseTable.Meta):
         model = RackGroup
         model = RackGroup
-        fields = ('pk', 'name', 'site', 'rack_count', 'slug', 'actions')
+        fields = ('pk', 'name', 'site', 'rack_count', 'description', 'slug', 'actions')
 
 
 
 
 #
 #
@@ -397,7 +400,9 @@ class ManufacturerTable(BaseTable):
 
 
     class Meta(BaseTable.Meta):
     class Meta(BaseTable.Meta):
         model = Manufacturer
         model = Manufacturer
-        fields = ('pk', 'name', 'devicetype_count', 'inventoryitem_count', 'platform_count', 'slug', 'actions')
+        fields = (
+            'pk', 'name', 'devicetype_count', 'inventoryitem_count', 'platform_count', 'description', 'slug', 'actions',
+        )
 
 
 
 
 #
 #
@@ -673,7 +678,9 @@ class PlatformTable(BaseTable):
 
 
     class Meta(BaseTable.Meta):
     class Meta(BaseTable.Meta):
         model = Platform
         model = Platform
-        fields = ('pk', 'name', 'manufacturer', 'device_count', 'vm_count', 'slug', 'napalm_driver', 'actions')
+        fields = (
+            'pk', 'name', 'manufacturer', 'device_count', 'vm_count', 'slug', 'napalm_driver', 'description', 'actions',
+        )
 
 
 
 
 #
 #

+ 20 - 92
netbox/dcim/tests/test_api.py

@@ -4,7 +4,6 @@ from netaddr import IPNetwork
 from rest_framework import status
 from rest_framework import status
 
 
 from circuits.models import Circuit, CircuitTermination, CircuitType, Provider
 from circuits.models import Circuit, CircuitTermination, CircuitType, Provider
-from dcim.api import serializers
 from dcim.choices import *
 from dcim.choices import *
 from dcim.constants import *
 from dcim.constants import *
 from dcim.models import (
 from dcim.models import (
@@ -15,7 +14,7 @@ from dcim.models import (
 )
 )
 from ipam.models import IPAddress, VLAN
 from ipam.models import IPAddress, VLAN
 from extras.models import Graph
 from extras.models import Graph
-from utilities.testing import APITestCase, choices_to_dict
+from utilities.testing import APITestCase
 from virtualization.models import Cluster, ClusterType
 from virtualization.models import Cluster, ClusterType
 
 
 
 
@@ -28,79 +27,6 @@ class AppTest(APITestCase):
 
 
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response.status_code, 200)
 
 
-    def test_choices(self):
-
-        url = reverse('dcim-api:field-choice-list')
-        response = self.client.get(url, **self.header)
-
-        self.assertEqual(response.status_code, 200)
-
-        # Cable
-        self.assertEqual(choices_to_dict(response.data.get('cable:length_unit')), CableLengthUnitChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('cable:status')), CableStatusChoices.as_dict())
-        content_types = ContentType.objects.filter(CABLE_TERMINATION_MODELS)
-        cable_termination_choices = {
-            "{}.{}".format(ct.app_label, ct.model): ct.name for ct in content_types
-        }
-        self.assertEqual(choices_to_dict(response.data.get('cable:termination_a_type')), cable_termination_choices)
-        self.assertEqual(choices_to_dict(response.data.get('cable:termination_b_type')), cable_termination_choices)
-        self.assertEqual(choices_to_dict(response.data.get('cable:type')), CableTypeChoices.as_dict())
-
-        # Console ports
-        self.assertEqual(choices_to_dict(response.data.get('console-port:type')), ConsolePortTypeChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('console-port:connection_status')), dict(CONNECTION_STATUS_CHOICES))
-        self.assertEqual(choices_to_dict(response.data.get('console-port-template:type')), ConsolePortTypeChoices.as_dict())
-
-        # Console server ports
-        self.assertEqual(choices_to_dict(response.data.get('console-server-port:type')), ConsolePortTypeChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('console-server-port-template:type')), ConsolePortTypeChoices.as_dict())
-
-        # Device
-        self.assertEqual(choices_to_dict(response.data.get('device:face')), DeviceFaceChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('device:status')), DeviceStatusChoices.as_dict())
-
-        # Device type
-        self.assertEqual(choices_to_dict(response.data.get('device-type:subdevice_role')), SubdeviceRoleChoices.as_dict())
-
-        # Front ports
-        self.assertEqual(choices_to_dict(response.data.get('front-port:type')), PortTypeChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('front-port-template:type')), PortTypeChoices.as_dict())
-
-        # Interfaces
-        self.assertEqual(choices_to_dict(response.data.get('interface:type')), InterfaceTypeChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('interface:mode')), InterfaceModeChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('interface-template:type')), InterfaceTypeChoices.as_dict())
-
-        # Power feed
-        self.assertEqual(choices_to_dict(response.data.get('power-feed:phase')), PowerFeedPhaseChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('power-feed:status')), PowerFeedStatusChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('power-feed:supply')), PowerFeedSupplyChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('power-feed:type')), PowerFeedTypeChoices.as_dict())
-
-        # Power outlets
-        self.assertEqual(choices_to_dict(response.data.get('power-outlet:type')), PowerOutletTypeChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('power-outlet:feed_leg')), PowerOutletFeedLegChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('power-outlet-template:type')), PowerOutletTypeChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('power-outlet-template:feed_leg')), PowerOutletFeedLegChoices.as_dict())
-
-        # Power ports
-        self.assertEqual(choices_to_dict(response.data.get('power-port:type')), PowerPortTypeChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('power-port:connection_status')), dict(CONNECTION_STATUS_CHOICES))
-        self.assertEqual(choices_to_dict(response.data.get('power-port-template:type')), PowerPortTypeChoices.as_dict())
-
-        # Rack
-        self.assertEqual(choices_to_dict(response.data.get('rack:type')), RackTypeChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('rack:width')), RackWidthChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('rack:status')), RackStatusChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('rack:outer_unit')), RackDimensionUnitChoices.as_dict())
-
-        # Rear ports
-        self.assertEqual(choices_to_dict(response.data.get('rear-port:type')), PortTypeChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('rear-port-template:type')), PortTypeChoices.as_dict())
-
-        # Site
-        self.assertEqual(choices_to_dict(response.data.get('site:status')), SiteStatusChoices.as_dict())
-
 
 
 class RegionTest(APITestCase):
 class RegionTest(APITestCase):
 
 
@@ -350,9 +276,11 @@ class RackGroupTest(APITestCase):
 
 
         self.site1 = Site.objects.create(name='Test Site 1', slug='test-site-1')
         self.site1 = Site.objects.create(name='Test Site 1', slug='test-site-1')
         self.site2 = Site.objects.create(name='Test Site 2', slug='test-site-2')
         self.site2 = Site.objects.create(name='Test Site 2', slug='test-site-2')
-        self.rackgroup1 = RackGroup.objects.create(site=self.site1, name='Test Rack Group 1', slug='test-rack-group-1')
-        self.rackgroup2 = RackGroup.objects.create(site=self.site1, name='Test Rack Group 2', slug='test-rack-group-2')
-        self.rackgroup3 = RackGroup.objects.create(site=self.site1, name='Test Rack Group 3', slug='test-rack-group-3')
+        self.parent_rackgroup1 = RackGroup.objects.create(site=self.site1, name='Parent Rack Group 1', slug='parent-rack-group-1')
+        self.parent_rackgroup2 = RackGroup.objects.create(site=self.site2, name='Parent Rack Group 2', slug='parent-rack-group-2')
+        self.rackgroup1 = RackGroup.objects.create(site=self.site1, name='Rack Group 1', slug='rack-group-1', parent=self.parent_rackgroup1)
+        self.rackgroup2 = RackGroup.objects.create(site=self.site1, name='Rack Group 2', slug='rack-group-2', parent=self.parent_rackgroup1)
+        self.rackgroup3 = RackGroup.objects.create(site=self.site1, name='Rack Group 3', slug='rack-group-3', parent=self.parent_rackgroup1)
 
 
     def test_get_rackgroup(self):
     def test_get_rackgroup(self):
 
 
@@ -366,7 +294,7 @@ class RackGroupTest(APITestCase):
         url = reverse('dcim-api:rackgroup-list')
         url = reverse('dcim-api:rackgroup-list')
         response = self.client.get(url, **self.header)
         response = self.client.get(url, **self.header)
 
 
-        self.assertEqual(response.data['count'], 3)
+        self.assertEqual(response.data['count'], 5)
 
 
     def test_list_rackgroups_brief(self):
     def test_list_rackgroups_brief(self):
 
 
@@ -381,20 +309,22 @@ class RackGroupTest(APITestCase):
     def test_create_rackgroup(self):
     def test_create_rackgroup(self):
 
 
         data = {
         data = {
-            'name': 'Test Rack Group 4',
-            'slug': 'test-rack-group-4',
+            'name': 'Rack Group 4',
+            'slug': 'rack-group-4',
             'site': self.site1.pk,
             'site': self.site1.pk,
+            'parent': self.parent_rackgroup1.pk,
         }
         }
 
 
         url = reverse('dcim-api:rackgroup-list')
         url = reverse('dcim-api:rackgroup-list')
         response = self.client.post(url, data, format='json', **self.header)
         response = self.client.post(url, data, format='json', **self.header)
 
 
         self.assertHttpStatus(response, status.HTTP_201_CREATED)
         self.assertHttpStatus(response, status.HTTP_201_CREATED)
-        self.assertEqual(RackGroup.objects.count(), 4)
+        self.assertEqual(RackGroup.objects.count(), 6)
         rackgroup4 = RackGroup.objects.get(pk=response.data['id'])
         rackgroup4 = RackGroup.objects.get(pk=response.data['id'])
         self.assertEqual(rackgroup4.name, data['name'])
         self.assertEqual(rackgroup4.name, data['name'])
         self.assertEqual(rackgroup4.slug, data['slug'])
         self.assertEqual(rackgroup4.slug, data['slug'])
         self.assertEqual(rackgroup4.site_id, data['site'])
         self.assertEqual(rackgroup4.site_id, data['site'])
+        self.assertEqual(rackgroup4.parent_id, data['parent'])
 
 
     def test_create_rackgroup_bulk(self):
     def test_create_rackgroup_bulk(self):
 
 
@@ -403,16 +333,19 @@ class RackGroupTest(APITestCase):
                 'name': 'Test Rack Group 4',
                 'name': 'Test Rack Group 4',
                 'slug': 'test-rack-group-4',
                 'slug': 'test-rack-group-4',
                 'site': self.site1.pk,
                 'site': self.site1.pk,
+                'parent': self.parent_rackgroup1.pk,
             },
             },
             {
             {
                 'name': 'Test Rack Group 5',
                 'name': 'Test Rack Group 5',
                 'slug': 'test-rack-group-5',
                 'slug': 'test-rack-group-5',
                 'site': self.site1.pk,
                 'site': self.site1.pk,
+                'parent': self.parent_rackgroup1.pk,
             },
             },
             {
             {
                 'name': 'Test Rack Group 6',
                 'name': 'Test Rack Group 6',
                 'slug': 'test-rack-group-6',
                 'slug': 'test-rack-group-6',
                 'site': self.site1.pk,
                 'site': self.site1.pk,
+                'parent': self.parent_rackgroup1.pk,
             },
             },
         ]
         ]
 
 
@@ -420,7 +353,7 @@ class RackGroupTest(APITestCase):
         response = self.client.post(url, data, format='json', **self.header)
         response = self.client.post(url, data, format='json', **self.header)
 
 
         self.assertHttpStatus(response, status.HTTP_201_CREATED)
         self.assertHttpStatus(response, status.HTTP_201_CREATED)
-        self.assertEqual(RackGroup.objects.count(), 6)
+        self.assertEqual(RackGroup.objects.count(), 8)
         self.assertEqual(response.data[0]['name'], data[0]['name'])
         self.assertEqual(response.data[0]['name'], data[0]['name'])
         self.assertEqual(response.data[1]['name'], data[1]['name'])
         self.assertEqual(response.data[1]['name'], data[1]['name'])
         self.assertEqual(response.data[2]['name'], data[2]['name'])
         self.assertEqual(response.data[2]['name'], data[2]['name'])
@@ -431,17 +364,19 @@ class RackGroupTest(APITestCase):
             'name': 'Test Rack Group X',
             'name': 'Test Rack Group X',
             'slug': 'test-rack-group-x',
             'slug': 'test-rack-group-x',
             'site': self.site2.pk,
             'site': self.site2.pk,
+            'parent': self.parent_rackgroup2.pk,
         }
         }
 
 
         url = reverse('dcim-api:rackgroup-detail', kwargs={'pk': self.rackgroup1.pk})
         url = reverse('dcim-api:rackgroup-detail', kwargs={'pk': self.rackgroup1.pk})
         response = self.client.put(url, data, format='json', **self.header)
         response = self.client.put(url, data, format='json', **self.header)
 
 
         self.assertHttpStatus(response, status.HTTP_200_OK)
         self.assertHttpStatus(response, status.HTTP_200_OK)
-        self.assertEqual(RackGroup.objects.count(), 3)
+        self.assertEqual(RackGroup.objects.count(), 5)
         rackgroup1 = RackGroup.objects.get(pk=response.data['id'])
         rackgroup1 = RackGroup.objects.get(pk=response.data['id'])
         self.assertEqual(rackgroup1.name, data['name'])
         self.assertEqual(rackgroup1.name, data['name'])
         self.assertEqual(rackgroup1.slug, data['slug'])
         self.assertEqual(rackgroup1.slug, data['slug'])
         self.assertEqual(rackgroup1.site_id, data['site'])
         self.assertEqual(rackgroup1.site_id, data['site'])
+        self.assertEqual(rackgroup1.parent_id, data['parent'])
 
 
     def test_delete_rackgroup(self):
     def test_delete_rackgroup(self):
 
 
@@ -449,7 +384,7 @@ class RackGroupTest(APITestCase):
         response = self.client.delete(url, **self.header)
         response = self.client.delete(url, **self.header)
 
 
         self.assertHttpStatus(response, status.HTTP_204_NO_CONTENT)
         self.assertHttpStatus(response, status.HTTP_204_NO_CONTENT)
-        self.assertEqual(RackGroup.objects.count(), 2)
+        self.assertEqual(RackGroup.objects.count(), 4)
 
 
 
 
 class RackRoleTest(APITestCase):
 class RackRoleTest(APITestCase):
@@ -589,13 +524,6 @@ class RackTest(APITestCase):
 
 
         self.assertEqual(response.data['name'], self.rack1.name)
         self.assertEqual(response.data['name'], self.rack1.name)
 
 
-    def test_get_rack_units(self):
-
-        url = reverse('dcim-api:rack-units', kwargs={'pk': self.rack1.pk})
-        response = self.client.get(url, **self.header)
-
-        self.assertEqual(response.data['count'], 42)
-
     def test_get_elevation_rack_units(self):
     def test_get_elevation_rack_units(self):
 
 
         url = '{}?q=3'.format(reverse('dcim-api:rack-elevation', kwargs={'pk': self.rack1.pk}))
         url = '{}?q=3'.format(reverse('dcim-api:rack-elevation', kwargs={'pk': self.rack1.pk}))

+ 72 - 56
netbox/dcim/tests/test_filters.py

@@ -17,14 +17,15 @@ from virtualization.models import Cluster, ClusterType
 
 
 class RegionTestCase(TestCase):
 class RegionTestCase(TestCase):
     queryset = Region.objects.all()
     queryset = Region.objects.all()
+    filterset = RegionFilterSet
 
 
     @classmethod
     @classmethod
     def setUpTestData(cls):
     def setUpTestData(cls):
 
 
         regions = (
         regions = (
-            Region(name='Region 1', slug='region-1'),
-            Region(name='Region 2', slug='region-2'),
-            Region(name='Region 3', slug='region-3'),
+            Region(name='Region 1', slug='region-1', description='A'),
+            Region(name='Region 2', slug='region-2', description='B'),
+            Region(name='Region 3', slug='region-3', description='C'),
         )
         )
         for region in regions:
         for region in regions:
             region.save()
             region.save()
@@ -43,22 +44,26 @@ class RegionTestCase(TestCase):
     def test_id(self):
     def test_id(self):
         id_list = self.queryset.values_list('id', flat=True)[:2]
         id_list = self.queryset.values_list('id', flat=True)[:2]
         params = {'id': [str(id) for id in id_list]}
         params = {'id': [str(id) for id in id_list]}
-        self.assertEqual(RegionFilterSet(params, self.queryset).qs.count(), 2)
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
 
     def test_name(self):
     def test_name(self):
         params = {'name': ['Region 1', 'Region 2']}
         params = {'name': ['Region 1', 'Region 2']}
-        self.assertEqual(RegionFilterSet(params, self.queryset).qs.count(), 2)
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
 
     def test_slug(self):
     def test_slug(self):
         params = {'slug': ['region-1', 'region-2']}
         params = {'slug': ['region-1', 'region-2']}
-        self.assertEqual(RegionFilterSet(params, self.queryset).qs.count(), 2)
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
+
+    def test_description(self):
+        params = {'description': ['A', 'B']}
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
 
     def test_parent(self):
     def test_parent(self):
         parent_regions = Region.objects.filter(parent__isnull=True)[:2]
         parent_regions = Region.objects.filter(parent__isnull=True)[:2]
         params = {'parent_id': [parent_regions[0].pk, parent_regions[1].pk]}
         params = {'parent_id': [parent_regions[0].pk, parent_regions[1].pk]}
-        self.assertEqual(RegionFilterSet(params, self.queryset).qs.count(), 4)
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4)
         params = {'parent': [parent_regions[0].slug, parent_regions[1].slug]}
         params = {'parent': [parent_regions[0].slug, parent_regions[1].slug]}
-        self.assertEqual(RegionFilterSet(params, self.queryset).qs.count(), 4)
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4)
 
 
 
 
 class SiteTestCase(TestCase):
 class SiteTestCase(TestCase):
@@ -81,7 +86,8 @@ class SiteTestCase(TestCase):
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
         )
         )
-        TenantGroup.objects.bulk_create(tenant_groups)
+        for tenantgroup in tenant_groups:
+            tenantgroup.save()
 
 
         tenants = (
         tenants = (
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
@@ -138,11 +144,6 @@ class SiteTestCase(TestCase):
         params = {'contact_email': ['contact1@example.com', 'contact2@example.com']}
         params = {'contact_email': ['contact1@example.com', 'contact2@example.com']}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
 
-    def test_id__in(self):
-        id_list = self.queryset.values_list('id', flat=True)[:2]
-        params = {'id__in': ','.join([str(id) for id in id_list])}
-        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
-
     def test_status(self):
     def test_status(self):
         params = {'status': [SiteStatusChoices.STATUS_ACTIVE, SiteStatusChoices.STATUS_PLANNED]}
         params = {'status': [SiteStatusChoices.STATUS_ACTIVE, SiteStatusChoices.STATUS_PLANNED]}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
@@ -191,12 +192,21 @@ class RackGroupTestCase(TestCase):
         )
         )
         Site.objects.bulk_create(sites)
         Site.objects.bulk_create(sites)
 
 
+        parent_rack_groups = (
+            RackGroup(name='Parent Rack Group 1', slug='parent-rack-group-1', site=sites[0]),
+            RackGroup(name='Parent Rack Group 2', slug='parent-rack-group-2', site=sites[1]),
+            RackGroup(name='Parent Rack Group 3', slug='parent-rack-group-3', site=sites[2]),
+        )
+        for rackgroup in parent_rack_groups:
+            rackgroup.save()
+
         rack_groups = (
         rack_groups = (
-            RackGroup(name='Rack Group 1', slug='rack-group-1', site=sites[0]),
-            RackGroup(name='Rack Group 2', slug='rack-group-2', site=sites[1]),
-            RackGroup(name='Rack Group 3', slug='rack-group-3', site=sites[2]),
+            RackGroup(name='Rack Group 1', slug='rack-group-1', site=sites[0], parent=parent_rack_groups[0], description='A'),
+            RackGroup(name='Rack Group 2', slug='rack-group-2', site=sites[1], parent=parent_rack_groups[1], description='B'),
+            RackGroup(name='Rack Group 3', slug='rack-group-3', site=sites[2], parent=parent_rack_groups[2], description='C'),
         )
         )
-        RackGroup.objects.bulk_create(rack_groups)
+        for rackgroup in rack_groups:
+            rackgroup.save()
 
 
     def test_id(self):
     def test_id(self):
         id_list = self.queryset.values_list('id', flat=True)[:2]
         id_list = self.queryset.values_list('id', flat=True)[:2]
@@ -211,18 +221,29 @@ class RackGroupTestCase(TestCase):
         params = {'slug': ['rack-group-1', 'rack-group-2']}
         params = {'slug': ['rack-group-1', 'rack-group-2']}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
 
+    def test_description(self):
+        params = {'description': ['A', 'B']}
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
+
     def test_region(self):
     def test_region(self):
         regions = Region.objects.all()[:2]
         regions = Region.objects.all()[:2]
         params = {'region_id': [regions[0].pk, regions[1].pk]}
         params = {'region_id': [regions[0].pk, regions[1].pk]}
-        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4)
         params = {'region': [regions[0].slug, regions[1].slug]}
         params = {'region': [regions[0].slug, regions[1].slug]}
-        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4)
 
 
     def test_site(self):
     def test_site(self):
         sites = Site.objects.all()[:2]
         sites = Site.objects.all()[:2]
         params = {'site_id': [sites[0].pk, sites[1].pk]}
         params = {'site_id': [sites[0].pk, sites[1].pk]}
-        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4)
         params = {'site': [sites[0].slug, sites[1].slug]}
         params = {'site': [sites[0].slug, sites[1].slug]}
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4)
+
+    def test_parent(self):
+        parent_groups = RackGroup.objects.filter(name__startswith='Parent')[:2]
+        params = {'parent_id': [parent_groups[0].pk, parent_groups[1].pk]}
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
+        params = {'parent': [parent_groups[0].slug, parent_groups[1].slug]}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
 
 
 
@@ -285,7 +306,8 @@ class RackTestCase(TestCase):
             RackGroup(name='Rack Group 2', slug='rack-group-2', site=sites[1]),
             RackGroup(name='Rack Group 2', slug='rack-group-2', site=sites[1]),
             RackGroup(name='Rack Group 3', slug='rack-group-3', site=sites[2]),
             RackGroup(name='Rack Group 3', slug='rack-group-3', site=sites[2]),
         )
         )
-        RackGroup.objects.bulk_create(rack_groups)
+        for rackgroup in rack_groups:
+            rackgroup.save()
 
 
         rack_roles = (
         rack_roles = (
             RackRole(name='Rack Role 1', slug='rack-role-1'),
             RackRole(name='Rack Role 1', slug='rack-role-1'),
@@ -299,7 +321,8 @@ class RackTestCase(TestCase):
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
         )
         )
-        TenantGroup.objects.bulk_create(tenant_groups)
+        for tenantgroup in tenant_groups:
+            tenantgroup.save()
 
 
         tenants = (
         tenants = (
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
@@ -365,11 +388,6 @@ class RackTestCase(TestCase):
         params = {'outer_unit': RackDimensionUnitChoices.UNIT_MILLIMETER}
         params = {'outer_unit': RackDimensionUnitChoices.UNIT_MILLIMETER}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
 
-    def test_id__in(self):
-        id_list = self.queryset.values_list('id', flat=True)[:2]
-        params = {'id__in': ','.join([str(id) for id in id_list])}
-        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
-
     def test_region(self):
     def test_region(self):
         regions = Region.objects.all()[:2]
         regions = Region.objects.all()[:2]
         params = {'region_id': [regions[0].pk, regions[1].pk]}
         params = {'region_id': [regions[0].pk, regions[1].pk]}
@@ -442,7 +460,8 @@ class RackReservationTestCase(TestCase):
             RackGroup(name='Rack Group 2', slug='rack-group-2', site=sites[1]),
             RackGroup(name='Rack Group 2', slug='rack-group-2', site=sites[1]),
             RackGroup(name='Rack Group 3', slug='rack-group-3', site=sites[2]),
             RackGroup(name='Rack Group 3', slug='rack-group-3', site=sites[2]),
         )
         )
-        RackGroup.objects.bulk_create(rack_groups)
+        for rackgroup in rack_groups:
+            rackgroup.save()
 
 
         racks = (
         racks = (
             Rack(name='Rack 1', site=sites[0], group=rack_groups[0]),
             Rack(name='Rack 1', site=sites[0], group=rack_groups[0]),
@@ -463,7 +482,8 @@ class RackReservationTestCase(TestCase):
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
         )
         )
-        TenantGroup.objects.bulk_create(tenant_groups)
+        for tenantgroup in tenant_groups:
+            tenantgroup.save()
 
 
         tenants = (
         tenants = (
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
@@ -479,11 +499,6 @@ class RackReservationTestCase(TestCase):
         )
         )
         RackReservation.objects.bulk_create(reservations)
         RackReservation.objects.bulk_create(reservations)
 
 
-    def test_id__in(self):
-        id_list = self.queryset.values_list('id', flat=True)[:2]
-        params = {'id__in': ','.join([str(id) for id in id_list])}
-        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
-
     def test_site(self):
     def test_site(self):
         sites = Site.objects.all()[:2]
         sites = Site.objects.all()[:2]
         params = {'site_id': [sites[0].pk, sites[1].pk]}
         params = {'site_id': [sites[0].pk, sites[1].pk]}
@@ -529,9 +544,9 @@ class ManufacturerTestCase(TestCase):
     def setUpTestData(cls):
     def setUpTestData(cls):
 
 
         manufacturers = (
         manufacturers = (
-            Manufacturer(name='Manufacturer 1', slug='manufacturer-1'),
-            Manufacturer(name='Manufacturer 2', slug='manufacturer-2'),
-            Manufacturer(name='Manufacturer 3', slug='manufacturer-3'),
+            Manufacturer(name='Manufacturer 1', slug='manufacturer-1', description='A'),
+            Manufacturer(name='Manufacturer 2', slug='manufacturer-2', description='B'),
+            Manufacturer(name='Manufacturer 3', slug='manufacturer-3', description='C'),
         )
         )
         Manufacturer.objects.bulk_create(manufacturers)
         Manufacturer.objects.bulk_create(manufacturers)
 
 
@@ -548,6 +563,10 @@ class ManufacturerTestCase(TestCase):
         params = {'slug': ['manufacturer-1', 'manufacturer-2']}
         params = {'slug': ['manufacturer-1', 'manufacturer-2']}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
 
+    def test_description(self):
+        params = {'description': ['A', 'B']}
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
+
 
 
 class DeviceTypeTestCase(TestCase):
 class DeviceTypeTestCase(TestCase):
     queryset = DeviceType.objects.all()
     queryset = DeviceType.objects.all()
@@ -631,11 +650,6 @@ class DeviceTypeTestCase(TestCase):
         params = {'subdevice_role': SubdeviceRoleChoices.ROLE_PARENT}
         params = {'subdevice_role': SubdeviceRoleChoices.ROLE_PARENT}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)
 
 
-    def test_id__in(self):
-        id_list = self.queryset.values_list('id', flat=True)[:2]
-        params = {'id__in': ','.join([str(id) for id in id_list])}
-        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
-
     def test_manufacturer(self):
     def test_manufacturer(self):
         manufacturers = Manufacturer.objects.all()[:2]
         manufacturers = Manufacturer.objects.all()[:2]
         params = {'manufacturer_id': [manufacturers[0].pk, manufacturers[1].pk]}
         params = {'manufacturer_id': [manufacturers[0].pk, manufacturers[1].pk]}
@@ -1080,9 +1094,9 @@ class PlatformTestCase(TestCase):
         Manufacturer.objects.bulk_create(manufacturers)
         Manufacturer.objects.bulk_create(manufacturers)
 
 
         platforms = (
         platforms = (
-            Platform(name='Platform 1', slug='platform-1', manufacturer=manufacturers[0], napalm_driver='driver-1'),
-            Platform(name='Platform 2', slug='platform-2', manufacturer=manufacturers[1], napalm_driver='driver-2'),
-            Platform(name='Platform 3', slug='platform-3', manufacturer=manufacturers[2], napalm_driver='driver-3'),
+            Platform(name='Platform 1', slug='platform-1', manufacturer=manufacturers[0], napalm_driver='driver-1', description='A'),
+            Platform(name='Platform 2', slug='platform-2', manufacturer=manufacturers[1], napalm_driver='driver-2', description='B'),
+            Platform(name='Platform 3', slug='platform-3', manufacturer=manufacturers[2], napalm_driver='driver-3', description='C'),
         )
         )
         Platform.objects.bulk_create(platforms)
         Platform.objects.bulk_create(platforms)
 
 
@@ -1099,6 +1113,10 @@ class PlatformTestCase(TestCase):
         params = {'slug': ['platform-1', 'platform-2']}
         params = {'slug': ['platform-1', 'platform-2']}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
 
+    def test_description(self):
+        params = {'description': ['A', 'B']}
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
+
     def test_napalm_driver(self):
     def test_napalm_driver(self):
         params = {'napalm_driver': ['driver-1', 'driver-2']}
         params = {'napalm_driver': ['driver-1', 'driver-2']}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
@@ -1166,7 +1184,8 @@ class DeviceTestCase(TestCase):
             RackGroup(name='Rack Group 2', slug='rack-group-2', site=sites[1]),
             RackGroup(name='Rack Group 2', slug='rack-group-2', site=sites[1]),
             RackGroup(name='Rack Group 3', slug='rack-group-3', site=sites[2]),
             RackGroup(name='Rack Group 3', slug='rack-group-3', site=sites[2]),
         )
         )
-        RackGroup.objects.bulk_create(rack_groups)
+        for rackgroup in rack_groups:
+            rackgroup.save()
 
 
         racks = (
         racks = (
             Rack(name='Rack 1', site=sites[0], group=rack_groups[0]),
             Rack(name='Rack 1', site=sites[0], group=rack_groups[0]),
@@ -1188,7 +1207,8 @@ class DeviceTestCase(TestCase):
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
         )
         )
-        TenantGroup.objects.bulk_create(tenant_groups)
+        for tenantgroup in tenant_groups:
+            tenantgroup.save()
 
 
         tenants = (
         tenants = (
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
@@ -1242,8 +1262,8 @@ class DeviceTestCase(TestCase):
 
 
         # Assign primary IPs for filtering
         # Assign primary IPs for filtering
         ipaddresses = (
         ipaddresses = (
-            IPAddress(family=4, address='192.0.2.1/24', interface=interfaces[0]),
-            IPAddress(family=4, address='192.0.2.2/24', interface=interfaces[1]),
+            IPAddress(address='192.0.2.1/24', interface=interfaces[0]),
+            IPAddress(address='192.0.2.2/24', interface=interfaces[1]),
         )
         )
         IPAddress.objects.bulk_create(ipaddresses)
         IPAddress.objects.bulk_create(ipaddresses)
         Device.objects.filter(pk=devices[0].pk).update(primary_ip4=ipaddresses[0])
         Device.objects.filter(pk=devices[0].pk).update(primary_ip4=ipaddresses[0])
@@ -1283,11 +1303,6 @@ class DeviceTestCase(TestCase):
         params = {'vc_priority': [1, 2]}
         params = {'vc_priority': [1, 2]}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
 
-    def test_id__in(self):
-        id_list = self.queryset.values_list('id', flat=True)[:2]
-        params = {'id__in': ','.join([str(id) for id in id_list])}
-        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
-
     def test_manufacturer(self):
     def test_manufacturer(self):
         manufacturers = Manufacturer.objects.all()[:2]
         manufacturers = Manufacturer.objects.all()[:2]
         params = {'manufacturer_id': [manufacturers[0].pk, manufacturers[1].pk]}
         params = {'manufacturer_id': [manufacturers[0].pk, manufacturers[1].pk]}
@@ -2584,7 +2599,8 @@ class PowerPanelTestCase(TestCase):
             RackGroup(name='Rack Group 2', slug='rack-group-2', site=sites[1]),
             RackGroup(name='Rack Group 2', slug='rack-group-2', site=sites[1]),
             RackGroup(name='Rack Group 3', slug='rack-group-3', site=sites[2]),
             RackGroup(name='Rack Group 3', slug='rack-group-3', site=sites[2]),
         )
         )
-        RackGroup.objects.bulk_create(rack_groups)
+        for rackgroup in rack_groups:
+            rackgroup.save()
 
 
         power_panels = (
         power_panels = (
             PowerPanel(name='Power Panel 1', site=sites[0], rack_group=rack_groups[0]),
             PowerPanel(name='Power Panel 1', site=sites[0], rack_group=rack_groups[0]),

+ 28 - 20
netbox/dcim/tests/test_views.py

@@ -46,13 +46,14 @@ class RegionTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
             'name': 'Region X',
             'name': 'Region X',
             'slug': 'region-x',
             'slug': 'region-x',
             'parent': regions[2].pk,
             'parent': regions[2].pk,
+            'description': 'A new region',
         }
         }
 
 
         cls.csv_data = (
         cls.csv_data = (
-            "name,slug",
-            "Region 4,region-4",
-            "Region 5,region-5",
-            "Region 6,region-6",
+            "name,slug,description",
+            "Region 4,region-4,Fourth region",
+            "Region 5,region-5,Fifth region",
+            "Region 6,region-6,Sixth region",
         )
         )
 
 
 
 
@@ -122,23 +123,26 @@ class RackGroupTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
         site = Site(name='Site 1', slug='site-1')
         site = Site(name='Site 1', slug='site-1')
         site.save()
         site.save()
 
 
-        RackGroup.objects.bulk_create([
+        rack_groups = (
             RackGroup(name='Rack Group 1', slug='rack-group-1', site=site),
             RackGroup(name='Rack Group 1', slug='rack-group-1', site=site),
             RackGroup(name='Rack Group 2', slug='rack-group-2', site=site),
             RackGroup(name='Rack Group 2', slug='rack-group-2', site=site),
             RackGroup(name='Rack Group 3', slug='rack-group-3', site=site),
             RackGroup(name='Rack Group 3', slug='rack-group-3', site=site),
-        ])
+        )
+        for rackgroup in rack_groups:
+            rackgroup.save()
 
 
         cls.form_data = {
         cls.form_data = {
             'name': 'Rack Group X',
             'name': 'Rack Group X',
             'slug': 'rack-group-x',
             'slug': 'rack-group-x',
             'site': site.pk,
             'site': site.pk,
+            'description': 'A new rack group',
         }
         }
 
 
         cls.csv_data = (
         cls.csv_data = (
-            "site,name,slug",
-            "Site 1,Rack Group 4,rack-group-4",
-            "Site 1,Rack Group 5,rack-group-5",
-            "Site 1,Rack Group 6,rack-group-6",
+            "site,name,slug,description",
+            "Site 1,Rack Group 4,rack-group-4,Fourth rack group",
+            "Site 1,Rack Group 5,rack-group-5,Fifth rack group",
+            "Site 1,Rack Group 6,rack-group-6,Sixth rack group",
         )
         )
 
 
 
 
@@ -227,7 +231,8 @@ class RackTestCase(ViewTestCases.PrimaryObjectViewTestCase):
             RackGroup(name='Rack Group 1', slug='rack-group-1', site=sites[0]),
             RackGroup(name='Rack Group 1', slug='rack-group-1', site=sites[0]),
             RackGroup(name='Rack Group 2', slug='rack-group-2', site=sites[1])
             RackGroup(name='Rack Group 2', slug='rack-group-2', site=sites[1])
         )
         )
-        RackGroup.objects.bulk_create(rackgroups)
+        for rackgroup in rackgroups:
+            rackgroup.save()
 
 
         rackroles = (
         rackroles = (
             RackRole(name='Rack Role 1', slug='rack-role-1'),
             RackRole(name='Rack Role 1', slug='rack-role-1'),
@@ -302,13 +307,14 @@ class ManufacturerTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
         cls.form_data = {
         cls.form_data = {
             'name': 'Manufacturer X',
             'name': 'Manufacturer X',
             'slug': 'manufacturer-x',
             'slug': 'manufacturer-x',
+            'description': 'A new manufacturer',
         }
         }
 
 
         cls.csv_data = (
         cls.csv_data = (
-            "name,slug",
-            "Manufacturer 4,manufacturer-4",
-            "Manufacturer 5,manufacturer-5",
-            "Manufacturer 6,manufacturer-6",
+            "name,slug,description",
+            "Manufacturer 4,manufacturer-4,Fourth manufacturer",
+            "Manufacturer 5,manufacturer-5,Fifth manufacturer",
+            "Manufacturer 6,manufacturer-6,Sixth manufacturer",
         )
         )
 
 
 
 
@@ -861,13 +867,14 @@ class PlatformTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
             'manufacturer': manufacturer.pk,
             'manufacturer': manufacturer.pk,
             'napalm_driver': 'junos',
             'napalm_driver': 'junos',
             'napalm_args': None,
             'napalm_args': None,
+            'description': 'A new platform',
         }
         }
 
 
         cls.csv_data = (
         cls.csv_data = (
-            "name,slug",
-            "Platform 4,platform-4",
-            "Platform 5,platform-5",
-            "Platform 6,platform-6",
+            "name,slug,description",
+            "Platform 4,platform-4,Fourth platform",
+            "Platform 5,platform-5,Fifth platform",
+            "Platform 6,platform-6,Sixth platform",
         )
         )
 
 
 
 
@@ -1566,7 +1573,8 @@ class PowerPanelTestCase(ViewTestCases.PrimaryObjectViewTestCase):
             RackGroup(name='Rack Group 1', slug='rack-group-1', site=sites[0]),
             RackGroup(name='Rack Group 1', slug='rack-group-1', site=sites[0]),
             RackGroup(name='Rack Group 2', slug='rack-group-2', site=sites[1]),
             RackGroup(name='Rack Group 2', slug='rack-group-2', site=sites[1]),
         )
         )
-        RackGroup.objects.bulk_create(rackgroups)
+        for rackgroup in rackgroups:
+            rackgroup.save()
 
 
         PowerPanel.objects.bulk_create((
         PowerPanel.objects.bulk_create((
             PowerPanel(site=sites[0], rack_group=rackgroups[0], name='Power Panel 1'),
             PowerPanel(site=sites[0], rack_group=rackgroups[0], name='Power Panel 1'),

+ 7 - 1
netbox/dcim/views.py

@@ -266,7 +266,13 @@ class SiteBulkDeleteView(PermissionRequiredMixin, BulkDeleteView):
 
 
 class RackGroupListView(PermissionRequiredMixin, ObjectListView):
 class RackGroupListView(PermissionRequiredMixin, ObjectListView):
     permission_required = 'dcim.view_rackgroup'
     permission_required = 'dcim.view_rackgroup'
-    queryset = RackGroup.objects.prefetch_related('site').annotate(rack_count=Count('racks'))
+    queryset = RackGroup.objects.add_related_count(
+        RackGroup.objects.all(),
+        Rack,
+        'group',
+        'rack_count',
+        cumulative=True
+    ).prefetch_related('site')
     filterset = filters.RackGroupFilterSet
     filterset = filters.RackGroupFilterSet
     filterset_form = forms.RackGroupFilterForm
     filterset_form = forms.RackGroupFilterForm
     table = tables.RackGroupTable
     table = tables.RackGroupTable

+ 4 - 4
netbox/extras/api/customfields.py

@@ -20,7 +20,10 @@ class CustomFieldDefaultValues:
     """
     """
     Return a dictionary of all CustomFields assigned to the parent model and their default values.
     Return a dictionary of all CustomFields assigned to the parent model and their default values.
     """
     """
-    def __call__(self):
+    requires_context = True
+
+    def __call__(self, serializer_field):
+        self.model = serializer_field.parent.Meta.model
 
 
         # Retrieve the CustomFields for the parent model
         # Retrieve the CustomFields for the parent model
         content_type = ContentType.objects.get_for_model(self.model)
         content_type = ContentType.objects.get_for_model(self.model)
@@ -49,9 +52,6 @@ class CustomFieldDefaultValues:
 
 
         return value
         return value
 
 
-    def set_context(self, serializer_field):
-        self.model = serializer_field.parent.Meta.model
-
 
 
 class CustomFieldsSerializer(serializers.BaseSerializer):
 class CustomFieldsSerializer(serializers.BaseSerializer):
 
 

+ 1 - 1
netbox/extras/api/serializers.py

@@ -92,7 +92,7 @@ class TagSerializer(ValidatedModelSerializer):
 
 
     class Meta:
     class Meta:
         model = Tag
         model = Tag
-        fields = ['id', 'name', 'slug', 'color', 'comments', 'tagged_items']
+        fields = ['id', 'name', 'slug', 'color', 'description', 'tagged_items']
 
 
 
 
 #
 #

+ 0 - 3
netbox/extras/api/urls.py

@@ -14,9 +14,6 @@ class ExtrasRootView(routers.APIRootView):
 router = routers.DefaultRouter()
 router = routers.DefaultRouter()
 router.APIRootView = ExtrasRootView
 router.APIRootView = ExtrasRootView
 
 
-# Field choices
-router.register('_choices', views.ExtrasFieldChoicesViewSet, basename='field-choice')
-
 # Custom field choices
 # Custom field choices
 router.register('_custom_field_choices', views.CustomFieldChoicesViewSet, basename='custom-field-choice')
 router.register('_custom_field_choices', views.CustomFieldChoicesViewSet, basename='custom-field-choice')
 
 

+ 1 - 13
netbox/extras/api/views.py

@@ -15,22 +15,10 @@ from extras.models import (
 )
 )
 from extras.reports import get_report, get_reports
 from extras.reports import get_report, get_reports
 from extras.scripts import get_script, get_scripts, run_script
 from extras.scripts import get_script, get_scripts, run_script
-from utilities.api import FieldChoicesViewSet, IsAuthenticatedOrLoginNotRequired, ModelViewSet
+from utilities.api import IsAuthenticatedOrLoginNotRequired, ModelViewSet
 from . import serializers
 from . import serializers
 
 
 
 
-#
-# Field choices
-#
-
-class ExtrasFieldChoicesViewSet(FieldChoicesViewSet):
-    fields = (
-        (serializers.ExportTemplateSerializer, ['template_language']),
-        (serializers.GraphSerializer, ['type', 'template_language']),
-        (serializers.ObjectChangeSerializer, ['action']),
-    )
-
-
 #
 #
 # Custom field choices
 # Custom field choices
 #
 #

+ 8 - 4
netbox/extras/forms.py

@@ -144,12 +144,11 @@ class CustomFieldFilterForm(forms.Form):
 
 
 class TagForm(BootstrapMixin, forms.ModelForm):
 class TagForm(BootstrapMixin, forms.ModelForm):
     slug = SlugField()
     slug = SlugField()
-    comments = CommentField()
 
 
     class Meta:
     class Meta:
         model = Tag
         model = Tag
         fields = [
         fields = [
-            'name', 'slug', 'color', 'comments'
+            'name', 'slug', 'color', 'description'
         ]
         ]
 
 
 
 
@@ -181,9 +180,13 @@ class TagBulkEditForm(BootstrapMixin, BulkEditForm):
         required=False,
         required=False,
         widget=ColorSelect()
         widget=ColorSelect()
     )
     )
+    description = forms.CharField(
+        max_length=200,
+        required=False
+    )
 
 
     class Meta:
     class Meta:
-        nullable_fields = []
+        nullable_fields = ['description']
 
 
 
 
 #
 #
@@ -432,7 +435,8 @@ class ScriptForm(BootstrapMixin, forms.Form):
             self.fields['_commit'].initial = False
             self.fields['_commit'].initial = False
 
 
         # Move _commit to the end of the form
         # Move _commit to the end of the form
-        self.fields.move_to_end('_commit', True)
+        commit = self.fields.pop('_commit')
+        self.fields['_commit'] = commit
 
 
     @property
     @property
     def requires_input(self):
     def requires_input(self):

+ 23 - 0
netbox/extras/migrations/0040_standardize_description.py

@@ -0,0 +1,23 @@
+# Generated by Django 3.0.3 on 2020-03-13 20:27
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('extras', '0039_update_features_content_types'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='configcontext',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='customfield',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+    ]

+ 23 - 0
netbox/extras/migrations/0041_tag_description.py

@@ -0,0 +1,23 @@
+# Generated by Django 3.0.3 on 2020-03-13 20:46
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('extras', '0040_standardize_description'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='tag',
+            name='comments',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.RenameField(
+            model_name='tag',
+            old_name='comments',
+            new_name='description',
+        ),
+    ]

+ 4 - 6
netbox/extras/models.py

@@ -243,7 +243,7 @@ class CustomField(models.Model):
                   'the field\'s name will be used)'
                   'the field\'s name will be used)'
     )
     )
     description = models.CharField(
     description = models.CharField(
-        max_length=100,
+        max_length=200,
         blank=True
         blank=True
     )
     )
     required = models.BooleanField(
     required = models.BooleanField(
@@ -551,7 +551,6 @@ class Graph(models.Model):
     def embed_url(self, obj):
     def embed_url(self, obj):
         context = {'obj': obj}
         context = {'obj': obj}
 
 
-        # TODO: Remove in v2.8
         if self.template_language == TemplateLanguageChoices.LANGUAGE_DJANGO:
         if self.template_language == TemplateLanguageChoices.LANGUAGE_DJANGO:
             template = Template(self.source)
             template = Template(self.source)
             return template.render(Context(context))
             return template.render(Context(context))
@@ -565,7 +564,6 @@ class Graph(models.Model):
 
 
         context = {'obj': obj}
         context = {'obj': obj}
 
 
-        # TODO: Remove in v2.8
         if self.template_language == TemplateLanguageChoices.LANGUAGE_DJANGO:
         if self.template_language == TemplateLanguageChoices.LANGUAGE_DJANGO:
             template = Template(self.link)
             template = Template(self.link)
             return template.render(Context(context))
             return template.render(Context(context))
@@ -767,7 +765,7 @@ class ConfigContext(models.Model):
         default=1000
         default=1000
     )
     )
     description = models.CharField(
     description = models.CharField(
-        max_length=100,
+        max_length=200,
         blank=True
         blank=True
     )
     )
     is_active = models.BooleanField(
     is_active = models.BooleanField(
@@ -1054,9 +1052,9 @@ class Tag(TagBase, ChangeLoggedModel):
     color = ColorField(
     color = ColorField(
         default='9e9e9e'
         default='9e9e9e'
     )
     )
-    comments = models.TextField(
+    description = models.CharField(
+        max_length=200,
         blank=True,
         blank=True,
-        default=''
     )
     )
 
 
     def get_absolute_url(self):
     def get_absolute_url(self):

+ 250 - 0
netbox/extras/plugins/__init__.py

@@ -0,0 +1,250 @@
+import collections
+import inspect
+from packaging import version
+
+from django.apps import AppConfig
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured
+from django.template.loader import get_template
+from django.utils.module_loading import import_string
+
+from extras.registry import registry
+from utilities.choices import ButtonColorChoices
+
+
+# Initialize plugin registry stores
+registry['plugin_template_extensions'] = collections.defaultdict(list)
+registry['plugin_menu_items'] = {}
+
+
+#
+# Plugin AppConfig class
+#
+
+class PluginConfig(AppConfig):
+    """
+    Subclass of Django's built-in AppConfig class, to be used for NetBox plugins.
+    """
+    # Plugin metadata
+    author = ''
+    author_email = ''
+    description = ''
+    version = ''
+
+    # Root URL path under /plugins. If not set, the plugin's label will be used.
+    base_url = None
+
+    # Minimum/maximum compatible versions of NetBox
+    min_version = None
+    max_version = None
+
+    # Default configuration parameters
+    default_settings = {}
+
+    # Mandatory configuration parameters
+    required_settings = []
+
+    # Middleware classes provided by the plugin
+    middleware = []
+
+    # Cacheops configuration. Cache all operations by default.
+    caching_config = {
+        '*': {'ops': 'all'},
+    }
+
+    # Default integration paths. Plugin authors can override these to customize the paths to
+    # integrated components.
+    template_extensions = 'template_content.template_extensions'
+    menu_items = 'navigation.menu_items'
+
+    def ready(self):
+
+        # Register template content
+        try:
+            template_extensions = import_string(f"{self.__module__}.{self.template_extensions}")
+            register_template_extensions(template_extensions)
+        except ImportError:
+            pass
+
+        # Register navigation menu items (if defined)
+        try:
+            menu_items = import_string(f"{self.__module__}.{self.menu_items}")
+            register_menu_items(self.verbose_name, menu_items)
+        except ImportError:
+            pass
+
+    @classmethod
+    def validate(cls, user_config):
+
+        # Enforce version constraints
+        current_version = version.parse(settings.VERSION)
+        if cls.min_version is not None:
+            min_version = version.parse(cls.min_version)
+            if current_version < min_version:
+                raise ImproperlyConfigured(
+                    f"Plugin {cls.__module__} requires NetBox minimum version {cls.min_version}."
+                )
+        if cls.max_version is not None:
+            max_version = version.parse(cls.max_version)
+            if current_version > max_version:
+                raise ImproperlyConfigured(
+                    f"Plugin {cls.__module__} requires NetBox maximum version {cls.max_version}."
+                )
+
+        # Verify required configuration settings
+        for setting in cls.required_settings:
+            if setting not in user_config:
+                raise ImproperlyConfigured(
+                    f"Plugin {cls.__module__} requires '{setting}' to be present in the PLUGINS_CONFIG section of "
+                    f"configuration.py."
+                )
+
+        # Apply default configuration values
+        for setting, value in cls.default_settings.items():
+            if setting not in user_config:
+                user_config[setting] = value
+
+
+#
+# Template content injection
+#
+
+class PluginTemplateExtension:
+    """
+    This class is used to register plugin content to be injected into core NetBox templates. It contains methods
+    that are overridden by plugin authors to return template content.
+
+    The `model` attribute on the class defines the which model detail page this class renders content for. It
+    should be set as a string in the form '<app_label>.<model_name>'. render() provides the following context data:
+
+    * object - The object being viewed
+    * request - The current request
+    * settings - Global NetBox settings
+    * config - Plugin-specific configuration parameters
+    """
+    model = None
+
+    def __init__(self, context):
+        self.context = context
+
+    def render(self, template_name, extra_context=None):
+        """
+        Convenience method for rendering the specified Django template using the default context data. An additional
+        context dictionary may be passed as `extra_context`.
+        """
+        if extra_context is None:
+            extra_context = {}
+        elif not isinstance(extra_context, dict):
+            raise TypeError("extra_context must be a dictionary")
+
+        return get_template(template_name).render({**self.context, **extra_context})
+
+    def left_page(self):
+        """
+        Content that will be rendered on the left of the detail page view. Content should be returned as an
+        HTML string. Note that content does not need to be marked as safe because this is automatically handled.
+        """
+        raise NotImplementedError
+
+    def right_page(self):
+        """
+        Content that will be rendered on the right of the detail page view. Content should be returned as an
+        HTML string. Note that content does not need to be marked as safe because this is automatically handled.
+        """
+        raise NotImplementedError
+
+    def full_width_page(self):
+        """
+        Content that will be rendered within the full width of the detail page view. Content should be returned as an
+        HTML string. Note that content does not need to be marked as safe because this is automatically handled.
+        """
+        raise NotImplementedError
+
+    def buttons(self):
+        """
+        Buttons that will be rendered and added to the existing list of buttons on the detail page view. Content
+        should be returned as an HTML string. Note that content does not need to be marked as safe because this is
+        automatically handled.
+        """
+        raise NotImplementedError
+
+
+def register_template_extensions(class_list):
+    """
+    Register a list of PluginTemplateExtension classes
+    """
+    # Validation
+    for template_extension in class_list:
+        if not inspect.isclass(template_extension):
+            raise TypeError(f"PluginTemplateExtension class {template_extension} was passes as an instance!")
+        if not issubclass(template_extension, PluginTemplateExtension):
+            raise TypeError(f"{template_extension} is not a subclass of extras.plugins.PluginTemplateExtension!")
+        if template_extension.model is None:
+            raise TypeError(f"PluginTemplateExtension class {template_extension} does not define a valid model!")
+
+        registry['plugin_template_extensions'][template_extension.model].append(template_extension)
+
+
+#
+# Navigation menu links
+#
+
+class PluginMenuItem:
+    """
+    This class represents a navigation menu item. This constitutes primary link and its text, but also allows for
+    specifying additional link buttons that appear to the right of the item in the van menu.
+
+    Links are specified as Django reverse URL strings.
+    Buttons are each specified as a list of PluginMenuButton instances.
+    """
+    permissions = []
+    buttons = []
+
+    def __init__(self, link, link_text, permissions=None, buttons=None):
+        self.link = link
+        self.link_text = link_text
+        if permissions is not None:
+            if type(permissions) not in (list, tuple):
+                raise TypeError("Permissions must be passed as a tuple or list.")
+            self.permissions = permissions
+        if buttons is not None:
+            if type(buttons) not in (list, tuple):
+                raise TypeError("Buttons must be passed as a tuple or list.")
+            self.buttons = buttons
+
+
+class PluginMenuButton:
+    """
+    This class represents a button within a PluginMenuItem. Note that button colors should come from
+    ButtonColorChoices.
+    """
+    color = ButtonColorChoices.DEFAULT
+    permissions = []
+
+    def __init__(self, link, title, icon_class, color=None, permissions=None):
+        self.link = link
+        self.title = title
+        self.icon_class = icon_class
+        if permissions is not None:
+            if type(permissions) not in (list, tuple):
+                raise TypeError("Permissions must be passed as a tuple or list.")
+            self.permissions = permissions
+        if color is not None:
+            if color not in ButtonColorChoices.values():
+                raise ValueError("Button color must be a choice within ButtonColorChoices.")
+            self.color = color
+
+
+def register_menu_items(section_name, class_list):
+    """
+    Register a list of PluginMenuItem instances for a given menu section (e.g. plugin name)
+    """
+    # Validation
+    for menu_link in class_list:
+        if not isinstance(menu_link, PluginMenuItem):
+            raise TypeError(f"{menu_link} must be an instance of extras.plugins.PluginMenuItem")
+        for button in menu_link.buttons:
+            if not isinstance(button, PluginMenuButton):
+                raise TypeError(f"{button} must be an instance of extras.plugins.PluginMenuButton")
+
+    registry['plugin_menu_items'][section_name] = class_list

+ 42 - 0
netbox/extras/plugins/urls.py

@@ -0,0 +1,42 @@
+from django.apps import apps
+from django.conf import settings
+from django.conf.urls import include
+from django.contrib.admin.views.decorators import staff_member_required
+from django.urls import path
+from django.utils.module_loading import import_string
+
+from . import views
+
+# Initialize URL base, API, and admin URL patterns for plugins
+plugin_patterns = []
+plugin_api_patterns = [
+    path('', views.PluginsAPIRootView.as_view(), name='api-root'),
+    path('installed-plugins/', views.InstalledPluginsAPIView.as_view(), name='plugins-list')
+]
+plugin_admin_patterns = [
+    path('installed-plugins/', staff_member_required(views.InstalledPluginsAdminView.as_view()), name='plugins_list')
+]
+
+# Register base/API URL patterns for each plugin
+for plugin_path in settings.PLUGINS:
+    plugin_name = plugin_path.split('.')[-1]
+    app = apps.get_app_config(plugin_name)
+    base_url = getattr(app, 'base_url') or app.label
+
+    # Check if the plugin specifies any base URLs
+    try:
+        urlpatterns = import_string(f"{plugin_path}.urls.urlpatterns")
+        plugin_patterns.append(
+            path(f"{base_url}/", include((urlpatterns, app.label)))
+        )
+    except ImportError:
+        pass
+
+    # Check if the plugin specifies any API URLs
+    try:
+        urlpatterns = import_string(f"{plugin_path}.api.urls.urlpatterns")
+        plugin_api_patterns.append(
+            path(f"{base_url}/", include((urlpatterns, f"{app.label}-api")))
+        )
+    except ImportError:
+        pass

+ 93 - 0
netbox/extras/plugins/views.py

@@ -0,0 +1,93 @@
+from collections import OrderedDict
+
+from django.apps import apps
+from django.conf import settings
+from django.shortcuts import render
+from django.urls.exceptions import NoReverseMatch
+from django.utils.module_loading import import_string
+from django.views.generic import View
+from rest_framework import permissions
+from rest_framework.response import Response
+from rest_framework.reverse import reverse
+from rest_framework.views import APIView
+
+
+class InstalledPluginsAdminView(View):
+    """
+    Admin view for listing all installed plugins
+    """
+    def get(self, request):
+        plugins = [apps.get_app_config(plugin) for plugin in settings.PLUGINS]
+        return render(request, 'extras/admin/plugins_list.html', {
+            'plugins': plugins,
+        })
+
+
+class InstalledPluginsAPIView(APIView):
+    """
+    API view for listing all installed plugins
+    """
+    permission_classes = [permissions.IsAdminUser]
+    _ignore_model_permissions = True
+    exclude_from_schema = True
+    swagger_schema = None
+
+    def get_view_name(self):
+        return "Installed Plugins"
+
+    @staticmethod
+    def _get_plugin_data(plugin_app_config):
+        return {
+            'name': plugin_app_config.verbose_name,
+            'package': plugin_app_config.name,
+            'author': plugin_app_config.author,
+            'author_email': plugin_app_config.author_email,
+            'description': plugin_app_config.description,
+            'verison': plugin_app_config.version
+        }
+
+    def get(self, request, format=None):
+        return Response([self._get_plugin_data(apps.get_app_config(plugin)) for plugin in settings.PLUGINS])
+
+
+class PluginsAPIRootView(APIView):
+    _ignore_model_permissions = True
+    exclude_from_schema = True
+    swagger_schema = None
+
+    def get_view_name(self):
+        return "Plugins"
+
+    @staticmethod
+    def _get_plugin_entry(plugin, app_config, request, format):
+        try:
+            api_app_name = import_string(f"{plugin}.api.urls.app_name")
+        except (ImportError, ModuleNotFoundError):
+            # Plugin does not expose an API
+            return None
+
+        try:
+            entry = (getattr(app_config, 'base_url', app_config.label), reverse(
+                f"plugins-api:{api_app_name}:api-root",
+                request=request,
+                format=format
+            ))
+        except NoReverseMatch:
+            # The plugin does not include an api-root
+            entry = None
+
+        return entry
+
+    def get(self, request, format=None):
+
+        entries = []
+        for plugin in settings.PLUGINS:
+            app_config = apps.get_app_config(plugin)
+            entry = self._get_plugin_entry(plugin, app_config, request, format)
+            if entry is not None:
+                entries.append(entry)
+
+        return Response(OrderedDict((
+            ('installed-plugins', reverse('plugins-api:plugins-list', request=request, format=format)),
+            *entries
+        )))

+ 15 - 0
netbox/extras/reports.py

@@ -1,5 +1,6 @@
 import importlib
 import importlib
 import inspect
 import inspect
+import logging
 import pkgutil
 import pkgutil
 from collections import OrderedDict
 from collections import OrderedDict
 
 
@@ -91,6 +92,8 @@ class Report(object):
         self.active_test = None
         self.active_test = None
         self.failed = False
         self.failed = False
 
 
+        self.logger = logging.getLogger(f"netbox.reports.{self.module}.{self.name}")
+
         # Compile test methods and initialize results skeleton
         # Compile test methods and initialize results skeleton
         test_methods = []
         test_methods = []
         for method in dir(self):
         for method in dir(self):
@@ -138,6 +141,7 @@ class Report(object):
         Log a message which is not associated with a particular object.
         Log a message which is not associated with a particular object.
         """
         """
         self._log(None, message, level=LOG_DEFAULT)
         self._log(None, message, level=LOG_DEFAULT)
+        self.logger.info(message)
 
 
     def log_success(self, obj, message=None):
     def log_success(self, obj, message=None):
         """
         """
@@ -146,6 +150,7 @@ class Report(object):
         if message:
         if message:
             self._log(obj, message, level=LOG_SUCCESS)
             self._log(obj, message, level=LOG_SUCCESS)
         self._results[self.active_test]['success'] += 1
         self._results[self.active_test]['success'] += 1
+        self.logger.info(f"Success | {obj}: {message}")
 
 
     def log_info(self, obj, message):
     def log_info(self, obj, message):
         """
         """
@@ -153,6 +158,7 @@ class Report(object):
         """
         """
         self._log(obj, message, level=LOG_INFO)
         self._log(obj, message, level=LOG_INFO)
         self._results[self.active_test]['info'] += 1
         self._results[self.active_test]['info'] += 1
+        self.logger.info(f"Info | {obj}: {message}")
 
 
     def log_warning(self, obj, message):
     def log_warning(self, obj, message):
         """
         """
@@ -160,6 +166,7 @@ class Report(object):
         """
         """
         self._log(obj, message, level=LOG_WARNING)
         self._log(obj, message, level=LOG_WARNING)
         self._results[self.active_test]['warning'] += 1
         self._results[self.active_test]['warning'] += 1
+        self.logger.info(f"Warning | {obj}: {message}")
 
 
     def log_failure(self, obj, message):
     def log_failure(self, obj, message):
         """
         """
@@ -167,12 +174,15 @@ class Report(object):
         """
         """
         self._log(obj, message, level=LOG_FAILURE)
         self._log(obj, message, level=LOG_FAILURE)
         self._results[self.active_test]['failure'] += 1
         self._results[self.active_test]['failure'] += 1
+        self.logger.info(f"Failure | {obj}: {message}")
         self.failed = True
         self.failed = True
 
 
     def run(self):
     def run(self):
         """
         """
         Run the report and return its results. Each test method will be executed in order.
         Run the report and return its results. Each test method will be executed in order.
         """
         """
+        self.logger.info(f"Running report")
+
         for method_name in self.test_methods:
         for method_name in self.test_methods:
             self.active_test = method_name
             self.active_test = method_name
             test_method = getattr(self, method_name)
             test_method = getattr(self, method_name)
@@ -184,6 +194,11 @@ class Report(object):
         result.save()
         result.save()
         self.result = result
         self.result = result
 
 
+        if self.failed:
+            self.logger.warning("Report failed")
+        else:
+            self.logger.info("Report completed successfully")
+
         # Perform any post-run tasks
         # Perform any post-run tasks
         self.post_run()
         self.post_run()
 
 

+ 13 - 0
netbox/extras/scripts.py

@@ -1,5 +1,6 @@
 import inspect
 import inspect
 import json
 import json
+import logging
 import os
 import os
 import pkgutil
 import pkgutil
 import time
 import time
@@ -255,6 +256,7 @@ class BaseScript:
     def __init__(self):
     def __init__(self):
 
 
         # Initiate the log
         # Initiate the log
+        self.logger = logging.getLogger(f"netbox.scripts.{self.module()}.{self.__class__.__name__}")
         self.log = []
         self.log = []
 
 
         # Declare the placeholder for the current request
         # Declare the placeholder for the current request
@@ -302,18 +304,23 @@ class BaseScript:
     # Logging
     # Logging
 
 
     def log_debug(self, message):
     def log_debug(self, message):
+        self.logger.log(logging.DEBUG, message)
         self.log.append((LOG_DEFAULT, message))
         self.log.append((LOG_DEFAULT, message))
 
 
     def log_success(self, message):
     def log_success(self, message):
+        self.logger.log(logging.INFO, message)  # No syslog equivalent for SUCCESS
         self.log.append((LOG_SUCCESS, message))
         self.log.append((LOG_SUCCESS, message))
 
 
     def log_info(self, message):
     def log_info(self, message):
+        self.logger.log(logging.INFO, message)
         self.log.append((LOG_INFO, message))
         self.log.append((LOG_INFO, message))
 
 
     def log_warning(self, message):
     def log_warning(self, message):
+        self.logger.log(logging.WARNING, message)
         self.log.append((LOG_WARNING, message))
         self.log.append((LOG_WARNING, message))
 
 
     def log_failure(self, message):
     def log_failure(self, message):
+        self.logger.log(logging.ERROR, message)
         self.log.append((LOG_FAILURE, message))
         self.log.append((LOG_FAILURE, message))
 
 
     # Convenience functions
     # Convenience functions
@@ -376,6 +383,10 @@ def run_script(script, data, request, commit=True):
     start_time = None
     start_time = None
     end_time = None
     end_time = None
 
 
+    script_name = script.__class__.__name__
+    logger = logging.getLogger(f"netbox.scripts.{script.module()}.{script_name}")
+    logger.info(f"Running script (commit={commit})")
+
     # Add files to form data
     # Add files to form data
     files = request.FILES
     files = request.FILES
     for field_name, fileobj in files.items():
     for field_name, fileobj in files.items():
@@ -405,6 +416,7 @@ def run_script(script, data, request, commit=True):
         script.log_failure(
         script.log_failure(
             "An exception occurred: `{}: {}`\n```\n{}\n```".format(type(e).__name__, e, stacktrace)
             "An exception occurred: `{}: {}`\n```\n{}\n```".format(type(e).__name__, e, stacktrace)
         )
         )
+        logger.error(f"Exception raised during script execution: {e}")
         commit = False
         commit = False
     finally:
     finally:
         if not commit:
         if not commit:
@@ -417,6 +429,7 @@ def run_script(script, data, request, commit=True):
     # Calculate execution time
     # Calculate execution time
     if end_time is not None:
     if end_time is not None:
         execution_time = end_time - start_time
         execution_time = end_time - start_time
+        logger.info(f"Script completed in {execution_time:.4f} seconds")
     else:
     else:
         execution_time = None
         execution_time = None
 
 

+ 1 - 1
netbox/extras/tables.py

@@ -77,7 +77,7 @@ class TagTable(BaseTable):
 
 
     class Meta(BaseTable.Meta):
     class Meta(BaseTable.Meta):
         model = Tag
         model = Tag
-        fields = ('pk', 'name', 'items', 'slug', 'color', 'actions')
+        fields = ('pk', 'name', 'items', 'slug', 'color', 'description', 'actions')
 
 
 
 
 class TaggedItemTable(BaseTable):
 class TaggedItemTable(BaseTable):

+ 73 - 0
netbox/extras/templatetags/plugins.py

@@ -0,0 +1,73 @@
+from django import template as template_
+from django.conf import settings
+from django.utils.safestring import mark_safe
+
+from extras.plugins import PluginTemplateExtension
+from extras.registry import registry
+
+register = template_.Library()
+
+
+def _get_registered_content(obj, method, template_context):
+    """
+    Given an object and a PluginTemplateExtension method name and the template context, return all the
+    registered content for the object's model.
+    """
+    html = ''
+    context = {
+        'object': obj,
+        'request': template_context['request'],
+        'settings': template_context['settings'],
+    }
+
+    model_name = obj._meta.label_lower
+    template_extensions = registry['plugin_template_extensions'].get(model_name, [])
+    for template_extension in template_extensions:
+
+        # If the class has not overridden the specified method, we can skip it (because we know it
+        # will raise NotImplementedError).
+        if getattr(template_extension, method) == getattr(PluginTemplateExtension, method):
+            continue
+
+        # Update context with plugin-specific configuration parameters
+        plugin_name = template_extension.__module__.split('.')[0]
+        context['config'] = settings.PLUGINS_CONFIG.get(plugin_name, {})
+
+        # Call the method to render content
+        instance = template_extension(context)
+        content = getattr(instance, method)()
+        html += content
+
+    return mark_safe(html)
+
+
+@register.simple_tag(takes_context=True)
+def plugin_buttons(context, obj):
+    """
+    Render all buttons registered by plugins
+    """
+    return _get_registered_content(obj, 'buttons', context)
+
+
+@register.simple_tag(takes_context=True)
+def plugin_left_page(context, obj):
+    """
+    Render all left page content registered by plugins
+    """
+    return _get_registered_content(obj, 'left_page', context)
+
+
+@register.simple_tag(takes_context=True)
+def plugin_right_page(context, obj):
+    """
+    Render all right page content registered by plugins
+    """
+    return _get_registered_content(obj, 'right_page', context)
+
+
+@register.simple_tag(takes_context=True)
+def plugin_full_width_page(context, obj):
+    """
+    Render all full width page content registered by plugins
+    """
+    return _get_registered_content(obj, 'full_width_page', context)

+ 17 - 0
netbox/extras/tests/dummy_plugin/__init__.py

@@ -0,0 +1,17 @@
+from extras.plugins import PluginConfig
+
+
+class DummyPluginConfig(PluginConfig):
+    name = 'extras.tests.dummy_plugin'
+    verbose_name = 'Dummy plugin'
+    version = '0.0'
+    description = 'For testing purposes only'
+    base_url = 'dummy-plugin'
+    min_version = '1.0'
+    max_version = '9.0'
+    middleware = [
+        'extras.tests.dummy_plugin.middleware.DummyMiddleware'
+    ]
+
+
+config = DummyPluginConfig

+ 9 - 0
netbox/extras/tests/dummy_plugin/admin.py

@@ -0,0 +1,9 @@
+from django.contrib import admin
+
+from netbox.admin import admin_site
+from .models import DummyModel
+
+
+@admin.register(DummyModel, site=admin_site)
+class DummyModelAdmin(admin.ModelAdmin):
+    list_display = ('name', 'number')

+ 9 - 0
netbox/extras/tests/dummy_plugin/api/serializers.py

@@ -0,0 +1,9 @@
+from rest_framework.serializers import ModelSerializer
+from extras.tests.dummy_plugin.models import DummyModel
+
+
+class DummySerializer(ModelSerializer):
+
+    class Meta:
+        model = DummyModel
+        fields = ('id', 'name', 'number')

+ 6 - 0
netbox/extras/tests/dummy_plugin/api/urls.py

@@ -0,0 +1,6 @@
+from rest_framework import routers
+from .views import DummyViewSet
+
+router = routers.DefaultRouter()
+router.register('dummy-models', DummyViewSet)
+urlpatterns = router.urls

+ 8 - 0
netbox/extras/tests/dummy_plugin/api/views.py

@@ -0,0 +1,8 @@
+from rest_framework.viewsets import ModelViewSet
+from extras.tests.dummy_plugin.models import DummyModel
+from .serializers import DummySerializer
+
+
+class DummyViewSet(ModelViewSet):
+    queryset = DummyModel.objects.all()
+    serializer_class = DummySerializer

+ 7 - 0
netbox/extras/tests/dummy_plugin/middleware.py

@@ -0,0 +1,7 @@
+class DummyMiddleware:
+
+    def __init__(self, get_response):
+        self.get_response = get_response
+
+    def __call__(self, request):
+        return self.get_response(request)

+ 23 - 0
netbox/extras/tests/dummy_plugin/migrations/0001_initial.py

@@ -0,0 +1,23 @@
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    initial = True
+
+    dependencies = [
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='DummyModel',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False)),
+                ('name', models.CharField(max_length=20)),
+                ('number', models.IntegerField(default=100)),
+            ],
+            options={
+                'ordering': ['name'],
+            },
+        ),
+    ]

+ 0 - 0
netbox/extras/tests/dummy_plugin/migrations/__init__.py


+ 13 - 0
netbox/extras/tests/dummy_plugin/models.py

@@ -0,0 +1,13 @@
+from django.db import models
+
+
+class DummyModel(models.Model):
+    name = models.CharField(
+        max_length=20
+    )
+    number = models.IntegerField(
+        default=100
+    )
+
+    class Meta:
+        ordering = ['name']

+ 25 - 0
netbox/extras/tests/dummy_plugin/navigation.py

@@ -0,0 +1,25 @@
+from extras.plugins import PluginMenuButton, PluginMenuItem
+
+
+menu_items = (
+    PluginMenuItem(
+        link='plugins:dummy_plugin:dummy_models',
+        link_text='Item 1',
+        buttons=(
+            PluginMenuButton(
+                link='admin:dummy_plugin_dummymodel_add',
+                title='Add a new dummy model',
+                icon_class='fa fa-plus',
+            ),
+            PluginMenuButton(
+                link='admin:dummy_plugin_dummymodel_add',
+                title='Add a new dummy model',
+                icon_class='fa fa-plus',
+            ),
+        )
+    ),
+    PluginMenuItem(
+        link='plugins:dummy_plugin:dummy_models',
+        link_text='Item 2',
+    ),
+)

+ 20 - 0
netbox/extras/tests/dummy_plugin/template_content.py

@@ -0,0 +1,20 @@
+from extras.plugins import PluginTemplateExtension
+
+
+class SiteContent(PluginTemplateExtension):
+    model = 'dcim.site'
+
+    def left_page(self):
+        return "SITE CONTENT - LEFT PAGE"
+
+    def right_page(self):
+        return "SITE CONTENT - RIGHT PAGE"
+
+    def full_width_page(self):
+        return "SITE CONTENT - FULL WIDTH PAGE"
+
+    def full_buttons(self):
+        return "SITE CONTENT - BUTTONS"
+
+
+template_extensions = [SiteContent]

+ 8 - 0
netbox/extras/tests/dummy_plugin/urls.py

@@ -0,0 +1,8 @@
+from django.urls import path
+
+from . import views
+
+
+urlpatterns = (
+    path('models/', views.DummyModelsView.as_view(), name='dummy_models'),
+)

+ 11 - 0
netbox/extras/tests/dummy_plugin/views.py

@@ -0,0 +1,11 @@
+from django.http import HttpResponse
+from django.views.generic import View
+
+from .models import DummyModel
+
+
+class DummyModelsView(View):
+
+    def get(self, request):
+        instance_count = DummyModel.objects.count()
+        return HttpResponse(f"Instances: {instance_count}")

+ 5 - 25
netbox/extras/tests/test_api.py

@@ -7,12 +7,11 @@ from rest_framework import status
 
 
 from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Platform, Rack, RackGroup, RackRole, Region, Site
 from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Platform, Rack, RackGroup, RackRole, Region, Site
 from extras.api.views import ScriptViewSet
 from extras.api.views import ScriptViewSet
-from extras.choices import *
 from extras.models import ConfigContext, Graph, ExportTemplate, Tag
 from extras.models import ConfigContext, Graph, ExportTemplate, Tag
 from extras.scripts import BooleanVar, IntegerVar, Script, StringVar
 from extras.scripts import BooleanVar, IntegerVar, Script, StringVar
 from extras.utils import FeatureQuery
 from extras.utils import FeatureQuery
 from tenancy.models import Tenant, TenantGroup
 from tenancy.models import Tenant, TenantGroup
-from utilities.testing import APITestCase, choices_to_dict
+from utilities.testing import APITestCase
 
 
 
 
 class AppTest(APITestCase):
 class AppTest(APITestCase):
@@ -24,27 +23,6 @@ class AppTest(APITestCase):
 
 
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response.status_code, 200)
 
 
-    def test_choices(self):
-
-        url = reverse('extras-api:field-choice-list')
-        response = self.client.get(url, **self.header)
-
-        self.assertEqual(response.status_code, 200)
-
-        # ExportTemplate
-        self.assertEqual(choices_to_dict(response.data.get('export-template:template_language')), TemplateLanguageChoices.as_dict())
-
-        # Graph
-        content_types = ContentType.objects.filter(FeatureQuery('graphs').get_query())
-        graph_type_choices = {
-            "{}.{}".format(ct.app_label, ct.model): ct.name for ct in content_types
-        }
-        self.assertEqual(choices_to_dict(response.data.get('graph:type')), graph_type_choices)
-        self.assertEqual(choices_to_dict(response.data.get('graph:template_language')), TemplateLanguageChoices.as_dict())
-
-        # ObjectChange
-        self.assertEqual(choices_to_dict(response.data.get('object-change:action')), ObjectChangeActionChoices.as_dict())
-
 
 
 class GraphTest(APITestCase):
 class GraphTest(APITestCase):
 
 
@@ -402,8 +380,10 @@ class ConfigContextTest(APITestCase):
         role2 = DeviceRole.objects.create(name='Test Role 2', slug='test-role-2')
         role2 = DeviceRole.objects.create(name='Test Role 2', slug='test-role-2')
         platform1 = Platform.objects.create(name='Test Platform 1', slug='test-platform-1')
         platform1 = Platform.objects.create(name='Test Platform 1', slug='test-platform-1')
         platform2 = Platform.objects.create(name='Test Platform 2', slug='test-platform-2')
         platform2 = Platform.objects.create(name='Test Platform 2', slug='test-platform-2')
-        tenantgroup1 = TenantGroup.objects.create(name='Test Tenant Group 1', slug='test-tenant-group-1')
-        tenantgroup2 = TenantGroup.objects.create(name='Test Tenant Group 2', slug='test-tenant-group-2')
+        tenantgroup1 = TenantGroup(name='Test Tenant Group 1', slug='test-tenant-group-1')
+        tenantgroup1.save()
+        tenantgroup2 = TenantGroup(name='Test Tenant Group 2', slug='test-tenant-group-2')
+        tenantgroup2.save()
         tenant1 = Tenant.objects.create(name='Test Tenant 1', slug='test-tenant-1')
         tenant1 = Tenant.objects.create(name='Test Tenant 1', slug='test-tenant-1')
         tenant2 = Tenant.objects.create(name='Test Tenant 2', slug='test-tenant-2')
         tenant2 = Tenant.objects.create(name='Test Tenant 2', slug='test-tenant-2')
         tag1 = Tag.objects.create(name='Test Tag 1', slug='test-tag-1')
         tag1 = Tag.objects.create(name='Test Tag 1', slug='test-tag-1')

+ 2 - 2
netbox/extras/tests/test_filters.py

@@ -36,7 +36,6 @@ class GraphTestCase(TestCase):
         params = {'type': content_type.pk}
         params = {'type': content_type.pk}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)
 
 
-    # TODO: Remove in v2.8
     def test_template_language(self):
     def test_template_language(self):
         params = {'template_language': TemplateLanguageChoices.LANGUAGE_JINJA2}
         params = {'template_language': TemplateLanguageChoices.LANGUAGE_JINJA2}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
@@ -128,7 +127,8 @@ class ConfigContextTestCase(TestCase):
             TenantGroup(name='Tenant Group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant Group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant Group 3', slug='tenant-group-3'),
             TenantGroup(name='Tenant Group 3', slug='tenant-group-3'),
         )
         )
-        TenantGroup.objects.bulk_create(tenant_groups)
+        for tenantgroup in tenant_groups:
+            tenantgroup.save()
 
 
         tenants = (
         tenants = (
             Tenant(name='Tenant 1', slug='tenant-1'),
             Tenant(name='Tenant 1', slug='tenant-1'),

+ 136 - 0
netbox/extras/tests/test_plugins.py

@@ -0,0 +1,136 @@
+from unittest import skipIf
+
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured
+from django.test import Client, TestCase, override_settings
+from django.urls import reverse
+
+from extras.registry import registry
+from extras.tests.dummy_plugin import config as dummy_config
+
+
+@skipIf('extras.tests.dummy_plugin' not in settings.PLUGINS, "dummy_plugin not in settings.PLUGINS")
+class PluginTest(TestCase):
+
+    def test_config(self):
+
+        self.assertIn('extras.tests.dummy_plugin.DummyPluginConfig', settings.INSTALLED_APPS)
+
+    def test_models(self):
+        from extras.tests.dummy_plugin.models import DummyModel
+
+        # Test saving an instance
+        instance = DummyModel(name='Instance 1', number=100)
+        instance.save()
+        self.assertIsNotNone(instance.pk)
+
+        # Test deleting an instance
+        instance.delete()
+        self.assertIsNone(instance.pk)
+
+    def test_admin(self):
+
+        # Test admin view URL resolution
+        url = reverse('admin:dummy_plugin_dummymodel_add')
+        self.assertEqual(url, '/admin/dummy_plugin/dummymodel/add/')
+
+    def test_views(self):
+
+        # Test URL resolution
+        url = reverse('plugins:dummy_plugin:dummy_models')
+        self.assertEqual(url, '/plugins/dummy-plugin/models/')
+
+        # Test GET request
+        client = Client()
+        response = client.get(url)
+        self.assertEqual(response.status_code, 200)
+
+    @override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
+    def test_api_views(self):
+
+        # Test URL resolution
+        url = reverse('plugins-api:dummy_plugin-api:dummymodel-list')
+        self.assertEqual(url, '/api/plugins/dummy-plugin/dummy-models/')
+
+        # Test GET request
+        client = Client()
+        response = client.get(url)
+        self.assertEqual(response.status_code, 200)
+
+    def test_menu_items(self):
+        """
+        Check that plugin MenuItems and MenuButtons are registered.
+        """
+        self.assertIn('Dummy plugin', registry['plugin_menu_items'])
+        menu_items = registry['plugin_menu_items']['Dummy plugin']
+        self.assertEqual(len(menu_items), 2)
+        self.assertEqual(len(menu_items[0].buttons), 2)
+
+    def test_template_extensions(self):
+        """
+        Check that plugin TemplateExtensions are registered.
+        """
+        from extras.tests.dummy_plugin.template_content import SiteContent
+
+        self.assertIn(SiteContent, registry['plugin_template_extensions']['dcim.site'])
+
+    def test_middleware(self):
+        """
+        Check that plugin middleware is registered.
+        """
+        self.assertIn('extras.tests.dummy_plugin.middleware.DummyMiddleware', settings.MIDDLEWARE)
+
+    def test_caching_config(self):
+        """
+        Check that plugin caching configuration is registered.
+        """
+        self.assertIn('extras.tests.dummy_plugin.*', settings.CACHEOPS)
+
+    @override_settings(VERSION='0.9')
+    def test_min_version(self):
+        """
+        Check enforcement of minimum NetBox version.
+        """
+        with self.assertRaises(ImproperlyConfigured):
+            dummy_config.validate({})
+
+    @override_settings(VERSION='10.0')
+    def test_max_version(self):
+        """
+        Check enforcement of maximum NetBox version.
+        """
+        with self.assertRaises(ImproperlyConfigured):
+            dummy_config.validate({})
+
+    def test_required_settings(self):
+        """
+        Validate enforcement of required settings.
+        """
+        class DummyConfigWithRequiredSettings(dummy_config):
+            required_settings = ['foo']
+
+        # Validation should pass when all required settings are present
+        DummyConfigWithRequiredSettings.validate({'foo': True})
+
+        # Validation should fail when a required setting is missing
+        with self.assertRaises(ImproperlyConfigured):
+            DummyConfigWithRequiredSettings.validate({})
+
+    def test_default_settings(self):
+        """
+        Validate population of default config settings.
+        """
+        class DummyConfigWithDefaultSettings(dummy_config):
+            default_settings = {
+                'bar': 123,
+            }
+
+        # Populate the default value if setting has not been specified
+        user_config = {}
+        DummyConfigWithDefaultSettings.validate(user_config)
+        self.assertEqual(user_config['bar'], 123)
+
+        # Don't overwrite specified values
+        user_config = {'bar': 456}
+        DummyConfigWithDefaultSettings.validate(user_config)
+        self.assertEqual(user_config['bar'], 456)

+ 1 - 1
netbox/extras/views.py

@@ -274,7 +274,7 @@ class ObjectChangeLogView(View):
             template.loader.get_template(base_template)
             template.loader.get_template(base_template)
             object_var = model._meta.model_name
             object_var = model._meta.model_name
         except template.TemplateDoesNotExist:
         except template.TemplateDoesNotExist:
-            base_template = '_base.html'
+            base_template = 'base.html'
             object_var = 'obj'
             object_var = 'obj'
 
 
         return render(request, 'extras/object_changelog.html', {
         return render(request, 'extras/object_changelog.html', {

+ 2 - 2
netbox/ipam/api/serializers.py

@@ -45,7 +45,7 @@ class RIRSerializer(ValidatedModelSerializer):
 
 
     class Meta:
     class Meta:
         model = RIR
         model = RIR
-        fields = ['id', 'name', 'slug', 'is_private', 'aggregate_count']
+        fields = ['id', 'name', 'slug', 'is_private', 'description', 'aggregate_count']
 
 
 
 
 class AggregateSerializer(TaggitSerializer, CustomFieldModelSerializer):
 class AggregateSerializer(TaggitSerializer, CustomFieldModelSerializer):
@@ -81,7 +81,7 @@ class VLANGroupSerializer(ValidatedModelSerializer):
 
 
     class Meta:
     class Meta:
         model = VLANGroup
         model = VLANGroup
-        fields = ['id', 'name', 'slug', 'site', 'vlan_count']
+        fields = ['id', 'name', 'slug', 'site', 'description', 'vlan_count']
         validators = []
         validators = []
 
 
     def validate(self, data):
     def validate(self, data):

+ 0 - 3
netbox/ipam/api/urls.py

@@ -14,9 +14,6 @@ class IPAMRootView(routers.APIRootView):
 router = routers.DefaultRouter()
 router = routers.DefaultRouter()
 router.APIRootView = IPAMRootView
 router.APIRootView = IPAMRootView
 
 
-# Field choices
-router.register('_choices', views.IPAMFieldChoicesViewSet, basename='field-choice')
-
 # VRFs
 # VRFs
 router.register('vrfs', views.VRFViewSet)
 router.register('vrfs', views.VRFViewSet)
 
 

+ 1 - 15
netbox/ipam/api/views.py

@@ -10,26 +10,12 @@ from rest_framework.response import Response
 from extras.api.views import CustomFieldModelViewSet
 from extras.api.views import CustomFieldModelViewSet
 from ipam import filters
 from ipam import filters
 from ipam.models import Aggregate, IPAddress, Prefix, RIR, Role, Service, VLAN, VLANGroup, VRF
 from ipam.models import Aggregate, IPAddress, Prefix, RIR, Role, Service, VLAN, VLANGroup, VRF
-from utilities.api import FieldChoicesViewSet, ModelViewSet
+from utilities.api import ModelViewSet
 from utilities.constants import ADVISORY_LOCK_KEYS
 from utilities.constants import ADVISORY_LOCK_KEYS
 from utilities.utils import get_subquery
 from utilities.utils import get_subquery
 from . import serializers
 from . import serializers
 
 
 
 
-#
-# Field choices
-#
-
-class IPAMFieldChoicesViewSet(FieldChoicesViewSet):
-    fields = (
-        (serializers.AggregateSerializer, ['family']),
-        (serializers.PrefixSerializer, ['family', 'status']),
-        (serializers.IPAddressSerializer, ['family', 'status', 'role']),
-        (serializers.VLANSerializer, ['status']),
-        (serializers.ServiceSerializer, ['protocol']),
-    )
-
-
 #
 #
 # VRFs
 # VRFs
 #
 #

+ 2 - 0
netbox/ipam/fields.py

@@ -63,6 +63,7 @@ IPNetworkField.register_lookup(lookups.NetContained)
 IPNetworkField.register_lookup(lookups.NetContainedOrEqual)
 IPNetworkField.register_lookup(lookups.NetContainedOrEqual)
 IPNetworkField.register_lookup(lookups.NetContains)
 IPNetworkField.register_lookup(lookups.NetContains)
 IPNetworkField.register_lookup(lookups.NetContainsOrEquals)
 IPNetworkField.register_lookup(lookups.NetContainsOrEquals)
+IPNetworkField.register_lookup(lookups.NetFamily)
 IPNetworkField.register_lookup(lookups.NetMaskLength)
 IPNetworkField.register_lookup(lookups.NetMaskLength)
 
 
 
 
@@ -90,4 +91,5 @@ IPAddressField.register_lookup(lookups.NetContainsOrEquals)
 IPAddressField.register_lookup(lookups.NetHost)
 IPAddressField.register_lookup(lookups.NetHost)
 IPAddressField.register_lookup(lookups.NetIn)
 IPAddressField.register_lookup(lookups.NetIn)
 IPAddressField.register_lookup(lookups.NetHostContained)
 IPAddressField.register_lookup(lookups.NetHostContained)
+IPAddressField.register_lookup(lookups.NetFamily)
 IPAddressField.register_lookup(lookups.NetMaskLength)
 IPAddressField.register_lookup(lookups.NetMaskLength)

+ 19 - 31
netbox/ipam/filters.py

@@ -8,8 +8,8 @@ from dcim.models import Device, Interface, Region, Site
 from extras.filters import CustomFieldFilterSet, CreatedUpdatedFilterSet
 from extras.filters import CustomFieldFilterSet, CreatedUpdatedFilterSet
 from tenancy.filters import TenancyFilterSet
 from tenancy.filters import TenancyFilterSet
 from utilities.filters import (
 from utilities.filters import (
-    BaseFilterSet, MultiValueCharFilter, MultiValueNumberFilter, NameSlugSearchFilterSet,
-    NumericInFilter, TagFilter, TreeNodeMultipleChoiceFilter,
+    BaseFilterSet, MultiValueCharFilter, MultiValueNumberFilter, NameSlugSearchFilterSet, TagFilter,
+    TreeNodeMultipleChoiceFilter,
 )
 )
 from virtualization.models import VirtualMachine
 from virtualization.models import VirtualMachine
 from .choices import *
 from .choices import *
@@ -30,10 +30,6 @@ __all__ = (
 
 
 
 
 class VRFFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
 class VRFFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
-    id__in = NumericInFilter(
-        field_name='id',
-        lookup_expr='in'
-    )
     q = django_filters.CharFilter(
     q = django_filters.CharFilter(
         method='search',
         method='search',
         label='Search',
         label='Search',
@@ -55,25 +51,21 @@ class VRFFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, Create
 
 
 
 
 class RIRFilterSet(BaseFilterSet, NameSlugSearchFilterSet):
 class RIRFilterSet(BaseFilterSet, NameSlugSearchFilterSet):
-    id__in = NumericInFilter(
-        field_name='id',
-        lookup_expr='in'
-    )
 
 
     class Meta:
     class Meta:
         model = RIR
         model = RIR
-        fields = ['name', 'slug', 'is_private']
+        fields = ['name', 'slug', 'is_private', 'description']
 
 
 
 
 class AggregateFilterSet(BaseFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
 class AggregateFilterSet(BaseFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
-    id__in = NumericInFilter(
-        field_name='id',
-        lookup_expr='in'
-    )
     q = django_filters.CharFilter(
     q = django_filters.CharFilter(
         method='search',
         method='search',
         label='Search',
         label='Search',
     )
     )
+    family = django_filters.NumberFilter(
+        field_name='prefix',
+        lookup_expr='family'
+    )
     prefix = django_filters.CharFilter(
     prefix = django_filters.CharFilter(
         method='filter_prefix',
         method='filter_prefix',
         label='Prefix',
         label='Prefix',
@@ -92,7 +84,7 @@ class AggregateFilterSet(BaseFilterSet, CustomFieldFilterSet, CreatedUpdatedFilt
 
 
     class Meta:
     class Meta:
         model = Aggregate
         model = Aggregate
-        fields = ['family', 'date_added']
+        fields = ('date_added',)
 
 
     def search(self, queryset, name, value):
     def search(self, queryset, name, value):
         if not value.strip():
         if not value.strip():
@@ -127,14 +119,14 @@ class RoleFilterSet(BaseFilterSet, NameSlugSearchFilterSet):
 
 
 
 
 class PrefixFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
 class PrefixFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
-    id__in = NumericInFilter(
-        field_name='id',
-        lookup_expr='in'
-    )
     q = django_filters.CharFilter(
     q = django_filters.CharFilter(
         method='search',
         method='search',
         label='Search',
         label='Search',
     )
     )
+    family = django_filters.NumberFilter(
+        field_name='prefix',
+        lookup_expr='family'
+    )
     prefix = django_filters.CharFilter(
     prefix = django_filters.CharFilter(
         method='filter_prefix',
         method='filter_prefix',
         label='Prefix',
         label='Prefix',
@@ -214,7 +206,7 @@ class PrefixFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, Cre
 
 
     class Meta:
     class Meta:
         model = Prefix
         model = Prefix
-        fields = ['family', 'is_pool']
+        fields = ('is_pool',)
 
 
     def search(self, queryset, name, value):
     def search(self, queryset, name, value):
         if not value.strip():
         if not value.strip():
@@ -277,14 +269,14 @@ class PrefixFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, Cre
 
 
 
 
 class IPAddressFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
 class IPAddressFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
-    id__in = NumericInFilter(
-        field_name='id',
-        lookup_expr='in'
-    )
     q = django_filters.CharFilter(
     q = django_filters.CharFilter(
         method='search',
         method='search',
         label='Search',
         label='Search',
     )
     )
+    family = django_filters.NumberFilter(
+        field_name='address',
+        lookup_expr='family'
+    )
     parent = django_filters.CharFilter(
     parent = django_filters.CharFilter(
         method='search_by_parent',
         method='search_by_parent',
         label='Parent prefix',
         label='Parent prefix',
@@ -353,7 +345,7 @@ class IPAddressFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet,
 
 
     class Meta:
     class Meta:
         model = IPAddress
         model = IPAddress
-        fields = ['family', 'dns_name']
+        fields = ('dns_name',)
 
 
     def search(self, queryset, name, value):
     def search(self, queryset, name, value):
         if not value.strip():
         if not value.strip():
@@ -427,14 +419,10 @@ class VLANGroupFilterSet(BaseFilterSet, NameSlugSearchFilterSet):
 
 
     class Meta:
     class Meta:
         model = VLANGroup
         model = VLANGroup
-        fields = ['id', 'name', 'slug']
+        fields = ['id', 'name', 'slug', 'description']
 
 
 
 
 class VLANFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
 class VLANFilterSet(BaseFilterSet, TenancyFilterSet, CustomFieldFilterSet, CreatedUpdatedFilterSet):
-    id__in = NumericInFilter(
-        field_name='id',
-        lookup_expr='in'
-    )
     q = django_filters.CharFilter(
     q = django_filters.CharFilter(
         method='search',
         method='search',
         label='Search',
         label='Search',

+ 2 - 2
netbox/ipam/forms.py

@@ -116,7 +116,7 @@ class RIRForm(BootstrapMixin, forms.ModelForm):
     class Meta:
     class Meta:
         model = RIR
         model = RIR
         fields = [
         fields = [
-            'name', 'slug', 'is_private',
+            'name', 'slug', 'is_private', 'description',
         ]
         ]
 
 
 
 
@@ -989,7 +989,7 @@ class VLANGroupForm(BootstrapMixin, forms.ModelForm):
     class Meta:
     class Meta:
         model = VLANGroup
         model = VLANGroup
         fields = [
         fields = [
-            'site', 'name', 'slug',
+            'site', 'name', 'slug', 'description',
         ]
         ]
 
 
 
 

+ 8 - 3
netbox/ipam/lookups.py

@@ -154,9 +154,14 @@ class NetHostContained(Lookup):
         return 'CAST(HOST(%s) AS INET) << %s' % (lhs, rhs), params
         return 'CAST(HOST(%s) AS INET) << %s' % (lhs, rhs), params
 
 
 
 
-#
-# Transforms
-#
+class NetFamily(Transform):
+    lookup_name = 'family'
+    function = 'FAMILY'
+
+    @property
+    def output_field(self):
+        return IntegerField()
+
 
 
 class NetMaskLength(Transform):
 class NetMaskLength(Transform):
     function = 'MASKLEN'
     function = 'MASKLEN'

+ 1 - 1
netbox/ipam/managers.py

@@ -14,4 +14,4 @@ class IPAddressManager(models.Manager):
         IP address as a /32 or /128.
         IP address as a /32 or /128.
         """
         """
         qs = super().get_queryset()
         qs = super().get_queryset()
-        return qs.order_by('family', Inet(Host('address')))
+        return qs.order_by(Inet(Host('address')))

+ 38 - 0
netbox/ipam/migrations/0035_drop_ip_family.py

@@ -0,0 +1,38 @@
+# Generated by Django 2.2.9 on 2020-02-14 19:36
+
+from django.db import migrations
+import django.db.models.expressions
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('ipam', '0034_fix_ipaddress_status_dhcp'),
+    ]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name='aggregate',
+            options={'ordering': ('prefix', 'pk')},
+        ),
+        migrations.AlterModelOptions(
+            name='ipaddress',
+            options={'ordering': ('address', 'pk'), 'verbose_name': 'IP address', 'verbose_name_plural': 'IP addresses'},
+        ),
+        migrations.AlterModelOptions(
+            name='prefix',
+            options={'ordering': (django.db.models.expressions.OrderBy(django.db.models.expressions.F('vrf'), nulls_first=True), 'prefix', 'pk'), 'verbose_name_plural': 'prefixes'},
+        ),
+        migrations.RemoveField(
+            model_name='aggregate',
+            name='family',
+        ),
+        migrations.RemoveField(
+            model_name='ipaddress',
+            name='family',
+        ),
+        migrations.RemoveField(
+            model_name='prefix',
+            name='family',
+        ),
+    ]

+ 58 - 0
netbox/ipam/migrations/0036_standardize_description.py

@@ -0,0 +1,58 @@
+# Generated by Django 3.0.3 on 2020-03-13 20:27
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('ipam', '0035_drop_ip_family'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='rir',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AddField(
+            model_name='vlangroup',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='aggregate',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='ipaddress',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='prefix',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='role',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='service',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='vlan',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+        migrations.AlterField(
+            model_name='vrf',
+            name='description',
+            field=models.CharField(blank=True, max_length=200),
+        ),
+    ]

+ 45 - 41
netbox/ipam/models.py

@@ -65,7 +65,7 @@ class VRF(ChangeLoggedModel, CustomFieldModel):
         help_text='Prevent duplicate prefixes/IP addresses within this VRF'
         help_text='Prevent duplicate prefixes/IP addresses within this VRF'
     )
     )
     description = models.CharField(
     description = models.CharField(
-        max_length=100,
+        max_length=200,
         blank=True
         blank=True
     )
     )
     custom_field_values = GenericRelation(
     custom_field_values = GenericRelation(
@@ -125,8 +125,12 @@ class RIR(ChangeLoggedModel):
         verbose_name='Private',
         verbose_name='Private',
         help_text='IP space managed by this RIR is considered private'
         help_text='IP space managed by this RIR is considered private'
     )
     )
+    description = models.CharField(
+        max_length=200,
+        blank=True
+    )
 
 
-    csv_headers = ['name', 'slug', 'is_private']
+    csv_headers = ['name', 'slug', 'is_private', 'description']
 
 
     class Meta:
     class Meta:
         ordering = ['name']
         ordering = ['name']
@@ -144,6 +148,7 @@ class RIR(ChangeLoggedModel):
             self.name,
             self.name,
             self.slug,
             self.slug,
             self.is_private,
             self.is_private,
+            self.description,
         )
         )
 
 
 
 
@@ -153,9 +158,6 @@ class Aggregate(ChangeLoggedModel, CustomFieldModel):
     An aggregate exists at the root level of the IP address space hierarchy in NetBox. Aggregates are used to organize
     An aggregate exists at the root level of the IP address space hierarchy in NetBox. Aggregates are used to organize
     the hierarchy and track the overall utilization of available address space. Each Aggregate is assigned to a RIR.
     the hierarchy and track the overall utilization of available address space. Each Aggregate is assigned to a RIR.
     """
     """
-    family = models.PositiveSmallIntegerField(
-        choices=IPAddressFamilyChoices
-    )
     prefix = IPNetworkField()
     prefix = IPNetworkField()
     rir = models.ForeignKey(
     rir = models.ForeignKey(
         to='ipam.RIR',
         to='ipam.RIR',
@@ -168,7 +170,7 @@ class Aggregate(ChangeLoggedModel, CustomFieldModel):
         null=True
         null=True
     )
     )
     description = models.CharField(
     description = models.CharField(
-        max_length=100,
+        max_length=200,
         blank=True
         blank=True
     )
     )
     custom_field_values = GenericRelation(
     custom_field_values = GenericRelation(
@@ -185,7 +187,7 @@ class Aggregate(ChangeLoggedModel, CustomFieldModel):
     ]
     ]
 
 
     class Meta:
     class Meta:
-        ordering = ('family', 'prefix', 'pk')  # (family, prefix) may be non-unique
+        ordering = ('prefix', 'pk')  # prefix may be non-unique
 
 
     def __str__(self):
     def __str__(self):
         return str(self.prefix)
         return str(self.prefix)
@@ -228,12 +230,6 @@ class Aggregate(ChangeLoggedModel, CustomFieldModel):
                     )
                     )
                 })
                 })
 
 
-    def save(self, *args, **kwargs):
-        if self.prefix:
-            # Infer address family from IPNetwork object
-            self.family = self.prefix.version
-        super().save(*args, **kwargs)
-
     def to_csv(self):
     def to_csv(self):
         return (
         return (
             self.prefix,
             self.prefix,
@@ -242,6 +238,12 @@ class Aggregate(ChangeLoggedModel, CustomFieldModel):
             self.description,
             self.description,
         )
         )
 
 
+    @property
+    def family(self):
+        if self.prefix:
+            return self.prefix.version
+        return None
+
     def get_utilization(self):
     def get_utilization(self):
         """
         """
         Determine the prefix utilization of the aggregate and return it as a percentage.
         Determine the prefix utilization of the aggregate and return it as a percentage.
@@ -267,7 +269,7 @@ class Role(ChangeLoggedModel):
         default=1000
         default=1000
     )
     )
     description = models.CharField(
     description = models.CharField(
-        max_length=100,
+        max_length=200,
         blank=True,
         blank=True,
     )
     )
 
 
@@ -295,10 +297,6 @@ class Prefix(ChangeLoggedModel, CustomFieldModel):
     VRFs. A Prefix must be assigned a status and may optionally be assigned a used-define Role. A Prefix can also be
     VRFs. A Prefix must be assigned a status and may optionally be assigned a used-define Role. A Prefix can also be
     assigned to a VLAN where appropriate.
     assigned to a VLAN where appropriate.
     """
     """
-    family = models.PositiveSmallIntegerField(
-        choices=IPAddressFamilyChoices,
-        editable=False
-    )
     prefix = IPNetworkField(
     prefix = IPNetworkField(
         help_text='IPv4 or IPv6 network with mask'
         help_text='IPv4 or IPv6 network with mask'
     )
     )
@@ -353,7 +351,7 @@ class Prefix(ChangeLoggedModel, CustomFieldModel):
         help_text='All IP addresses within this prefix are considered usable'
         help_text='All IP addresses within this prefix are considered usable'
     )
     )
     description = models.CharField(
     description = models.CharField(
-        max_length=100,
+        max_length=200,
         blank=True
         blank=True
     )
     )
     custom_field_values = GenericRelation(
     custom_field_values = GenericRelation(
@@ -380,7 +378,7 @@ class Prefix(ChangeLoggedModel, CustomFieldModel):
     }
     }
 
 
     class Meta:
     class Meta:
-        ordering = (F('vrf').asc(nulls_first=True), 'family', 'prefix', 'pk')  # (vrf, family, prefix) may be non-unique
+        ordering = (F('vrf').asc(nulls_first=True), 'prefix', 'pk')  # (vrf, prefix) may be non-unique
         verbose_name_plural = 'prefixes'
         verbose_name_plural = 'prefixes'
 
 
     def __str__(self):
     def __str__(self):
@@ -427,9 +425,6 @@ class Prefix(ChangeLoggedModel, CustomFieldModel):
             # Clear host bits from prefix
             # Clear host bits from prefix
             self.prefix = self.prefix.cidr
             self.prefix = self.prefix.cidr
 
 
-            # Record address family
-            self.family = self.prefix.version
-
         super().save(*args, **kwargs)
         super().save(*args, **kwargs)
 
 
     def to_csv(self):
     def to_csv(self):
@@ -446,6 +441,12 @@ class Prefix(ChangeLoggedModel, CustomFieldModel):
             self.description,
             self.description,
         )
         )
 
 
+    @property
+    def family(self):
+        if self.prefix:
+            return self.prefix.version
+        return None
+
     def _set_prefix_length(self, value):
     def _set_prefix_length(self, value):
         """
         """
         Expose the IPNetwork object's prefixlen attribute on the parent model so that it can be manipulated directly,
         Expose the IPNetwork object's prefixlen attribute on the parent model so that it can be manipulated directly,
@@ -505,9 +506,9 @@ class Prefix(ChangeLoggedModel, CustomFieldModel):
 
 
         # All IP addresses within a point-to-point prefix (IPv4 /31 or IPv6 /127) are considered usable
         # All IP addresses within a point-to-point prefix (IPv4 /31 or IPv6 /127) are considered usable
         if (
         if (
-            self.family == 4 and self.prefix.prefixlen == 31  # RFC 3021
+            self.prefix.version == 4 and self.prefix.prefixlen == 31  # RFC 3021
         ) or (
         ) or (
-            self.family == 6 and self.prefix.prefixlen == 127  # RFC 6164
+            self.prefix.version == 6 and self.prefix.prefixlen == 127  # RFC 6164
         ):
         ):
             return available_ips
             return available_ips
 
 
@@ -550,7 +551,7 @@ class Prefix(ChangeLoggedModel, CustomFieldModel):
             # Compile an IPSet to avoid counting duplicate IPs
             # Compile an IPSet to avoid counting duplicate IPs
             child_count = netaddr.IPSet([ip.address.ip for ip in self.get_child_ips()]).size
             child_count = netaddr.IPSet([ip.address.ip for ip in self.get_child_ips()]).size
             prefix_size = self.prefix.size
             prefix_size = self.prefix.size
-            if self.family == 4 and self.prefix.prefixlen < 31 and not self.is_pool:
+            if self.prefix.version == 4 and self.prefix.prefixlen < 31 and not self.is_pool:
                 prefix_size -= 2
                 prefix_size -= 2
             return int(float(child_count) / prefix_size * 100)
             return int(float(child_count) / prefix_size * 100)
 
 
@@ -567,10 +568,6 @@ class IPAddress(ChangeLoggedModel, CustomFieldModel):
     for example, when mapping public addresses to private addresses. When an Interface has been assigned an IPAddress
     for example, when mapping public addresses to private addresses. When an Interface has been assigned an IPAddress
     which has a NAT outside IP, that Interface's Device can use either the inside or outside IP as its primary IP.
     which has a NAT outside IP, that Interface's Device can use either the inside or outside IP as its primary IP.
     """
     """
-    family = models.PositiveSmallIntegerField(
-        choices=IPAddressFamilyChoices,
-        editable=False
-    )
     address = IPAddressField(
     address = IPAddressField(
         help_text='IPv4 or IPv6 address (with mask)'
         help_text='IPv4 or IPv6 address (with mask)'
     )
     )
@@ -625,7 +622,7 @@ class IPAddress(ChangeLoggedModel, CustomFieldModel):
         help_text='Hostname or FQDN (not case-sensitive)'
         help_text='Hostname or FQDN (not case-sensitive)'
     )
     )
     description = models.CharField(
     description = models.CharField(
-        max_length=100,
+        max_length=200,
         blank=True
         blank=True
     )
     )
     custom_field_values = GenericRelation(
     custom_field_values = GenericRelation(
@@ -664,7 +661,7 @@ class IPAddress(ChangeLoggedModel, CustomFieldModel):
     }
     }
 
 
     class Meta:
     class Meta:
-        ordering = ('family', 'address', 'pk')  # (family, address) may be non-unique
+        ordering = ('address', 'pk')  # address may be non-unique
         verbose_name = 'IP address'
         verbose_name = 'IP address'
         verbose_name_plural = 'IP addresses'
         verbose_name_plural = 'IP addresses'
 
 
@@ -732,10 +729,6 @@ class IPAddress(ChangeLoggedModel, CustomFieldModel):
 
 
     def save(self, *args, **kwargs):
     def save(self, *args, **kwargs):
 
 
-        # Record address family
-        if isinstance(self.address, netaddr.IPNetwork):
-            self.family = self.address.version
-
         # Force dns_name to lowercase
         # Force dns_name to lowercase
         self.dns_name = self.dns_name.lower()
         self.dns_name = self.dns_name.lower()
 
 
@@ -759,9 +752,9 @@ class IPAddress(ChangeLoggedModel, CustomFieldModel):
     def to_csv(self):
     def to_csv(self):
 
 
         # Determine if this IP is primary for a Device
         # Determine if this IP is primary for a Device
-        if self.family == 4 and getattr(self, 'primary_ip4_for', False):
+        if self.address.version == 4 and getattr(self, 'primary_ip4_for', False):
             is_primary = True
             is_primary = True
-        elif self.family == 6 and getattr(self, 'primary_ip6_for', False):
+        elif self.address.version == 6 and getattr(self, 'primary_ip6_for', False):
             is_primary = True
             is_primary = True
         else:
         else:
             is_primary = False
             is_primary = False
@@ -780,6 +773,12 @@ class IPAddress(ChangeLoggedModel, CustomFieldModel):
             self.description,
             self.description,
         )
         )
 
 
+    @property
+    def family(self):
+        if self.address:
+            return self.address.version
+        return None
+
     def _set_mask_length(self, value):
     def _set_mask_length(self, value):
         """
         """
         Expose the IPNetwork object's prefixlen attribute on the parent model so that it can be manipulated directly,
         Expose the IPNetwork object's prefixlen attribute on the parent model so that it can be manipulated directly,
@@ -823,8 +822,12 @@ class VLANGroup(ChangeLoggedModel):
         blank=True,
         blank=True,
         null=True
         null=True
     )
     )
+    description = models.CharField(
+        max_length=200,
+        blank=True
+    )
 
 
-    csv_headers = ['name', 'slug', 'site']
+    csv_headers = ['name', 'slug', 'site', 'description']
 
 
     class Meta:
     class Meta:
         ordering = ('site', 'name', 'pk')  # (site, name) may be non-unique
         ordering = ('site', 'name', 'pk')  # (site, name) may be non-unique
@@ -846,6 +849,7 @@ class VLANGroup(ChangeLoggedModel):
             self.name,
             self.name,
             self.slug,
             self.slug,
             self.site.name if self.site else None,
             self.site.name if self.site else None,
+            self.description,
         )
         )
 
 
     def get_next_available_vid(self):
     def get_next_available_vid(self):
@@ -910,7 +914,7 @@ class VLAN(ChangeLoggedModel, CustomFieldModel):
         null=True
         null=True
     )
     )
     description = models.CharField(
     description = models.CharField(
-        max_length=100,
+        max_length=200,
         blank=True
         blank=True
     )
     )
     custom_field_values = GenericRelation(
     custom_field_values = GenericRelation(
@@ -1023,7 +1027,7 @@ class Service(ChangeLoggedModel, CustomFieldModel):
         verbose_name='IP addresses'
         verbose_name='IP addresses'
     )
     )
     description = models.CharField(
     description = models.CharField(
-        max_length=100,
+        max_length=200,
         blank=True
         blank=True
     )
     )
     custom_field_values = GenericRelation(
     custom_field_values = GenericRelation(

+ 2 - 2
netbox/ipam/tables.py

@@ -211,7 +211,7 @@ class RIRTable(BaseTable):
 
 
     class Meta(BaseTable.Meta):
     class Meta(BaseTable.Meta):
         model = RIR
         model = RIR
-        fields = ('pk', 'name', 'is_private', 'aggregate_count', 'actions')
+        fields = ('pk', 'name', 'is_private', 'aggregate_count', 'description', 'actions')
 
 
 
 
 class RIRDetailTable(RIRTable):
 class RIRDetailTable(RIRTable):
@@ -410,7 +410,7 @@ class VLANGroupTable(BaseTable):
 
 
     class Meta(BaseTable.Meta):
     class Meta(BaseTable.Meta):
         model = VLANGroup
         model = VLANGroup
-        fields = ('pk', 'name', 'site', 'vlan_count', 'slug', 'actions')
+        fields = ('pk', 'name', 'site', 'vlan_count', 'slug', 'description', 'actions')
 
 
 
 
 #
 #

+ 1 - 26
netbox/ipam/tests/test_api.py

@@ -7,7 +7,7 @@ from rest_framework import status
 from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site
 from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site
 from ipam.choices import *
 from ipam.choices import *
 from ipam.models import Aggregate, IPAddress, Prefix, RIR, Role, Service, VLAN, VLANGroup, VRF
 from ipam.models import Aggregate, IPAddress, Prefix, RIR, Role, Service, VLAN, VLANGroup, VRF
-from utilities.testing import APITestCase, choices_to_dict, disable_warnings
+from utilities.testing import APITestCase, disable_warnings
 
 
 
 
 class AppTest(APITestCase):
 class AppTest(APITestCase):
@@ -19,31 +19,6 @@ class AppTest(APITestCase):
 
 
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response.status_code, 200)
 
 
-    def test_choices(self):
-
-        url = reverse('ipam-api:field-choice-list')
-        response = self.client.get(url, **self.header)
-
-        self.assertEqual(response.status_code, 200)
-
-        # Aggregate
-        # self.assertEqual(choices_to_dict(response.data.get('aggregate:family')), )
-
-        # Prefix
-        # self.assertEqual(choices_to_dict(response.data.get('prefix:family')), )
-        self.assertEqual(choices_to_dict(response.data.get('prefix:status')), PrefixStatusChoices.as_dict())
-
-        # IPAddress
-        # self.assertEqual(choices_to_dict(response.data.get('ip-address:family')), )
-        self.assertEqual(choices_to_dict(response.data.get('ip-address:role')), IPAddressRoleChoices.as_dict())
-        self.assertEqual(choices_to_dict(response.data.get('ip-address:status')), IPAddressStatusChoices.as_dict())
-
-        # VLAN
-        self.assertEqual(choices_to_dict(response.data.get('vlan:status')), VLANStatusChoices.as_dict())
-
-        # Service
-        self.assertEqual(choices_to_dict(response.data.get('service:protocol')), ServiceProtocolChoices.as_dict())
-
 
 
 class VRFTest(APITestCase):
 class VRFTest(APITestCase):
 
 

+ 51 - 65
netbox/ipam/tests/test_filters.py

@@ -20,7 +20,8 @@ class VRFTestCase(TestCase):
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
         )
         )
-        TenantGroup.objects.bulk_create(tenant_groups)
+        for tenantgroup in tenant_groups:
+            tenantgroup.save()
 
 
         tenants = (
         tenants = (
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
@@ -53,11 +54,6 @@ class VRFTestCase(TestCase):
         params = {'enforce_unique': 'false'}
         params = {'enforce_unique': 'false'}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3)
 
 
-    def test_id__in(self):
-        id_list = self.queryset.values_list('id', flat=True)[:3]
-        params = {'id__in': ','.join([str(id) for id in id_list])}
-        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3)
-
     def test_tenant(self):
     def test_tenant(self):
         tenants = Tenant.objects.all()[:2]
         tenants = Tenant.objects.all()[:2]
         params = {'tenant_id': [tenants[0].pk, tenants[1].pk]}
         params = {'tenant_id': [tenants[0].pk, tenants[1].pk]}
@@ -81,12 +77,12 @@ class RIRTestCase(TestCase):
     def setUpTestData(cls):
     def setUpTestData(cls):
 
 
         rirs = (
         rirs = (
-            RIR(name='RIR 1', slug='rir-1', is_private=False),
-            RIR(name='RIR 2', slug='rir-2', is_private=False),
-            RIR(name='RIR 3', slug='rir-3', is_private=False),
-            RIR(name='RIR 4', slug='rir-4', is_private=True),
-            RIR(name='RIR 5', slug='rir-5', is_private=True),
-            RIR(name='RIR 6', slug='rir-6', is_private=True),
+            RIR(name='RIR 1', slug='rir-1', is_private=False, description='A'),
+            RIR(name='RIR 2', slug='rir-2', is_private=False, description='B'),
+            RIR(name='RIR 3', slug='rir-3', is_private=False, description='C'),
+            RIR(name='RIR 4', slug='rir-4', is_private=True, description='D'),
+            RIR(name='RIR 5', slug='rir-5', is_private=True, description='E'),
+            RIR(name='RIR 6', slug='rir-6', is_private=True, description='F'),
         )
         )
         RIR.objects.bulk_create(rirs)
         RIR.objects.bulk_create(rirs)
 
 
@@ -98,17 +94,16 @@ class RIRTestCase(TestCase):
         params = {'slug': ['rir-1', 'rir-2']}
         params = {'slug': ['rir-1', 'rir-2']}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
 
+    def test_description(self):
+        params = {'description': ['A', 'B']}
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
+
     def test_is_private(self):
     def test_is_private(self):
         params = {'is_private': 'true'}
         params = {'is_private': 'true'}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3)
         params = {'is_private': 'false'}
         params = {'is_private': 'false'}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3)
 
 
-    def test_id__in(self):
-        id_list = self.queryset.values_list('id', flat=True)[:3]
-        params = {'id__in': ','.join([str(id) for id in id_list])}
-        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3)
-
 
 
 class AggregateTestCase(TestCase):
 class AggregateTestCase(TestCase):
     queryset = Aggregate.objects.all()
     queryset = Aggregate.objects.all()
@@ -125,12 +120,12 @@ class AggregateTestCase(TestCase):
         RIR.objects.bulk_create(rirs)
         RIR.objects.bulk_create(rirs)
 
 
         aggregates = (
         aggregates = (
-            Aggregate(family=4, prefix='10.1.0.0/16', rir=rirs[0], date_added='2020-01-01'),
-            Aggregate(family=4, prefix='10.2.0.0/16', rir=rirs[0], date_added='2020-01-02'),
-            Aggregate(family=4, prefix='10.3.0.0/16', rir=rirs[1], date_added='2020-01-03'),
-            Aggregate(family=6, prefix='2001:db8:1::/48', rir=rirs[1], date_added='2020-01-04'),
-            Aggregate(family=6, prefix='2001:db8:2::/48', rir=rirs[2], date_added='2020-01-05'),
-            Aggregate(family=6, prefix='2001:db8:3::/48', rir=rirs[2], date_added='2020-01-06'),
+            Aggregate(prefix='10.1.0.0/16', rir=rirs[0], date_added='2020-01-01'),
+            Aggregate(prefix='10.2.0.0/16', rir=rirs[0], date_added='2020-01-02'),
+            Aggregate(prefix='10.3.0.0/16', rir=rirs[1], date_added='2020-01-03'),
+            Aggregate(prefix='2001:db8:1::/48', rir=rirs[1], date_added='2020-01-04'),
+            Aggregate(prefix='2001:db8:2::/48', rir=rirs[2], date_added='2020-01-05'),
+            Aggregate(prefix='2001:db8:3::/48', rir=rirs[2], date_added='2020-01-06'),
         )
         )
         Aggregate.objects.bulk_create(aggregates)
         Aggregate.objects.bulk_create(aggregates)
 
 
@@ -232,7 +227,8 @@ class PrefixTestCase(TestCase):
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
         )
         )
-        TenantGroup.objects.bulk_create(tenant_groups)
+        for tenantgroup in tenant_groups:
+            tenantgroup.save()
 
 
         tenants = (
         tenants = (
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
@@ -242,16 +238,16 @@ class PrefixTestCase(TestCase):
         Tenant.objects.bulk_create(tenants)
         Tenant.objects.bulk_create(tenants)
 
 
         prefixes = (
         prefixes = (
-            Prefix(family=4, prefix='10.0.0.0/24', tenant=None, site=None, vrf=None, vlan=None, role=None, is_pool=True),
-            Prefix(family=4, prefix='10.0.1.0/24', tenant=tenants[0], site=sites[0], vrf=vrfs[0], vlan=vlans[0], role=roles[0]),
-            Prefix(family=4, prefix='10.0.2.0/24', tenant=tenants[1], site=sites[1], vrf=vrfs[1], vlan=vlans[1], role=roles[1], status=PrefixStatusChoices.STATUS_DEPRECATED),
-            Prefix(family=4, prefix='10.0.3.0/24', tenant=tenants[2], site=sites[2], vrf=vrfs[2], vlan=vlans[2], role=roles[2], status=PrefixStatusChoices.STATUS_RESERVED),
-            Prefix(family=6, prefix='2001:db8::/64', tenant=None, site=None, vrf=None, vlan=None, role=None, is_pool=True),
-            Prefix(family=6, prefix='2001:db8:0:1::/64', tenant=tenants[0], site=sites[0], vrf=vrfs[0], vlan=vlans[0], role=roles[0]),
-            Prefix(family=6, prefix='2001:db8:0:2::/64', tenant=tenants[1], site=sites[1], vrf=vrfs[1], vlan=vlans[1], role=roles[1], status=PrefixStatusChoices.STATUS_DEPRECATED),
-            Prefix(family=6, prefix='2001:db8:0:3::/64', tenant=tenants[2], site=sites[2], vrf=vrfs[2], vlan=vlans[2], role=roles[2], status=PrefixStatusChoices.STATUS_RESERVED),
-            Prefix(family=4, prefix='10.0.0.0/16'),
-            Prefix(family=6, prefix='2001:db8::/32'),
+            Prefix(prefix='10.0.0.0/24', tenant=None, site=None, vrf=None, vlan=None, role=None, is_pool=True),
+            Prefix(prefix='10.0.1.0/24', tenant=tenants[0], site=sites[0], vrf=vrfs[0], vlan=vlans[0], role=roles[0]),
+            Prefix(prefix='10.0.2.0/24', tenant=tenants[1], site=sites[1], vrf=vrfs[1], vlan=vlans[1], role=roles[1], status=PrefixStatusChoices.STATUS_DEPRECATED),
+            Prefix(prefix='10.0.3.0/24', tenant=tenants[2], site=sites[2], vrf=vrfs[2], vlan=vlans[2], role=roles[2], status=PrefixStatusChoices.STATUS_RESERVED),
+            Prefix(prefix='2001:db8::/64', tenant=None, site=None, vrf=None, vlan=None, role=None, is_pool=True),
+            Prefix(prefix='2001:db8:0:1::/64', tenant=tenants[0], site=sites[0], vrf=vrfs[0], vlan=vlans[0], role=roles[0]),
+            Prefix(prefix='2001:db8:0:2::/64', tenant=tenants[1], site=sites[1], vrf=vrfs[1], vlan=vlans[1], role=roles[1], status=PrefixStatusChoices.STATUS_DEPRECATED),
+            Prefix(prefix='2001:db8:0:3::/64', tenant=tenants[2], site=sites[2], vrf=vrfs[2], vlan=vlans[2], role=roles[2], status=PrefixStatusChoices.STATUS_RESERVED),
+            Prefix(prefix='10.0.0.0/16'),
+            Prefix(prefix='2001:db8::/32'),
         )
         )
         Prefix.objects.bulk_create(prefixes)
         Prefix.objects.bulk_create(prefixes)
 
 
@@ -265,11 +261,6 @@ class PrefixTestCase(TestCase):
         params = {'is_pool': 'false'}
         params = {'is_pool': 'false'}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 8)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 8)
 
 
-    def test_id__in(self):
-        id_list = self.queryset.values_list('id', flat=True)[:3]
-        params = {'id__in': ','.join([str(id) for id in id_list])}
-        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3)
-
     def test_within(self):
     def test_within(self):
         params = {'within': '10.0.0.0/16'}
         params = {'within': '10.0.0.0/16'}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4)
@@ -394,7 +385,8 @@ class IPAddressTestCase(TestCase):
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
         )
         )
-        TenantGroup.objects.bulk_create(tenant_groups)
+        for tenantgroup in tenant_groups:
+            tenantgroup.save()
 
 
         tenants = (
         tenants = (
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
@@ -404,17 +396,16 @@ class IPAddressTestCase(TestCase):
         Tenant.objects.bulk_create(tenants)
         Tenant.objects.bulk_create(tenants)
 
 
         ipaddresses = (
         ipaddresses = (
-            IPAddress(family=4, address='10.0.0.1/24', tenant=None, vrf=None, interface=None, status=IPAddressStatusChoices.STATUS_ACTIVE, dns_name='ipaddress-a'),
-            IPAddress(family=4, address='10.0.0.2/24', tenant=tenants[0], vrf=vrfs[0], interface=interfaces[0], status=IPAddressStatusChoices.STATUS_ACTIVE, dns_name='ipaddress-b'),
-            IPAddress(family=4, address='10.0.0.3/24', tenant=tenants[1], vrf=vrfs[1], interface=interfaces[1], status=IPAddressStatusChoices.STATUS_RESERVED, role=IPAddressRoleChoices.ROLE_VIP, dns_name='ipaddress-c'),
-            IPAddress(family=4, address='10.0.0.4/24', tenant=tenants[2], vrf=vrfs[2], interface=interfaces[2], status=IPAddressStatusChoices.STATUS_DEPRECATED, role=IPAddressRoleChoices.ROLE_SECONDARY, dns_name='ipaddress-d'),
-            IPAddress(family=4, address='10.0.0.1/25', tenant=None, vrf=None, interface=None, status=IPAddressStatusChoices.STATUS_ACTIVE),
-            IPAddress(family=6, address='2001:db8::1/64', tenant=None, vrf=None, interface=None, status=IPAddressStatusChoices.STATUS_ACTIVE, dns_name='ipaddress-a'),
-            IPAddress(family=6, address='2001:db8::2/64', tenant=tenants[0], vrf=vrfs[0], interface=interfaces[3], status=IPAddressStatusChoices.STATUS_ACTIVE, dns_name='ipaddress-b'),
-            IPAddress(family=6, address='2001:db8::3/64', tenant=tenants[1], vrf=vrfs[1], interface=interfaces[4], status=IPAddressStatusChoices.STATUS_RESERVED, role=IPAddressRoleChoices.ROLE_VIP, dns_name='ipaddress-c'),
-            IPAddress(family=6, address='2001:db8::4/64', tenant=tenants[2], vrf=vrfs[2], interface=interfaces[5], status=IPAddressStatusChoices.STATUS_DEPRECATED, role=IPAddressRoleChoices.ROLE_SECONDARY, dns_name='ipaddress-d'),
-            IPAddress(family=6, address='2001:db8::1/65', tenant=None, vrf=None, interface=None, status=IPAddressStatusChoices.STATUS_ACTIVE),
-
+            IPAddress(address='10.0.0.1/24', tenant=None, vrf=None, interface=None, status=IPAddressStatusChoices.STATUS_ACTIVE, dns_name='ipaddress-a'),
+            IPAddress(address='10.0.0.2/24', tenant=tenants[0], vrf=vrfs[0], interface=interfaces[0], status=IPAddressStatusChoices.STATUS_ACTIVE, dns_name='ipaddress-b'),
+            IPAddress(address='10.0.0.3/24', tenant=tenants[1], vrf=vrfs[1], interface=interfaces[1], status=IPAddressStatusChoices.STATUS_RESERVED, role=IPAddressRoleChoices.ROLE_VIP, dns_name='ipaddress-c'),
+            IPAddress(address='10.0.0.4/24', tenant=tenants[2], vrf=vrfs[2], interface=interfaces[2], status=IPAddressStatusChoices.STATUS_DEPRECATED, role=IPAddressRoleChoices.ROLE_SECONDARY, dns_name='ipaddress-d'),
+            IPAddress(address='10.0.0.1/25', tenant=None, vrf=None, interface=None, status=IPAddressStatusChoices.STATUS_ACTIVE),
+            IPAddress(address='2001:db8::1/64', tenant=None, vrf=None, interface=None, status=IPAddressStatusChoices.STATUS_ACTIVE, dns_name='ipaddress-a'),
+            IPAddress(address='2001:db8::2/64', tenant=tenants[0], vrf=vrfs[0], interface=interfaces[3], status=IPAddressStatusChoices.STATUS_ACTIVE, dns_name='ipaddress-b'),
+            IPAddress(address='2001:db8::3/64', tenant=tenants[1], vrf=vrfs[1], interface=interfaces[4], status=IPAddressStatusChoices.STATUS_RESERVED, role=IPAddressRoleChoices.ROLE_VIP, dns_name='ipaddress-c'),
+            IPAddress(address='2001:db8::4/64', tenant=tenants[2], vrf=vrfs[2], interface=interfaces[5], status=IPAddressStatusChoices.STATUS_DEPRECATED, role=IPAddressRoleChoices.ROLE_SECONDARY, dns_name='ipaddress-d'),
+            IPAddress(address='2001:db8::1/65', tenant=None, vrf=None, interface=None, status=IPAddressStatusChoices.STATUS_ACTIVE),
         )
         )
         IPAddress.objects.bulk_create(ipaddresses)
         IPAddress.objects.bulk_create(ipaddresses)
 
 
@@ -426,11 +417,6 @@ class IPAddressTestCase(TestCase):
         params = {'dns_name': ['ipaddress-a', 'ipaddress-b']}
         params = {'dns_name': ['ipaddress-a', 'ipaddress-b']}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4)
 
 
-    def test_id__in(self):
-        id_list = self.queryset.values_list('id', flat=True)[:3]
-        params = {'id__in': ','.join([str(id) for id in id_list])}
-        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3)
-
     def test_parent(self):
     def test_parent(self):
         params = {'parent': '10.0.0.0/24'}
         params = {'parent': '10.0.0.0/24'}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 5)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 5)
@@ -537,9 +523,9 @@ class VLANGroupTestCase(TestCase):
         Site.objects.bulk_create(sites)
         Site.objects.bulk_create(sites)
 
 
         vlan_groups = (
         vlan_groups = (
-            VLANGroup(name='VLAN Group 1', slug='vlan-group-1', site=sites[0]),
-            VLANGroup(name='VLAN Group 2', slug='vlan-group-2', site=sites[1]),
-            VLANGroup(name='VLAN Group 3', slug='vlan-group-3', site=sites[2]),
+            VLANGroup(name='VLAN Group 1', slug='vlan-group-1', site=sites[0], description='A'),
+            VLANGroup(name='VLAN Group 2', slug='vlan-group-2', site=sites[1], description='B'),
+            VLANGroup(name='VLAN Group 3', slug='vlan-group-3', site=sites[2], description='C'),
             VLANGroup(name='VLAN Group 4', slug='vlan-group-4', site=None),
             VLANGroup(name='VLAN Group 4', slug='vlan-group-4', site=None),
         )
         )
         VLANGroup.objects.bulk_create(vlan_groups)
         VLANGroup.objects.bulk_create(vlan_groups)
@@ -557,6 +543,10 @@ class VLANGroupTestCase(TestCase):
         params = {'slug': ['vlan-group-1', 'vlan-group-2']}
         params = {'slug': ['vlan-group-1', 'vlan-group-2']}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
 
+    def test_description(self):
+        params = {'description': ['A', 'B']}
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
+
     def test_region(self):
     def test_region(self):
         regions = Region.objects.all()[:2]
         regions = Region.objects.all()[:2]
         params = {'region_id': [regions[0].pk, regions[1].pk]}
         params = {'region_id': [regions[0].pk, regions[1].pk]}
@@ -614,7 +604,8 @@ class VLANTestCase(TestCase):
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 2', slug='tenant-group-2'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
             TenantGroup(name='Tenant group 3', slug='tenant-group-3'),
         )
         )
-        TenantGroup.objects.bulk_create(tenant_groups)
+        for tenantgroup in tenant_groups:
+            tenantgroup.save()
 
 
         tenants = (
         tenants = (
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
             Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
@@ -641,11 +632,6 @@ class VLANTestCase(TestCase):
         params = {'vid': ['101', '201', '301']}
         params = {'vid': ['101', '201', '301']}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3)
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3)
 
 
-    def test_id__in(self):
-        id_list = self.queryset.values_list('id', flat=True)[:3]
-        params = {'id__in': ','.join([str(id) for id in id_list])}
-        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3)
-
     def test_region(self):
     def test_region(self):
         regions = Region.objects.all()[:2]
         regions = Region.objects.all()[:2]
         params = {'region_id': [regions[0].pk, regions[1].pk]}
         params = {'region_id': [regions[0].pk, regions[1].pk]}

+ 47 - 48
netbox/ipam/tests/test_models.py

@@ -15,22 +15,22 @@ class TestAggregate(TestCase):
 
 
         # 25% utilization
         # 25% utilization
         Prefix.objects.bulk_create((
         Prefix.objects.bulk_create((
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.0.0.0/12')),
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.16.0.0/12')),
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.32.0.0/12')),
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.48.0.0/12')),
+            Prefix(prefix=netaddr.IPNetwork('10.0.0.0/12')),
+            Prefix(prefix=netaddr.IPNetwork('10.16.0.0/12')),
+            Prefix(prefix=netaddr.IPNetwork('10.32.0.0/12')),
+            Prefix(prefix=netaddr.IPNetwork('10.48.0.0/12')),
         ))
         ))
         self.assertEqual(aggregate.get_utilization(), 25)
         self.assertEqual(aggregate.get_utilization(), 25)
 
 
         # 50% utilization
         # 50% utilization
         Prefix.objects.bulk_create((
         Prefix.objects.bulk_create((
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.64.0.0/10')),
+            Prefix(prefix=netaddr.IPNetwork('10.64.0.0/10')),
         ))
         ))
         self.assertEqual(aggregate.get_utilization(), 50)
         self.assertEqual(aggregate.get_utilization(), 50)
 
 
         # 100% utilization
         # 100% utilization
         Prefix.objects.bulk_create((
         Prefix.objects.bulk_create((
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.128.0.0/9')),
+            Prefix(prefix=netaddr.IPNetwork('10.128.0.0/9')),
         ))
         ))
         self.assertEqual(aggregate.get_utilization(), 100)
         self.assertEqual(aggregate.get_utilization(), 100)
 
 
@@ -39,9 +39,9 @@ class TestPrefix(TestCase):
 
 
     def test_get_duplicates(self):
     def test_get_duplicates(self):
         prefixes = Prefix.objects.bulk_create((
         prefixes = Prefix.objects.bulk_create((
-            Prefix(family=4, prefix=netaddr.IPNetwork('192.0.2.0/24')),
-            Prefix(family=4, prefix=netaddr.IPNetwork('192.0.2.0/24')),
-            Prefix(family=4, prefix=netaddr.IPNetwork('192.0.2.0/24')),
+            Prefix(prefix=netaddr.IPNetwork('192.0.2.0/24')),
+            Prefix(prefix=netaddr.IPNetwork('192.0.2.0/24')),
+            Prefix(prefix=netaddr.IPNetwork('192.0.2.0/24')),
         ))
         ))
         duplicate_prefix_pks = [p.pk for p in prefixes[0].get_duplicates()]
         duplicate_prefix_pks = [p.pk for p in prefixes[0].get_duplicates()]
 
 
@@ -54,11 +54,11 @@ class TestPrefix(TestCase):
             VRF(name='VRF 3'),
             VRF(name='VRF 3'),
         ))
         ))
         prefixes = Prefix.objects.bulk_create((
         prefixes = Prefix.objects.bulk_create((
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.0.0.0/16'), status=PrefixStatusChoices.STATUS_CONTAINER),
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.0.0.0/24'), vrf=None),
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.0.1.0/24'), vrf=vrfs[0]),
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.0.2.0/24'), vrf=vrfs[1]),
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.0.3.0/24'), vrf=vrfs[2]),
+            Prefix(prefix=netaddr.IPNetwork('10.0.0.0/16'), status=PrefixStatusChoices.STATUS_CONTAINER),
+            Prefix(prefix=netaddr.IPNetwork('10.0.0.0/24'), vrf=None),
+            Prefix(prefix=netaddr.IPNetwork('10.0.1.0/24'), vrf=vrfs[0]),
+            Prefix(prefix=netaddr.IPNetwork('10.0.2.0/24'), vrf=vrfs[1]),
+            Prefix(prefix=netaddr.IPNetwork('10.0.3.0/24'), vrf=vrfs[2]),
         ))
         ))
         child_prefix_pks = {p.pk for p in prefixes[0].get_child_prefixes()}
         child_prefix_pks = {p.pk for p in prefixes[0].get_child_prefixes()}
 
 
@@ -79,13 +79,13 @@ class TestPrefix(TestCase):
             VRF(name='VRF 3'),
             VRF(name='VRF 3'),
         ))
         ))
         parent_prefix = Prefix.objects.create(
         parent_prefix = Prefix.objects.create(
-            family=4, prefix=netaddr.IPNetwork('10.0.0.0/16'), status=PrefixStatusChoices.STATUS_CONTAINER
+            prefix=netaddr.IPNetwork('10.0.0.0/16'), status=PrefixStatusChoices.STATUS_CONTAINER
         )
         )
         ips = IPAddress.objects.bulk_create((
         ips = IPAddress.objects.bulk_create((
-            IPAddress(family=4, address=netaddr.IPNetwork('10.0.0.1/24'), vrf=None),
-            IPAddress(family=4, address=netaddr.IPNetwork('10.0.1.1/24'), vrf=vrfs[0]),
-            IPAddress(family=4, address=netaddr.IPNetwork('10.0.2.1/24'), vrf=vrfs[1]),
-            IPAddress(family=4, address=netaddr.IPNetwork('10.0.3.1/24'), vrf=vrfs[2]),
+            IPAddress(address=netaddr.IPNetwork('10.0.0.1/24'), vrf=None),
+            IPAddress(address=netaddr.IPNetwork('10.0.1.1/24'), vrf=vrfs[0]),
+            IPAddress(address=netaddr.IPNetwork('10.0.2.1/24'), vrf=vrfs[1]),
+            IPAddress(address=netaddr.IPNetwork('10.0.3.1/24'), vrf=vrfs[2]),
         ))
         ))
         child_ip_pks = {p.pk for p in parent_prefix.get_child_ips()}
         child_ip_pks = {p.pk for p in parent_prefix.get_child_ips()}
 
 
@@ -102,10 +102,10 @@ class TestPrefix(TestCase):
     def test_get_available_prefixes(self):
     def test_get_available_prefixes(self):
 
 
         prefixes = Prefix.objects.bulk_create((
         prefixes = Prefix.objects.bulk_create((
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.0.0.0/16')),  # Parent prefix
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.0.0.0/20')),
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.0.32.0/20')),
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.0.128.0/18')),
+            Prefix(prefix=netaddr.IPNetwork('10.0.0.0/16')),  # Parent prefix
+            Prefix(prefix=netaddr.IPNetwork('10.0.0.0/20')),
+            Prefix(prefix=netaddr.IPNetwork('10.0.32.0/20')),
+            Prefix(prefix=netaddr.IPNetwork('10.0.128.0/18')),
         ))
         ))
         missing_prefixes = netaddr.IPSet([
         missing_prefixes = netaddr.IPSet([
             netaddr.IPNetwork('10.0.16.0/20'),
             netaddr.IPNetwork('10.0.16.0/20'),
@@ -119,15 +119,15 @@ class TestPrefix(TestCase):
 
 
     def test_get_available_ips(self):
     def test_get_available_ips(self):
 
 
-        parent_prefix = Prefix.objects.create(family=4, prefix=netaddr.IPNetwork('10.0.0.0/28'))
+        parent_prefix = Prefix.objects.create(prefix=netaddr.IPNetwork('10.0.0.0/28'))
         IPAddress.objects.bulk_create((
         IPAddress.objects.bulk_create((
-            IPAddress(family=4, address=netaddr.IPNetwork('10.0.0.1/26')),
-            IPAddress(family=4, address=netaddr.IPNetwork('10.0.0.3/26')),
-            IPAddress(family=4, address=netaddr.IPNetwork('10.0.0.5/26')),
-            IPAddress(family=4, address=netaddr.IPNetwork('10.0.0.7/26')),
-            IPAddress(family=4, address=netaddr.IPNetwork('10.0.0.9/26')),
-            IPAddress(family=4, address=netaddr.IPNetwork('10.0.0.11/26')),
-            IPAddress(family=4, address=netaddr.IPNetwork('10.0.0.13/26')),
+            IPAddress(address=netaddr.IPNetwork('10.0.0.1/26')),
+            IPAddress(address=netaddr.IPNetwork('10.0.0.3/26')),
+            IPAddress(address=netaddr.IPNetwork('10.0.0.5/26')),
+            IPAddress(address=netaddr.IPNetwork('10.0.0.7/26')),
+            IPAddress(address=netaddr.IPNetwork('10.0.0.9/26')),
+            IPAddress(address=netaddr.IPNetwork('10.0.0.11/26')),
+            IPAddress(address=netaddr.IPNetwork('10.0.0.13/26')),
         ))
         ))
         missing_ips = netaddr.IPSet([
         missing_ips = netaddr.IPSet([
             '10.0.0.2/32',
             '10.0.0.2/32',
@@ -145,40 +145,39 @@ class TestPrefix(TestCase):
     def test_get_first_available_prefix(self):
     def test_get_first_available_prefix(self):
 
 
         prefixes = Prefix.objects.bulk_create((
         prefixes = Prefix.objects.bulk_create((
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.0.0.0/16')),  # Parent prefix
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.0.0.0/24')),
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.0.1.0/24')),
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.0.2.0/24')),
+            Prefix(prefix=netaddr.IPNetwork('10.0.0.0/16')),  # Parent prefix
+            Prefix(prefix=netaddr.IPNetwork('10.0.0.0/24')),
+            Prefix(prefix=netaddr.IPNetwork('10.0.1.0/24')),
+            Prefix(prefix=netaddr.IPNetwork('10.0.2.0/24')),
         ))
         ))
         self.assertEqual(prefixes[0].get_first_available_prefix(), netaddr.IPNetwork('10.0.3.0/24'))
         self.assertEqual(prefixes[0].get_first_available_prefix(), netaddr.IPNetwork('10.0.3.0/24'))
 
 
-        Prefix.objects.create(family=4, prefix=netaddr.IPNetwork('10.0.3.0/24'))
+        Prefix.objects.create(prefix=netaddr.IPNetwork('10.0.3.0/24'))
         self.assertEqual(prefixes[0].get_first_available_prefix(), netaddr.IPNetwork('10.0.4.0/22'))
         self.assertEqual(prefixes[0].get_first_available_prefix(), netaddr.IPNetwork('10.0.4.0/22'))
 
 
     def test_get_first_available_ip(self):
     def test_get_first_available_ip(self):
 
 
-        parent_prefix = Prefix.objects.create(family=4, prefix=netaddr.IPNetwork('10.0.0.0/24'))
+        parent_prefix = Prefix.objects.create(prefix=netaddr.IPNetwork('10.0.0.0/24'))
         IPAddress.objects.bulk_create((
         IPAddress.objects.bulk_create((
-            IPAddress(family=4, address=netaddr.IPNetwork('10.0.0.1/24')),
-            IPAddress(family=4, address=netaddr.IPNetwork('10.0.0.2/24')),
-            IPAddress(family=4, address=netaddr.IPNetwork('10.0.0.3/24')),
+            IPAddress(address=netaddr.IPNetwork('10.0.0.1/24')),
+            IPAddress(address=netaddr.IPNetwork('10.0.0.2/24')),
+            IPAddress(address=netaddr.IPNetwork('10.0.0.3/24')),
         ))
         ))
         self.assertEqual(parent_prefix.get_first_available_ip(), '10.0.0.4/24')
         self.assertEqual(parent_prefix.get_first_available_ip(), '10.0.0.4/24')
 
 
-        IPAddress.objects.create(family=4, address=netaddr.IPNetwork('10.0.0.4/24'))
+        IPAddress.objects.create(address=netaddr.IPNetwork('10.0.0.4/24'))
         self.assertEqual(parent_prefix.get_first_available_ip(), '10.0.0.5/24')
         self.assertEqual(parent_prefix.get_first_available_ip(), '10.0.0.5/24')
 
 
     def test_get_utilization(self):
     def test_get_utilization(self):
 
 
         # Container Prefix
         # Container Prefix
         prefix = Prefix.objects.create(
         prefix = Prefix.objects.create(
-            family=4,
             prefix=netaddr.IPNetwork('10.0.0.0/24'),
             prefix=netaddr.IPNetwork('10.0.0.0/24'),
             status=PrefixStatusChoices.STATUS_CONTAINER
             status=PrefixStatusChoices.STATUS_CONTAINER
         )
         )
         Prefix.objects.bulk_create((
         Prefix.objects.bulk_create((
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.0.0.0/26')),
-            Prefix(family=4, prefix=netaddr.IPNetwork('10.0.0.128/26')),
+            Prefix(prefix=netaddr.IPNetwork('10.0.0.0/26')),
+            Prefix(prefix=netaddr.IPNetwork('10.0.0.128/26')),
         ))
         ))
         self.assertEqual(prefix.get_utilization(), 50)
         self.assertEqual(prefix.get_utilization(), 50)
 
 
@@ -187,7 +186,7 @@ class TestPrefix(TestCase):
         prefix.save()
         prefix.save()
         IPAddress.objects.bulk_create(
         IPAddress.objects.bulk_create(
             # Create 32 IPAddresses within the Prefix
             # Create 32 IPAddresses within the Prefix
-            [IPAddress(family=4, address=netaddr.IPNetwork('10.0.0.{}/24'.format(i))) for i in range(1, 33)]
+            [IPAddress(address=netaddr.IPNetwork('10.0.0.{}/24'.format(i))) for i in range(1, 33)]
         )
         )
         self.assertEqual(prefix.get_utilization(), 12)  # ~= 12%
         self.assertEqual(prefix.get_utilization(), 12)  # ~= 12%
 
 
@@ -224,9 +223,9 @@ class TestIPAddress(TestCase):
 
 
     def test_get_duplicates(self):
     def test_get_duplicates(self):
         ips = IPAddress.objects.bulk_create((
         ips = IPAddress.objects.bulk_create((
-            IPAddress(family=4, address=netaddr.IPNetwork('192.0.2.1/24')),
-            IPAddress(family=4, address=netaddr.IPNetwork('192.0.2.1/24')),
-            IPAddress(family=4, address=netaddr.IPNetwork('192.0.2.1/24')),
+            IPAddress(address=netaddr.IPNetwork('192.0.2.1/24')),
+            IPAddress(address=netaddr.IPNetwork('192.0.2.1/24')),
+            IPAddress(address=netaddr.IPNetwork('192.0.2.1/24')),
         ))
         ))
         duplicate_ip_pks = [p.pk for p in ips[0].get_duplicates()]
         duplicate_ip_pks = [p.pk for p in ips[0].get_duplicates()]
 
 

+ 81 - 81
netbox/ipam/tests/test_ordering.py

@@ -42,45 +42,45 @@ class PrefixOrderingTestCase(OrderingTestBase):
 
 
         # Setup Prefixes
         # Setup Prefixes
         prefixes = (
         prefixes = (
-            Prefix(status=PrefixStatusChoices.STATUS_CONTAINER, vrf=None, family=4, prefix=netaddr.IPNetwork('192.168.0.0/16')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=None, family=4, prefix=netaddr.IPNetwork('192.168.0.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=None, family=4, prefix=netaddr.IPNetwork('192.168.1.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=None, family=4, prefix=netaddr.IPNetwork('192.168.2.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=None, family=4, prefix=netaddr.IPNetwork('192.168.3.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=None, family=4, prefix=netaddr.IPNetwork('192.168.4.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=None, family=4, prefix=netaddr.IPNetwork('192.168.5.0/24')),
-
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.0.0.0/8')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.0.0.0/16')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.0.0.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.0.1.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.0.2.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.0.3.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.0.4.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.1.0.0/16')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.1.1.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.1.2.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.1.3.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.1.4.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.2.0.0/16')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.2.1.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.2.2.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.2.3.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.2.4.0/24')),
-
-            Prefix(status=PrefixStatusChoices.STATUS_CONTAINER, vrf=vrfb, family=4, prefix=netaddr.IPNetwork('172.16.0.0/12')),
-            Prefix(status=PrefixStatusChoices.STATUS_CONTAINER, vrf=vrfb, family=4, prefix=netaddr.IPNetwork('172.16.0.0/16')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, prefix=netaddr.IPNetwork('172.16.0.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, prefix=netaddr.IPNetwork('172.16.1.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, prefix=netaddr.IPNetwork('172.16.2.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, prefix=netaddr.IPNetwork('172.16.3.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, prefix=netaddr.IPNetwork('172.16.4.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_CONTAINER, vrf=vrfb, family=4, prefix=netaddr.IPNetwork('172.17.0.0/16')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, prefix=netaddr.IPNetwork('172.17.0.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, prefix=netaddr.IPNetwork('172.17.1.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, prefix=netaddr.IPNetwork('172.17.2.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, prefix=netaddr.IPNetwork('172.17.3.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, prefix=netaddr.IPNetwork('172.17.4.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_CONTAINER, vrf=None, prefix=netaddr.IPNetwork('192.168.0.0/16')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=None, prefix=netaddr.IPNetwork('192.168.0.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=None, prefix=netaddr.IPNetwork('192.168.1.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=None, prefix=netaddr.IPNetwork('192.168.2.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=None, prefix=netaddr.IPNetwork('192.168.3.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=None, prefix=netaddr.IPNetwork('192.168.4.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=None, prefix=netaddr.IPNetwork('192.168.5.0/24')),
+
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.0.0.0/8')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.0.0.0/16')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.0.0.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.0.1.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.0.2.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.0.3.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.0.4.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.1.0.0/16')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.1.1.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.1.2.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.1.3.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.1.4.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.2.0.0/16')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.2.1.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.2.2.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.2.3.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.2.4.0/24')),
+
+            Prefix(status=PrefixStatusChoices.STATUS_CONTAINER, vrf=vrfb, prefix=netaddr.IPNetwork('172.16.0.0/12')),
+            Prefix(status=PrefixStatusChoices.STATUS_CONTAINER, vrf=vrfb, prefix=netaddr.IPNetwork('172.16.0.0/16')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, prefix=netaddr.IPNetwork('172.16.0.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, prefix=netaddr.IPNetwork('172.16.1.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, prefix=netaddr.IPNetwork('172.16.2.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, prefix=netaddr.IPNetwork('172.16.3.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, prefix=netaddr.IPNetwork('172.16.4.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_CONTAINER, vrf=vrfb, prefix=netaddr.IPNetwork('172.17.0.0/16')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, prefix=netaddr.IPNetwork('172.17.0.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, prefix=netaddr.IPNetwork('172.17.1.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, prefix=netaddr.IPNetwork('172.17.2.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, prefix=netaddr.IPNetwork('172.17.3.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfb, prefix=netaddr.IPNetwork('172.17.4.0/24')),
         )
         )
 
 
         Prefix.objects.bulk_create(prefixes)
         Prefix.objects.bulk_create(prefixes)
@@ -109,15 +109,15 @@ class PrefixOrderingTestCase(OrderingTestBase):
 
 
         # Setup Prefixes
         # Setup Prefixes
         prefixes = [
         prefixes = [
-            Prefix(status=PrefixStatusChoices.STATUS_CONTAINER, vrf=None, family=4, prefix=netaddr.IPNetwork('10.0.0.0/8')),
-            Prefix(status=PrefixStatusChoices.STATUS_CONTAINER, vrf=None, family=4, prefix=netaddr.IPNetwork('10.0.0.0/16')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=None, family=4, prefix=netaddr.IPNetwork('10.1.0.0/16')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=None, family=4, prefix=netaddr.IPNetwork('192.168.0.0/16')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.0.0.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.0.1.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.0.1.0/25')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.1.0.0/24')),
-            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, prefix=netaddr.IPNetwork('10.1.1.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_CONTAINER, vrf=None, prefix=netaddr.IPNetwork('10.0.0.0/8')),
+            Prefix(status=PrefixStatusChoices.STATUS_CONTAINER, vrf=None, prefix=netaddr.IPNetwork('10.0.0.0/16')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=None, prefix=netaddr.IPNetwork('10.1.0.0/16')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=None, prefix=netaddr.IPNetwork('192.168.0.0/16')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.0.0.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.0.1.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.0.1.0/25')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.1.0.0/24')),
+            Prefix(status=PrefixStatusChoices.STATUS_ACTIVE, vrf=vrfa, prefix=netaddr.IPNetwork('10.1.1.0/24')),
         ]
         ]
         Prefix.objects.bulk_create(prefixes)
         Prefix.objects.bulk_create(prefixes)
 
 
@@ -136,39 +136,39 @@ class IPAddressOrderingTestCase(OrderingTestBase):
 
 
         # Setup Addresses
         # Setup Addresses
         addresses = (
         addresses = (
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, address=netaddr.IPNetwork('10.0.0.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, address=netaddr.IPNetwork('10.0.1.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, address=netaddr.IPNetwork('10.0.2.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, address=netaddr.IPNetwork('10.0.3.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, address=netaddr.IPNetwork('10.0.4.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, address=netaddr.IPNetwork('10.1.0.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, address=netaddr.IPNetwork('10.1.1.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, address=netaddr.IPNetwork('10.1.2.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, address=netaddr.IPNetwork('10.1.3.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, address=netaddr.IPNetwork('10.1.4.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, address=netaddr.IPNetwork('10.2.0.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, address=netaddr.IPNetwork('10.2.1.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, address=netaddr.IPNetwork('10.2.2.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, address=netaddr.IPNetwork('10.2.3.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, family=4, address=netaddr.IPNetwork('10.2.4.1/24')),
-
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, address=netaddr.IPNetwork('172.16.0.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, address=netaddr.IPNetwork('172.16.1.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, address=netaddr.IPNetwork('172.16.2.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, address=netaddr.IPNetwork('172.16.3.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, address=netaddr.IPNetwork('172.16.4.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, address=netaddr.IPNetwork('172.17.0.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, address=netaddr.IPNetwork('172.17.1.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, address=netaddr.IPNetwork('172.17.2.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, address=netaddr.IPNetwork('172.17.3.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, family=4, address=netaddr.IPNetwork('172.17.4.1/24')),
-
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=None, family=4, address=netaddr.IPNetwork('192.168.0.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=None, family=4, address=netaddr.IPNetwork('192.168.1.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=None, family=4, address=netaddr.IPNetwork('192.168.2.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=None, family=4, address=netaddr.IPNetwork('192.168.3.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=None, family=4, address=netaddr.IPNetwork('192.168.4.1/24')),
-            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=None, family=4, address=netaddr.IPNetwork('192.168.5.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, address=netaddr.IPNetwork('10.0.0.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, address=netaddr.IPNetwork('10.0.1.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, address=netaddr.IPNetwork('10.0.2.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, address=netaddr.IPNetwork('10.0.3.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, address=netaddr.IPNetwork('10.0.4.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, address=netaddr.IPNetwork('10.1.0.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, address=netaddr.IPNetwork('10.1.1.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, address=netaddr.IPNetwork('10.1.2.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, address=netaddr.IPNetwork('10.1.3.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, address=netaddr.IPNetwork('10.1.4.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, address=netaddr.IPNetwork('10.2.0.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, address=netaddr.IPNetwork('10.2.1.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, address=netaddr.IPNetwork('10.2.2.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, address=netaddr.IPNetwork('10.2.3.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfa, address=netaddr.IPNetwork('10.2.4.1/24')),
+
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, address=netaddr.IPNetwork('172.16.0.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, address=netaddr.IPNetwork('172.16.1.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, address=netaddr.IPNetwork('172.16.2.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, address=netaddr.IPNetwork('172.16.3.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, address=netaddr.IPNetwork('172.16.4.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, address=netaddr.IPNetwork('172.17.0.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, address=netaddr.IPNetwork('172.17.1.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, address=netaddr.IPNetwork('172.17.2.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, address=netaddr.IPNetwork('172.17.3.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=vrfb, address=netaddr.IPNetwork('172.17.4.1/24')),
+
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=None, address=netaddr.IPNetwork('192.168.0.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=None, address=netaddr.IPNetwork('192.168.1.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=None, address=netaddr.IPNetwork('192.168.2.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=None, address=netaddr.IPNetwork('192.168.3.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=None, address=netaddr.IPNetwork('192.168.4.1/24')),
+            IPAddress(status=IPAddressStatusChoices.STATUS_ACTIVE, vrf=None, address=netaddr.IPNetwork('192.168.5.1/24')),
         )
         )
         IPAddress.objects.bulk_create(addresses)
         IPAddress.objects.bulk_create(addresses)
 
 

+ 19 - 18
netbox/ipam/tests/test_views.py

@@ -59,13 +59,14 @@ class RIRTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
             'name': 'RIR X',
             'name': 'RIR X',
             'slug': 'rir-x',
             'slug': 'rir-x',
             'is_private': True,
             'is_private': True,
+            'description': 'A new RIR',
         }
         }
 
 
         cls.csv_data = (
         cls.csv_data = (
-            "name,slug",
-            "RIR 4,rir-4",
-            "RIR 5,rir-5",
-            "RIR 6,rir-6",
+            "name,slug,description",
+            "RIR 4,rir-4,Fourth RIR",
+            "RIR 5,rir-5,Fifth RIR",
+            "RIR 6,rir-6,Sixth RIR",
         )
         )
 
 
 
 
@@ -82,13 +83,12 @@ class AggregateTestCase(ViewTestCases.PrimaryObjectViewTestCase):
         RIR.objects.bulk_create(rirs)
         RIR.objects.bulk_create(rirs)
 
 
         Aggregate.objects.bulk_create([
         Aggregate.objects.bulk_create([
-            Aggregate(family=4, prefix=IPNetwork('10.1.0.0/16'), rir=rirs[0]),
-            Aggregate(family=4, prefix=IPNetwork('10.2.0.0/16'), rir=rirs[0]),
-            Aggregate(family=4, prefix=IPNetwork('10.3.0.0/16'), rir=rirs[0]),
+            Aggregate(prefix=IPNetwork('10.1.0.0/16'), rir=rirs[0]),
+            Aggregate(prefix=IPNetwork('10.2.0.0/16'), rir=rirs[0]),
+            Aggregate(prefix=IPNetwork('10.3.0.0/16'), rir=rirs[0]),
         ])
         ])
 
 
         cls.form_data = {
         cls.form_data = {
-            'family': 4,
             'prefix': IPNetwork('10.99.0.0/16'),
             'prefix': IPNetwork('10.99.0.0/16'),
             'rir': rirs[1].pk,
             'rir': rirs[1].pk,
             'date_added': datetime.date(2020, 1, 1),
             'date_added': datetime.date(2020, 1, 1),
@@ -161,9 +161,9 @@ class PrefixTestCase(ViewTestCases.PrimaryObjectViewTestCase):
         )
         )
 
 
         Prefix.objects.bulk_create([
         Prefix.objects.bulk_create([
-            Prefix(family=4, prefix=IPNetwork('10.1.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]),
-            Prefix(family=4, prefix=IPNetwork('10.2.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]),
-            Prefix(family=4, prefix=IPNetwork('10.3.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]),
+            Prefix(prefix=IPNetwork('10.1.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]),
+            Prefix(prefix=IPNetwork('10.2.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]),
+            Prefix(prefix=IPNetwork('10.3.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]),
         ])
         ])
 
 
         cls.form_data = {
         cls.form_data = {
@@ -210,9 +210,9 @@ class IPAddressTestCase(ViewTestCases.PrimaryObjectViewTestCase):
         VRF.objects.bulk_create(vrfs)
         VRF.objects.bulk_create(vrfs)
 
 
         IPAddress.objects.bulk_create([
         IPAddress.objects.bulk_create([
-            IPAddress(family=4, address=IPNetwork('192.0.2.1/24'), vrf=vrfs[0]),
-            IPAddress(family=4, address=IPNetwork('192.0.2.2/24'), vrf=vrfs[0]),
-            IPAddress(family=4, address=IPNetwork('192.0.2.3/24'), vrf=vrfs[0]),
+            IPAddress(address=IPNetwork('192.0.2.1/24'), vrf=vrfs[0]),
+            IPAddress(address=IPNetwork('192.0.2.2/24'), vrf=vrfs[0]),
+            IPAddress(address=IPNetwork('192.0.2.3/24'), vrf=vrfs[0]),
         ])
         ])
 
 
         cls.form_data = {
         cls.form_data = {
@@ -263,13 +263,14 @@ class VLANGroupTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
             'name': 'VLAN Group X',
             'name': 'VLAN Group X',
             'slug': 'vlan-group-x',
             'slug': 'vlan-group-x',
             'site': site.pk,
             'site': site.pk,
+            'description': 'A new VLAN group',
         }
         }
 
 
         cls.csv_data = (
         cls.csv_data = (
-            "name,slug",
-            "VLAN Group 4,vlan-group-4",
-            "VLAN Group 5,vlan-group-5",
-            "VLAN Group 6,vlan-group-6",
+            "name,slug,description",
+            "VLAN Group 4,vlan-group-4,Fourth VLAN group",
+            "VLAN Group 5,vlan-group-5,Fifth VLAN group",
+            "VLAN Group 6,vlan-group-6,Sixth VLAN group",
         )
         )
 
 
 
 

+ 1 - 1
netbox/ipam/views.py

@@ -207,7 +207,7 @@ class RIRListView(PermissionRequiredMixin, ObjectListView):
                 'deprecated': 0,
                 'deprecated': 0,
                 'available': 0,
                 'available': 0,
             }
             }
-            aggregate_list = Aggregate.objects.filter(family=family, rir=rir)
+            aggregate_list = Aggregate.objects.filter(prefix__family=family, rir=rir)
             for aggregate in aggregate_list:
             for aggregate in aggregate_list:
 
 
                 queryset = Prefix.objects.filter(prefix__net_contained_or_equal=str(aggregate.prefix))
                 queryset = Prefix.objects.filter(prefix__net_contained_or_equal=str(aggregate.prefix))

+ 1 - 0
netbox/netbox/admin.py

@@ -8,6 +8,7 @@ from taggit.models import Tag
 admin_site.site_header = 'NetBox Administration'
 admin_site.site_header = 'NetBox Administration'
 admin_site.site_title = 'NetBox'
 admin_site.site_title = 'NetBox'
 admin_site.site_url = '/{}'.format(settings.BASE_PATH)
 admin_site.site_url = '/{}'.format(settings.BASE_PATH)
+admin_site.index_template = 'admin/index.html'
 
 
 # Unregister the unused stock Tag model provided by django-taggit
 # Unregister the unused stock Tag model provided by django-taggit
 admin_site.unregister(Tag)
 admin_site.unregister(Tag)

+ 26 - 0
netbox/netbox/configuration.example.py

@@ -175,10 +175,24 @@ NAPALM_ARGS = {}
 # Determine how many objects to display per page within a list. (Default: 50)
 # Determine how many objects to display per page within a list. (Default: 50)
 PAGINATE_COUNT = 50
 PAGINATE_COUNT = 50
 
 
+# Enable installed plugins. Add the name of each plugin to the list.
+PLUGINS = []
+
+# Configure enabled plugins. This should be a dictionary of dictionaries, mapping each plugin by name to its configuration parameters.
+PLUGINS_CONFIG = {}
+
 # When determining the primary IP address for a device, IPv6 is preferred over IPv4 by default. Set this to True to
 # When determining the primary IP address for a device, IPv6 is preferred over IPv4 by default. Set this to True to
 # prefer IPv4 instead.
 # prefer IPv4 instead.
 PREFER_IPV4 = False
 PREFER_IPV4 = False
 
 
+# Remote authentication support
+REMOTE_AUTH_ENABLED = False
+REMOTE_AUTH_BACKEND = 'utilities.auth_backends.RemoteUserBackend'
+REMOTE_AUTH_HEADER = 'HTTP_REMOTE_USER'
+REMOTE_AUTH_AUTO_CREATE_USER = True
+REMOTE_AUTH_DEFAULT_GROUPS = []
+REMOTE_AUTH_DEFAULT_PERMISSIONS = []
+
 # This determines how often the GitHub API is called to check the latest release of NetBox. Must be at least 1 hour.
 # This determines how often the GitHub API is called to check the latest release of NetBox. Must be at least 1 hour.
 RELEASE_CHECK_TIMEOUT = 24 * 3600
 RELEASE_CHECK_TIMEOUT = 24 * 3600
 
 
@@ -195,6 +209,18 @@ RELEASE_CHECK_URL = None
 # this setting is derived from the installed location.
 # this setting is derived from the installed location.
 # SCRIPTS_ROOT = '/opt/netbox/netbox/scripts'
 # SCRIPTS_ROOT = '/opt/netbox/netbox/scripts'
 
 
+# Enable plugin support in netbox. This setting must be enabled for any installed plugins to function.
+PLUGINS_ENABLED = False
+
+# Plugins configuration settings. These settings are used by various plugins that the user may have installed.
+# Each key in the dictionary is the name of an installed plugin and its value is a dictionary of settings.
+# PLUGINS_CONFIG = {
+#     'my_plugin': {
+#         'foo': 'bar',
+#         'buzz': 'bazz'
+#     }
+# }
+
 # By default, NetBox will store session data in the database. Alternatively, a file path can be specified here to use
 # By default, NetBox will store session data in the database. Alternatively, a file path can be specified here to use
 # local file storage instead. (This can be useful for enabling authentication on a standby instance with read-only
 # local file storage instead. (This can be useful for enabling authentication on a standby instance with read-only
 # database access.) Note that the user as which NetBox runs must have read and write permissions to this path.
 # database access.) Note that the user as which NetBox runs must have read and write permissions to this path.

+ 40 - 0
netbox/netbox/configuration.testing.py

@@ -0,0 +1,40 @@
+###################################################################
+#  This file serves as a base configuration for testing purposes  #
+#  only. It is not intended for production use.                   #
+###################################################################
+
+ALLOWED_HOSTS = ['*']
+
+DATABASE = {
+    'NAME': 'netbox',
+    'USER': '',
+    'PASSWORD': '',
+    'HOST': 'localhost',
+    'PORT': '',
+    'CONN_MAX_AGE': 300,
+}
+
+PLUGINS = [
+    'extras.tests.dummy_plugin',
+]
+
+REDIS = {
+    'tasks': {
+        'HOST': 'localhost',
+        'PORT': 6379,
+        'PASSWORD': '',
+        'DATABASE': 0,
+        'DEFAULT_TIMEOUT': 300,
+        'SSL': False,
+    },
+    'caching': {
+        'HOST': 'localhost',
+        'PORT': 6379,
+        'PASSWORD': '',
+        'DATABASE': 1,
+        'DEFAULT_TIMEOUT': 300,
+        'SSL': False,
+    }
+}
+
+SECRET_KEY = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'

+ 62 - 14
netbox/netbox/settings.py

@@ -1,3 +1,4 @@
+import importlib
 import logging
 import logging
 import os
 import os
 import platform
 import platform
@@ -15,7 +16,7 @@ from django.core.validators import URLValidator
 # Environment setup
 # Environment setup
 #
 #
 
 
-VERSION = '2.7.13-dev'
+VERSION = '2.8.0-dev'
 
 
 # Hostname
 # Hostname
 HOSTNAME = platform.node()
 HOSTNAME = platform.node()
@@ -24,15 +25,9 @@ HOSTNAME = platform.node()
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
 
 # Validate Python version
 # Validate Python version
-if platform.python_version_tuple() < ('3', '5'):
+if platform.python_version_tuple() < ('3', '6'):
     raise RuntimeError(
     raise RuntimeError(
-        "NetBox requires Python 3.5 or higher (current: Python {})".format(platform.python_version())
-    )
-elif platform.python_version_tuple() < ('3', '6'):
-    warnings.warn(
-        "Python 3.6 or higher will be required starting with NetBox v2.8 (current: Python {})".format(
-            platform.python_version()
-        )
+        "NetBox requires Python 3.6 or higher (current: Python {})".format(platform.python_version())
     )
     )
 
 
 
 
@@ -96,7 +91,15 @@ NAPALM_PASSWORD = getattr(configuration, 'NAPALM_PASSWORD', '')
 NAPALM_TIMEOUT = getattr(configuration, 'NAPALM_TIMEOUT', 30)
 NAPALM_TIMEOUT = getattr(configuration, 'NAPALM_TIMEOUT', 30)
 NAPALM_USERNAME = getattr(configuration, 'NAPALM_USERNAME', '')
 NAPALM_USERNAME = getattr(configuration, 'NAPALM_USERNAME', '')
 PAGINATE_COUNT = getattr(configuration, 'PAGINATE_COUNT', 50)
 PAGINATE_COUNT = getattr(configuration, 'PAGINATE_COUNT', 50)
+PLUGINS = getattr(configuration, 'PLUGINS', [])
+PLUGINS_CONFIG = getattr(configuration, 'PLUGINS_CONFIG', {})
 PREFER_IPV4 = getattr(configuration, 'PREFER_IPV4', False)
 PREFER_IPV4 = getattr(configuration, 'PREFER_IPV4', False)
+REMOTE_AUTH_AUTO_CREATE_USER = getattr(configuration, 'REMOTE_AUTH_AUTO_CREATE_USER', False)
+REMOTE_AUTH_BACKEND = getattr(configuration, 'REMOTE_AUTH_BACKEND', 'utilities.auth_backends.RemoteUserBackend')
+REMOTE_AUTH_DEFAULT_GROUPS = getattr(configuration, 'REMOTE_AUTH_DEFAULT_GROUPS', [])
+REMOTE_AUTH_DEFAULT_PERMISSIONS = getattr(configuration, 'REMOTE_AUTH_DEFAULT_PERMISSIONS', [])
+REMOTE_AUTH_ENABLED = getattr(configuration, 'REMOTE_AUTH_ENABLED', False)
+REMOTE_AUTH_HEADER = getattr(configuration, 'REMOTE_AUTH_HEADER', 'HTTP_REMOTE_USER')
 RELEASE_CHECK_URL = getattr(configuration, 'RELEASE_CHECK_URL', None)
 RELEASE_CHECK_URL = getattr(configuration, 'RELEASE_CHECK_URL', None)
 RELEASE_CHECK_TIMEOUT = getattr(configuration, 'RELEASE_CHECK_TIMEOUT', 24 * 3600)
 RELEASE_CHECK_TIMEOUT = getattr(configuration, 'RELEASE_CHECK_TIMEOUT', 24 * 3600)
 REPORTS_ROOT = getattr(configuration, 'REPORTS_ROOT', os.path.join(BASE_DIR, 'reports')).rstrip('/')
 REPORTS_ROOT = getattr(configuration, 'REPORTS_ROOT', os.path.join(BASE_DIR, 'reports')).rstrip('/')
@@ -287,7 +290,7 @@ INSTALLED_APPS = [
 ]
 ]
 
 
 # Middleware
 # Middleware
-MIDDLEWARE = (
+MIDDLEWARE = [
     'debug_toolbar.middleware.DebugToolbarMiddleware',
     'debug_toolbar.middleware.DebugToolbarMiddleware',
     'django_prometheus.middleware.PrometheusBeforeMiddleware',
     'django_prometheus.middleware.PrometheusBeforeMiddleware',
     'corsheaders.middleware.CorsMiddleware',
     'corsheaders.middleware.CorsMiddleware',
@@ -299,11 +302,12 @@ MIDDLEWARE = (
     'django.middleware.clickjacking.XFrameOptionsMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware',
     'django.middleware.security.SecurityMiddleware',
     'django.middleware.security.SecurityMiddleware',
     'utilities.middleware.ExceptionHandlingMiddleware',
     'utilities.middleware.ExceptionHandlingMiddleware',
+    'utilities.middleware.RemoteUserMiddleware',
     'utilities.middleware.LoginRequiredMiddleware',
     'utilities.middleware.LoginRequiredMiddleware',
     'utilities.middleware.APIVersionMiddleware',
     'utilities.middleware.APIVersionMiddleware',
     'extras.middleware.ObjectChangeMiddleware',
     'extras.middleware.ObjectChangeMiddleware',
     'django_prometheus.middleware.PrometheusAfterMiddleware',
     'django_prometheus.middleware.PrometheusAfterMiddleware',
-)
+]
 
 
 ROOT_URLCONF = 'netbox.urls'
 ROOT_URLCONF = 'netbox.urls'
 
 
@@ -320,14 +324,15 @@ TEMPLATES = [
                 'django.template.context_processors.media',
                 'django.template.context_processors.media',
                 'django.contrib.auth.context_processors.auth',
                 'django.contrib.auth.context_processors.auth',
                 'django.contrib.messages.context_processors.messages',
                 'django.contrib.messages.context_processors.messages',
-                'utilities.context_processors.settings',
+                'utilities.context_processors.settings_and_registry',
             ],
             ],
         },
         },
     },
     },
 ]
 ]
 
 
-# Authentication
+# Set up authentication backends
 AUTHENTICATION_BACKENDS = [
 AUTHENTICATION_BACKENDS = [
+    REMOTE_AUTH_BACKEND,
     'utilities.auth_backends.ViewExemptModelBackend',
     'utilities.auth_backends.ViewExemptModelBackend',
 ]
 ]
 
 
@@ -340,6 +345,7 @@ USE_TZ = True
 WSGI_APPLICATION = 'netbox.wsgi.application'
 WSGI_APPLICATION = 'netbox.wsgi.application'
 SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
 SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
 USE_X_FORWARDED_HOST = True
 USE_X_FORWARDED_HOST = True
+X_FRAME_OPTIONS = 'SAMEORIGIN'
 
 
 # Static files (CSS, JavaScript, Images)
 # Static files (CSS, JavaScript, Images)
 STATIC_ROOT = BASE_DIR + '/static'
 STATIC_ROOT = BASE_DIR + '/static'
@@ -551,7 +557,6 @@ SWAGGER_SETTINGS = {
         'drf_yasg.inspectors.StringDefaultFieldInspector',
         'drf_yasg.inspectors.StringDefaultFieldInspector',
     ],
     ],
     'DEFAULT_FILTER_INSPECTORS': [
     'DEFAULT_FILTER_INSPECTORS': [
-        'utilities.custom_inspectors.IdInFilterInspector',
         'drf_yasg.inspectors.CoreAPICompatInspector',
         'drf_yasg.inspectors.CoreAPICompatInspector',
     ],
     ],
     'DEFAULT_INFO': 'netbox.urls.openapi_info',
     'DEFAULT_INFO': 'netbox.urls.openapi_info',
@@ -626,3 +631,46 @@ PER_PAGE_DEFAULTS = [
 if PAGINATE_COUNT not in PER_PAGE_DEFAULTS:
 if PAGINATE_COUNT not in PER_PAGE_DEFAULTS:
     PER_PAGE_DEFAULTS.append(PAGINATE_COUNT)
     PER_PAGE_DEFAULTS.append(PAGINATE_COUNT)
     PER_PAGE_DEFAULTS = sorted(PER_PAGE_DEFAULTS)
     PER_PAGE_DEFAULTS = sorted(PER_PAGE_DEFAULTS)
+
+
+#
+# Plugins
+#
+
+for plugin_name in PLUGINS:
+
+    # Import plugin module
+    try:
+        plugin = importlib.import_module(plugin_name)
+    except ImportError:
+        raise ImproperlyConfigured(
+            f"Unable to import plugin {plugin_name}: Module not found. Check that the plugin module has been "
+            f"installed within the correct Python environment."
+        )
+
+    # Determine plugin config and add to INSTALLED_APPS.
+    try:
+        plugin_config = plugin.config
+        INSTALLED_APPS.append(f"{plugin_config.__module__}.{plugin_config.__name__}")
+    except AttributeError:
+        raise ImproperlyConfigured(
+            f"Plugin {plugin_name} does not provide a 'config' variable. This should be defined in the plugin's "
+            f"__init__.py file and point to the PluginConfig subclass."
+        )
+
+    # Validate user-provided configuration settings and assign defaults
+    if plugin_name not in PLUGINS_CONFIG:
+        PLUGINS_CONFIG[plugin_name] = {}
+    plugin_config.validate(PLUGINS_CONFIG[plugin_name])
+
+    # Add middleware
+    plugin_middleware = plugin_config.middleware
+    if plugin_middleware and type(plugin_middleware) in (list, tuple):
+        MIDDLEWARE.extend(plugin_middleware)
+
+    # Apply cacheops config
+    if type(plugin_config.caching_config) is not dict:
+        raise ImproperlyConfigured(f"Plugin {plugin_name} caching_config must be a dictionary.")
+    CACHEOPS.update({
+        f"{plugin_name}.{key}": value for key, value in plugin_config.caching_config.items()
+    })

+ 159 - 0
netbox/netbox/tests/test_authentication.py

@@ -0,0 +1,159 @@
+from django.conf import settings
+from django.contrib.auth.models import Group, User
+from django.test import Client, TestCase
+from django.test.utils import override_settings
+from django.urls import reverse
+
+
+class ExternalAuthenticationTestCase(TestCase):
+
+    @classmethod
+    def setUpTestData(cls):
+        cls.user = User.objects.create(username='remoteuser1')
+
+    def setUp(self):
+        self.client = Client()
+
+    @override_settings(
+        LOGIN_REQUIRED=True
+    )
+    def test_remote_auth_disabled(self):
+        """
+        Test enabling remote authentication with the default configuration.
+        """
+        headers = {
+            'HTTP_REMOTE_USER': 'remoteuser1',
+        }
+
+        self.assertFalse(settings.REMOTE_AUTH_ENABLED)
+        self.assertEqual(settings.REMOTE_AUTH_HEADER, 'HTTP_REMOTE_USER')
+
+        # Client should not be authenticated
+        response = self.client.get(reverse('home'), follow=True, **headers)
+        self.assertNotIn('_auth_user_id', self.client.session)
+
+    @override_settings(
+        REMOTE_AUTH_ENABLED=True,
+        LOGIN_REQUIRED=True
+    )
+    def test_remote_auth_enabled(self):
+        """
+        Test enabling remote authentication with the default configuration.
+        """
+        headers = {
+            'HTTP_REMOTE_USER': 'remoteuser1',
+        }
+
+        self.assertTrue(settings.REMOTE_AUTH_ENABLED)
+        self.assertEqual(settings.REMOTE_AUTH_HEADER, 'HTTP_REMOTE_USER')
+
+        response = self.client.get(reverse('home'), follow=True, **headers)
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(int(self.client.session.get('_auth_user_id')), self.user.pk, msg='Authentication failed')
+
+    @override_settings(
+        REMOTE_AUTH_ENABLED=True,
+        REMOTE_AUTH_HEADER='HTTP_FOO',
+        LOGIN_REQUIRED=True
+    )
+    def test_remote_auth_custom_header(self):
+        """
+        Test enabling remote authentication with a custom HTTP header.
+        """
+        headers = {
+            'HTTP_FOO': 'remoteuser1',
+        }
+
+        self.assertTrue(settings.REMOTE_AUTH_ENABLED)
+        self.assertEqual(settings.REMOTE_AUTH_HEADER, 'HTTP_FOO')
+
+        response = self.client.get(reverse('home'), follow=True, **headers)
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(int(self.client.session.get('_auth_user_id')), self.user.pk, msg='Authentication failed')
+
+    @override_settings(
+        REMOTE_AUTH_ENABLED=True,
+        REMOTE_AUTH_AUTO_CREATE_USER=True,
+        LOGIN_REQUIRED=True
+    )
+    def test_remote_auth_auto_create(self):
+        """
+        Test enabling remote authentication with automatic user creation disabled.
+        """
+        headers = {
+            'HTTP_REMOTE_USER': 'remoteuser2',
+        }
+
+        self.assertTrue(settings.REMOTE_AUTH_ENABLED)
+        self.assertTrue(settings.REMOTE_AUTH_AUTO_CREATE_USER)
+        self.assertEqual(settings.REMOTE_AUTH_HEADER, 'HTTP_REMOTE_USER')
+
+        response = self.client.get(reverse('home'), follow=True, **headers)
+        self.assertEqual(response.status_code, 200)
+
+        # Local user should have been automatically created
+        new_user = User.objects.get(username='remoteuser2')
+        self.assertEqual(int(self.client.session.get('_auth_user_id')), new_user.pk, msg='Authentication failed')
+
+    @override_settings(
+        REMOTE_AUTH_ENABLED=True,
+        REMOTE_AUTH_AUTO_CREATE_USER=True,
+        REMOTE_AUTH_DEFAULT_GROUPS=['Group 1', 'Group 2'],
+        LOGIN_REQUIRED=True
+    )
+    def test_remote_auth_default_groups(self):
+        """
+        Test enabling remote authentication with the default configuration.
+        """
+        headers = {
+            'HTTP_REMOTE_USER': 'remoteuser2',
+        }
+
+        self.assertTrue(settings.REMOTE_AUTH_ENABLED)
+        self.assertTrue(settings.REMOTE_AUTH_AUTO_CREATE_USER)
+        self.assertEqual(settings.REMOTE_AUTH_HEADER, 'HTTP_REMOTE_USER')
+        self.assertEqual(settings.REMOTE_AUTH_DEFAULT_GROUPS, ['Group 1', 'Group 2'])
+
+        # Create required groups
+        groups = (
+            Group(name='Group 1'),
+            Group(name='Group 2'),
+            Group(name='Group 3'),
+        )
+        Group.objects.bulk_create(groups)
+
+        response = self.client.get(reverse('home'), follow=True, **headers)
+        self.assertEqual(response.status_code, 200)
+
+        new_user = User.objects.get(username='remoteuser2')
+        self.assertEqual(int(self.client.session.get('_auth_user_id')), new_user.pk, msg='Authentication failed')
+        self.assertListEqual(
+            [groups[0], groups[1]],
+            list(new_user.groups.all())
+        )
+
+    @override_settings(
+        REMOTE_AUTH_ENABLED=True,
+        REMOTE_AUTH_AUTO_CREATE_USER=True,
+        REMOTE_AUTH_DEFAULT_PERMISSIONS=['dcim.add_site', 'dcim.change_site'],
+        LOGIN_REQUIRED=True
+    )
+    def test_remote_auth_default_permissions(self):
+        """
+        Test enabling remote authentication with the default configuration.
+        """
+        headers = {
+            'HTTP_REMOTE_USER': 'remoteuser2',
+        }
+
+        self.assertTrue(settings.REMOTE_AUTH_ENABLED)
+        self.assertTrue(settings.REMOTE_AUTH_AUTO_CREATE_USER)
+        self.assertEqual(settings.REMOTE_AUTH_HEADER, 'HTTP_REMOTE_USER')
+        self.assertEqual(settings.REMOTE_AUTH_DEFAULT_PERMISSIONS, ['dcim.add_site', 'dcim.change_site'])
+
+        response = self.client.get(reverse('home'), follow=True, **headers)
+        self.assertEqual(response.status_code, 200)
+
+        new_user = User.objects.get(username='remoteuser2')
+        self.assertEqual(int(self.client.session.get('_auth_user_id')), new_user.pk, msg='Authentication failed')
+        self.assertTrue(new_user.has_perms(['dcim.add_site', 'dcim.change_site']))

+ 6 - 0
netbox/netbox/urls.py

@@ -6,6 +6,7 @@ from django.views.static import serve
 from drf_yasg import openapi
 from drf_yasg import openapi
 from drf_yasg.views import get_schema_view
 from drf_yasg.views import get_schema_view
 
 
+from extras.plugins.urls import plugin_admin_patterns, plugin_patterns, plugin_api_patterns
 from netbox.views import APIRootView, HomeView, StaticMediaFailureView, SearchView
 from netbox.views import APIRootView, HomeView, StaticMediaFailureView, SearchView
 from users.views import LoginView, LogoutView
 from users.views import LoginView, LogoutView
 from .admin import admin_site
 from .admin import admin_site
@@ -81,8 +82,13 @@ _patterns = [
     # Errors
     # Errors
     path('media-failure/', StaticMediaFailureView.as_view(), name='media_failure'),
     path('media-failure/', StaticMediaFailureView.as_view(), name='media_failure'),
 
 
+    # Plugins
+    path('plugins/', include((plugin_patterns, 'plugins'))),
+    path('api/plugins/', include((plugin_api_patterns, 'plugins-api'))),
+    path('admin/plugins/', include(plugin_admin_patterns))
 ]
 ]
 
 
+
 if settings.DEBUG:
 if settings.DEBUG:
     import debug_toolbar
     import debug_toolbar
     _patterns += [
     _patterns += [

+ 1 - 0
netbox/netbox/views.py

@@ -341,6 +341,7 @@ class APIRootView(APIView):
             ('dcim', reverse('dcim-api:api-root', request=request, format=format)),
             ('dcim', reverse('dcim-api:api-root', request=request, format=format)),
             ('extras', reverse('extras-api:api-root', request=request, format=format)),
             ('extras', reverse('extras-api:api-root', request=request, format=format)),
             ('ipam', reverse('ipam-api:api-root', request=request, format=format)),
             ('ipam', reverse('ipam-api:api-root', request=request, format=format)),
+            ('plugins', reverse('plugins-api:api-root', request=request, format=format)),
             ('secrets', reverse('secrets-api:api-root', request=request, format=format)),
             ('secrets', reverse('secrets-api:api-root', request=request, format=format)),
             ('tenancy', reverse('tenancy-api:api-root', request=request, format=format)),
             ('tenancy', reverse('tenancy-api:api-root', request=request, format=format)),
             ('virtualization', reverse('virtualization-api:api-root', request=request, format=format)),
             ('virtualization', reverse('virtualization-api:api-root', request=request, format=format)),

+ 7 - 1
netbox/project-static/css/base.css

@@ -34,6 +34,9 @@ body {
 footer p {
 footer p {
     margin: 20px 0;
     margin: 20px 0;
 }
 }
+#navbar_search {
+    padding: 0 8px;
+}
 
 
 /* Hide the username in the navigation menu on displays less than 1400px wide */
 /* Hide the username in the navigation menu on displays less than 1400px wide */
 @media (max-width: 1399px) {
 @media (max-width: 1399px) {
@@ -64,7 +67,7 @@ footer p {
 
 
 /* Scroll the drop-down menus at or above 768px wide to match bootstrap's behavior for hiding dropdown menus */
 /* Scroll the drop-down menus at or above 768px wide to match bootstrap's behavior for hiding dropdown menus */
 @media (min-width: 768px) {
 @media (min-width: 768px) {
-    .navbar-nav>li>ul {
+    .navbar-nav > li > ul {
         max-height: calc(80vh - 50px);
         max-height: calc(80vh - 50px);
         overflow-y: auto;
         overflow-y: auto;
     }
     }
@@ -133,6 +136,9 @@ ul.dropdown-menu div.buttons a {
 ul.dropdown-menu > li > a {
 ul.dropdown-menu > li > a {
     clear: left;
     clear: left;
 }
 }
+.nav > li > a.dropdown-toggle {
+    padding: 15px 12px;
+}
 
 
 /* Forms */
 /* Forms */
 label {
 label {

+ 11 - 0
netbox/secrets/__init__.py

@@ -0,0 +1,11 @@
+# TODO: Rename the secrets app, probably
+# Python 3.6 introduced a standard library named "secrets," which obviously conflicts with this Django app. To avoid
+# renaming the app, we hotwire the components of the standard library that Django calls. (I don't like this any more
+# than you do, but it works for now.) The only references to the secrets modules are in django/utils/crypto.py.
+#
+# First, we copy secrets.compare_digest, which comes from the hmac module:
+from hmac import compare_digest
+
+# Then, we instantiate SystemRandom and map its choice() function:
+from random import SystemRandom
+choice = SystemRandom().choice

+ 0 - 8
netbox/secrets/api/serializers.py

@@ -1,5 +1,4 @@
 from rest_framework import serializers
 from rest_framework import serializers
-from rest_framework.validators import UniqueTogetherValidator
 from taggit_serializer.serializers import TaggitSerializer, TagListSerializerField
 from taggit_serializer.serializers import TaggitSerializer, TagListSerializerField
 
 
 from dcim.api.nested_serializers import NestedDeviceSerializer
 from dcim.api.nested_serializers import NestedDeviceSerializer
@@ -43,13 +42,6 @@ class SecretSerializer(TaggitSerializer, CustomFieldModelSerializer):
             data['ciphertext'] = s.ciphertext
             data['ciphertext'] = s.ciphertext
             data['hash'] = s.hash
             data['hash'] = s.hash
 
 
-        # Validate uniqueness of name if one has been provided.
-        if data.get('name'):
-            validator = UniqueTogetherValidator(queryset=Secret.objects.all(), fields=('device', 'role', 'name'))
-            validator.set_context(self)
-            validator(data)
-
-        # Enforce model validation
         super().validate(data)
         super().validate(data)
 
 
         return data
         return data

+ 0 - 3
netbox/secrets/api/urls.py

@@ -14,9 +14,6 @@ class SecretsRootView(routers.APIRootView):
 router = routers.DefaultRouter()
 router = routers.DefaultRouter()
 router.APIRootView = SecretsRootView
 router.APIRootView = SecretsRootView
 
 
-# Field choices
-router.register('_choices', views.SecretsFieldChoicesViewSet, basename='field-choice')
-
 # Secrets
 # Secrets
 router.register('secret-roles', views.SecretRoleViewSet)
 router.register('secret-roles', views.SecretRoleViewSet)
 router.register('secrets', views.SecretViewSet)
 router.register('secrets', views.SecretViewSet)

+ 1 - 9
netbox/secrets/api/views.py

@@ -11,7 +11,7 @@ from rest_framework.viewsets import ViewSet
 from secrets import filters
 from secrets import filters
 from secrets.exceptions import InvalidKey
 from secrets.exceptions import InvalidKey
 from secrets.models import Secret, SecretRole, SessionKey, UserKey
 from secrets.models import Secret, SecretRole, SessionKey, UserKey
-from utilities.api import FieldChoicesViewSet, ModelViewSet
+from utilities.api import ModelViewSet
 from . import serializers
 from . import serializers
 
 
 ERR_USERKEY_MISSING = "No UserKey found for the current user."
 ERR_USERKEY_MISSING = "No UserKey found for the current user."
@@ -20,14 +20,6 @@ ERR_PRIVKEY_MISSING = "Private key was not provided."
 ERR_PRIVKEY_INVALID = "Invalid private key."
 ERR_PRIVKEY_INVALID = "Invalid private key."
 
 
 
 
-#
-# Field choices
-#
-
-class SecretsFieldChoicesViewSet(FieldChoicesViewSet):
-    fields = ()
-
-
 #
 #
 # Secret Roles
 # Secret Roles
 #
 #

Некоторые файлы не были показаны из-за большого количества измененных файлов