def api_interfaces(self, request, vlan_id): """ Returns a dict of {untagged: ..., tagged: ...., l3: ...} :param request: :param vlan_id: :return: """ vlan = self.get_object_or_404(VLAN, id=vlan_id) # Managed objects in VC domain objects = NetworkSegment.get_vlan_domain_object_ids(vlan.segment) # Find untagged interfaces si_objects = defaultdict(list) for si in SubInterface.objects.filter(managed_object__in=objects, untagged_vlan=vlan.vlan, enabled_afi="BRIDGE"): si_objects[si.managed_object] += [{"name": si.name}] untagged = [{ "managed_object_id": o.id, "managed_object_name": o.name, "interfaces": sorted(si_objects[o], key=lambda x: x["name"]), } for o in si_objects] # Find tagged interfaces si_objects = defaultdict(list) for si in SubInterface.objects.filter(managed_object__in=objects, tagged_vlans=vlan.vlan, enabled_afi="BRIDGE"): si_objects[si.managed_object] += [{"name": si.name}] tagged = [{ "managed_object_id": o.id, "managed_object_name": o.name, "interfaces": sorted(si_objects[o], key=lambda x: x["name"]), } for o in si_objects] # Find l3 interfaces si_objects = defaultdict(list) for si in SubInterface.objects.filter(managed_object__in=objects, vlan_ids=vlan.vlan): si_objects[si.managed_object] += [{ "name": si.name, "ipv4_addresses": si.ipv4_addresses, "ipv6_addresses": si.ipv6_addresses, }] l3 = [{ "managed_object_id": o.id, "managed_object_name": o.name, "interfaces": sorted(si_objects[o], key=lambda x: x["name"]), } for o in si_objects] # return { "untagged": sorted(untagged, key=lambda x: x["managed_object_name"]), "tagged": sorted(tagged, key=lambda x: x["managed_object_name"]), "l3": sorted(l3, key=lambda x: x["managed_object_name"]), }
def get_interfaces(self): # Managed objects in VC domain objects = NetworkSegment.get_vlan_domain_object_ids( self.object.segment) # Find untagged interfaces si_objects = defaultdict(list) for si in SubInterface.objects.filter(managed_object__in=objects, untagged_vlan=self.object.vlan, enabled_afi="BRIDGE"): si_objects[si.managed_object] += [{"name": si.name}] untagged = [{ "managed_object": o, "interfaces": sorted(si_objects[o], key=lambda x: x["name"]) } for o in si_objects] # Find tagged interfaces si_objects = defaultdict(list) for si in SubInterface.objects.filter(managed_object__in=objects, tagged_vlans=self.object.vlan, enabled_afi="BRIDGE"): si_objects[si.managed_object] += [{"name": si.name}] tagged = [{ "managed_object": o, "interfaces": sorted(si_objects[o], key=lambda x: x["name"]) } for o in si_objects] # Find l3 interfaces si_objects = defaultdict(list) for si in SubInterface.objects.filter(managed_object__in=objects, vlan_ids=self.object.vlan): si_objects[si.managed_object] += [{ "name": si.name, "ipv4_addresses": si.ipv4_addresses, "ipv6_addresses": si.ipv6_addresses, }] l3 = [{ "managed_object": o, "interfaces": sorted(si_objects[o], key=lambda x: x["name"]) } for o in si_objects] # return { "has_interfaces": bool(len(untagged) + len(tagged) + len(l3)), "untagged": sorted(untagged, key=lambda x: x["managed_object"].name), "tagged": sorted(tagged, key=lambda x: x["managed_object"].name), "l3": sorted(l3, key=lambda x: x["managed_object"].name), }