netbox-plugin-tutorial icon indicating copy to clipboard operation
netbox-plugin-tutorial copied to clipboard

Update plugin tutorial to align with NetBox 4.0 plugin API changes

Open jeffgdotorg opened this issue 1 year ago • 4 comments

This tutorial needs some updates to bring it fully into line with the plugin API changes introduced in NetBox 4.0.

As a starting point, tutorial/step01-initial-setup.md still instructs the author to import from extras.plugins, instead of the new netbox.plugins package.

Refer to Migrating Your Plugin to NetBox 4.0 in the main NetBox docs for the full set of needed changes.

jeffgdotorg avatar May 09 '24 20:05 jeffgdotorg

FWIW, I ran across the following issue in step09-rest-api.md (Step 9: REST API) against a development instance of Netbox (v4.2.1):

$ python3 manage.py runserver 0.0.0.0:8000 --insecure
[...]
  File "/home/agt/netbox-plugin-demo/netbox_access_lists/api/urls.py", line 2, in <module>
    from . import views
  File "/home/agt/netbox-plugin-demo/netbox_access_lists/api/views.py", line 6, in <module>
    from .serializers import AccessListSerializer, AccessListRuleSerializer
  File "/home/agt/netbox-plugin-demo/netbox_access_lists/api/serializers.py", line 3, in <module>
    from ipam.api.serializers import NestedPrefixSerializer
ImportError: cannot import name 'NestedPrefixSerializer' from 'ipam.api.serializers' (/opt/netbox/netbox/ipam/api/serializers.py). Did you mean: 'PrefixSerializer'?

The following change was enough to fix the problem (at first glance):

$ git diff remotes/origin/step09-rest-api -- ':!netbox_access_lists/__init__.py' ':!netbox_access_lists/navigation.py' ':!netbox_access_lists/migrations/0001_initial.py' ':!*.pyc'
diff --git a/netbox_access_lists/api/serializers.py b/netbox_access_lists/api/serializers.py
index 00c509e..4a58a92 100644
--- a/netbox_access_lists/api/serializers.py
+++ b/netbox_access_lists/api/serializers.py
@@ -1,6 +1,6 @@
 from rest_framework import serializers

-from ipam.api.serializers import NestedPrefixSerializer
+from ipam.api.serializers import PrefixSerializer
 from netbox.api.serializers import NetBoxModelSerializer, WritableNestedSerializer
 from ..models import AccessList, AccessListRule

@@ -52,8 +52,8 @@ class AccessListRuleSerializer(NetBoxModelSerializer):
         view_name='plugins-api:netbox_access_lists-api:accesslistrule-detail'
     )
     access_list = NestedAccessListSerializer()
-    source_prefix = NestedPrefixSerializer()
-    destination_prefix = NestedPrefixSerializer()
+    source_prefix = PrefixSerializer(nested=True)
+    destination_prefix = PrefixSerializer(nested=True)

     class Meta:
         model = AccessListRule

agt-the-walker avatar Feb 04 '25 17:02 agt-the-walker

I just spent half an hour on this wondering why my buttons weren't showing up, because /AFAICT/ netbox appears to have hidden whatever errors this causes.

As a starting point, tutorial/step01-initial-setup.md still instructs the author to import from extras.plugins, instead of the new netbox.plugins package.

deliciouslytyped avatar Feb 06 '25 23:02 deliciouslytyped

Also, it still uses a setup.py, while https://netboxlabs.com/docs/netbox/plugins/development/ mentions a pyproject.toml

hylkevds avatar Jul 03 '25 11:07 hylkevds

The tutorial in step 7 uses wrong imports. It uses from extras.plugins import PluginMenuButton, PluginMenuItem from utilities.choices import ButtonColorChoices While the documentation says to use from netbox.plugins import PluginMenuButton, PluginMenuItem from netbox.choices import ButtonColorChoices

It is simillar to a previous comment here.

I just spent half an hour on this wondering why my buttons weren't showing up, because /AFAICT/ netbox appears to have hidden whatever errors this causes.

As a starting point, tutorial/step01-initial-setup.md still instructs the author to import from extras.plugins, instead of the new netbox.plugins package.

Just putting it here in case anyone is wondering why are the plugins missing from the navigation toolbar.

y3man avatar Aug 07 '25 13:08 y3man