From 969feab8ef3a03eab54a3961ee000f64f7b3376c Mon Sep 17 00:00:00 2001 From: Mateusz Kurek Date: Wed, 22 Mar 2017 11:08:15 +0100 Subject: [PATCH] Allow cloud host to be parent of VM in API (#2984) --- src/ralph/virtual/api.py | 4 ++-- src/ralph/virtual/tests/test_api.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/ralph/virtual/api.py b/src/ralph/virtual/api.py index d9a90cb312..15d6d4b98f 100644 --- a/src/ralph/virtual/api.py +++ b/src/ralph/virtual/api.py @@ -18,7 +18,7 @@ ) from ralph.assets.models import Ethernet from ralph.data_center.api.serializers import DataCenterAssetSimpleSerializer -from ralph.data_center.models import DataCenterAsset +from ralph.data_center.models import DCHost from ralph.virtual.admin import VirtualServerAdmin from ralph.virtual.models import ( CloudFlavor, @@ -145,7 +145,7 @@ class Meta(BaseObjectSerializer.Meta): class VirtualServerSaveSerializer(RalphAPISaveSerializer): hypervisor = relations.PrimaryKeyRelatedField( - source='parent', queryset=DataCenterAsset.objects.all(), + source='parent', queryset=DCHost.objects, ) class Meta: diff --git a/src/ralph/virtual/tests/test_api.py b/src/ralph/virtual/tests/test_api.py index a1b01077ef..f1adc03200 100644 --- a/src/ralph/virtual/tests/test_api.py +++ b/src/ralph/virtual/tests/test_api.py @@ -313,6 +313,7 @@ class VirtualServerAPITestCase(RalphAPITestCase): def setUp(self): super().setUp() self.hypervisor = DataCenterAssetFactory() + self.cloud_hypervisor = CloudHostFactory() self.cluster = ClusterFactory() self.type = VirtualServerType.objects.create(name='XEN') self.virtual_server = VirtualServerFullFactory( @@ -381,6 +382,25 @@ def test_create_virtual_server(self): self.assertEqual(virtual_server.parent.id, self.hypervisor.id) self.assertEqual(virtual_server.sn, data['sn']) + def test_create_virtual_server_with_cloud_host_as_parent(self): + virtual_server_count = VirtualServer.objects.count() + url = reverse('virtualserver-list') + data = { + 'hostname': 's1234.local', + 'type': self.type.id, + 'sn': '143ed36a-3e86-457d-9e19-3dcfe4d5ed26', + 'hypervisor': self.cloud_hypervisor.id, + } + response = self.client.post(url, data, format='json') + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertEqual( + VirtualServer.objects.count(), virtual_server_count + 1 + ) + virtual_server = VirtualServer.objects.get(pk=response.data['id']) + self.assertEqual(virtual_server.hostname, data['hostname']) + self.assertEqual(virtual_server.parent.id, self.cloud_hypervisor.id) + self.assertEqual(virtual_server.sn, data['sn']) + def test_patch_virtual_server(self): url = reverse('virtualserver-detail', args=(self.virtual_server.id,)) data = {