|
|
@@ -1,42 +1,57 @@
|
|
|
-from __future__ import unicode_literals
|
|
|
-
|
|
|
from rest_framework import serializers
|
|
|
from rest_framework.validators import UniqueTogetherValidator
|
|
|
from taggit_serializer.serializers import TaggitSerializer, TagListSerializerField
|
|
|
|
|
|
-from circuits.models import Circuit, CircuitTermination
|
|
|
-from dcim.constants import (
|
|
|
- CONNECTION_STATUS_CHOICES, DEVICE_STATUS_CHOICES, IFACE_FF_CHOICES, IFACE_MODE_CHOICES, IFACE_ORDERING_CHOICES,
|
|
|
- RACK_FACE_CHOICES, RACK_TYPE_CHOICES, RACK_WIDTH_CHOICES, SITE_STATUS_CHOICES, SUBDEVICE_ROLE_CHOICES,
|
|
|
-)
|
|
|
+from dcim.constants import *
|
|
|
from dcim.models import (
|
|
|
- ConsolePort, ConsolePortTemplate, ConsoleServerPort, ConsoleServerPortTemplate, Device, DeviceBay,
|
|
|
- DeviceBayTemplate, DeviceType, DeviceRole, Interface, InterfaceConnection, InterfaceTemplate, Manufacturer,
|
|
|
- InventoryItem, Platform, PowerOutlet, PowerOutletTemplate, PowerPort, PowerPortTemplate, Rack, RackGroup,
|
|
|
- RackReservation, RackRole, Region, Site, VirtualChassis,
|
|
|
+ Cable, ConsolePort, ConsolePortTemplate, ConsoleServerPort, ConsoleServerPortTemplate, Device, DeviceBay,
|
|
|
+ DeviceBayTemplate, DeviceType, DeviceRole, FrontPort, FrontPortTemplate, Interface, InterfaceTemplate,
|
|
|
+ Manufacturer, InventoryItem, Platform, PowerOutlet, PowerOutletTemplate, PowerPort, PowerPortTemplate, Rack,
|
|
|
+ RackGroup, RackReservation, RackRole, RearPort, RearPortTemplate, Region, Site, VirtualChassis,
|
|
|
)
|
|
|
from extras.api.customfields import CustomFieldModelSerializer
|
|
|
-from ipam.models import IPAddress, VLAN
|
|
|
-from tenancy.api.serializers import NestedTenantSerializer
|
|
|
-from users.api.serializers import NestedUserSerializer
|
|
|
+from ipam.api.nested_serializers import NestedIPAddressSerializer, NestedVLANSerializer
|
|
|
+from ipam.models import VLAN
|
|
|
+from tenancy.api.nested_serializers import NestedTenantSerializer
|
|
|
+from users.api.nested_serializers import NestedUserSerializer
|
|
|
from utilities.api import (
|
|
|
- ChoiceField, SerializedPKRelatedField, TimeZoneField, ValidatedModelSerializer,
|
|
|
- WritableNestedSerializer,
|
|
|
+ ChoiceField, ContentTypeField, SerializedPKRelatedField, TimeZoneField, ValidatedModelSerializer,
|
|
|
+ WritableNestedSerializer, get_serializer_for_model,
|
|
|
)
|
|
|
-from virtualization.models import Cluster
|
|
|
+from virtualization.api.nested_serializers import NestedClusterSerializer
|
|
|
+from .nested_serializers import *
|
|
|
|
|
|
|
|
|
-#
|
|
|
-# Regions
|
|
|
-#
|
|
|
+class ConnectedEndpointSerializer(ValidatedModelSerializer):
|
|
|
+ connected_endpoint_type = serializers.SerializerMethodField(read_only=True)
|
|
|
+ connected_endpoint = serializers.SerializerMethodField(read_only=True)
|
|
|
+ connection_status = ChoiceField(choices=CONNECTION_STATUS_CHOICES, read_only=True)
|
|
|
|
|
|
-class NestedRegionSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:region-detail')
|
|
|
+ def get_connected_endpoint_type(self, obj):
|
|
|
+ if hasattr(obj, 'connected_endpoint') and obj.connected_endpoint is not None:
|
|
|
+ return '{}.{}'.format(
|
|
|
+ obj.connected_endpoint._meta.app_label,
|
|
|
+ obj.connected_endpoint._meta.model_name
|
|
|
+ )
|
|
|
+ return None
|
|
|
|
|
|
- class Meta:
|
|
|
- model = Region
|
|
|
- fields = ['id', 'url', 'name', 'slug']
|
|
|
+ def get_connected_endpoint(self, obj):
|
|
|
+ """
|
|
|
+ Return the appropriate serializer for the type of connected object.
|
|
|
+ """
|
|
|
+ if getattr(obj, 'connected_endpoint', None) is None:
|
|
|
+ return None
|
|
|
+
|
|
|
+ serializer = get_serializer_for_model(obj.connected_endpoint, prefix='Nested')
|
|
|
+ context = {'request': self.context['request']}
|
|
|
+ data = serializer(obj.connected_endpoint, context=context).data
|
|
|
|
|
|
+ return data
|
|
|
+
|
|
|
+
|
|
|
+#
|
|
|
+# Regions/sites
|
|
|
+#
|
|
|
|
|
|
class RegionSerializer(serializers.ModelSerializer):
|
|
|
parent = NestedRegionSerializer(required=False, allow_null=True)
|
|
|
@@ -46,10 +61,6 @@ class RegionSerializer(serializers.ModelSerializer):
|
|
|
fields = ['id', 'name', 'slug', 'parent']
|
|
|
|
|
|
|
|
|
-#
|
|
|
-# Sites
|
|
|
-#
|
|
|
-
|
|
|
class SiteSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
|
|
status = ChoiceField(choices=SITE_STATUS_CHOICES, required=False)
|
|
|
region = NestedRegionSerializer(required=False, allow_null=True)
|
|
|
@@ -72,16 +83,8 @@ class SiteSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
|
|
]
|
|
|
|
|
|
|
|
|
-class NestedSiteSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:site-detail')
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = Site
|
|
|
- fields = ['id', 'url', 'name', 'slug']
|
|
|
-
|
|
|
-
|
|
|
#
|
|
|
-# Rack groups
|
|
|
+# Racks
|
|
|
#
|
|
|
|
|
|
class RackGroupSerializer(ValidatedModelSerializer):
|
|
|
@@ -92,18 +95,6 @@ class RackGroupSerializer(ValidatedModelSerializer):
|
|
|
fields = ['id', 'name', 'slug', 'site']
|
|
|
|
|
|
|
|
|
-class NestedRackGroupSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:rackgroup-detail')
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = RackGroup
|
|
|
- fields = ['id', 'url', 'name', 'slug']
|
|
|
-
|
|
|
-
|
|
|
-#
|
|
|
-# Rack roles
|
|
|
-#
|
|
|
-
|
|
|
class RackRoleSerializer(ValidatedModelSerializer):
|
|
|
|
|
|
class Meta:
|
|
|
@@ -111,32 +102,23 @@ class RackRoleSerializer(ValidatedModelSerializer):
|
|
|
fields = ['id', 'name', 'slug', 'color']
|
|
|
|
|
|
|
|
|
-class NestedRackRoleSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:rackrole-detail')
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = RackRole
|
|
|
- fields = ['id', 'url', 'name', 'slug']
|
|
|
-
|
|
|
-
|
|
|
-#
|
|
|
-# Racks
|
|
|
-#
|
|
|
-
|
|
|
class RackSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
|
|
site = NestedSiteSerializer()
|
|
|
group = NestedRackGroupSerializer(required=False, allow_null=True, default=None)
|
|
|
tenant = NestedTenantSerializer(required=False, allow_null=True)
|
|
|
+ status = ChoiceField(choices=RACK_STATUS_CHOICES, required=False)
|
|
|
role = NestedRackRoleSerializer(required=False, allow_null=True)
|
|
|
type = ChoiceField(choices=RACK_TYPE_CHOICES, required=False, allow_null=True)
|
|
|
width = ChoiceField(choices=RACK_WIDTH_CHOICES, required=False)
|
|
|
+ outer_unit = ChoiceField(choices=RACK_DIMENSION_UNIT_CHOICES, required=False)
|
|
|
tags = TagListSerializerField(required=False)
|
|
|
|
|
|
class Meta:
|
|
|
model = Rack
|
|
|
fields = [
|
|
|
- 'id', 'name', 'facility_id', 'display_name', 'site', 'group', 'tenant', 'role', 'serial', 'type', 'width',
|
|
|
- 'u_height', 'desc_units', 'comments', 'tags', 'custom_fields', 'created', 'last_updated',
|
|
|
+ 'id', 'name', 'facility_id', 'display_name', 'site', 'group', 'tenant', 'status', 'role', 'serial',
|
|
|
+ 'asset_tag', 'type', 'width', 'u_height', 'desc_units', 'outer_width', 'outer_depth', 'outer_unit',
|
|
|
+ 'comments', 'tags', 'custom_fields', 'created', 'last_updated',
|
|
|
]
|
|
|
# Omit the UniqueTogetherValidator that would be automatically added to validate (group, facility_id). This
|
|
|
# prevents facility_id from being interpreted as a required field.
|
|
|
@@ -153,31 +135,11 @@ class RackSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
|
|
validator(data)
|
|
|
|
|
|
# Enforce model validation
|
|
|
- super(RackSerializer, self).validate(data)
|
|
|
+ super().validate(data)
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
-class NestedRackSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:rack-detail')
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = Rack
|
|
|
- fields = ['id', 'url', 'name', 'display_name']
|
|
|
-
|
|
|
-
|
|
|
-#
|
|
|
-# Rack units
|
|
|
-#
|
|
|
-
|
|
|
-class NestedDeviceSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:device-detail')
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = Device
|
|
|
- fields = ['id', 'url', 'name', 'display_name']
|
|
|
-
|
|
|
-
|
|
|
class RackUnitSerializer(serializers.Serializer):
|
|
|
"""
|
|
|
A rack unit is an abstraction formed by the set (rack, position, face); it does not exist as a row in the database.
|
|
|
@@ -188,10 +150,6 @@ class RackUnitSerializer(serializers.Serializer):
|
|
|
device = NestedDeviceSerializer(read_only=True)
|
|
|
|
|
|
|
|
|
-#
|
|
|
-# Rack reservations
|
|
|
-#
|
|
|
-
|
|
|
class RackReservationSerializer(ValidatedModelSerializer):
|
|
|
rack = NestedRackSerializer()
|
|
|
user = NestedUserSerializer()
|
|
|
@@ -203,7 +161,7 @@ class RackReservationSerializer(ValidatedModelSerializer):
|
|
|
|
|
|
|
|
|
#
|
|
|
-# Manufacturers
|
|
|
+# Device types
|
|
|
#
|
|
|
|
|
|
class ManufacturerSerializer(ValidatedModelSerializer):
|
|
|
@@ -213,21 +171,8 @@ class ManufacturerSerializer(ValidatedModelSerializer):
|
|
|
fields = ['id', 'name', 'slug']
|
|
|
|
|
|
|
|
|
-class NestedManufacturerSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:manufacturer-detail')
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = Manufacturer
|
|
|
- fields = ['id', 'url', 'name', 'slug']
|
|
|
-
|
|
|
-
|
|
|
-#
|
|
|
-# Device types
|
|
|
-#
|
|
|
-
|
|
|
class DeviceTypeSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
|
|
manufacturer = NestedManufacturerSerializer()
|
|
|
- interface_ordering = ChoiceField(choices=IFACE_ORDERING_CHOICES, required=False)
|
|
|
subdevice_role = ChoiceField(choices=SUBDEVICE_ROLE_CHOICES, required=False, allow_null=True)
|
|
|
instance_count = serializers.IntegerField(source='instances.count', read_only=True)
|
|
|
tags = TagListSerializerField(required=False)
|
|
|
@@ -235,25 +180,11 @@ class DeviceTypeSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
|
|
class Meta:
|
|
|
model = DeviceType
|
|
|
fields = [
|
|
|
- 'id', 'manufacturer', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth', 'interface_ordering',
|
|
|
- 'is_console_server', 'is_pdu', 'is_network_device', 'subdevice_role', 'comments', 'tags', 'custom_fields',
|
|
|
- 'created', 'last_updated', 'instance_count',
|
|
|
+ 'id', 'manufacturer', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth', 'subdevice_role',
|
|
|
+ 'comments', 'tags', 'custom_fields', 'created', 'last_updated', 'instance_count',
|
|
|
]
|
|
|
|
|
|
|
|
|
-class NestedDeviceTypeSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:devicetype-detail')
|
|
|
- manufacturer = NestedManufacturerSerializer(read_only=True)
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = DeviceType
|
|
|
- fields = ['id', 'url', 'manufacturer', 'model', 'slug']
|
|
|
-
|
|
|
-
|
|
|
-#
|
|
|
-# Console port templates
|
|
|
-#
|
|
|
-
|
|
|
class ConsolePortTemplateSerializer(ValidatedModelSerializer):
|
|
|
device_type = NestedDeviceTypeSerializer()
|
|
|
|
|
|
@@ -262,10 +193,6 @@ class ConsolePortTemplateSerializer(ValidatedModelSerializer):
|
|
|
fields = ['id', 'device_type', 'name']
|
|
|
|
|
|
|
|
|
-#
|
|
|
-# Console server port templates
|
|
|
-#
|
|
|
-
|
|
|
class ConsoleServerPortTemplateSerializer(ValidatedModelSerializer):
|
|
|
device_type = NestedDeviceTypeSerializer()
|
|
|
|
|
|
@@ -274,10 +201,6 @@ class ConsoleServerPortTemplateSerializer(ValidatedModelSerializer):
|
|
|
fields = ['id', 'device_type', 'name']
|
|
|
|
|
|
|
|
|
-#
|
|
|
-# Power port templates
|
|
|
-#
|
|
|
-
|
|
|
class PowerPortTemplateSerializer(ValidatedModelSerializer):
|
|
|
device_type = NestedDeviceTypeSerializer()
|
|
|
|
|
|
@@ -286,10 +209,6 @@ class PowerPortTemplateSerializer(ValidatedModelSerializer):
|
|
|
fields = ['id', 'device_type', 'name']
|
|
|
|
|
|
|
|
|
-#
|
|
|
-# Power outlet templates
|
|
|
-#
|
|
|
-
|
|
|
class PowerOutletTemplateSerializer(ValidatedModelSerializer):
|
|
|
device_type = NestedDeviceTypeSerializer()
|
|
|
|
|
|
@@ -298,10 +217,6 @@ class PowerOutletTemplateSerializer(ValidatedModelSerializer):
|
|
|
fields = ['id', 'device_type', 'name']
|
|
|
|
|
|
|
|
|
-#
|
|
|
-# Interface templates
|
|
|
-#
|
|
|
-
|
|
|
class InterfaceTemplateSerializer(ValidatedModelSerializer):
|
|
|
device_type = NestedDeviceTypeSerializer()
|
|
|
form_factor = ChoiceField(choices=IFACE_FF_CHOICES, required=False)
|
|
|
@@ -311,9 +226,24 @@ class InterfaceTemplateSerializer(ValidatedModelSerializer):
|
|
|
fields = ['id', 'device_type', 'name', 'form_factor', 'mgmt_only']
|
|
|
|
|
|
|
|
|
-#
|
|
|
-# Device bay templates
|
|
|
-#
|
|
|
+class RearPortTemplateSerializer(ValidatedModelSerializer):
|
|
|
+ device_type = NestedDeviceTypeSerializer()
|
|
|
+ type = ChoiceField(choices=PORT_TYPE_CHOICES)
|
|
|
+
|
|
|
+ class Meta:
|
|
|
+ model = RearPortTemplate
|
|
|
+ fields = ['id', 'device_type', 'name', 'type', 'positions']
|
|
|
+
|
|
|
+
|
|
|
+class FrontPortTemplateSerializer(ValidatedModelSerializer):
|
|
|
+ device_type = NestedDeviceTypeSerializer()
|
|
|
+ type = ChoiceField(choices=PORT_TYPE_CHOICES)
|
|
|
+ rear_port = NestedRearPortTemplateSerializer()
|
|
|
+
|
|
|
+ class Meta:
|
|
|
+ model = FrontPortTemplate
|
|
|
+ fields = ['id', 'device_type', 'name', 'type', 'rear_port', 'rear_port_position']
|
|
|
+
|
|
|
|
|
|
class DeviceBayTemplateSerializer(ValidatedModelSerializer):
|
|
|
device_type = NestedDeviceTypeSerializer()
|
|
|
@@ -324,7 +254,7 @@ class DeviceBayTemplateSerializer(ValidatedModelSerializer):
|
|
|
|
|
|
|
|
|
#
|
|
|
-# Device roles
|
|
|
+# Devices
|
|
|
#
|
|
|
|
|
|
class DeviceRoleSerializer(ValidatedModelSerializer):
|
|
|
@@ -334,64 +264,12 @@ class DeviceRoleSerializer(ValidatedModelSerializer):
|
|
|
fields = ['id', 'name', 'slug', 'color', 'vm_role']
|
|
|
|
|
|
|
|
|
-class NestedDeviceRoleSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:devicerole-detail')
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = DeviceRole
|
|
|
- fields = ['id', 'url', 'name', 'slug']
|
|
|
-
|
|
|
-
|
|
|
-#
|
|
|
-# Platforms
|
|
|
-#
|
|
|
-
|
|
|
class PlatformSerializer(ValidatedModelSerializer):
|
|
|
manufacturer = NestedManufacturerSerializer(required=False, allow_null=True)
|
|
|
|
|
|
class Meta:
|
|
|
model = Platform
|
|
|
- fields = ['id', 'name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'rpc_client']
|
|
|
-
|
|
|
-
|
|
|
-class NestedPlatformSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:platform-detail')
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = Platform
|
|
|
- fields = ['id', 'url', 'name', 'slug']
|
|
|
-
|
|
|
-
|
|
|
-#
|
|
|
-# Devices
|
|
|
-#
|
|
|
-
|
|
|
-# Cannot import ipam.api.NestedIPAddressSerializer due to circular dependency
|
|
|
-class DeviceIPAddressSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='ipam-api:ipaddress-detail')
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = IPAddress
|
|
|
- fields = ['id', 'url', 'family', 'address']
|
|
|
-
|
|
|
-
|
|
|
-# Cannot import virtualization.api.NestedClusterSerializer due to circular dependency
|
|
|
-class NestedClusterSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='virtualization-api:cluster-detail')
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = Cluster
|
|
|
- fields = ['id', 'url', 'name']
|
|
|
-
|
|
|
-
|
|
|
-# Cannot import NestedVirtualChassisSerializer due to circular dependency
|
|
|
-class DeviceVirtualChassisSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:virtualchassis-detail')
|
|
|
- master = NestedDeviceSerializer()
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = VirtualChassis
|
|
|
- fields = ['id', 'url', 'master']
|
|
|
+ fields = ['id', 'name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args']
|
|
|
|
|
|
|
|
|
class DeviceSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
|
|
@@ -403,12 +281,12 @@ class DeviceSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
|
|
rack = NestedRackSerializer(required=False, allow_null=True)
|
|
|
face = ChoiceField(choices=RACK_FACE_CHOICES, required=False, allow_null=True)
|
|
|
status = ChoiceField(choices=DEVICE_STATUS_CHOICES, required=False)
|
|
|
- primary_ip = DeviceIPAddressSerializer(read_only=True)
|
|
|
- primary_ip4 = DeviceIPAddressSerializer(required=False, allow_null=True)
|
|
|
- primary_ip6 = DeviceIPAddressSerializer(required=False, allow_null=True)
|
|
|
+ primary_ip = NestedIPAddressSerializer(read_only=True)
|
|
|
+ primary_ip4 = NestedIPAddressSerializer(required=False, allow_null=True)
|
|
|
+ primary_ip6 = NestedIPAddressSerializer(required=False, allow_null=True)
|
|
|
parent_device = serializers.SerializerMethodField()
|
|
|
cluster = NestedClusterSerializer(required=False, allow_null=True)
|
|
|
- virtual_chassis = DeviceVirtualChassisSerializer(required=False, allow_null=True)
|
|
|
+ virtual_chassis = NestedVirtualChassisSerializer(required=False, allow_null=True)
|
|
|
tags = TagListSerializerField(required=False)
|
|
|
|
|
|
class Meta:
|
|
|
@@ -416,8 +294,8 @@ class DeviceSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
|
|
fields = [
|
|
|
'id', 'name', 'display_name', 'device_type', 'device_role', 'tenant', 'platform', 'serial', 'asset_tag',
|
|
|
'site', 'rack', 'position', 'face', 'parent_device', 'status', 'primary_ip', 'primary_ip4', 'primary_ip6',
|
|
|
- 'cluster', 'virtual_chassis', 'vc_position', 'vc_priority', 'comments', 'tags', 'custom_fields', 'created',
|
|
|
- 'last_updated', 'local_context_data',
|
|
|
+ 'cluster', 'virtual_chassis', 'vc_position', 'vc_priority', 'comments', 'local_context_data', 'tags',
|
|
|
+ 'custom_fields', 'created', 'last_updated',
|
|
|
]
|
|
|
validators = []
|
|
|
|
|
|
@@ -430,7 +308,7 @@ class DeviceSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
|
|
validator(data)
|
|
|
|
|
|
# Enforce model validation
|
|
|
- super(DeviceSerializer, self).validate(data)
|
|
|
+ super().validate(data)
|
|
|
|
|
|
return data
|
|
|
|
|
|
@@ -452,203 +330,90 @@ class DeviceWithConfigContextSerializer(DeviceSerializer):
|
|
|
fields = [
|
|
|
'id', 'name', 'display_name', 'device_type', 'device_role', 'tenant', 'platform', 'serial', 'asset_tag',
|
|
|
'site', 'rack', 'position', 'face', 'parent_device', 'status', 'primary_ip', 'primary_ip4', 'primary_ip6',
|
|
|
- 'cluster', 'virtual_chassis', 'vc_position', 'vc_priority', 'comments', 'tags', 'custom_fields',
|
|
|
- 'config_context', 'created', 'last_updated', 'local_context_data',
|
|
|
+ 'cluster', 'virtual_chassis', 'vc_position', 'vc_priority', 'comments', 'local_context_data', 'tags',
|
|
|
+ 'custom_fields', 'config_context', 'created', 'last_updated',
|
|
|
]
|
|
|
|
|
|
def get_config_context(self, obj):
|
|
|
return obj.get_config_context()
|
|
|
|
|
|
|
|
|
-#
|
|
|
-# Console server ports
|
|
|
-#
|
|
|
-
|
|
|
-class ConsoleServerPortSerializer(TaggitSerializer, ValidatedModelSerializer):
|
|
|
+class ConsoleServerPortSerializer(TaggitSerializer, ConnectedEndpointSerializer):
|
|
|
device = NestedDeviceSerializer()
|
|
|
+ cable = NestedCableSerializer(read_only=True)
|
|
|
tags = TagListSerializerField(required=False)
|
|
|
|
|
|
class Meta:
|
|
|
model = ConsoleServerPort
|
|
|
- fields = ['id', 'device', 'name', 'connected_console', 'tags']
|
|
|
- read_only_fields = ['connected_console']
|
|
|
-
|
|
|
-
|
|
|
-class NestedConsoleServerPortSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:consoleserverport-detail')
|
|
|
- device = NestedDeviceSerializer(read_only=True)
|
|
|
- is_connected = serializers.SerializerMethodField(read_only=True)
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = ConsoleServerPort
|
|
|
- fields = ['id', 'url', 'device', 'name', 'is_connected']
|
|
|
-
|
|
|
- def get_is_connected(self, obj):
|
|
|
- return hasattr(obj, 'connected_console') and obj.connected_console is not None
|
|
|
-
|
|
|
+ fields = [
|
|
|
+ 'id', 'device', 'name', 'connected_endpoint_type', 'connected_endpoint', 'connection_status', 'cable',
|
|
|
+ 'tags',
|
|
|
+ ]
|
|
|
|
|
|
-#
|
|
|
-# Console ports
|
|
|
-#
|
|
|
|
|
|
-class ConsolePortSerializer(TaggitSerializer, ValidatedModelSerializer):
|
|
|
+class ConsolePortSerializer(TaggitSerializer, ConnectedEndpointSerializer):
|
|
|
device = NestedDeviceSerializer()
|
|
|
- cs_port = NestedConsoleServerPortSerializer(required=False, allow_null=True)
|
|
|
+ cable = NestedCableSerializer(read_only=True)
|
|
|
tags = TagListSerializerField(required=False)
|
|
|
|
|
|
class Meta:
|
|
|
model = ConsolePort
|
|
|
- fields = ['id', 'device', 'name', 'cs_port', 'connection_status', 'tags']
|
|
|
-
|
|
|
-
|
|
|
-class NestedConsolePortSerializer(TaggitSerializer, ValidatedModelSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:consoleport-detail')
|
|
|
- device = NestedDeviceSerializer(read_only=True)
|
|
|
- is_connected = serializers.SerializerMethodField(read_only=True)
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = ConsolePort
|
|
|
- fields = ['id', 'url', 'device', 'name', 'is_connected']
|
|
|
-
|
|
|
- def get_is_connected(self, obj):
|
|
|
- return obj.cs_port is not None
|
|
|
-
|
|
|
+ fields = [
|
|
|
+ 'id', 'device', 'name', 'connected_endpoint_type', 'connected_endpoint', 'connection_status', 'cable',
|
|
|
+ 'tags',
|
|
|
+ ]
|
|
|
|
|
|
-#
|
|
|
-# Power outlets
|
|
|
-#
|
|
|
|
|
|
-class PowerOutletSerializer(TaggitSerializer, ValidatedModelSerializer):
|
|
|
+class PowerOutletSerializer(TaggitSerializer, ConnectedEndpointSerializer):
|
|
|
device = NestedDeviceSerializer()
|
|
|
+ cable = NestedCableSerializer(read_only=True)
|
|
|
tags = TagListSerializerField(required=False)
|
|
|
|
|
|
class Meta:
|
|
|
model = PowerOutlet
|
|
|
- fields = ['id', 'device', 'name', 'connected_port', 'tags']
|
|
|
- read_only_fields = ['connected_port']
|
|
|
-
|
|
|
-
|
|
|
-class NestedPowerOutletSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:poweroutlet-detail')
|
|
|
- device = NestedDeviceSerializer(read_only=True)
|
|
|
- is_connected = serializers.SerializerMethodField(read_only=True)
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = PowerOutlet
|
|
|
- fields = ['id', 'url', 'device', 'name', 'is_connected']
|
|
|
-
|
|
|
- def get_is_connected(self, obj):
|
|
|
- return hasattr(obj, 'connected_port') and obj.connected_port is not None
|
|
|
-
|
|
|
+ fields = [
|
|
|
+ 'id', 'device', 'name', 'connected_endpoint_type', 'connected_endpoint', 'connection_status', 'cable',
|
|
|
+ 'tags',
|
|
|
+ ]
|
|
|
|
|
|
-#
|
|
|
-# Power ports
|
|
|
-#
|
|
|
|
|
|
-class PowerPortSerializer(TaggitSerializer, ValidatedModelSerializer):
|
|
|
+class PowerPortSerializer(TaggitSerializer, ConnectedEndpointSerializer):
|
|
|
device = NestedDeviceSerializer()
|
|
|
- power_outlet = NestedPowerOutletSerializer(required=False, allow_null=True)
|
|
|
+ cable = NestedCableSerializer(read_only=True)
|
|
|
tags = TagListSerializerField(required=False)
|
|
|
|
|
|
class Meta:
|
|
|
model = PowerPort
|
|
|
- fields = ['id', 'device', 'name', 'power_outlet', 'connection_status', 'tags']
|
|
|
-
|
|
|
-
|
|
|
-class NestedPowerPortSerializer(TaggitSerializer, ValidatedModelSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:powerport-detail')
|
|
|
- device = NestedDeviceSerializer(read_only=True)
|
|
|
- is_connected = serializers.SerializerMethodField(read_only=True)
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = PowerPort
|
|
|
- fields = ['id', 'url', 'device', 'name', 'is_connected']
|
|
|
-
|
|
|
- def get_is_connected(self, obj):
|
|
|
- return obj.power_outlet is not None
|
|
|
-
|
|
|
-
|
|
|
-#
|
|
|
-# Interfaces
|
|
|
-#
|
|
|
-
|
|
|
-class IsConnectedMixin(object):
|
|
|
- """
|
|
|
- Provide a method for setting is_connected on Interface serializers.
|
|
|
- """
|
|
|
- def get_is_connected(self, obj):
|
|
|
- """
|
|
|
- Return True if the interface has a connected interface or circuit.
|
|
|
- """
|
|
|
- if obj.connection:
|
|
|
- return True
|
|
|
- if hasattr(obj, 'circuit_termination') and obj.circuit_termination is not None:
|
|
|
- return True
|
|
|
- return False
|
|
|
-
|
|
|
-
|
|
|
-class NestedInterfaceSerializer(IsConnectedMixin, WritableNestedSerializer):
|
|
|
- device = NestedDeviceSerializer(read_only=True)
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:interface-detail')
|
|
|
- is_connected = serializers.SerializerMethodField(read_only=True)
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = Interface
|
|
|
- fields = ['id', 'url', 'device', 'name', 'is_connected']
|
|
|
-
|
|
|
-
|
|
|
-class InterfaceNestedCircuitSerializer(serializers.ModelSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='circuits-api:circuit-detail')
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = Circuit
|
|
|
- fields = ['id', 'url', 'cid']
|
|
|
-
|
|
|
-
|
|
|
-class InterfaceCircuitTerminationSerializer(WritableNestedSerializer):
|
|
|
- circuit = InterfaceNestedCircuitSerializer(read_only=True)
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = CircuitTermination
|
|
|
fields = [
|
|
|
- 'id', 'circuit', 'term_side', 'port_speed', 'upstream_speed', 'xconnect_id', 'pp_info',
|
|
|
+ 'id', 'device', 'name', 'connected_endpoint_type', 'connected_endpoint', 'connection_status', 'cable',
|
|
|
+ 'tags',
|
|
|
]
|
|
|
|
|
|
|
|
|
-# Cannot import ipam.api.NestedVLANSerializer due to circular dependency
|
|
|
-class InterfaceVLANSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='ipam-api:vlan-detail')
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = VLAN
|
|
|
- fields = ['id', 'url', 'vid', 'name', 'display_name']
|
|
|
-
|
|
|
-
|
|
|
-class InterfaceSerializer(TaggitSerializer, IsConnectedMixin, ValidatedModelSerializer):
|
|
|
+class InterfaceSerializer(TaggitSerializer, ConnectedEndpointSerializer):
|
|
|
device = NestedDeviceSerializer()
|
|
|
form_factor = ChoiceField(choices=IFACE_FF_CHOICES, required=False)
|
|
|
lag = NestedInterfaceSerializer(required=False, allow_null=True)
|
|
|
- is_connected = serializers.SerializerMethodField(read_only=True)
|
|
|
- interface_connection = serializers.SerializerMethodField(read_only=True)
|
|
|
- circuit_termination = InterfaceCircuitTerminationSerializer(read_only=True)
|
|
|
mode = ChoiceField(choices=IFACE_MODE_CHOICES, required=False, allow_null=True)
|
|
|
- untagged_vlan = InterfaceVLANSerializer(required=False, allow_null=True)
|
|
|
+ untagged_vlan = NestedVLANSerializer(required=False, allow_null=True)
|
|
|
tagged_vlans = SerializedPKRelatedField(
|
|
|
queryset=VLAN.objects.all(),
|
|
|
- serializer=InterfaceVLANSerializer,
|
|
|
+ serializer=NestedVLANSerializer,
|
|
|
required=False,
|
|
|
many=True
|
|
|
)
|
|
|
+ cable = NestedCableSerializer(read_only=True)
|
|
|
tags = TagListSerializerField(required=False)
|
|
|
|
|
|
class Meta:
|
|
|
model = Interface
|
|
|
fields = [
|
|
|
'id', 'device', 'name', 'form_factor', 'enabled', 'lag', 'mtu', 'mac_address', 'mgmt_only', 'description',
|
|
|
- 'is_connected', 'interface_connection', 'circuit_termination', 'mode', 'untagged_vlan', 'tagged_vlans',
|
|
|
- 'tags',
|
|
|
+ 'connected_endpoint_type', 'connected_endpoint', 'connection_status', 'cable', 'mode', 'untagged_vlan',
|
|
|
+ 'tagged_vlans', 'tags', 'count_ipaddresses',
|
|
|
]
|
|
|
|
|
|
+ # TODO: This validation should be handled by Interface.clean()
|
|
|
def validate(self, data):
|
|
|
|
|
|
# All associated VLANs be global or assigned to the parent device's site.
|
|
|
@@ -666,39 +431,51 @@ class InterfaceSerializer(TaggitSerializer, IsConnectedMixin, ValidatedModelSeri
|
|
|
"be global.".format(vlan)
|
|
|
})
|
|
|
|
|
|
- return super(InterfaceSerializer, self).validate(data)
|
|
|
+ return super().validate(data)
|
|
|
|
|
|
- def get_interface_connection(self, obj):
|
|
|
- if obj.connection:
|
|
|
- context = {
|
|
|
- 'request': self.context['request'],
|
|
|
- 'interface': obj.connected_interface,
|
|
|
- }
|
|
|
- return ContextualInterfaceConnectionSerializer(obj.connection, context=context).data
|
|
|
- return None
|
|
|
|
|
|
+class RearPortSerializer(ValidatedModelSerializer):
|
|
|
+ device = NestedDeviceSerializer()
|
|
|
+ type = ChoiceField(choices=PORT_TYPE_CHOICES)
|
|
|
+ cable = NestedCableSerializer(read_only=True)
|
|
|
+ tags = TagListSerializerField(required=False)
|
|
|
|
|
|
-#
|
|
|
-# Device bays
|
|
|
-#
|
|
|
+ class Meta:
|
|
|
+ model = RearPort
|
|
|
+ fields = ['id', 'device', 'name', 'type', 'positions', 'description', 'cable', 'tags']
|
|
|
|
|
|
-class DeviceBaySerializer(TaggitSerializer, ValidatedModelSerializer):
|
|
|
+
|
|
|
+class FrontPortRearPortSerializer(WritableNestedSerializer):
|
|
|
+ """
|
|
|
+ NestedRearPortSerializer but with parent device omitted (since front and rear ports must belong to same device)
|
|
|
+ """
|
|
|
+ url = serializers.HyperlinkedIdentityField(view_name='dcim-api:rearport-detail')
|
|
|
+
|
|
|
+ class Meta:
|
|
|
+ model = RearPort
|
|
|
+ fields = ['id', 'url', 'name']
|
|
|
+
|
|
|
+
|
|
|
+class FrontPortSerializer(ValidatedModelSerializer):
|
|
|
device = NestedDeviceSerializer()
|
|
|
- installed_device = NestedDeviceSerializer(required=False, allow_null=True)
|
|
|
+ type = ChoiceField(choices=PORT_TYPE_CHOICES)
|
|
|
+ rear_port = FrontPortRearPortSerializer()
|
|
|
+ cable = NestedCableSerializer(read_only=True)
|
|
|
tags = TagListSerializerField(required=False)
|
|
|
|
|
|
class Meta:
|
|
|
- model = DeviceBay
|
|
|
- fields = ['id', 'device', 'name', 'installed_device', 'tags']
|
|
|
+ model = FrontPort
|
|
|
+ fields = ['id', 'device', 'name', 'type', 'rear_port', 'rear_port_position', 'description', 'cable', 'tags']
|
|
|
|
|
|
|
|
|
-class NestedDeviceBaySerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:devicebay-detail')
|
|
|
- device = NestedDeviceSerializer(read_only=True)
|
|
|
+class DeviceBaySerializer(TaggitSerializer, ValidatedModelSerializer):
|
|
|
+ device = NestedDeviceSerializer()
|
|
|
+ installed_device = NestedDeviceSerializer(required=False, allow_null=True)
|
|
|
+ tags = TagListSerializerField(required=False)
|
|
|
|
|
|
class Meta:
|
|
|
model = DeviceBay
|
|
|
- fields = ['id', 'url', 'device', 'name']
|
|
|
+ fields = ['id', 'device', 'name', 'installed_device', 'tags']
|
|
|
|
|
|
|
|
|
#
|
|
|
@@ -721,40 +498,75 @@ class InventoryItemSerializer(TaggitSerializer, ValidatedModelSerializer):
|
|
|
|
|
|
|
|
|
#
|
|
|
-# Interface connections
|
|
|
+# Cables
|
|
|
#
|
|
|
|
|
|
-class InterfaceConnectionSerializer(ValidatedModelSerializer):
|
|
|
- interface_a = NestedInterfaceSerializer()
|
|
|
- interface_b = NestedInterfaceSerializer()
|
|
|
- connection_status = ChoiceField(choices=CONNECTION_STATUS_CHOICES, required=False)
|
|
|
+class CableSerializer(ValidatedModelSerializer):
|
|
|
+ termination_a_type = ContentTypeField()
|
|
|
+ termination_b_type = ContentTypeField()
|
|
|
+ termination_a = serializers.SerializerMethodField(read_only=True)
|
|
|
+ termination_b = serializers.SerializerMethodField(read_only=True)
|
|
|
+ status = ChoiceField(choices=CONNECTION_STATUS_CHOICES, required=False)
|
|
|
+ length_unit = ChoiceField(choices=CABLE_LENGTH_UNIT_CHOICES, required=False)
|
|
|
|
|
|
class Meta:
|
|
|
- model = InterfaceConnection
|
|
|
- fields = ['id', 'interface_a', 'interface_b', 'connection_status']
|
|
|
+ model = Cable
|
|
|
+ fields = [
|
|
|
+ 'id', 'termination_a_type', 'termination_a_id', 'termination_a', 'termination_b_type', 'termination_b_id',
|
|
|
+ 'termination_b', 'type', 'status', 'label', 'color', 'length', 'length_unit',
|
|
|
+ ]
|
|
|
|
|
|
+ def _get_termination(self, obj, side):
|
|
|
+ """
|
|
|
+ Serialize a nested representation of a termination.
|
|
|
+ """
|
|
|
+ if side.lower() not in ['a', 'b']:
|
|
|
+ raise ValueError("Termination side must be either A or B.")
|
|
|
+ termination = getattr(obj, 'termination_{}'.format(side.lower()))
|
|
|
+ if termination is None:
|
|
|
+ return None
|
|
|
+ serializer = get_serializer_for_model(termination, prefix='Nested')
|
|
|
+ context = {'request': self.context['request']}
|
|
|
+ data = serializer(termination, context=context).data
|
|
|
+
|
|
|
+ return data
|
|
|
|
|
|
-class NestedInterfaceConnectionSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:interfaceconnection-detail')
|
|
|
+ def get_termination_a(self, obj):
|
|
|
+ return self._get_termination(obj, 'a')
|
|
|
|
|
|
- class Meta:
|
|
|
- model = InterfaceConnection
|
|
|
- fields = ['id', 'url', 'connection_status']
|
|
|
+ def get_termination_b(self, obj):
|
|
|
+ return self._get_termination(obj, 'b')
|
|
|
|
|
|
|
|
|
-class ContextualInterfaceConnectionSerializer(serializers.ModelSerializer):
|
|
|
+class TracedCableSerializer(serializers.ModelSerializer):
|
|
|
"""
|
|
|
- A read-only representation of an InterfaceConnection from the perspective of either of its two connected Interfaces.
|
|
|
+ Used only while tracing a cable path.
|
|
|
"""
|
|
|
- interface = serializers.SerializerMethodField(read_only=True)
|
|
|
- connection_status = ChoiceField(choices=CONNECTION_STATUS_CHOICES, read_only=True)
|
|
|
+ url = serializers.HyperlinkedIdentityField(view_name='dcim-api:cable-detail')
|
|
|
|
|
|
class Meta:
|
|
|
- model = InterfaceConnection
|
|
|
- fields = ['id', 'interface', 'connection_status']
|
|
|
+ model = Cable
|
|
|
+ fields = [
|
|
|
+ 'id', 'url', 'type', 'status', 'label', 'color', 'length', 'length_unit',
|
|
|
+ ]
|
|
|
|
|
|
- def get_interface(self, obj):
|
|
|
- return NestedInterfaceSerializer(self.context['interface'], context=self.context).data
|
|
|
+
|
|
|
+#
|
|
|
+# Interface connections
|
|
|
+#
|
|
|
+
|
|
|
+class InterfaceConnectionSerializer(ValidatedModelSerializer):
|
|
|
+ interface_a = serializers.SerializerMethodField()
|
|
|
+ interface_b = NestedInterfaceSerializer(source='connected_endpoint')
|
|
|
+ connection_status = ChoiceField(choices=CONNECTION_STATUS_CHOICES, required=False)
|
|
|
+
|
|
|
+ class Meta:
|
|
|
+ model = Interface
|
|
|
+ fields = ['interface_a', 'interface_b', 'connection_status']
|
|
|
+
|
|
|
+ def get_interface_a(self, obj):
|
|
|
+ context = {'request': self.context['request']}
|
|
|
+ return NestedInterfaceSerializer(instance=obj, context=context).data
|
|
|
|
|
|
|
|
|
#
|
|
|
@@ -768,11 +580,3 @@ class VirtualChassisSerializer(TaggitSerializer, ValidatedModelSerializer):
|
|
|
class Meta:
|
|
|
model = VirtualChassis
|
|
|
fields = ['id', 'master', 'domain', 'tags']
|
|
|
-
|
|
|
-
|
|
|
-class NestedVirtualChassisSerializer(WritableNestedSerializer):
|
|
|
- url = serializers.HyperlinkedIdentityField(view_name='dcim-api:virtualchassis-detail')
|
|
|
-
|
|
|
- class Meta:
|
|
|
- model = VirtualChassis
|
|
|
- fields = ['id', 'url']
|