class ManufacturerViewSet(ModelViewSet): queryset = Manufacturer.objects.annotate( devicetype_count=get_subquery(DeviceType, 'manufacturer'), inventoryitem_count=get_subquery(InventoryItem, 'manufacturer'), platform_count=get_subquery(Platform, 'manufacturer')) serializer_class = serializers.ManufacturerSerializer filterset_class = filters.ManufacturerFilterSet
class RackViewSet(CustomFieldModelViewSet): queryset = Rack.objects.prefetch_related( 'site', 'group__site', 'role', 'tenant', 'tags').annotate(device_count=get_subquery(Device, 'rack'), powerfeed_count=get_subquery(PowerFeed, 'rack')) serializer_class = serializers.RackSerializer filterset_class = filters.RackFilter @action(detail=True) def units(self, request, pk=None): """ List rack units (by rack) """ rack = get_object_or_404(Rack, pk=pk) face = request.GET.get('face', 0) exclude_pk = request.GET.get('exclude', None) if exclude_pk is not None: try: exclude_pk = int(exclude_pk) except ValueError: exclude_pk = None elevation = rack.get_rack_units(face, exclude_pk) # Enable filtering rack units by ID q = request.GET.get('q', None) if q: elevation = [u for u in elevation if q in str(u['id'])] page = self.paginate_queryset(elevation) if page is not None: rack_units = serializers.RackUnitSerializer( page, many=True, context={'request': request}) return self.get_paginated_response(rack_units.data)
class VRFViewSet(CustomFieldModelViewSet): queryset = VRF.objects.prefetch_related('tenant').prefetch_related('tags').annotate( ipaddress_count=get_subquery(IPAddress, 'vrf'), prefix_count=get_subquery(Prefix, 'vrf') ) serializer_class = serializers.VRFSerializer filterset_class = filters.VRFFilterSet
class RoleViewSet(ModelViewSet): queryset = Role.objects.annotate( prefix_count=get_subquery(Prefix, 'role'), vlan_count=get_subquery(VLAN, 'role') ) serializer_class = serializers.RoleSerializer filterset_class = filters.RoleFilterSet
class ClusterViewSet(CustomFieldModelViewSet): queryset = Cluster.objects.prefetch_related( 'type', 'group', 'tenant', 'site', 'tags').annotate( device_count=get_subquery(Device, 'cluster'), virtualmachine_count=get_subquery(VirtualMachine, 'cluster')) serializer_class = serializers.ClusterSerializer filterset_class = filters.ClusterFilterSet
class PlatformViewSet(ModelViewSet): queryset = Platform.objects.annotate( device_count=get_subquery(Device, 'platform'), virtualmachine_count=get_subquery(VirtualMachine, 'platform') ) serializer_class = serializers.PlatformSerializer filterset_class = filters.PlatformFilterSet
class DeviceRoleViewSet(ModelViewSet): queryset = DeviceRole.objects.annotate( device_count=get_subquery(Device, 'device_role'), virtualmachine_count=get_subquery(VirtualMachine, 'role') ) serializer_class = serializers.DeviceRoleSerializer filterset_class = filters.DeviceRoleFilterSet
class RackViewSet(CustomFieldModelViewSet): queryset = Rack.objects.prefetch_related( 'site', 'group__site', 'role', 'tenant', 'tags').annotate(device_count=get_subquery(Device, 'rack'), powerfeed_count=get_subquery( PowerFeed, 'rack')).order_by(*Rack._meta.ordering) serializer_class = serializers.RackSerializer filterset_class = filters.RackFilterSet @swagger_auto_schema( responses={200: serializers.RackUnitSerializer(many=True)}, query_serializer=serializers.RackElevationDetailFilterSerializer) @action(detail=True) def elevation(self, request, pk=None): """ Rack elevation representing the list of rack units. Also supports rendering the elevation as an SVG. """ rack = get_object_or_404(self.queryset, pk=pk) serializer = serializers.RackElevationDetailFilterSerializer( data=request.GET) if not serializer.is_valid(): return Response(serializer.errors, 400) data = serializer.validated_data if data['render'] == 'svg': # Render and return the elevation as an SVG drawing with the correct content type drawing = rack.get_elevation_svg( face=data['face'], user=request.user, unit_width=data['unit_width'], unit_height=data['unit_height'], legend_width=data['legend_width'], include_images=data['include_images'], base_url=request.build_absolute_uri('/')) return HttpResponse(drawing.tostring(), content_type='image/svg+xml') else: # Return a JSON representation of the rack units in the elevation elevation = rack.get_rack_units( face=data['face'], user=request.user, exclude=data['exclude'], expand_devices=data['expand_devices']) # Enable filtering rack units by ID q = data['q'] if q: elevation = [ u for u in elevation if q in str(u['id']) or q in str(u['name']) ] page = self.paginate_queryset(elevation) if page is not None: rack_units = serializers.RackUnitSerializer( page, many=True, context={'request': request}) return self.get_paginated_response(rack_units.data)
class ClusterListView(generic.ObjectListView): permission_required = 'virtualization.view_cluster' queryset = Cluster.objects.annotate( device_count=get_subquery(Device, 'cluster'), vm_count=get_subquery(VirtualMachine, 'cluster')) table = tables.ClusterTable filterset = filters.ClusterFilterSet filterset_form = forms.ClusterFilterForm
class ClusterListView(ObjectListView): permission_required = 'virtualization.view_cluster' queryset = Cluster.objects.prefetch_related( 'type', 'group', 'site', 'tenant').annotate(device_count=get_subquery(Device, 'cluster'), vm_count=get_subquery(VirtualMachine, 'cluster')) table = tables.ClusterTable filterset = filters.ClusterFilterSet filterset_form = forms.ClusterFilterForm
class ProviderBulkEditView(BulkEditView): queryset = Provider.objects.annotate( count_circuits=get_subquery(Circuit, 'provider') ) filterset = filters.ProviderFilterSet table = tables.ProviderTable form = forms.ProviderBulkEditForm
class ProviderListView(ObjectListView): queryset = Provider.objects.annotate( count_circuits=get_subquery(Circuit, 'provider') ) filterset = filters.ProviderFilterSet filterset_form = forms.ProviderFilterForm table = tables.ProviderTable
class RIRListView(generic.ObjectListView): queryset = RIR.objects.annotate( aggregate_count=get_subquery(Aggregate, 'rir')) filterset = filters.RIRFilterSet filterset_form = forms.RIRFilterForm table = tables.RIRTable template_name = 'ipam/rir_list.html'
class VLANViewSet(CustomFieldModelViewSet): queryset = VLAN.objects.prefetch_related( 'site', 'group', 'tenant', 'role', 'tags').annotate(prefix_count=get_subquery(Prefix, 'vlan')).order_by( *VLAN._meta.ordering) serializer_class = serializers.VLANSerializer filterset_class = filters.VLANFilterSet
class TenantViewSet(CustomFieldModelViewSet): queryset = Tenant.objects.prefetch_related('group', 'tags').annotate( circuit_count=get_subquery(Circuit, 'tenant'), device_count=get_subquery(Device, 'tenant'), ipaddress_count=get_subquery(IPAddress, 'tenant'), prefix_count=get_subquery(Prefix, 'tenant'), rack_count=get_subquery(Rack, 'tenant'), site_count=get_subquery(Site, 'tenant'), virtualmachine_count=get_subquery(VirtualMachine, 'tenant'), vlan_count=get_subquery(VLAN, 'tenant'), vrf_count=get_subquery(VRF, 'tenant')) serializer_class = serializers.TenantSerializer filterset_class = filters.TenantFilterSet
class SiteViewSet(CustomFieldModelViewSet): queryset = Site.objects.select_related( 'region', 'tenant' ).prefetch_related( 'tags' ).annotate( device_count=get_subquery(Device, 'site'), rack_count=get_subquery(Rack, 'site'), prefix_count=get_subquery(Prefix, 'site'), vlan_count=get_subquery(VLAN, 'site'), circuit_count=get_subquery(Circuit, 'terminations__site'), virtualmachine_count=get_subquery(VirtualMachine, 'cluster__site'), ) serializer_class = serializers.SiteSerializer filterset_class = filters.SiteFilter @action(detail=True) def graphs(self, request, pk=None): """ A convenience method for rendering graphs for a particular site. """ site = get_object_or_404(Site, pk=pk) queryset = Graph.objects.filter(type=GRAPH_TYPE_SITE) serializer = RenderedGraphSerializer(queryset, many=True, context={'graphed_object': site}) return Response(serializer.data)
class ProviderViewSet(CustomFieldModelViewSet): queryset = Provider.objects.prefetch_related('tags').annotate( circuit_count=get_subquery(Circuit, 'provider') ) serializer_class = serializers.ProviderSerializer filterset_class = filters.ProviderFilterSet @action(detail=True) def graphs(self, request, pk): """ A convenience method for rendering graphs for a particular provider. """ provider = get_object_or_404(self.queryset, pk=pk) queryset = Graph.objects.restrict(request.user).filter(type__model='provider') serializer = RenderedGraphSerializer(queryset, many=True, context={'graphed_object': provider}) return Response(serializer.data)
class SiteViewSet(CustomFieldModelViewSet): queryset = Site.objects.prefetch_related( 'region', 'tenant', 'tags').annotate( device_count=Coalesce(get_subquery(Device, 'site'), 0), rack_count=Coalesce(get_subquery(Rack, 'site'), 0), prefix_count=Coalesce(get_subquery(Prefix, 'site'), 0), vlan_count=Coalesce(get_subquery(VLAN, 'site'), 0), circuit_count=Coalesce(get_subquery(Circuit, 'terminations__site'), 0), virtualmachine_count=Coalesce( get_subquery(VirtualMachine, 'cluster__site'), 0), ) serializer_class = serializers.SiteSerializer filterset_class = filters.SiteFilterSet
class VLANGroupViewSet(ModelViewSet): queryset = VLANGroup.objects.prefetch_related('site').annotate( vlan_count=Coalesce(get_subquery(VLAN, 'group'), 0)) serializer_class = serializers.VLANGroupSerializer filterset_class = filters.VLANGroupFilterSet
class ClusterTypeViewSet(ModelViewSet): queryset = ClusterType.objects.annotate( cluster_count=get_subquery(Cluster, 'type')) serializer_class = serializers.ClusterTypeSerializer filterset_class = filters.ClusterTypeFilterSet
class RoleListView(PermissionRequiredMixin, ObjectListView): permission_required = 'ipam.view_role' queryset = Role.objects.annotate(prefix_count=get_subquery(Prefix, 'role'), vlan_count=get_subquery(VLAN, 'role')) table = tables.RoleTable
class ClusterGroupViewSet(ModelViewSet): queryset = ClusterGroup.objects.annotate( cluster_count=get_subquery(Cluster, 'group')) serializer_class = serializers.ClusterGroupSerializer filterset_class = filters.ClusterGroupFilterSet
class RackViewSet(CustomFieldModelViewSet): queryset = Rack.objects.prefetch_related( 'site', 'group__site', 'role', 'tenant', 'tags' ).annotate( device_count=get_subquery(Device, 'rack'), powerfeed_count=get_subquery(PowerFeed, 'rack') ) serializer_class = serializers.RackSerializer filterset_class = filters.RackFilterSet @swagger_auto_schema(deprecated=True) @action(detail=True) def units(self, request, pk=None): """ List rack units (by rack) """ # TODO: Remove this action detail route in v2.8 rack = get_object_or_404(Rack, pk=pk) face = request.GET.get('face', 'front') exclude_pk = request.GET.get('exclude', None) if exclude_pk is not None: try: exclude_pk = int(exclude_pk) except ValueError: exclude_pk = None elevation = rack.get_rack_units(face, exclude_pk) # Enable filtering rack units by ID q = request.GET.get('q', None) if q: elevation = [u for u in elevation if q in str(u['id'])] page = self.paginate_queryset(elevation) if page is not None: rack_units = serializers.RackUnitSerializer(page, many=True, context={'request': request}) return self.get_paginated_response(rack_units.data) @swagger_auto_schema( responses={200: serializers.RackUnitSerializer(many=True)}, query_serializer=serializers.RackElevationDetailFilterSerializer ) @action(detail=True) def elevation(self, request, pk=None): """ Rack elevation representing the list of rack units. Also supports rendering the elevation as an SVG. """ rack = get_object_or_404(Rack, pk=pk) serializer = serializers.RackElevationDetailFilterSerializer(data=request.GET) if not serializer.is_valid(): return Response(serializer.errors, 400) data = serializer.validated_data if data['render'] == 'svg': # Render and return the elevation as an SVG drawing with the correct content type drawing = rack.get_elevation_svg(data['face'], data['unit_width'], data['unit_height']) return HttpResponse(drawing.tostring(), content_type='image/svg+xml') else: # Return a JSON representation of the rack units in the elevation elevation = rack.get_rack_units( face=data['face'], exclude=data['exclude'], expand_devices=data['expand_devices'] ) page = self.paginate_queryset(elevation) if page is not None: rack_units = serializers.RackUnitSerializer(page, many=True, context={'request': request}) return self.get_paginated_response(rack_units.data)
class ClusterGroupBulkDeleteView(BulkDeleteView): queryset = ClusterGroup.objects.annotate( cluster_count=get_subquery(Cluster, 'group') ) table = tables.ClusterGroupTable
class ClusterGroupListView(ObjectListView): queryset = ClusterGroup.objects.annotate( cluster_count=get_subquery(Cluster, 'group') ) table = tables.ClusterGroupTable
class ClusterTypeBulkDeleteView(BulkDeleteView): queryset = ClusterType.objects.annotate( cluster_count=get_subquery(Cluster, 'type') ) table = tables.ClusterTypeTable
class ClusterTypeListView(ObjectListView): queryset = ClusterType.objects.annotate( cluster_count=get_subquery(Cluster, 'type') ) table = tables.ClusterTypeTable
class RIRViewSet(ModelViewSet): queryset = RIR.objects.annotate( aggregate_count=Coalesce(get_subquery(Aggregate, 'rir'), 0)) serializer_class = serializers.RIRSerializer filterset_class = filters.RIRFilterSet
class RoleListView(ObjectListView): queryset = Role.objects.annotate(prefix_count=get_subquery(Prefix, 'role'), vlan_count=get_subquery(VLAN, 'role')) table = tables.RoleTable
class TenantGroupViewSet(ModelViewSet): queryset = TenantGroup.objects.annotate( tenant_count=get_subquery(Tenant, 'group')) serializer_class = serializers.TenantGroupSerializer filterset_class = filters.TenantGroupFilter