Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for NetBox v3.6.x #27

Merged
merged 10 commits into from
Jan 16, 2024
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ jobs:
uses: actions/checkout@v3
- id: docker-test
name: Test the image
run: ./test.sh snapshot
run: ./test.sh v3.4.10
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ To use GraphQL API you need to set GitHub personal access token in plugin settin
|:--------------:|:--------------:|
| 3.4.x | 0.0.x |
| 3.5.x | 0.1.x |
| 3.6.x | 0.1.1 |

## Installation

Expand Down
4 changes: 2 additions & 2 deletions netbox_metatype_importer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class NetBoxMetatypeImporterConfig(PluginConfig):
version = metadata.get('Version')
author = metadata.get('Author')
author_email = metadata.get('Author-email')
min_version = '3.5.0'
max_version = '3.5.99'
min_version = '3.6.0'
max_version = '3.6.99'
default_settings = {
'repo_owner': 'netbox-community',
'repo': 'devicetype-library',
Expand Down
6 changes: 6 additions & 0 deletions netbox_metatype_importer/gql.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ def get_tree(self):
data = self.get_query(query)
if not data:
return result

try:
data['data']['repository']['object']['entries']
except TypeError:
return None

for vendor in data['data']['repository']['object']['entries']:
result[vendor['name']] = {}
for model in vendor['object'].get('entries', []):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends 'generic/object_list.html' %}
{% load buttons %}

{% block title %}DeviceType Imports{% endblock %}

Expand All @@ -16,6 +17,7 @@
{% endif %}
</div>
{% endblock %}

{% block bulk_buttons %}
{% if perms.netbox_metatype_importer.add_metadevicetype %}
<button type="submit" name="_edit" formaction="
Expand All @@ -24,4 +26,11 @@
<span class="mdi mdi-upload" aria-hidden="true"></span> Import Selected
</button>
{% endif %}
{% if perms.netbox_metatype_importer.delete_metadevicetype %}
<button type="submit" name="_edit" formaction="
{% url 'plugins:netbox_metatype_importer:bulk_metadevicetype_delete' %}{% if request.GET %}?{{ request.GET.urlencode }}{% endif %}"
class="btn btn-danger btn-sm">
<span class="mdi mdi-trash-can-outline" aria-hidden="true"></span> Delete Selected
</button>
{% endif %}
{% endblock %}
4 changes: 3 additions & 1 deletion netbox_metatype_importer/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
path('meta-device-types/', views.MetaDeviceTypeListView.as_view(), name='metadevicetype_list'),
path('meta-device-types/load/', views.MetaDeviceTypeLoadView.as_view(), name='metadevicetype_load'),
path('meta-device-types/import/', views.MetaDeviceTypeImportView.as_view(), name='metadevicetype_import'),
path('meta-device-types/delete/', views.MetaDeviceTypeBulkDeleteView.as_view(), name='bulk_metadevicetype_delete'),
# Module types
path('meta-module-types/', views.MetaModuleTypeListView.as_view(), name='metamoduletype_list'),
path('meta-module-types/load/', views.MetaModuleTypeLoadView.as_view(), name='metamoduletype_load'),
path('meta-module-types/import/', views.MetaModuleTypeImportView.as_view(), name='metamoduletype_import')
path('meta-module-types/import/', views.MetaModuleTypeImportView.as_view(), name='metamoduletype_import'),
path('meta-module-types/delete/', views.MetaModuleTypeBulkDeleteView.as_view(), name='bulk_metamoduletype_delete'),
]
25 changes: 18 additions & 7 deletions netbox_metatype_importer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def post(self, request):
messages.error(request, message=f'GraphQL API Error: {e.message}')
return redirect('plugins:netbox_metatype_importer:metadevicetype_list')

if models is None:
messages.error(request, 'Check your plugin settings and try again')
models = {}

for vendor, models in models.items():
for model, model_data in models.items():
loaded += 1
Expand All @@ -91,7 +95,8 @@ def post(self, request):
type=self.path
)
created += 1
messages.success(request, f'Loaded: {loaded}, Created: {created}, Updated: {updated}')
if models:
messages.success(request, f'Loaded: {loaded}, Created: {created}, Updated: {updated}')
return redirect(return_url)


Expand Down Expand Up @@ -197,10 +202,6 @@ def post(self, request):

model_form = self.model_form(data)

for field_name, field in model_form.fields.items():
abhi1693 marked this conversation as resolved.
Show resolved Hide resolved
if field_name not in data and hasattr(field, 'initial'):
model_form.data[field_name] = field.initial

if model_form.is_valid():
try:
with transaction.atomic():
Expand Down Expand Up @@ -245,9 +246,9 @@ def post(self, request):
messages.error(request, f'Failed: {errored}')
qparams = urlencode({'id': imported_dt}, doseq=True)
# Black magic to get the url path from the type
return redirect(reverse(f'dcim:{str(self.type).replace("-", "").rstrip("s")}_list') + '?' + qparams)
return redirect(reverse(f'dcim:{self.type_model._meta.model_name}_list') + '?' + qparams)
else:
messages.error(request, 'Can not import Device Types')
messages.error(request, f'Can not import {self.type_model.__name__}')
return redirect(return_url)


Expand All @@ -265,3 +266,13 @@ class MetaModuleTypeImportView(GenericTypeImportView):
type_model = ModuleType
model_form = forms.ModuleTypeImportForm
related_object = 'module_type'


class MetaDeviceTypeBulkDeleteView(generic.BulkDeleteView):
queryset = MetaType.objects.filter(type=TypeChoices.TYPE_DEVICE)
table = MetaTypeTable


class MetaModuleTypeBulkDeleteView(generic.BulkDeleteView):
queryset = MetaType.objects.filter(type=TypeChoices.TYPE_MODULE)
table = MetaTypeTable
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='netbox-metatype-importer',
version='0.1.0',
version='0.1.1',
description='Import Metatypes',
long_description='Import MetaTypes into NetBox',
long_description_content_type="text/markdown",
Expand Down
Loading