Repository: digitalocean/pynetbox
Pull request #290 (2020-09-16 08:27:26)

Basic plugins support


Patch:
diff --git a/pynetbox/core/api.py b/pynetbox/core/api.py
index c557a77..86c32d1 100644
--- a/pynetbox/core/api.py
+++ b/pynetbox/core/api.py
@@ -18,7 +18,7 @@ import sys
 import requests
 
 from pynetbox.core.query import Request
-from pynetbox.core.app import App
+from pynetbox.core.app import App, PluginsApp
 
 
 class Api(object):
@@ -100,6 +100,7 @@ class Api(object):
         self.tenancy = App(self, "tenancy")
         self.extras = App(self, "extras")
         self.virtualization = App(self, "virtualization")
+        self.plugins = PluginsApp(self)
 
     @property
     def version(self):
diff --git a/pynetbox/core/app.py b/pynetbox/core/app.py
index c51e7ed..54dda45 100644
--- a/pynetbox/core/app.py
+++ b/pynetbox/core/app.py
@@ -109,3 +109,42 @@ class App(object):
             http_session=self.api.http_session,
         ).get()
         return custom_field_choices
+
+
+class PluginsApp(object):
+    """
+    Basically valid plugins api could be handled by same App class,
+    but you need to add plugins to request url path.
+
+    :returns: :py:class:`.App` with added plugins into path.
+
+    """
+
+    def __init__(self, api):
+        self.api = api
+
+    def __getattr__(self, name):
+        return App(self.api, 'plugins/{}'.format(name))
+
+    def installed_plugins(self):
+        """ Returns raw response with installed plugins
+
+        :returns: Raw response NetBox's installed plugins.
+        :Example:
+
+        >>> nb.plugins.installed_plugins()
+        [{
+            'name': 'test_plugin', 
+            'package': 'test_plugin', 
+            'author': 'Dmitry', 
+            'description': 'Netbox test plugin', 
+            'verison': '0.10'
+        }]
+        """
+        installed_plugins = Request(
+            base="{}/plugins/installed-plugins".format(self.api.base_url,),
+            token=self.api.token,
+            private_key=self.api.private_key,
+            http_session=self.api.http_session,
+        ).get()
+        return installed_plugins


Test patch:
diff --git a/tests/test_app.py b/tests/test_app.py
index 632ee82..582c83e 100644
--- a/tests/test_app.py
+++ b/tests/test_app.py
@@ -29,3 +29,31 @@ class AppCustomChoicesTestCase(unittest.TestCase):
         choices = api.extras.custom_choices()
         self.assertEqual(len(choices), 2)
         self.assertEqual(sorted(choices.keys()), ["Testfield1", "Testfield2"])
+
+
+class PluginAppCustomChoicesTestCase(unittest.TestCase):
+    @patch(
+        "pynetbox.core.query.Request.get",
+        return_value={
+            "Testfield1": {"TF1_1": 1, "TF1_2": 2},
+            "Testfield2": {"TF2_1": 3, "TF2_2": 4},
+        },
+    )
+    def test_custom_choices(self, *_):
+        api = pynetbox.api(host, **def_kwargs)
+        choices = api.plugins.test_plugin.custom_choices()
+        self.assertEqual(len(choices), 2)
+        self.assertEqual(sorted(choices.keys()), ["Testfield1", "Testfield2"])
+
+    @patch(
+        "pynetbox.core.query.Request.get",
+        return_value=[{
+            "name": "test_plugin",
+            "package": "netbox_test_plugin",
+        }],
+    )
+    def test_installed_plugins(self, *_):
+        api = pynetbox.api(host, **def_kwargs)
+        plugins = api.plugins.installed_plugins()
+        self.assertEqual(len(plugins), 1)
+        self.assertEqual(plugins[0]["name"], "test_plugin")