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

Fix related hosts #3869

Draft
wants to merge 1 commit into
base: ng
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion src/ralph/data_center/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class Meta(SimpleRackSerializer.Meta):
class DataCenterAssetSimpleSerializer(RalphAPISerializer):
class Meta:
model = DataCenterAsset
fields = ['hostname', 'url']
fields = ['id', 'hostname', 'url']
_skip_tags_field = True


Expand Down Expand Up @@ -164,6 +164,8 @@ def _get_cloud_hosts(self, obj):
)

def get_related_hosts(self, obj):
if hasattr(obj, 'content_type'):
obj = obj.content_type.get_object_for_this_type(pk=obj.pk)
return {
"virtual_servers": self._get_virtual_servers(obj),
"physical_servers": self._get_physical_servers(obj),
Expand Down
45 changes: 45 additions & 0 deletions src/ralph/data_center/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,51 @@ def test_get_data_center_asset_details_related_hosts(self):
response.data['related_hosts']['physical_servers'][0]['hostname'],
dc_asset_4.hostname
)
def test_get_data_center_asset_list_related_hosts(self):
dc_asset_5 = DataCenterAssetFullFactory()
cloud_host = CloudHostFactory(
hypervisor=dc_asset_5
)
virtual_server = VirtualServerFactory(
parent=dc_asset_5
)
virtual_server_2 = VirtualServerFactory(
parent=dc_asset_5,
hostname='random_test_hostname'
)
dc_asset_6 = DataCenterAssetFullFactory(
parent=dc_asset_5
)
url = f"{reverse('datacenterasset-list')}?hostname={dc_asset_5.hostname}"
response = self.client.get(url, format='json')
self.assertEqual(
len(response.data['results']), 1
)
self.assertEqual(
len(response.data['results'][0]['related_hosts']['cloud_hosts']), 1
)
self.assertEqual(
len(response.data['results'][0]['related_hosts']['virtual_servers']), 2
)
self.assertIn(
response.data['results'][0]['related_hosts']['virtual_servers'][0]['hostname'],
(virtual_server.hostname, virtual_server_2.hostname)
)
self.assertIn(
response.data['results'][0]['related_hosts']['virtual_servers'][1]['hostname'],
(virtual_server.hostname, virtual_server_2.hostname)
)
self.assertEqual(
response.data['results'][0]['related_hosts']['cloud_hosts'][0]['hostname'],
cloud_host.hostname
)
self.assertEqual(
len(response.data['results'][0]['related_hosts']['physical_servers']), 1
)
self.assertEqual(
response.data['results'][0]['related_hosts']['physical_servers'][0]['hostname'],
dc_asset_6.hostname
)

def test_create_data_center_asset(self):
url = reverse('datacenterasset-list')
Expand Down
4 changes: 2 additions & 2 deletions src/ralph/virtual/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class CloudHostSimpleSerializer(BaseObjectSerializer):

class Meta:
model = CloudHost
fields = ['hostname', 'ip_addresses', 'url']
fields = ['id', 'hostname', 'ip_addresses', 'url']


class CloudProjectSimpleSerializer(BaseObjectSerializer):
Expand Down Expand Up @@ -152,7 +152,7 @@ class Meta:
class VirtualServerSimpleSerializer(BaseObjectSerializer):
class Meta(BaseObjectSerializer.Meta):
model = VirtualServer
fields = ['hostname', 'url']
fields = ['id', 'hostname', 'url']
_skip_tags_field = True
exclude = None

Expand Down
Loading