Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
wkoot committed Sep 22, 2023
1 parent 2501e41 commit deeb88e
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 7 deletions.
1 change: 1 addition & 0 deletions netbox_slm/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Meta:
"url",
"device",
"virtualmachine",
"cluster",
"software_product",
"version",
"tags",
Expand Down
1 change: 1 addition & 0 deletions netbox_slm/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ def search(self, queryset, name, value):
| Q(version__name__icontains=value)
| Q(installation__device__name__icontains=value)
| Q(installation__virtualmachine__name__icontains=value)
| Q(installation__cluster__name__icontains=value)
)
return queryset.filter(qs_filter)
9 changes: 5 additions & 4 deletions netbox_slm/forms/software_product_installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
from netbox_slm.models import SoftwareProductInstallation, SoftwareProduct, SoftwareProductVersion
from utilities.forms.fields import DynamicModelChoiceField, TagFilterField
from utilities.forms.widgets import APISelect
from virtualization.models import VirtualMachine
from virtualization.models import VirtualMachine, Cluster


class SoftwareProductInstallationForm(NetBoxModelForm):
"""Form for creating a new SoftwareProductInstallation object."""

device = DynamicModelChoiceField(queryset=Device.objects.all(), required=False)
virtualmachine = DynamicModelChoiceField(queryset=VirtualMachine.objects.all(), required=False)
cluster = DynamicModelChoiceField(queryset=Cluster.objects.all(), required=False)
software_product = DynamicModelChoiceField(
queryset=SoftwareProduct.objects.all(),
required=True,
Expand All @@ -31,7 +32,7 @@ class SoftwareProductInstallationForm(NetBoxModelForm):

class Meta:
model = SoftwareProductInstallation
fields = ("device", "virtualmachine", "software_product", "version", "tags")
fields = ("device", "virtualmachine", "cluster", "software_product", "version", "tags")

def clean_version(self):
version = self.cleaned_data["version"]
Expand All @@ -46,8 +47,8 @@ def clean_version(self):
return version

def clean(self):
if not any([self.cleaned_data["device"], self.cleaned_data["virtualmachine"]]):
raise forms.ValidationError(_("Installation requires atleast one virtualmachine or device destination."))
if not any([self.cleaned_data["device"], self.cleaned_data["virtualmachine"], self.cleaned_data["cluster"]]):
raise forms.ValidationError(_("Installation requires at least one platform destination."))
return super(SoftwareProductInstallationForm, self).clean()


Expand Down
24 changes: 24 additions & 0 deletions netbox_slm/migrations/0007_softwareproductinstallation_cluster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.5 on 2023-09-22 02:01

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('virtualization', '0036_virtualmachine_config_template'),
('netbox_slm', '0006_softwarelicense_stored_location_url'),
]

operations = [
migrations.AddField(
model_name='softwareproductinstallation',
name='cluster',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='virtualization.cluster'),
),
migrations.AddConstraint(
model_name='softwareproductinstallation',
constraint=models.CheckConstraint(check=models.Q(models.Q(('cluster__isnull', True), ('device__isnull', False), ('virtualmachine__isnull', True)), models.Q(('cluster__isnull', True), ('device__isnull', True), ('virtualmachine__isnull', False)), models.Q(('cluster__isnull', False), ('device__isnull', True), ('virtualmachine__isnull', True)), _connector='OR'), name='netbox_slm_softwareproductinstallation_platform'),
),
]
21 changes: 19 additions & 2 deletions netbox_slm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class SoftwareProductInstallation(NetBoxModel):
virtualmachine = models.ForeignKey(
to="virtualization.VirtualMachine", on_delete=models.PROTECT, null=True, blank=True
)
cluster = models.ForeignKey(to="virtualization.Cluster", on_delete=models.PROTECT, null=True, blank=True)
software_product = models.ForeignKey(to="netbox_slm.SoftwareProduct", on_delete=models.PROTECT)
version = models.ForeignKey(to="netbox_slm.SoftwareProductVersion", on_delete=models.PROTECT)

Expand All @@ -87,15 +88,31 @@ class SoftwareProductInstallation(NetBoxModel):
def __str__(self):
return f"{self.pk} ({self.platform})"

class Meta:
constraints = [
models.CheckConstraint(
name="%(app_label)s_%(class)s_platform",
check=(
models.Q(device__isnull=False, virtualmachine__isnull=True, cluster__isnull=True)
| models.Q(device__isnull=True, virtualmachine__isnull=False, cluster__isnull=True)
| models.Q(device__isnull=True, virtualmachine__isnull=True, cluster__isnull=False)
),
)
]

def get_absolute_url(self):
return reverse("plugins:netbox_slm:softwareproductinstallation", kwargs={"pk": self.pk})

@property
def platform(self):
return self.device or self.virtualmachine
return self.device or self.virtualmachine or self.cluster

def render_type(self):
return "device" if self.device else "virtualmachine"
if self.device:
return "device"
if self.virtualmachine:
return "virtualmachine"
return "cluster"


class SoftwareLicense(NetBoxModel):
Expand Down
4 changes: 4 additions & 0 deletions netbox_slm/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ class SoftwareProductInstallationTable(NetBoxTable):

pk = ToggleColumn()
name = tables.LinkColumn()

device = tables.Column(accessor="device", linkify=True)
virtualmachine = tables.Column(accessor="virtualmachine", linkify=True)
cluster = tables.Column(accessor="cluster", linkify=True)
platform = tables.Column(accessor="platform", linkify=True)
type = tables.Column(accessor="render_type")
software_product = tables.Column(accessor="software_product", linkify=True)
Expand Down Expand Up @@ -125,10 +127,12 @@ class Meta(NetBoxTable.Meta):
)

def order_platform(self, queryset, is_descending):
# TODO - what do with "cluster" ?
queryset = queryset.order_by(("device" if is_descending else "virtualmachine"))
return queryset, True

def order_type(self, queryset, is_descending):
# TODO - what do with "cluster" ?
queryset = queryset.order_by(("device" if is_descending else "virtualmachine"))
return queryset, True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ <h5 class="card-header">
<th scope="row">Device</th>
<td>{{ object.device }}</td>
</tr>
{% else %}
{% elif object.virtualmachine %}
<tr>
<th scope="row">Virtualmachine</th>
<td>{{ object.virtualmachine }}</td>
</tr>
{% else %}
<tr>
<th scope="row">Cluster</th>
<td>{{ object.cluster }}</td>
</tr>
{% endif %}
<tr>
<th scope="row">Software Product</th>
Expand Down

0 comments on commit deeb88e

Please sign in to comment.