Skip to content

Commit

Permalink
Fixes netbox-community#359: Use standard serializers for related objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Jul 21, 2016
1 parent 3259d94 commit e7d3980
Showing 1 changed file with 22 additions and 39 deletions.
61 changes: 22 additions & 39 deletions netbox/dcim/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,53 +419,36 @@ def get(self, request):
return Response()

else:
raise MissingFilterException(detail='Must specify search parameters (peer-device and peer-interface).')
raise MissingFilterException(detail='Must specify search parameters "peer-device" and "peer-interface".')

# Initialize response skeleton
response = dict()
response['device'] = serializers.DeviceSerializer(device).data
response['console-ports'] = []
response['power-ports'] = []
response['interfaces'] = []

# Build console connections
response = {
'device': serializers.DeviceSerializer(device).data,
'console-ports': [],
'power-ports': [],
'interfaces': [],
}

# Console connections
console_ports = ConsolePort.objects.filter(device=device).select_related('cs_port__device')
for cp in console_ports:
cp_info = dict()
cp_info['name'] = cp.name
if cp.cs_port:
cp_info['console-server'] = cp.cs_port.device.name
cp_info['port'] = cp.cs_port.name
else:
cp_info['console-server'] = None
cp_info['port'] = None
response['console-ports'].append(cp_info)
data = serializers.ConsolePortSerializer(instance=cp).data
del(data['device'])
response['console-ports'].append(data)

# Build power connections
# Power connections
power_ports = PowerPort.objects.filter(device=device).select_related('power_outlet__device')
for pp in power_ports:
pp_info = dict()
pp_info['name'] = pp.name
if pp.power_outlet:
pp_info['pdu'] = pp.power_outlet.device.name
pp_info['outlet'] = pp.power_outlet.name
else:
pp_info['pdu'] = None
pp_info['outlet'] = None
response['power-ports'].append(pp_info)
data = serializers.PowerPortSerializer(instance=pp).data
del(data['device'])
response['power-ports'].append(data)

# Built interface connections
interfaces = Interface.objects.filter(device=device)
# Interface connections
interfaces = Interface.objects.filter(device=device).select_related('connected_as_a', 'connected_as_b',
'circuit')
for iface in interfaces:
iface_info = dict()
iface_info['name'] = iface.name
peer_interface = iface.get_connected_interface()
if peer_interface:
iface_info['device'] = peer_interface.device.name
iface_info['interface'] = peer_interface.name
else:
iface_info['device'] = None
iface_info['interface'] = None
response['interfaces'].append(iface_info)
data = serializers.InterfaceDetailSerializer(instance=iface).data
del(data['device'])
response['interfaces'].append(data)

return Response(response)

0 comments on commit e7d3980

Please sign in to comment.