2020-08-13 12:45:38 -04:00
|
|
|
from rest_framework.routers import APIRootView
|
|
|
|
|
2019-04-19 16:50:42 -04:00
|
|
|
from circuits.models import Circuit
|
|
|
|
from dcim.models import Device, Rack, Site
|
2017-05-24 11:33:11 -04:00
|
|
|
from extras.api.views import CustomFieldModelViewSet
|
2019-04-19 16:50:42 -04:00
|
|
|
from ipam.models import IPAddress, Prefix, VLAN, VRF
|
2017-06-19 16:10:18 -04:00
|
|
|
from tenancy import filters
|
2016-07-26 14:58:37 -04:00
|
|
|
from tenancy.models import Tenant, TenantGroup
|
2020-12-17 14:47:49 -05:00
|
|
|
from utilities.utils import count_related
|
2019-04-19 16:50:42 -04:00
|
|
|
from virtualization.models import VirtualMachine
|
2016-07-26 14:58:37 -04:00
|
|
|
from . import serializers
|
|
|
|
|
|
|
|
|
2020-08-13 12:45:38 -04:00
|
|
|
class TenancyRootView(APIRootView):
|
|
|
|
"""
|
|
|
|
Tenancy API root view
|
|
|
|
"""
|
|
|
|
def get_view_name(self):
|
|
|
|
return 'Tenancy'
|
|
|
|
|
|
|
|
|
2017-01-24 17:12:16 -05:00
|
|
|
#
|
|
|
|
# Tenant Groups
|
|
|
|
#
|
2016-07-26 14:58:37 -04:00
|
|
|
|
2021-02-25 15:58:13 -05:00
|
|
|
class TenantGroupViewSet(CustomFieldModelViewSet):
|
2020-07-20 12:07:19 -04:00
|
|
|
queryset = TenantGroup.objects.add_related_count(
|
|
|
|
TenantGroup.objects.all(),
|
|
|
|
Tenant,
|
|
|
|
'group',
|
|
|
|
'tenant_count',
|
|
|
|
cumulative=True
|
2019-04-12 17:07:56 -04:00
|
|
|
)
|
2016-07-26 14:58:37 -04:00
|
|
|
serializer_class = serializers.TenantGroupSerializer
|
2020-01-09 20:40:32 -05:00
|
|
|
filterset_class = filters.TenantGroupFilterSet
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
|
2017-01-24 17:12:16 -05:00
|
|
|
#
|
|
|
|
# Tenants
|
|
|
|
#
|
2016-07-26 14:58:37 -04:00
|
|
|
|
2017-11-07 15:36:10 -05:00
|
|
|
class TenantViewSet(CustomFieldModelViewSet):
|
2019-08-19 01:53:39 -04:00
|
|
|
queryset = Tenant.objects.prefetch_related(
|
|
|
|
'group', 'tags'
|
2019-04-19 16:50:42 -04:00
|
|
|
).annotate(
|
2020-12-17 14:47:49 -05:00
|
|
|
circuit_count=count_related(Circuit, 'tenant'),
|
|
|
|
device_count=count_related(Device, 'tenant'),
|
|
|
|
ipaddress_count=count_related(IPAddress, 'tenant'),
|
|
|
|
prefix_count=count_related(Prefix, 'tenant'),
|
|
|
|
rack_count=count_related(Rack, 'tenant'),
|
|
|
|
site_count=count_related(Site, 'tenant'),
|
|
|
|
virtualmachine_count=count_related(VirtualMachine, 'tenant'),
|
|
|
|
vlan_count=count_related(VLAN, 'tenant'),
|
|
|
|
vrf_count=count_related(VRF, 'tenant')
|
2019-04-19 16:50:42 -04:00
|
|
|
)
|
2016-07-26 14:58:37 -04:00
|
|
|
serializer_class = serializers.TenantSerializer
|
2020-01-09 20:40:32 -05:00
|
|
|
filterset_class = filters.TenantFilterSet
|