Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hypervisor to dc hosts cloud host #3867

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions src/ralph/assets/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
48 changes: 48 additions & 0 deletions src/ralph/assets/api/serializers_dchosts.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions src/ralph/assets/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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],
Expand Down
6 changes: 6 additions & 0 deletions src/ralph/assets/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
6 changes: 2 additions & 4 deletions src/ralph/data_center/publishers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading