Skip to content

Commit

Permalink
Allow cloud host to be parent of VM in API (allegro#2984)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurek authored Mar 22, 2017
1 parent 9d50d28 commit 969feab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ralph/virtual/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions src/ralph/virtual/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 969feab

Please sign in to comment.