Add config_template FK to Platform

This commit is contained in:
jeremystretch 2023-02-16 13:44:57 -05:00
parent 73e18f26b3
commit 3d5b16b677
12 changed files with 64 additions and 12 deletions

View File

@ -22,6 +22,10 @@ A unique URL-friendly identifier. (This value can be used for filtering.)
If designated, this platform will be available for use only to devices assigned to this [manufacturer](./manufacturer.md). This can be handy e.g. for limiting network operating systems to use on hardware produced by the relevant vendor. However, it should not be used when defining general-purpose software platforms. If designated, this platform will be available for use only to devices assigned to this [manufacturer](./manufacturer.md). This can be handy e.g. for limiting network operating systems to use on hardware produced by the relevant vendor. However, it should not be used when defining general-purpose software platforms.
### Configuration Template
The default [configuration template](../extras/configtemplate.md) for devices assigned to this platform.
### NAPALM Driver ### NAPALM Driver
The [NAPALM driver](https://napalm.readthedocs.io/en/latest/support/index.html) associated with this platform. The [NAPALM driver](https://napalm.readthedocs.io/en/latest/support/index.html) associated with this platform.

View File

@ -620,8 +620,8 @@ class PlatformSerializer(NetBoxModelSerializer):
class Meta: class Meta:
model = Platform model = Platform
fields = [ fields = [
'id', 'url', 'display', 'name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'description', 'id', 'url', 'display', 'name', 'slug', 'manufacturer', 'config_template', 'napalm_driver', 'napalm_args',
'tags', 'custom_fields', 'created', 'last_updated', 'device_count', 'virtualmachine_count', 'description', 'tags', 'custom_fields', 'created', 'last_updated', 'device_count', 'virtualmachine_count',
] ]

View File

@ -379,7 +379,7 @@ class DeviceRoleViewSet(NetBoxModelViewSet):
# #
class PlatformViewSet(NetBoxModelViewSet): class PlatformViewSet(NetBoxModelViewSet):
queryset = Platform.objects.prefetch_related('tags').annotate( queryset = Platform.objects.prefetch_related('config_template', 'tags').annotate(
device_count=count_related(Device, 'platform'), device_count=count_related(Device, 'platform'),
virtualmachine_count=count_related(VirtualMachine, 'platform') virtualmachine_count=count_related(VirtualMachine, 'platform')
) )

View File

@ -799,6 +799,10 @@ class PlatformFilterSet(OrganizationalModelFilterSet):
to_field_name='slug', to_field_name='slug',
label=_('Manufacturer (slug)'), label=_('Manufacturer (slug)'),
) )
config_template_id = django_filters.ModelMultipleChoiceFilter(
queryset=ConfigTemplate.objects.all(),
label=_('Config template (ID)'),
)
class Meta: class Meta:
model = Platform model = Platform

View File

@ -480,7 +480,10 @@ class PlatformBulkEditForm(NetBoxModelBulkEditForm):
max_length=50, max_length=50,
required=False required=False
) )
# TODO: Bulk edit support for napalm_args config_template = DynamicModelChoiceField(
queryset=ConfigTemplate.objects.all(),
required=False
)
description = forms.CharField( description = forms.CharField(
max_length=200, max_length=200,
required=False required=False
@ -488,9 +491,9 @@ class PlatformBulkEditForm(NetBoxModelBulkEditForm):
model = Platform model = Platform
fieldsets = ( fieldsets = (
(None, ('manufacturer', 'napalm_driver', 'description')), (None, ('manufacturer', 'config_template', 'napalm_driver', 'description')),
) )
nullable_fields = ('manufacturer', 'napalm_driver', 'description') nullable_fields = ('manufacturer', 'config_template', 'napalm_driver', 'description')
class DeviceBulkEditForm(NetBoxModelBulkEditForm): class DeviceBulkEditForm(NetBoxModelBulkEditForm):

View File

@ -332,10 +332,18 @@ class PlatformImportForm(NetBoxModelImportForm):
to_field_name='name', to_field_name='name',
help_text=_('Limit platform assignments to this manufacturer') help_text=_('Limit platform assignments to this manufacturer')
) )
config_template = CSVModelChoiceField(
queryset=ConfigTemplate.objects.all(),
to_field_name='name',
required=False,
help_text=_('Config template')
)
class Meta: class Meta:
model = Platform model = Platform
fields = ('name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'description', 'tags') fields = (
'name', 'slug', 'manufacturer', 'config_template', 'napalm_driver', 'napalm_args', 'description', 'tags',
)
class BaseDeviceImportForm(NetBoxModelImportForm): class BaseDeviceImportForm(NetBoxModelImportForm):

View File

