Skip to content

Commit

Permalink
added CSV import to all object types
Browse files Browse the repository at this point in the history
  • Loading branch information
viroge committed Feb 13, 2023
1 parent 259bdac commit 1506407
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 25 deletions.
2 changes: 1 addition & 1 deletion netbox_storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class NetBoxStorageConfig(PluginConfig):
name = 'netbox_storage'
verbose_name = ' NetBox Storage'
description = 'Netbox Storage Administration Plugin'
version = '0.4'
version = '0.5'
base_url = 'storage'


Expand Down
78 changes: 66 additions & 12 deletions netbox_storage/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django import forms
from netbox.forms import NetBoxModelForm, NetBoxModelFilterSetForm, NetBoxModelCSVForm
from utilities.forms.fields import DynamicModelChoiceField, CSVModelChoiceField, DynamicModelMultipleChoiceField
from utilities.forms.fields import DynamicModelChoiceField, CSVModelChoiceField, DynamicModelMultipleChoiceField, CSVModelMultipleChoiceField
from dcim.models import Device
from virtualization.models import Cluster, VirtualMachine
from .models import StoragePool, StorageSession, LUN, Datastore, VMDK
Expand Down Expand Up @@ -141,16 +141,70 @@ class VMDKFilterForm(NetBoxModelFilterSetForm):
)


# #
# # CSV Forms
# #
#
# CSV Forms
#

class StoragePoolCSVForm(NetBoxModelCSVForm):
device = CSVModelChoiceField(
queryset=Device.objects.all(),
to_field_name='name',
)

class Meta:
model = StoragePool
fields = ('name', 'size', 'device', 'description')


class LUNCSVForm(NetBoxModelCSVForm):
storage_pool = CSVModelChoiceField(
queryset=StoragePool.objects.all(),
to_field_name='name',
)

class Meta:
model = LUN
fields = ('storage_pool', 'name', 'size', 'wwn', 'description')

# class StoragePoolCSVForm(NetBoxModelCSVForm):
# device = CSVModelChoiceField(
# queryset=Device.objects.all(),
# to_field_name='name',
# )

# class Meta:
# model = StoragePool
# fields = ('name', 'size', 'device', 'description')
class DatastoreCSVForm(NetBoxModelCSVForm):
lun = CSVModelMultipleChoiceField(
queryset=LUN.objects.all(),
to_field_name='name',
help_text='A single LUN name or multiple LUN names separated by commas ("LUN" or "LUN1,LUN2,LUN3")'
)

class Meta:
model = Datastore
fields = ('lun', 'name', 'description')


class StorageSessionCSVForm(NetBoxModelCSVForm):
cluster = CSVModelChoiceField(
queryset=Cluster.objects.all(),
to_field_name='name',
)
datastores = CSVModelMultipleChoiceField(
queryset=Datastore.objects.all(),
to_field_name='name',
help_text='A single Datastore name or multiple Datastore names separated by commas ("datastore1" or "datastore1,datastore2,datastore3")'
)

class Meta:
model = StorageSession
fields = ('cluster', 'datastores', 'name', 'description')


class VMDKCSVForm(NetBoxModelCSVForm):
vm = CSVModelChoiceField(
queryset=VirtualMachine.objects.all(),
to_field_name='name',
)
datastore = CSVModelChoiceField(
queryset=Datastore.objects.all(),
to_field_name='name',
)

class Meta:
model = VMDK
fields = ('vm', 'datastore', 'name', 'size')
36 changes: 30 additions & 6 deletions netbox_storage/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
icon_class='mdi mdi-plus-thick',
color=ButtonColorChoices.GREEN
),
# PluginMenuButton(
# link='plugins:netbox_storage:storagepool_import',
# title='Import',
# icon_class='mdi mdi-upload',
# color=ButtonColorChoices.CYAN
# )
PluginMenuButton(
link='plugins:netbox_storage:storagepool_import',
title='Import',
icon_class='mdi mdi-upload',
color=ButtonColorChoices.CYAN
)
]

lun_buttons = [
Expand All @@ -22,6 +22,12 @@
title='Add',
icon_class='mdi mdi-plus-thick',
color=ButtonColorChoices.GREEN
),
PluginMenuButton(
link='plugins:netbox_storage:lun_import',
title='Import',
icon_class='mdi mdi-upload',
color=ButtonColorChoices.CYAN
)
]

Expand All @@ -31,6 +37,12 @@
title='Add',
icon_class='mdi mdi-plus-thick',
color=ButtonColorChoices.GREEN
),
PluginMenuButton(
link='plugins:netbox_storage:datastore_import',
title='Import',
icon_class='mdi mdi-upload',
color=ButtonColorChoices.CYAN
)
]

Expand All @@ -40,6 +52,12 @@
title='Add',
icon_class='mdi mdi-plus-thick',
color=ButtonColorChoices.GREEN
),
PluginMenuButton(
link='plugins:netbox_storage:storagesession_import',
title='Import',
icon_class='mdi mdi-upload',
color=ButtonColorChoices.CYAN
)
]

Expand All @@ -49,6 +67,12 @@
title='Add',
icon_class='mdi mdi-plus-thick',
color=ButtonColorChoices.GREEN
),
PluginMenuButton(
link='plugins:netbox_storage:vmdk_import',
title='Import',
icon_class='mdi mdi-upload',
color=ButtonColorChoices.CYAN
)
]

