Skip to content

Commit

Permalink
Closes #10543: Introduce get_plugin_config() utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Nov 3, 2022
1 parent e7f54c5 commit 13afc52
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
6 changes: 3 additions & 3 deletions docs/plugins/development/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ NetBox looks for the `config` variable within a plugin's `__init__.py` to load i
All required settings must be configured by the user. If a configuration parameter is listed in both `required_settings` and `default_settings`, the default setting will be ignored.

!!! tip "Accessing Config Parameters"
Plugin configuration parameters can be accessed in `settings.PLUGINS_CONFIG`, mapped by plugin name. For example:
Plugin configuration parameters can be accessed using the `get_plugin_config()` function. For example:

```python
from django.conf import settings
settings.PLUGINS_CONFIG['myplugin']['verbose_name']
from extras.plugins import get_plugin_config
get_plugin_config('my_plugin', 'verbose_name')
```

#### Important Notes About `django_apps`
Expand Down
21 changes: 21 additions & 0 deletions netbox/extras/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import collections

from django.apps import AppConfig
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.module_loading import import_string
from packaging import version
Expand Down Expand Up @@ -140,3 +141,23 @@ def validate(cls, user_config, netbox_version):
for setting, value in cls.default_settings.items():
if setting not in user_config:
user_config[setting] = value


#
# Utilities
#

def get_plugin_config(plugin_name, parameter, default=None):
"""
Return the value of the specified plugin configuration parameter.
Args:
plugin_name: The name of the plugin
parameter: The name of the configuration parameter
default: The value to return if the parameter is not defined (default: None)
"""
try:
plugin_config = settings.PLUGINS_CONFIG[plugin_name]
return plugin_config.get(parameter, default)
except KeyError:
raise ImproperlyConfigured(f"Plugin {plugin_name} is not registered.")
12 changes: 11 additions & 1 deletion netbox/extras/tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.test import Client, TestCase, override_settings
from django.urls import reverse

from extras.plugins import PluginMenu
from extras.plugins import PluginMenu, get_plugin_config
from extras.tests.dummy_plugin import config as dummy_config
from netbox.graphql.schema import Query
from netbox.registry import registry
Expand Down Expand Up @@ -173,3 +173,13 @@ def test_graphql(self):

self.assertIn(DummyQuery, registry['plugins']['graphql_schemas'])
self.assertTrue(issubclass(Query, DummyQuery))

@override_settings(PLUGINS_CONFIG={'extras.tests.dummy_plugin': {'foo': 123}})
def test_get_plugin_config(self):
"""
Validate that get_plugin_config() returns config parameters correctly.
"""
plugin = 'extras.tests.dummy_plugin'
self.assertEqual(get_plugin_config(plugin, 'foo'), 123)
self.assertEqual(get_plugin_config(plugin, 'bar'), None)
self.assertEqual(get_plugin_config(plugin, 'bar', default=456), 456)

0 comments on commit 13afc52

Please sign in to comment.