@ -584,6 +584,11 @@ class PlatformFilterForm(NetBoxModelFilterSetForm):
required=False, required=False,
label=_('Manufacturer') label=_('Manufacturer')
) )
config_template_id = DynamicModelMultipleChoiceField(
queryset=ConfigTemplate.objects.all(),
required=False,
label=_('Config template')
)
tag = TagFilterField(model) tag = TagFilterField(model)

View File

@ -441,13 +441,17 @@ class PlatformForm(NetBoxModelForm):
queryset=Manufacturer.objects.all(), queryset=Manufacturer.objects.all(),
required=False required=False
) )
config_template = DynamicModelChoiceField(
queryset=ConfigTemplate.objects.all(),
required=False
)
slug = SlugField( slug = SlugField(
max_length=64 max_length=64
) )
fieldsets = ( fieldsets = (
('Platform', ( ('Platform', (
'name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'description', 'tags', 'name', 'slug', 'manufacturer', 'config_template', 'napalm_driver', 'napalm_args', 'description', 'tags',
)), )),
) )
@ -455,7 +459,7 @@ class PlatformForm(NetBoxModelForm):
class Meta: class Meta:
model = Platform model = Platform
fields = [ fields = [
'name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'description', 'tags', 'name', 'slug', 'manufacturer', 'config_template', 'napalm_driver', 'napalm_args', 'description', 'tags',
] ]
widgets = { widgets = {
'napalm_args': forms.Textarea(), 'napalm_args': forms.Textarea(),

View File

@ -20,4 +20,9 @@ class Migration(migrations.Migration):
name='config_template', name='config_template',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='device_roles', to='extras.configtemplate'), field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='device_roles', to='extras.configtemplate'),
), ),
migrations.AddField(
model_name='platform',
name='config_template',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='platforms', to='extras.configtemplate'),
),
] ]

View File

@ -436,6 +436,13 @@ class Platform(OrganizationalModel):
null=True, null=True,
help_text=_('Optionally limit this platform to devices of a certain manufacturer') help_text=_('Optionally limit this platform to devices of a certain manufacturer')
) )
config_template = models.ForeignKey(
to='extras.ConfigTemplate',
on_delete=models.PROTECT,
related_name='platforms',
blank=True,
null=True
)
napalm_driver = models.CharField( napalm_driver = models.CharField(
max_length=50, max_length=50,
blank=True, blank=True,
@ -880,7 +887,12 @@ class Device(PrimaryModel, ConfigContextModel):
""" """
Return the appropriate ConfigTemplate (if any) for this Device. Return the appropriate ConfigTemplate (if any) for this Device.
""" """
return self.config_template or self.device_role.config_template if self.config_template:
return self.config_template
if self.device_role.config_template:
return self.device_role.config_template
if self.platform and self.platform.config_template:
return self.platform.config_template
def get_vc_master(self): def get_vc_master(self):
""" """

View File

@ -113,6 +113,9 @@ class PlatformTable(NetBoxTable):
manufacturer = tables.Column( manufacturer = tables.Column(
linkify=True linkify=True
) )
config_template = tables.Column(
linkify=True
)
device_count = columns.LinkedCountColumn( device_count = columns.LinkedCountColumn(
viewname='dcim:device_list', viewname='dcim:device_list',
url_params={'platform_id': 'pk'}, url_params={'platform_id': 'pk'},
@ -130,8 +133,8 @@ class PlatformTable(NetBoxTable):
class Meta(NetBoxTable.Meta): class Meta(NetBoxTable.Meta):
model = models.Platform model = models.Platform
fields = ( fields = (
'pk', 'id', 'name', 'manufacturer', 'device_count', 'vm_count', 'slug', 'napalm_driver', 'napalm_args', 'pk', 'id', 'name', 'manufacturer', 'device_count', 'vm_count', 'slug', 'config_template', 'napalm_driver',
'description', 'tags', 'actions', 'created', 'last_updated', 'napalm_args', 'description', 'tags', 'actions', 'created', 'last_updated',
) )
default_columns = ( default_columns = (
'pk', 'name', 'manufacturer', 'device_count', 'vm_count', 'napalm_driver', 'description', 'pk', 'name', 'manufacturer', 'device_count', 'vm_count', 'napalm_driver', 'description',

View File

@ -39,6 +39,10 @@
<th scope="row">Manufacturer</th> <th scope="row">Manufacturer</th>
<td>{{ object.manufacturer|linkify|placeholder }}</td> <td>{{ object.manufacturer|linkify|placeholder }}</td>
</tr> </tr>
<tr>
<th scope="row">Config Template</th>
<td>{{ object.config_template|linkify|placeholder }}</td>
</tr>
<tr> <tr>
<th scope="row">NAPALM Driver</th> <th scope="row">NAPALM Driver</th>
<td>{{ object.napalm_driver|placeholder }}</td> <td>{{ object.napalm_driver|placeholder }}</td>