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

7537 add serial number to virtual machines #16407

Merged
merged 11 commits into from
Jun 12, 2024
5 changes: 5 additions & 0 deletions docs/models/virtualization/virtualmachine.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ The amount of running memory provisioned, in megabytes.
### Disk

The amount of disk storage provisioned, in gigabytes.

### Serial Number

Optional serial number assigned to this VM.

4 changes: 4 additions & 0 deletions netbox/templates/virtualization/virtualmachine.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ <h5 class="card-header">{% trans "Virtual Machine" %}</h5>
<th scope="row">{% trans "Description" %}</th>
<td>{{ object.description|placeholder }}</td>
</tr>
<tr>
<th scope="row">{% trans "Serial Number" %}</th>
<td>{{ object.serial_number|placeholder }}</td>
</tr>
<tr>
<th scope="row">{% trans "Tenant" %}</th>
<td>
Expand Down
6 changes: 3 additions & 3 deletions netbox/virtualization/api/serializers_/virtualmachines.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class VirtualMachineSerializer(NetBoxModelSerializer):
class Meta:
model = VirtualMachine
fields = [
'id', 'url', 'display', 'name', 'status', 'site', 'cluster', 'device', 'role', 'tenant', 'platform',
'primary_ip', 'primary_ip4', 'primary_ip6', 'vcpus', 'memory', 'disk', 'description', 'comments',
'config_template', 'local_context_data', 'tags', 'custom_fields', 'created', 'last_updated',
'id', 'url', 'display', 'name', 'status', 'site', 'cluster', 'device', 'serial_number', 'role', 'tenant',
'platform', 'primary_ip', 'primary_ip4', 'primary_ip6', 'vcpus', 'memory', 'disk', 'description',
'comments', 'config_template', 'local_context_data', 'tags', 'custom_fields', 'created', 'last_updated',
'interface_count', 'virtual_disk_count',
]
brief_fields = ('id', 'url', 'display', 'name', 'description')
Expand Down
8 changes: 6 additions & 2 deletions netbox/virtualization/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,10 @@ class VirtualMachineFilterSet(

class Meta:
model = VirtualMachine
fields = ('id', 'cluster', 'vcpus', 'memory', 'disk', 'description', 'interface_count', 'virtual_disk_count')
fields = (
'id', 'cluster', 'vcpus', 'memory', 'disk', 'description', 'interface_count', 'virtual_disk_count',
'serial_number'
)

def search(self, queryset, name, value):
if not value.strip():
Expand All @@ -250,7 +253,8 @@ def search(self, queryset, name, value):
Q(description__icontains=value) |
Q(comments__icontains=value) |
Q(primary_ip4__address__startswith=value) |
Q(primary_ip6__address__startswith=value)
Q(primary_ip6__address__startswith=value) |
Q(serial_number__icontains=value)
)

def _has_primary_ip(self, queryset, name, value):
Expand Down
2 changes: 1 addition & 1 deletion netbox/virtualization/forms/bulk_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class Meta:
model = VirtualMachine
fields = (
'name', 'status', 'role', 'site', 'cluster', 'device', 'tenant', 'platform', 'vcpus', 'memory', 'disk',
'description', 'config_template', 'comments', 'tags',
'description', 'serial_number', 'config_template', 'comments', 'tags',
)


