Просмотр исходного кода

Closes #2649: Add connected_endpoint_type to connectable device component API representations

Jeremy Stretch 7 лет назад
Родитель
Сommit
45a1dfbd8a
3 измененных файлов с 29 добавлено и 7 удалено
  1. 1 0
      CHANGELOG.md
  2. 1 1
      netbox/circuits/api/serializers.py
  3. 27 6
      netbox/dcim/api/serializers.py

+ 1 - 0
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
 

+ 1 - 1
netbox/circuits/api/serializers.py

@@ -60,5 +60,5 @@ class CircuitTerminationSerializer(ConnectedEndpointSerializer):
         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',
         ]

+ 27 - 6
netbox/dcim/api/serializers.py

@@ -23,9 +23,18 @@ from .nested_serializers import *
 
 
 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 InterfaceSerializer(TaggitSerializer, ConnectedEndpointSerializer):
         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()