diff --git a/src/ralph/assets/api/serializers.py b/src/ralph/assets/api/serializers.py index c5e5ba397a..580c288bb4 100644 --- a/src/ralph/assets/api/serializers.py +++ b/src/ralph/assets/api/serializers.py @@ -37,7 +37,6 @@ Processor ) from ralph.configuration_management.api import SCMInfoSerializer -from ralph.data_center.models import DCHost from ralph.lib.custom_fields.api import WithCustomFieldsSerializerMixin from ralph.licences.api_simple import SimpleBaseObjectLicenceSerializer from ralph.networks.api_simple import IPAddressSimpleSerializer @@ -458,40 +457,3 @@ class SecurityScanField(serializers.Field): def to_representation(self, value): if value and value.pk: return SecurityScanSerializer().to_representation(value) - - -class DCHostSerializer(ComponentSerializerMixin, BaseObjectSerializer): - hostname = fields.CharField() - securityscan = SecurityScanField() - - class Meta: - model = DCHost - fields = [ - 'id', - 'url', - 'ethernet', - 'ipaddresses', - 'custom_fields', - 'tags', - 'securityscan', - 'object_type', - '__str__', - 'service_env', 'configuration_path', - 'hostname', - 'created', 'modified', 'remarks', 'parent', - 'configuration_variables', - ] - - -class DCHostPhysicalSerializer(DCHostSerializer): - model = serializers.SerializerMethodField() - - class Meta: - model = BaseObject - fields = DCHostSerializer.Meta.fields + ['model'] - - def get_model(self, obj): - try: - return str(obj.model) - except AttributeError: - return None diff --git a/src/ralph/assets/api/serializers_dchosts.py b/src/ralph/assets/api/serializers_dchosts.py new file mode 100644 index 0000000000..3b0c00d9a4 --- /dev/null +++ b/src/ralph/assets/api/serializers_dchosts.py @@ -0,0 +1,48 @@ +from rest_framework import fields, serializers + +from ralph.assets.api.serializers import ( + BaseObjectSerializer, + ComponentSerializerMixin, + SecurityScanField +) +from ralph.assets.models import BaseObject +from ralph.data_center.api.serializers import DataCenterAssetSimpleSerializer +from ralph.data_center.models import DCHost + + +class DCHostSerializer(ComponentSerializerMixin, BaseObjectSerializer): + hostname = fields.CharField() + securityscan = SecurityScanField() + hypervisor = DataCenterAssetSimpleSerializer(required=False) + + class Meta: + model = DCHost + fields = [ + 'id', + 'url', + 'ethernet', + 'ipaddresses', + 'custom_fields', + 'tags', + 'securityscan', + 'object_type', + '__str__', + 'service_env', 'configuration_path', + 'hostname', + 'created', 'modified', 'remarks', 'parent', + 'configuration_variables', 'hypervisor' + ] + + +class DCHostPhysicalSerializer(DCHostSerializer): + model = serializers.SerializerMethodField() + + class Meta: + model = BaseObject + fields = DCHostSerializer.Meta.fields + ['model'] + + def get_model(self, obj): + try: + return str(obj.model) + except AttributeError: + return None diff --git a/src/ralph/assets/api/views.py b/src/ralph/assets/api/views.py index b0c25e25a2..b82db4d11c 100644 --- a/src/ralph/assets/api/views.py +++ b/src/ralph/assets/api/views.py @@ -4,6 +4,7 @@ from rest_framework.exceptions import NotFound, ValidationError from rest_framework.permissions import SAFE_METHODS +import ralph.assets.api.serializers_dchosts from ralph.api import RalphAPIViewSet from ralph.api.filters import BooleanFilter from ralph.api.utils import PolymorphicViewSetMixin @@ -261,7 +262,7 @@ class DCHostViewSet(BaseObjectViewSetMixin, RalphAPIViewSet): queryset = ( BaseObject.polymorphic_objects ) - serializer_class = serializers.DCHostSerializer + serializer_class = ralph.assets.api.serializers_dchosts.DCHostSerializer renderer_classes = renderer_classes_without_form(RalphAPIViewSet.renderer_classes) http_method_names = ["get", "options", "head", "patch", "post"] filter_fields = [ @@ -321,13 +322,13 @@ def get_serializer_class(self, *args, **kwargs): raise NotFound() except AssertionError: # for some reason when opening browsable api this raises pass - return serializers.DCHostSerializer + return ralph.assets.api.serializers_dchosts.DCHostSerializer def get_queryset(self): return ( self.queryset.dc_hosts() .select_related(*self.select_related) - .polymorphic_select_related(Cluster=['type']) + .polymorphic_select_related(Cluster=['type'], CloudHost=['hypervisor']) .polymorphic_prefetch_related( Cluster=[*self.prefetch_related], DataCenterAsset=[*self.prefetch_related], diff --git a/src/ralph/assets/tests/test_api.py b/src/ralph/assets/tests/test_api.py index 9420ba5f7a..a38c4dbd0c 100644 --- a/src/ralph/assets/tests/test_api.py +++ b/src/ralph/assets/tests/test_api.py @@ -971,6 +971,12 @@ def test_get_dc_host_details(self): response = self.client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) + def test_get_dc_host_cloud_host_details(self): + url = reverse('dchost-detail', args=(self.cloud_host.pk,)) + response = self.client.get(url, format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.json()['hypervisor']['hostname'], self.dc_asset.hostname) + def test_filter_by_type_dc_asset(self): url = '{}?{}'.format( reverse('dchost-list'), diff --git a/src/ralph/data_center/publishers.py b/src/ralph/data_center/publishers.py index 06ca42e314..3e24f9b35f 100644 --- a/src/ralph/data_center/publishers.py +++ b/src/ralph/data_center/publishers.py @@ -10,10 +10,8 @@ def _get_host_data(instance): - from ralph.assets.api.serializers import ( - DCHostSerializer, - DCHostPhysicalSerializer - ) + from ralph.assets.api.serializers_dchosts import DCHostPhysicalSerializer + from ralph.assets.api.serializers_dchosts import DCHostSerializer from ralph.data_center.models import DataCenterAsset if isinstance(instance, DataCenterAsset): serializer = DCHostPhysicalSerializer(instance=instance)