Expand Down
6 changes: 5 additions & 1 deletion netbox_storage/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Storage pools
path('storagepool/', views.StoragePoolListView.as_view(), name='storagepool_list'),
path('storagepool/add/', views.StoragePoolEditView.as_view(), name='storagepool_add'),
# path('storagepool/import/', views.StoragePoolImportView.as_view(), name='storagepool_import'),
path('storagepool/import/', views.StoragePoolImportView.as_view(), name='storagepool_import'),
path('storagepool/<int:pk>/', views.StoragePoolView.as_view(), name='storagepool'),
path('storagepool/<int:pk>/edit/', views.StoragePoolEditView.as_view(), name='storagepool_edit'),
path('storagepool/<int:pk>/delete/', views.StoragePoolDeleteView.as_view(), name='storagepool_delete'),
Expand All @@ -20,6 +20,7 @@
# LUNs
path('lun/', views.LUNListView.as_view(), name='lun_list'),
path('lun/add/', views.LUNEditView.as_view(), name='lun_add'),
path('lun/import/', views.LUNImportView.as_view(), name='lun_import'),
path('lun/<int:pk>/', views.LUNView.as_view(), name='lun'),
path('lun/<int:pk>/edit/', views.LUNEditView.as_view(), name='lun_edit'),
path('lun/<int:pk>/delete/', views.LUNDeleteView.as_view(), name='lun_delete'),
Expand All @@ -31,6 +32,7 @@
# Datastores
path('datastore/', views.DatastoreListView.as_view(), name='datastore_list'),
path('datastore/add/', views.DatastoreEditView.as_view(), name='datastore_add'),
path('datastore/import/', views.DatastoreImportView.as_view(), name='datastore_import'),
path('datastore/<int:pk>/', views.DatastoreView.as_view(), name='datastore'),
path('datastore/<int:pk>/edit/', views.DatastoreEditView.as_view(), name='datastore_edit'),
path('datastore/<int:pk>/delete/', views.DatastoreDeleteView.as_view(), name='datastore_delete'),
Expand All @@ -42,6 +44,7 @@
# Storage sessions
path('storagesession/', views.StorageSessionListView.as_view(), name='storagesession_list'),
path('storagesession/add/', views.StorageSessionEditView.as_view(), name='storagesession_add'),
path('storagesession/import/', views.StorageSessionImportView.as_view(), name='storagesession_import'),
path('storagesession/<int:pk>/', views.StorageSessionView.as_view(), name='storagesession'),
path('storagesession/<int:pk>/edit/', views.StorageSessionEditView.as_view(), name='storagesession_edit'),
path('storagesession/<int:pk>/delete/', views.StorageSessionDeleteView.as_view(), name='storagesession_delete'),
Expand All @@ -53,6 +56,7 @@
# VMDK
path('vmdk/', views.VMDKListView.as_view(), name='vmdk_list'),
path('vmdk/add/', views.VMDKEditView.as_view(), name='vmdk_add'),
path('vmdk/import/', views.VMDKImportView.as_view(), name='vmdk_import'),
path('vmdk/<int:pk>/', views.VMDKView.as_view(), name='vmdk'),
path('vmdk/<int:pk>/edit/', views.VMDKEditView.as_view(), name='vmdk_edit'),
path('vmdk/<int:pk>/delete/', views.VMDKDeleteView.as_view(), name='vmdk_delete'),
Expand Down
32 changes: 28 additions & 4 deletions netbox_storage/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class StoragePoolBulkDeleteView(generic.BulkDeleteView):
filterset = filtersets.StoragePoolFilterSet


# class StoragePoolImportView(generic.BulkImportView):
# queryset = models.StoragePool.objects.all()
# model_form = forms.StoragePoolCSVForm
# table = tables.StoragePoolTable
class StoragePoolImportView(generic.BulkImportView):
queryset = models.StoragePool.objects.all()
model_form = forms.StoragePoolCSVForm
table = tables.StoragePoolTable


#
Expand Down Expand Up @@ -76,6 +76,12 @@ class LUNBulkDeleteView(generic.BulkDeleteView):
filterset = filtersets.LUNFilterSet


class LUNImportView(generic.BulkImportView):
queryset = models.LUN.objects.all()
model_form = forms.LUNCSVForm
table = tables.LUNTable


#
# StorageLUNGroup views
#
Expand Down Expand Up @@ -118,6 +124,12 @@ class DatastoreBulkDeleteView(generic.BulkDeleteView):
filterset = filtersets.DatastoreFilterSet


class DatastoreImportView(generic.BulkImportView):
queryset = models.Datastore.objects.all()
model_form = forms.DatastoreCSVForm
table = tables.DatastoreTable


#
# StorageSession views
#
Expand Down Expand Up @@ -148,6 +160,12 @@ class StorageSessionBulkDeleteView(generic.BulkDeleteView):
filterset = filtersets.StorageSessionFilterSet


class StorageSessionImportView(generic.BulkImportView):
queryset = models.StorageSession.objects.all()
model_form = forms.StorageSessionCSVForm
table = tables.StorageSessionTable


#
# VMDK views
#
Expand Down Expand Up @@ -176,3 +194,9 @@ class VMDKBulkDeleteView(generic.BulkDeleteView):
queryset = models.VMDK.objects.all()
filterset = filtersets.VMDKFilterSet
table = tables.VMDKTable


class VMDKImportView(generic.BulkImportView):
queryset = models.VMDK.objects.all()
model_form = forms.VMDKCSVForm
table = tables.VMDKTable
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-storage',
version='0.4',
version='0.5',
description='NetBox storage plugin',
install_requires=[],
packages=find_packages(),
Expand Down

0 comments on commit 1506407

Please sign in to comment.