2019-04-12 17:07:56 -04:00
|
|
|
from django.db.models import Count
|
|
|
|
|
2019-04-19 16:50:42 -04:00
|
|
|
from dcim.models import Device, Interface
|
2017-08-08 16:33:34 -04:00
|
|
|
from extras.api.views import CustomFieldModelViewSet
|
2017-11-07 15:36:10 -05:00
|
|
|
from utilities.api import FieldChoicesViewSet, ModelViewSet
|
2019-04-19 16:50:42 -04:00
|
|
|
from utilities.utils import get_subquery
|
2017-08-16 17:00:17 -04:00
|
|
|
from virtualization import filters
|
2017-08-29 14:24:58 -04:00
|
|
|
from virtualization.models import Cluster, ClusterGroup, ClusterType, VirtualMachine
|
2017-08-08 16:33:34 -04:00
|
|
|
from . import serializers
|
|
|
|
|
|
|
|
|
2017-10-10 16:41:35 -04:00
|
|
|
#
|
|
|
|
# Field choices
|
|
|
|
#
|
|
|
|
|
|
|
|
class VirtualizationFieldChoicesViewSet(FieldChoicesViewSet):
|
|
|
|
fields = (
|
|
|
|
(VirtualMachine, ['status']),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-08-08 16:33:34 -04:00
|
|
|
#
|
|
|
|
# Clusters
|
|
|
|
#
|
|
|
|
|
|
|
|
class ClusterTypeViewSet(ModelViewSet):
|
2019-04-12 17:07:56 -04:00
|
|
|
queryset = ClusterType.objects.annotate(
|
|
|
|
cluster_count=Count('clusters')
|
|
|
|
)
|
2017-08-08 16:33:34 -04:00
|
|
|
serializer_class = serializers.ClusterTypeSerializer
|
2018-11-02 13:46:28 -04:00
|
|
|
filterset_class = filters.ClusterTypeFilter
|
2017-08-08 16:33:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ClusterGroupViewSet(ModelViewSet):
|
2019-04-12 17:07:56 -04:00
|
|
|
queryset = ClusterGroup.objects.annotate(
|
|
|
|
cluster_count=Count('clusters')
|
|
|
|
)
|
2017-08-08 16:33:34 -04:00
|
|
|
serializer_class = serializers.ClusterGroupSerializer
|
2018-11-02 13:46:28 -04:00
|
|
|
filterset_class = filters.ClusterGroupFilter
|
2017-08-08 16:33:34 -04:00
|
|
|
|
|
|
|
|
2017-11-07 15:36:10 -05:00
|
|
|
class ClusterViewSet(CustomFieldModelViewSet):
|
2019-08-19 01:53:39 -04:00
|
|
|
queryset = Cluster.objects.prefetch_related(
|
2019-10-07 08:29:32 +02:00
|
|
|
'type', 'group', 'tenant', 'site', 'tags'
|
2019-04-19 16:50:42 -04:00
|
|
|
).annotate(
|
|
|
|
device_count=get_subquery(Device, 'cluster'),
|
|
|
|
virtualmachine_count=get_subquery(VirtualMachine, 'cluster')
|
2019-04-12 17:07:56 -04:00
|
|
|
)
|
2017-08-08 16:33:34 -04:00
|
|
|
serializer_class = serializers.ClusterSerializer
|
2018-11-02 13:46:28 -04:00
|
|
|
filterset_class = filters.ClusterFilter
|
2017-08-08 16:33:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Virtual machines
|
|
|
|
#
|
|
|
|
|
2017-11-07 15:36:10 -05:00
|
|
|
class VirtualMachineViewSet(CustomFieldModelViewSet):
|
2019-08-19 01:53:39 -04:00
|
|
|
queryset = VirtualMachine.objects.prefetch_related(
|
|
|
|
'cluster__site', 'role', 'tenant', 'platform', 'primary_ip4', 'primary_ip6', 'tags'
|
|
|
|
)
|
2018-11-02 13:46:28 -04:00
|
|
|
filterset_class = filters.VirtualMachineFilter
|
2017-08-08 16:33:34 -04:00
|
|
|
|
2018-07-03 12:29:20 -04:00
|
|
|
def get_serializer_class(self):
|
|
|
|
"""
|
2019-02-18 22:10:05 -05:00
|
|
|
Select the specific serializer based on the request context.
|
|
|
|
|
|
|
|
If the `brief` query param equates to True, return the NestedVirtualMachineSerializer
|
|
|
|
|
|
|
|
If the `exclude` query param includes `config_context` as a value, return the VirtualMachineSerializer
|
|
|
|
|
|
|
|
Else, return the VirtualMachineWithConfigContextSerializer
|
2018-07-03 12:29:20 -04:00
|
|
|
"""
|
2018-10-04 16:20:01 -04:00
|
|
|
|
|
|
|
request = self.get_serializer_context()['request']
|
|
|
|
if request.query_params.get('brief', False):
|
|
|
|
return serializers.NestedVirtualMachineSerializer
|
|
|
|
|
2019-02-18 22:10:05 -05:00
|
|
|
elif 'config_context' in request.query_params.get('exclude', []):
|
|
|
|
return serializers.VirtualMachineSerializer
|
|
|
|
|
|
|
|
return serializers.VirtualMachineWithConfigContextSerializer
|
2018-06-27 16:02:34 -04:00
|
|
|
|
2017-08-08 16:33:34 -04:00
|
|
|
|
2017-11-07 15:36:10 -05:00
|
|
|
class InterfaceViewSet(ModelViewSet):
|
2018-09-28 16:44:05 -04:00
|
|
|
queryset = Interface.objects.filter(
|
|
|
|
virtual_machine__isnull=False
|
2019-08-19 01:53:39 -04:00
|
|
|
).prefetch_related(
|
|
|
|
'virtual_machine', 'tags'
|
|
|
|
)
|
2017-08-29 14:24:58 -04:00
|
|
|
serializer_class = serializers.InterfaceSerializer
|
2018-11-02 13:46:28 -04:00
|
|
|
filterset_class = filters.InterfaceFilter
|
2018-10-04 16:20:01 -04:00
|
|
|
|
|
|
|
def get_serializer_class(self):
|
|
|
|
request = self.get_serializer_context()['request']
|
|
|
|
if request.query_params.get('brief', False):
|
|
|
|
# Override get_serializer_for_model(), which will return the DCIM NestedInterfaceSerializer
|
|
|
|
return serializers.NestedInterfaceSerializer
|
|
|
|
return serializers.InterfaceSerializer
|