Skip to content

Commit

Permalink
fixed filter, api issues; cosmetic changes;
Browse files Browse the repository at this point in the history
recreated migrations
  • Loading branch information
viroge committed Feb 18, 2023
1 parent af79f75 commit 524eb1b
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 81 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.5.1'
version = '0.6'
base_url = 'storage'
min_version = "3.4.0"
author = 'Gabor Somogyvari'
Expand Down
18 changes: 16 additions & 2 deletions netbox_storage/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from rest_framework import serializers

from ..models import StoragePool, LUN, StorageSession, Datastore, VMDK
from virtualization.api.serializers import NestedClusterSerializer
from virtualization.api.serializers import NestedClusterSerializer, NestedVirtualMachineSerializer
from dcim.api.serializers import NestedDeviceSerializer
from netbox.api.serializers import NetBoxModelSerializer, WritableNestedSerializer
from netbox.api.fields import SerializedPKRelatedField
from ..models import StoragePool, LUN, StorageSession, Datastore, VMDK


#
Expand Down Expand Up @@ -67,6 +69,7 @@ class StoragePoolSerializer(NetBoxModelSerializer):
url = serializers.HyperlinkedIdentityField(
view_name='plugins-api:netbox_storage-api:storagepool-detail'
)
device = NestedDeviceSerializer()

class Meta:
model = StoragePool
Expand Down Expand Up @@ -95,6 +98,11 @@ class DatastoreSerializer(NetBoxModelSerializer):
url = serializers.HyperlinkedIdentityField(
view_name='plugins-api:netbox_storage-api:datastore-detail'
)
lun = SerializedPKRelatedField(
queryset=LUN.objects.all(),
serializer=NestedLUNSerializer,
many=True
)

class Meta:
model = Datastore
Expand All @@ -109,6 +117,11 @@ class StorageSessionSerializer(NetBoxModelSerializer):
view_name='plugins-api:netbox_storage-api:storagesession-detail'
)
cluster = NestedClusterSerializer()
datastores = SerializedPKRelatedField(
queryset=Datastore.objects.all(),
serializer=NestedDatastoreSerializer,
many=True
)

class Meta:
model = StorageSession
Expand All @@ -123,6 +136,7 @@ class VMDKSerializer(NetBoxModelSerializer):
view_name='plugins-api:netbox_storage-api:vmdk-detail'
)
datastore = NestedDatastoreSerializer()
vm = NestedVirtualMachineSerializer()

class Meta:
model = VMDK
Expand Down
19 changes: 13 additions & 6 deletions netbox_storage/filtersets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from netbox.filtersets import NetBoxModelFilterSet
import django_filters
from virtualization.models import VirtualMachine
from .models import StoragePool, LUN, StorageSession, Datastore, VMDK


Expand All @@ -9,7 +11,7 @@ class Meta:
fields = ('id', 'name', 'device')

def search(self, queryset, name, value):
return queryset.filter(description__icontains=value)
return queryset.filter(name__icontains=value)


class LUNFilterSet(NetBoxModelFilterSet):
Expand All @@ -19,17 +21,22 @@ class Meta:
fields = ('id', 'storage_pool', 'name', 'wwn',)

def search(self, queryset, name, value):
return queryset.filter(description__icontains=value)
return queryset.filter(name__icontains=value)


class DatastoreFilterSet(NetBoxModelFilterSet):
reachable_by_vm = django_filters.ModelMultipleChoiceFilter(
field_name='storage_sessions__cluster__virtual_machines',
queryset=VirtualMachine.objects.all(),
label='Reachable by these Virtual Machines'
)

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

def search(self, queryset, name, value):
return queryset.filter(description__icontains=value)
return queryset.filter(name__icontains=value)


class StorageSessionFilterSet(NetBoxModelFilterSet):
Expand All @@ -41,7 +48,7 @@ class Meta:
)

def search(self, queryset, name, value):
return queryset.filter(description__icontains=value)
return queryset.filter(name__icontains=value)


class VMDKFilterSet(NetBoxModelFilterSet):
Expand All @@ -53,4 +60,4 @@ class Meta:
)

