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

Fixes #17413: Duplicate platform names allowed for different manufacturers #19061

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions netbox/dcim/migrations/0201_alter_platform_name_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.1.5 on 2025-02-19 09:24

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dcim', '0200_populate_mac_addresses'),
('extras', '0122_charfield_null_choices'),
]

operations = [
migrations.AlterField(
model_name='platform',
name='name',
field=models.CharField(max_length=100),
),
migrations.AddConstraint(
model_name='platform',
constraint=models.UniqueConstraint(fields=('name', 'manufacturer'), name='unique_platform_per_manufacturer'),
),
]
11 changes: 10 additions & 1 deletion netbox/dcim/models/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,10 @@ class Platform(OrganizationalModel):
Platform refers to the software or firmware running on a Device. For example, "Cisco IOS-XR" or "Juniper Junos". A
Platform may optionally be associated with a particular Manufacturer.
"""
name = models.CharField(
verbose_name=_('name'),
max_length=100
)
manufacturer = models.ForeignKey(
to='dcim.Manufacturer',
on_delete=models.PROTECT,
Expand All @@ -522,7 +526,12 @@ class Meta:
ordering = ('name',)
verbose_name = _('platform')
verbose_name_plural = _('platforms')

constraints = [
models.UniqueConstraint(
fields=['name', 'manufacturer'],
name='unique_platform_per_manufacturer'
)
]

def update_interface_bridges(device, interface_templates, module=None):
"""
Expand Down
24 changes: 22 additions & 2 deletions netbox/virtualization/forms/bulk_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dcim.choices import InterfaceModeChoices
from dcim.forms.mixins import ScopedImportForm
from dcim.models import Device, DeviceRole, Platform, Site
from dcim.models import Device, DeviceRole, Platform, Site, Manufacturer
from extras.models import ConfigTemplate
from ipam.models import VRF
from netbox.forms import NetBoxModelImportForm
Expand Down Expand Up @@ -124,6 +124,13 @@ class VirtualMachineImportForm(NetBoxModelImportForm):
to_field_name='name',
help_text=_('Assigned tenant')
)
manufacturer = CSVModelChoiceField(
label=_('Manufacturer'),
queryset=Manufacturer.objects.all(),
required=False,
to_field_name='name',
help_text=_('Device type manufacturer'),
)
platform = CSVModelChoiceField(
label=_('Platform'),
queryset=Platform.objects.all(),
Expand All @@ -138,12 +145,25 @@ class VirtualMachineImportForm(NetBoxModelImportForm):
label=_('Config template'),
help_text=_('Config template')
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
manufacturer_name = self.data.get('manufacturer') or self.initial.get('manufacturer')
platform_name = self.data.get('platform') or self.initial.get('platform')

if manufacturer_name:
try:
manufacturer = Manufacturer.objects.get(name=manufacturer_name)
self.fields['platform'].queryset = Platform.objects.filter(manufacturer=manufacturer)
except Manufacturer.DoesNotExist:
self.fields['platform'].queryset = Platform.objects.none()
elif platform_name:
self.add_error('manufacturer', _("Manufacturer is required if Platform is provided"))

class Meta:
model = VirtualMachine
fields = (
'name', 'status', 'role', 'site', 'cluster', 'device', 'tenant', 'platform', 'vcpus', 'memory', 'disk',
'description', 'serial', 'config_template', 'comments', 'tags',
'description', 'serial', 'config_template', 'comments', 'tags','manufacturer'
)


Expand Down
Loading