diff --git a/CHANGELOG.md b/CHANGELOG.md index b50a68cf20f..a43232716ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ NetBox now supports modeling physical cables for console, power, and interface c * [#2632](https://github.com/digitalocean/netbox/issues/2632) - Change representation of null values from `0` to `null` * [#2639](https://github.com/digitalocean/netbox/issues/2639) - Fix preservation of length/dimensions unit for racks and cables * [#2648](https://github.com/digitalocean/netbox/issues/2648) - Include the `connection_status` field in nested represenations of connectable device components +* [#2649](https://github.com/digitalocean/netbox/issues/2649) - Add `connected_endpoint_type` to connectable device component API representations ## API Changes diff --git a/netbox/circuits/api/serializers.py b/netbox/circuits/api/serializers.py index d32e298de80..e94875c21df 100644 --- a/netbox/circuits/api/serializers.py +++ b/netbox/circuits/api/serializers.py @@ -60,5 +60,5 @@ class Meta: model = CircuitTermination fields = [ 'id', 'circuit', 'term_side', 'site', 'port_speed', 'upstream_speed', 'xconnect_id', 'pp_info', - 'description', 'connected_endpoint', 'connection_status', 'cable', + 'description', 'connected_endpoint_type', 'connected_endpoint', 'connection_status', 'cable', ] diff --git a/netbox/dcim/api/serializers.py b/netbox/dcim/api/serializers.py index 29e0b2151a1..5ed336c95b4 100644 --- a/netbox/dcim/api/serializers.py +++ b/netbox/dcim/api/serializers.py @@ -23,9 +23,18 @@ 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) + def get_connected_endpoint_type(self, obj): + if obj.connected_endpoint is None: + return None + return '{}.{}'.format( + obj.connected_endpoint._meta.app_label, + obj.connected_endpoint._meta.model_name + ) + def get_connected_endpoint(self, obj): """ Return the appropriate serializer for the type of connected object. @@ -331,7 +340,10 @@ class ConsoleServerPortSerializer(TaggitSerializer, ConnectedEndpointSerializer) class Meta: model = ConsoleServerPort - fields = ['id', 'device', 'name', 'connected_endpoint', 'connection_status', 'cable', 'tags'] + fields = [ + 'id', 'device', 'name', 'connected_endpoint_type', 'connected_endpoint', 'connection_status', 'cable', + 'tags', + ] class ConsolePortSerializer(TaggitSerializer, ConnectedEndpointSerializer): @@ -341,7 +353,10 @@ class ConsolePortSerializer(TaggitSerializer, ConnectedEndpointSerializer): class Meta: model = ConsolePort - fields = ['id', 'device', 'name', 'connected_endpoint', 'connection_status', 'cable', 'tags'] + fields = [ + 'id', 'device', 'name', 'connected_endpoint_type', 'connected_endpoint', 'connection_status', 'cable', + 'tags', + ] class PowerOutletSerializer(TaggitSerializer, ConnectedEndpointSerializer): @@ -351,7 +366,10 @@ class PowerOutletSerializer(TaggitSerializer, ConnectedEndpointSerializer): class Meta: model = PowerOutlet - fields = ['id', 'device', 'name', 'connected_endpoint', 'connection_status', 'cable', 'tags'] + fields = [ + 'id', 'device', 'name', 'connected_endpoint_type', 'connected_endpoint', 'connection_status', 'cable', + 'tags', + ] class PowerPortSerializer(TaggitSerializer, ConnectedEndpointSerializer): @@ -361,7 +379,10 @@ class PowerPortSerializer(TaggitSerializer, ConnectedEndpointSerializer): class Meta: model = PowerPort - fields = ['id', 'device', 'name', 'connected_endpoint', 'connection_status', 'cable', 'tags'] + fields = [ + 'id', 'device', 'name', 'connected_endpoint_type', 'connected_endpoint', 'connection_status', 'cable', + 'tags', + ] class InterfaceSerializer(TaggitSerializer, ConnectedEndpointSerializer): @@ -383,8 +404,8 @@ class Meta: model = Interface fields = [ 'id', 'device', 'name', 'form_factor', 'enabled', 'lag', 'mtu', 'mac_address', 'mgmt_only', 'description', - 'connected_endpoint', 'connection_status', 'cable', 'mode', 'untagged_vlan', 'tagged_vlans', 'tags', - 'count_ipaddresses', + '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()