Expand Down
6 changes: 5 additions & 1 deletion netbox/virtualization/forms/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class VirtualMachineFilterForm(
FieldSet('region_id', 'site_group_id', 'site_id', name=_('Location')),
FieldSet(
'status', 'role_id', 'platform_id', 'mac_address', 'has_primary_ip', 'config_template_id',
'local_context_data', name=_('Attributes')
'local_context_data', 'serial_number', name=_('Attributes')
),
FieldSet('tenant_group_id', 'tenant_id', name=_('Tenant')),
FieldSet('contact', 'contact_role', 'contact_group', name=_('Contacts')),
Expand Down Expand Up @@ -178,6 +178,10 @@ class VirtualMachineFilterForm(
choices=BOOLEAN_WITH_BLANK_CHOICES
)
)
serial_number = forms.CharField(
required=False,
label=_('Serial number')
)
config_template_id = DynamicModelMultipleChoiceField(
queryset=ConfigTemplate.objects.all(),
required=False,
Expand Down
6 changes: 3 additions & 3 deletions netbox/virtualization/forms/model_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class VirtualMachineForm(TenancyForm, NetBoxModelForm):
comments = CommentField()

fieldsets = (
FieldSet('name', 'role', 'status', 'description', 'tags', name=_('Virtual Machine')),
FieldSet('name', 'role', 'status', 'description', 'serial_number', 'tags', name=_('Virtual Machine')),
FieldSet('site', 'cluster', 'device', name=_('Site/Cluster')),
FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
FieldSet('platform', 'primary_ip4', 'primary_ip6', 'config_template', name=_('Management')),
Expand All @@ -229,8 +229,8 @@ class Meta:
model = VirtualMachine
fields = [
'name', 'status', 'site', 'cluster', 'device', 'role', 'tenant_group', 'tenant', 'platform', 'primary_ip4',
'primary_ip6', 'vcpus', 'memory', 'disk', 'description', 'comments', 'tags', 'local_context_data',
'config_template',
'primary_ip6', 'vcpus', 'memory', 'disk', 'description', 'serial_number', 'comments', 'tags',
'local_context_data', 'config_template',
]

def __init__(self, *args, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.6 on 2024-06-04 17:09

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('virtualization', '0038_virtualdisk'),
]

operations = [
migrations.AddField(
model_name='virtualmachine',
name='serial_number',
field=models.CharField(blank=True, max_length=50),
),
]
5 changes: 5 additions & 0 deletions netbox/virtualization/models/virtualmachines.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ class VirtualMachine(ContactsMixin, ImageAttachmentsMixin, RenderConfigMixin, Co
null=True,
verbose_name=_('disk (GB)')
)
serial_number = models.CharField(
arthanson marked this conversation as resolved.
Show resolved Hide resolved
verbose_name=_('serial number'),
blank=True,
max_length=50
)

# Counter fields
interface_count = CounterCacheField(
Expand Down
3 changes: 2 additions & 1 deletion netbox/virtualization/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ class VirtualMachineIndex(SearchIndex):
model = models.VirtualMachine
fields = (
('name', 100),
('serial_number', 300),
('description', 500),
('comments', 5000),
)
display_attrs = ('site', 'cluster', 'device', 'tenant', 'platform', 'status', 'role', 'description')
display_attrs = ('site', 'cluster', 'device', 'tenant', 'platform', 'status', 'serial_number', 'role', 'description')


@register_search
Expand Down
2 changes: 1 addition & 1 deletion netbox/virtualization/tables/virtualmachines.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class Meta(NetBoxTable.Meta):
fields = (
'pk', 'id', 'name', 'status', 'site', 'cluster', 'device', 'role', 'tenant', 'tenant_group', 'vcpus',
'memory', 'disk', 'primary_ip4', 'primary_ip6', 'primary_ip', 'description', 'comments', 'config_template',
'contacts', 'tags', 'created', 'last_updated',
'serial_number', 'contacts', 'tags', 'created', 'last_updated',
)
default_columns = (
'pk', 'name', 'status', 'site', 'cluster', 'role', 'tenant', 'vcpus', 'memory', 'disk', 'primary_ip',
Expand Down
10 changes: 8 additions & 2 deletions netbox/virtualization/tests/test_filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ def setUpTestData(cls):
memory=1,
disk=1,
description='foobar1',
local_context_data={"foo": 123}
local_context_data={"foo": 123},
serial_number='111-aaa'
),
VirtualMachine(
name='Virtual Machine 2',
Expand All @@ -341,7 +342,8 @@ def setUpTestData(cls):
vcpus=2,
memory=2,
disk=2,
description='foobar2'
description='foobar2',
serial_number='222-bbb'
),
VirtualMachine(
name='Virtual Machine 3',
Expand Down Expand Up @@ -518,6 +520,10 @@ def test_primary_ip6(self):
params = {'primary_ip6_id': [addresses[2].pk]}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 0)

def test_serial_number(self):
params = {'serial_number': ['111-aaa', '222-bbb']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)


class VMInterfaceTestCase(TestCase, ChangeLoggedFilterSetTests):
queryset = VMInterface.objects.all()
Expand Down
1 change: 1 addition & 0 deletions netbox/virtualization/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ def setUpTestData(cls):
'vcpus': 4,
'memory': 32768,
'disk': 4000,
'serial_number': 'aaa-111',
'comments': 'Some comments',
'tags': [t.pk for t in tags],
'local_context_data': None,
Expand Down