def search(self, queryset, name, value):
return queryset.filter(description__icontains=value)
return queryset.filter(name__icontains=value)
15 changes: 12 additions & 3 deletions netbox_storage/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ class Meta:


class VMDKForm(NetBoxModelForm):
datastore = DynamicModelChoiceField(
queryset=Datastore.objects.all(),
)
vm = DynamicModelChoiceField(
queryset=VirtualMachine.objects.all(),
label='Virtual Machine'
)
datastore = DynamicModelChoiceField(
queryset=Datastore.objects.all(),
query_params={
'reachable_by_vm': '$vm'
}
)

class Meta:
Expand Down Expand Up @@ -109,6 +113,11 @@ class DatastoreFilterForm(NetBoxModelFilterSetForm):
name = forms.CharField(
required=False
)
reachable_by_vm = DynamicModelMultipleChoiceField(
queryset=VirtualMachine.objects.all(),
required=False,
label='Reachable by these Virtual Machines'
)


class StorageSessionFilterForm(NetBoxModelFilterSetForm):
Expand Down
14 changes: 8 additions & 6 deletions netbox_storage/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.1.6 on 2023-02-08 16:56
# Generated by Django 4.1.6 on 2023-02-18 06:38

from django.db import migrations, models
import django.db.models.deletion
Expand All @@ -11,8 +11,8 @@ class Migration(migrations.Migration):
initial = True

dependencies = [
('virtualization', '0034_standardize_description_comments'),
('extras', '0084_staging'),
('virtualization', '0034_standardize_description_comments'),
('dcim', '0167_module_status'),
]

