diff --git a/docs/models/dcim/devicetype.md b/docs/models/dcim/devicetype.md index a7e00dbc6..b919465c8 100644 --- a/docs/models/dcim/devicetype.md +++ b/docs/models/dcim/devicetype.md @@ -12,3 +12,5 @@ Some devices house child devices which share physical resources, like space and !!! note This parent/child relationship is **not** suitable for modeling chassis-based devices, wherein child members share a common control plane. Instead, line cards and similarly non-autonomous hardware should be modeled as inventory items within a device, with any associated interfaces or other components assigned directly to the device. + +A device type may optionally specify an airflow direction, such as front-to-rear, rear-to-front, or passive. Airflow direction may also be set separately per device. If it is not defined for a device at the time of its creation, it will inherit the airflow setting of its device type. diff --git a/docs/release-notes/version-3.1.md b/docs/release-notes/version-3.1.md index 23567d68e..c49552edd 100644 --- a/docs/release-notes/version-3.1.md +++ b/docs/release-notes/version-3.1.md @@ -6,6 +6,7 @@ ### Enhancements * [#1337](https://github.com/netbox-community/netbox/issues/1337) - Add WWN field to interfaces +* [#3839](https://github.com/netbox-community/netbox/issues/3839) - Add `airflow` field for devices types and devices * [#6711](https://github.com/netbox-community/netbox/issues/6711) - Add `longtext` custom field type with Markdown support * [#6874](https://github.com/netbox-community/netbox/issues/6874) - Add tenant assignment for locations diff --git a/netbox/dcim/api/serializers.py b/netbox/dcim/api/serializers.py index e42b0246b..9d261d9e8 100644 --- a/netbox/dcim/api/serializers.py +++ b/netbox/dcim/api/serializers.py @@ -465,6 +465,7 @@ class DeviceSerializer(PrimaryModelSerializer): rack = NestedRackSerializer(required=False, allow_null=True) face = ChoiceField(choices=DeviceFaceChoices, allow_blank=True, required=False) status = ChoiceField(choices=DeviceStatusChoices, required=False) + airflow = ChoiceField(choices=DeviceAirflowChoices, allow_blank=True, required=False) primary_ip = NestedIPAddressSerializer(read_only=True) primary_ip4 = NestedIPAddressSerializer(required=False, allow_null=True) primary_ip6 = NestedIPAddressSerializer(required=False, allow_null=True) @@ -476,9 +477,9 @@ class DeviceSerializer(PrimaryModelSerializer): model = Device fields = [ 'id', 'url', 'display', 'name', 'device_type', 'device_role', 'tenant', 'platform', 'serial', 'asset_tag', - 'site', 'location', 'rack', 'position', 'face', 'parent_device', 'status', 'primary_ip', 'primary_ip4', - 'primary_ip6', 'cluster', 'virtual_chassis', 'vc_position', 'vc_priority', 'comments', 'local_context_data', - 'tags', 'custom_fields', 'created', 'last_updated', + 'site', 'location', 'rack', 'position', 'face', 'parent_device', 'status', 'airflow', 'primary_ip', + 'primary_ip4', 'primary_ip6', 'cluster', 'virtual_chassis', 'vc_position', 'vc_priority', 'comments', + 'local_context_data', 'tags', 'custom_fields', 'created', 'last_updated', ] validators = [] diff --git a/netbox/dcim/filtersets.py b/netbox/dcim/filtersets.py index ee7957a92..c3de7cb08 100644 --- a/netbox/dcim/filtersets.py +++ b/netbox/dcim/filtersets.py @@ -751,7 +751,7 @@ class DeviceFilterSet(PrimaryModelFilterSet, TenancyFilterSet, LocalConfigContex class Meta: model = Device - fields = ['id', 'name', 'asset_tag', 'face', 'position', 'vc_position', 'vc_priority'] + fields = ['id', 'name', 'asset_tag', 'face', 'position', 'airflow', 'vc_position', 'vc_priority'] def search(self, queryset, name, value): if not value.strip(): diff --git a/netbox/dcim/forms/bulk_edit.py b/netbox/dcim/forms/bulk_edit.py index 1cc79ee48..289057be9 100644 --- a/netbox/dcim/forms/bulk_edit.py +++ b/netbox/dcim/forms/bulk_edit.py @@ -342,7 +342,7 @@ class DeviceTypeBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldModel ) class Meta: - nullable_fields = [] + nullable_fields = ['airflow'] class DeviceRoleBulkEditForm(BootstrapMixin, CustomFieldModelBulkEditForm): @@ -434,6 +434,11 @@ class DeviceBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldModelBulk required=False, widget=StaticSelect() ) + airflow = forms.ChoiceField( + choices=add_blank_choice(DeviceAirflowChoices), + required=False, + widget=StaticSelect() + ) serial = forms.CharField( max_length=50, required=False, @@ -442,7 +447,7 @@ class DeviceBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldModelBulk class Meta: nullable_fields = [ - 'tenant', 'platform', 'serial', + 'tenant', 'platform', 'serial', 'airflow', ] diff --git a/netbox/dcim/forms/bulk_import.py b/netbox/dcim/forms/bulk_import.py index ff9ab6fff..bd9e8cd4a 100644 --- a/netbox/dcim/forms/bulk_import.py +++ b/netbox/dcim/forms/bulk_import.py @@ -369,12 +369,17 @@ class DeviceCSVForm(BaseDeviceCSVForm): required=False, help_text='Mounted rack face' ) + airflow = CSVChoiceField( + choices=DeviceAirflowChoices, + required=False, + help_text='Airflow direction' + ) class Meta(BaseDeviceCSVForm.Meta): fields = [ 'name', 'device_role', 'tenant', 'manufacturer', 'device_type', 'platform', 'serial', 'asset_tag', 'status', - 'site', 'location', 'rack', 'position', 'face', 'virtual_chassis', 'vc_position', 'vc_priority', 'cluster', - 'comments', + 'site', 'location', 'rack', 'position', 'face', 'airflow', 'virtual_chassis', 'vc_position', 'vc_priority', + 'cluster', 'comments', ] def __init__(self, data=None, *args, **kwargs): diff --git a/netbox/dcim/forms/filtersets.py b/netbox/dcim/forms/filtersets.py index e6b9ec8c4..94e7bce05 100644 --- a/netbox/dcim/forms/filtersets.py +++ b/netbox/dcim/forms/filtersets.py @@ -490,7 +490,7 @@ class DeviceFilterForm(BootstrapMixin, LocalConfigContextFilterForm, TenancyFilt field_groups = [ ['q', 'tag'], ['region_id', 'site_group_id', 'site_id', 'location_id', 'rack_id'], - ['status', 'role_id', 'serial', 'asset_tag', 'mac_address'], + ['status', 'role_id', 'airflow', 'serial', 'asset_tag', 'mac_address'], ['manufacturer_id', 'device_type_id', 'platform_id'], ['tenant_group_id', 'tenant_id'], [ @@ -579,6 +579,11 @@ class DeviceFilterForm(BootstrapMixin, LocalConfigContextFilterForm, TenancyFilt required=False, widget=StaticSelectMultiple() ) + airflow = forms.MultipleChoiceField( + choices=add_blank_choice(DeviceAirflowChoices), + required=False, + widget=StaticSelectMultiple() + ) serial = forms.CharField( required=False ) diff --git a/netbox/dcim/forms/models.py b/netbox/dcim/forms/models.py index f0059e770..cb690840f 100644 --- a/netbox/dcim/forms/models.py +++ b/netbox/dcim/forms/models.py @@ -522,8 +522,8 @@ class DeviceForm(BootstrapMixin, TenancyForm, CustomFieldModelForm): model = Device fields = [ 'name', 'device_role', 'device_type', 'serial', 'asset_tag', 'region', 'site_group', 'site', 'rack', - 'location', 'position', 'face', 'status', 'platform', 'primary_ip4', 'primary_ip6', 'cluster_group', - 'cluster', 'tenant_group', 'tenant', 'comments', 'tags', 'local_context_data' + 'location', 'position', 'face', 'status', 'airflow', 'platform', 'primary_ip4', 'primary_ip6', + 'cluster_group', 'cluster', 'tenant_group', 'tenant', 'comments', 'tags', 'local_context_data' ] help_texts = { 'device_role': "The function this device serves", @@ -534,6 +534,7 @@ class DeviceForm(BootstrapMixin, TenancyForm, CustomFieldModelForm): widgets = { 'face': StaticSelect(), 'status': StaticSelect(), + 'airflow': StaticSelect(), 'primary_ip4': StaticSelect(), 'primary_ip6': StaticSelect(), } diff --git a/netbox/dcim/graphql/types.py b/netbox/dcim/graphql/types.py index 0f186c5d4..80c32e66d 100644 --- a/netbox/dcim/graphql/types.py +++ b/netbox/dcim/graphql/types.py @@ -144,6 +144,9 @@ class DeviceType(ConfigContextMixin, ImageAttachmentsMixin, PrimaryObjectType): def resolve_face(self, info): return self.face or None + def resolve_airflow(self, info): + return self.airflow or None + class DeviceBayType(ComponentObjectType): diff --git a/netbox/dcim/migrations/0136_devicetype_airflow.py b/netbox/dcim/migrations/0136_device_airflow.py similarity index 67% rename from netbox/dcim/migrations/0136_devicetype_airflow.py rename to netbox/dcim/migrations/0136_device_airflow.py index 2b3bd215f..a0887a0b4 100644 --- a/netbox/dcim/migrations/0136_devicetype_airflow.py +++ b/netbox/dcim/migrations/0136_device_airflow.py @@ -1,5 +1,3 @@ -# Generated by Django 3.2.8 on 2021-10-14 19:29 - from django.db import migrations, models @@ -15,4 +13,9 @@ class Migration(migrations.Migration): name='airflow', field=models.CharField(blank=True, max_length=50), ), + migrations.AddField( + model_name='device', + name='airflow', + field=models.CharField(blank=True, max_length=50), + ), ] diff --git a/netbox/dcim/models/devices.py b/netbox/dcim/models/devices.py index 2a4f58d10..669f5cfbd 100644 --- a/netbox/dcim/models/devices.py +++ b/netbox/dcim/models/devices.py @@ -118,8 +118,7 @@ class DeviceType(PrimaryModel): airflow = models.CharField( max_length=50, choices=DeviceAirflowChoices, - blank=True, - verbose_name='Airflow direction' + blank=True ) front_image = models.ImageField( upload_to='devicetype-images', @@ -537,6 +536,11 @@ class Device(PrimaryModel, ConfigContextModel): choices=DeviceStatusChoices, default=DeviceStatusChoices.STATUS_ACTIVE ) + airflow = models.CharField( + max_length=50, + choices=DeviceAirflowChoices, + blank=True + ) primary_ip4 = models.OneToOneField( to='ipam.IPAddress', on_delete=models.SET_NULL, @@ -587,7 +591,7 @@ class Device(PrimaryModel, ConfigContextModel): objects = ConfigContextModelQuerySet.as_manager() clone_fields = [ - 'device_type', 'device_role', 'tenant', 'platform', 'site', 'location', 'rack', 'status', 'cluster', + 'device_type', 'device_role', 'tenant', 'platform', 'site', 'location', 'rack', 'status', 'airflow', 'cluster', ] class Meta: @@ -748,9 +752,12 @@ class Device(PrimaryModel, ConfigContextModel): }) def save(self, *args, **kwargs): - is_new = not bool(self.pk) + # Inherit airflow attribute from DeviceType if not set + if is_new and not self.airflow: + self.airflow = self.device_type.airflow + super().save(*args, **kwargs) # If this is a new Device, instantiate all of the related components per the DeviceType definition diff --git a/netbox/dcim/tables/devices.py b/netbox/dcim/tables/devices.py index c2b4b907b..a2d3f3da2 100644 --- a/netbox/dcim/tables/devices.py +++ b/netbox/dcim/tables/devices.py @@ -197,8 +197,8 @@ class DeviceTable(BaseTable): model = Device fields = ( 'pk', 'name', 'status', 'tenant', 'device_role', 'manufacturer', 'device_type', 'platform', 'serial', - 'asset_tag', 'site', 'location', 'rack', 'position', 'face', 'primary_ip', 'primary_ip4', 'primary_ip6', - 'cluster', 'virtual_chassis', 'vc_position', 'vc_priority', 'comments', 'tags', + 'asset_tag', 'site', 'location', 'rack', 'position', 'face', 'airflow', 'primary_ip', 'primary_ip4', + 'primary_ip6', 'cluster', 'virtual_chassis', 'vc_position', 'vc_priority', 'comments', 'tags', ) default_columns = ( 'pk', 'name', 'status', 'tenant', 'site', 'location', 'rack', 'device_role', 'manufacturer', 'device_type', diff --git a/netbox/dcim/tests/test_filtersets.py b/netbox/dcim/tests/test_filtersets.py index f9ecf103f..fcee2914b 100644 --- a/netbox/dcim/tests/test_filtersets.py +++ b/netbox/dcim/tests/test_filtersets.py @@ -1239,8 +1239,8 @@ class DeviceTestCase(TestCase, ChangeLoggedFilterSetTests): devices = ( Device(name='Device 1', device_type=device_types[0], device_role=device_roles[0], platform=platforms[0], tenant=tenants[0], serial='ABC', asset_tag='1001', site=sites[0], location=locations[0], rack=racks[0], position=1, face=DeviceFaceChoices.FACE_FRONT, status=DeviceStatusChoices.STATUS_ACTIVE, cluster=clusters[0], local_context_data={"foo": 123}), - Device(name='Device 2', device_type=device_types[1], device_role=device_roles[1], platform=platforms[1], tenant=tenants[1], serial='DEF', asset_tag='1002', site=sites[1], location=locations[1], rack=racks[1], position=2, face=DeviceFaceChoices.FACE_FRONT, status=DeviceStatusChoices.STATUS_STAGED, cluster=clusters[1]), - Device(name='Device 3', device_type=device_types[2], device_role=device_roles[2], platform=platforms[2], tenant=tenants[2], serial='GHI', asset_tag='1003', site=sites[2], location=locations[2], rack=racks[2], position=3, face=DeviceFaceChoices.FACE_REAR, status=DeviceStatusChoices.STATUS_FAILED, cluster=clusters[2]), + Device(name='Device 2', device_type=device_types[1], device_role=device_roles[1], platform=platforms[1], tenant=tenants[1], serial='DEF', asset_tag='1002', site=sites[1], location=locations[1], rack=racks[1], position=2, face=DeviceFaceChoices.FACE_FRONT, status=DeviceStatusChoices.STATUS_STAGED, airflow=DeviceAirflowChoices.AIRFLOW_FRONT_TO_REAR, cluster=clusters[1]), + Device(name='Device 3', device_type=device_types[2], device_role=device_roles[2], platform=platforms[2], tenant=tenants[2], serial='GHI', asset_tag='1003', site=sites[2], location=locations[2], rack=racks[2], position=3, face=DeviceFaceChoices.FACE_REAR, status=DeviceStatusChoices.STATUS_FAILED, airflow=DeviceAirflowChoices.AIRFLOW_REAR_TO_FRONT, cluster=clusters[2]), ) Device.objects.bulk_create(devices) @@ -1394,6 +1394,10 @@ class DeviceTestCase(TestCase, ChangeLoggedFilterSetTests): params = {'is_full_depth': 'false'} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) + def test_airflow(self): + params = {'airflow': DeviceAirflowChoices.AIRFLOW_FRONT_TO_REAR} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) + def test_mac_address(self): params = {'mac_address': ['00-00-00-00-00-01', '00-00-00-00-00-02']} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) diff --git a/netbox/templates/dcim/device.html b/netbox/templates/dcim/device.html index 9d1868e1e..ec1ea3fa1 100644 --- a/netbox/templates/dcim/device.html +++ b/netbox/templates/dcim/device.html @@ -93,6 +93,12 @@ {{ object.device_type }} ({{ object.device_type.u_height }}U) + + Airflow + + {{ object.get_airflow_display|placeholder }} + + Serial Number {{ object.serial|placeholder }} diff --git a/netbox/templates/dcim/device_edit.html b/netbox/templates/dcim/device_edit.html index fbafa197d..1be272d3a 100644 --- a/netbox/templates/dcim/device_edit.html +++ b/netbox/templates/dcim/device_edit.html @@ -19,6 +19,7 @@ {% render_field form.manufacturer %} {% render_field form.device_type %} + {% render_field form.airflow %} {% render_field form.serial %} {% render_field form.asset_tag %} diff --git a/netbox/templates/dcim/devicetype.html b/netbox/templates/dcim/devicetype.html index 2db37121f..40955f5d6 100644 --- a/netbox/templates/dcim/devicetype.html +++ b/netbox/templates/dcim/devicetype.html @@ -91,7 +91,7 @@ - Airflow direction + Airflow {{ object.get_airflow_display|placeholder }}