Skip to content

Commit

Permalink
Fix related hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
matyldv committed Nov 12, 2024
1 parent 530cac3 commit 6271315
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
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

0 comments on commit 6271315

Please sign in to comment.