Expand All @@ -39,12 +39,13 @@ class Migration(migrations.Migration):
('last_updated', models.DateTimeField(auto_now=True, null=True)),
('custom_field_data', models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder)),
('name', models.CharField(max_length=100)),
('size', models.PositiveIntegerField()),
('size', models.PositiveBigIntegerField()),
('datastore', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='vmdks', to='netbox_storage.datastore')),
('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')),
('vm', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='vmdks', to='virtualization.virtualmachine')),
],
options={
'verbose_name': 'VMDK',
'ordering': ('datastore', 'name'),
},
),
Expand Down Expand Up @@ -73,7 +74,7 @@ class Migration(migrations.Migration):
('last_updated', models.DateTimeField(auto_now=True, null=True)),
('custom_field_data', models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder)),
('name', models.CharField(max_length=100)),
('size', models.PositiveIntegerField()),
('size', models.PositiveBigIntegerField()),
('description', models.TextField(blank=True)),
('device', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='dcim.device')),
('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')),
Expand All @@ -90,8 +91,9 @@ class Migration(migrations.Migration):
('last_updated', models.DateTimeField(auto_now=True, null=True)),
('custom_field_data', models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder)),
('name', models.CharField(max_length=100)),
('size', models.PositiveIntegerField()),
('size', models.PositiveBigIntegerField()),
('description', models.TextField(blank=True)),
('wwn', models.CharField(blank=True, max_length=64)),
('storage_pool', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='luns', to='netbox_storage.storagepool')),
('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')),
],
Expand All @@ -103,7 +105,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='datastore',
name='lun',
field=models.ManyToManyField(related_name='lun_groups', to='netbox_storage.lun'),
field=models.ManyToManyField(related_name='datastores', to='netbox_storage.lun'),
),
migrations.AddField(
model_name='datastore',
Expand Down

This file was deleted.

18 changes: 0 additions & 18 deletions netbox_storage/migrations/0003_lun_wwn.py

This file was deleted.

10 changes: 6 additions & 4 deletions netbox_storage/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_utilization(self):
else:
utilization = 0

return min(utilization, 100)
return utilization


class LUN(NetBoxModel):
Expand Down Expand Up @@ -73,7 +73,7 @@ def get_absolute_url(self):
class Datastore(NetBoxModel):
lun = models.ManyToManyField(
to=LUN,
related_name='lun_groups'
related_name='datastores'
)
name = models.CharField(
max_length=100
Expand All @@ -99,7 +99,7 @@ def get_utilization(self):
else:
utilization = 0

return min(utilization, 100)
return utilization


class StorageSession(NetBoxModel):
Expand Down Expand Up @@ -133,7 +133,8 @@ class VMDK(NetBoxModel):
vm = models.ForeignKey(
to='virtualization.virtualmachine',
on_delete=models.PROTECT,
related_name='vmdks'
related_name='vmdks',
verbose_name='Virtual Machine'
)
name = models.CharField(
max_length=100
Expand All @@ -149,6 +150,7 @@ class VMDK(NetBoxModel):

class Meta:
ordering = ('datastore', 'name',)
verbose_name = 'VMDK'

def __str__(self):
return f'{self.vm}-{self.datastore}-{self.name}'
Expand Down
6 changes: 6 additions & 0 deletions netbox_storage/templates/netbox_storage/datastore.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ <h5 class="card-header">Storage Sessions</h5>
{% render_table sessions_table %}
</div>
</div>
<div class="card">
<h5 class="card-header">VMDKs on this Datastore</h5>
<div class="card-body table-responsive">
{% render_table vmdks_table %}
</div>
</div>
</div>
</div>
{% endblock content %}
12 changes: 2 additions & 10 deletions netbox_storage/templates/netbox_storage/lun.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,14 @@ <h5 class="card-header">LUN</h5>
{% include 'inc/panels/tags.html' %}
</div>
</div>
{% comment %}
<div class="row">
<div class="col col-md-12">
<div class="card">
<h5 class="card-header">Storage Space Allocations</h5>
<h5 class="card-header">LUNs</h5>
<div class="card-body table-responsive">
{% render_table spaces_table %}
</div>
</div>
<div class="card">
<h5 class="card-header">Storage Sessions</h5>
<div class="card-body table-responsive">
{% render_table sessions_table %}
{% render_table datastores_table %}
</div>
</div>
</div>
</div>
{% endcomment %}
{% endblock content %}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h5 class="card-header">Attached VMDKs</h5>
<tr>
<td><a href="{{ vmdk.datastore.get_absolute_url }}">{{ vmdk.datastore }}</a></td>
<td><a href="{{ vmdk.get_absolute_url }}">{{ vmdk.name }}</a></td>
<td>{{ vmdk.size }}</td>
<td>{{ vmdk.size|filesizeformat }}</td>
<td class="noprint text-end text-nowrap">
<a href="{% url 'plugins:netbox_storage:vmdk_edit' pk=vmdk.pk %}?return_url={{ object.get_absolute_url }}" class="btn btn-sm btn-warning">
<i class="mdi mdi-pencil"></i>
Expand Down
2 changes: 1 addition & 1 deletion netbox_storage/templates/netbox_storage/vmdk.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h5 class="card-header">VMDK</h5>
<div class="card-body">
<table class="table table-hover attr-table">
<tr>
<th scope="row">VM</th>
<th scope="row">Virtal Machine</th>
<td><a href="{{ object.vm.get_absolute_url }}">{{ object.vm }}</a></td>
</tr>
<tr>
Expand Down
12 changes: 12 additions & 0 deletions netbox_storage/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ class StoragePoolImportView(generic.BulkImportView):
class LUNView(generic.ObjectView):
queryset = models.LUN.objects.all()

def get_extra_context(self, request, instance):
datastores_table = tables.DatastoreTable(instance.datastores.all())
datastores_table.configure(request)

return {
'datastores_table': datastores_table,
}


class LUNListView(generic.ObjectListView):
queryset = models.LUN.objects.all()
Expand Down Expand Up @@ -96,9 +104,13 @@ def get_extra_context(self, request, instance):
sessions_table = tables.StorageSessionTable(instance.storage_sessions.all())
sessions_table.configure(request)

vmdks_table = tables.VMDKTable(instance.vmdks.all())
vmdks_table.configure(request)

return {
'luns_table': luns_table,
'sessions_table': sessions_table,
'vmdks_table': vmdks_table,
}


Expand Down
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.5.1',
version='0.6',
description='NetBox storage plugin',
install_requires=[],
packages=find_packages(),
Expand Down

0 comments on commit 524eb1b

Please sign in to comment.