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

This commit is contained in:
Jeremy Stretch 2018-12-06 16:14:03 -05:00
parent 360303f86c
commit 45a1dfbd8a
3 changed files with 29 additions and 7 deletions

View File

@ -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` * [#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 * [#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 * [#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 ## API Changes

View File

@ -60,5 +60,5 @@ class CircuitTerminationSerializer(ConnectedEndpointSerializer):
model = CircuitTermination model = CircuitTermination
fields = [ fields = [
'id', 'circuit', 'term_side', 'site', 'port_speed', 'upstream_speed', 'xconnect_id', 'pp_info', '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',
] ]

View File

@ -23,9 +23,18 @@ from .nested_serializers import *
class ConnectedEndpointSerializer(ValidatedModelSerializer): class ConnectedEndpointSerializer(ValidatedModelSerializer):
connected_endpoint_type = serializers.SerializerMethodField(read_only=True)
connected_endpoint = serializers.SerializerMethodField(read_only=True) connected_endpoint = serializers.SerializerMethodField(read_only=True)
connection_status = ChoiceField(choices=CONNECTION_STATUS_CHOICES, 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): def get_connected_endpoint(self, obj):
""" """
Return the appropriate serializer for the type of connected object. Return the appropriate serializer for the type of connected object.
@ -331,7 +340,10 @@ class ConsoleServerPortSerializer(TaggitSerializer, ConnectedEndpointSerializer)
class Meta: class Meta:
model = ConsoleServerPort 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): class ConsolePortSerializer(TaggitSerializer, ConnectedEndpointSerializer):
@ -341,7 +353,10 @@ class ConsolePortSerializer(TaggitSerializer, ConnectedEndpointSerializer):
class Meta: class Meta:
model = ConsolePort 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): class PowerOutletSerializer(TaggitSerializer, ConnectedEndpointSerializer):
@ -351,7 +366,10 @@ class PowerOutletSerializer(TaggitSerializer, ConnectedEndpointSerializer):
class Meta: class Meta:
model = PowerOutlet 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): class PowerPortSerializer(TaggitSerializer, ConnectedEndpointSerializer):
@ -361,7 +379,10 @@ class PowerPortSerializer(TaggitSerializer, ConnectedEndpointSerializer):
class Meta: class Meta:
model = PowerPort 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): class InterfaceSerializer(TaggitSerializer, ConnectedEndpointSerializer):
@ -383,8 +404,8 @@ class InterfaceSerializer(TaggitSerializer, ConnectedEndpointSerializer):
model = Interface model = Interface
fields = [ fields = [
'id', 'device', 'name', 'form_factor', 'enabled', 'lag', 'mtu', 'mac_address', 'mgmt_only', 'description', 'id', 'device', 'name', 'form_factor', 'enabled', 'lag', 'mtu', 'mac_address', 'mgmt_only', 'description',
'connected_endpoint', 'connection_status', 'cable', 'mode', 'untagged_vlan', 'tagged_vlans', 'tags', 'connected_endpoint_type', 'connected_endpoint', 'connection_status', 'cable', 'mode', 'untagged_vlan',
'count_ipaddresses', 'tagged_vlans', 'tags', 'count_ipaddresses',
] ]
# TODO: This validation should be handled by Interface.clean() # TODO: This validation should be handled by Interface.clean()