Skip to content

Commit

Permalink
Allow updating dchosts via dchost-list endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hipek8 committed Nov 7, 2024
1 parent 4565808 commit 9cf0ac0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/ralph/assets/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import django_filters
from django.db.models import Prefetch
from rest_framework.exceptions import ValidationError
from rest_framework.permissions import SAFE_METHODS

from ralph.api import RalphAPIViewSet
from ralph.api.filters import BooleanFilter
Expand All @@ -10,9 +11,11 @@
from ralph.assets.api import serializers
from ralph.assets.api.filters import NetworkableObjectFilters
from ralph.assets.models import BaseObject
from ralph.data_center.models import Cluster, DataCenterAsset
from ralph.licences.api import BaseObjectLicenceViewSet
from ralph.licences.models import BaseObjectLicence
from ralph.networks.models import IPAddress
from ralph.virtual.models import CloudHost, VirtualServer


class BusinessSegmentViewSet(RalphAPIViewSet):
Expand Down Expand Up @@ -255,10 +258,10 @@ class Meta(NetworkableObjectFilters.Meta):
# TODO: move to data_center and use DCHost proxy model
class DCHostViewSet(BaseObjectViewSetMixin, RalphAPIViewSet):
queryset = (
BaseObject.polymorphic_objects
BaseObject.polymorphic_objects.dc_hosts()
)
serializer_class = serializers.DCHostSerializer
http_method_names = ["get", "options", "head"]
http_method_names = ["get", "options", "head", "patch", "post"]
filter_fields = [
"id",
"service_env",
Expand Down Expand Up @@ -296,9 +299,26 @@ class DCHostViewSet(BaseObjectViewSetMixin, RalphAPIViewSet):
}
additional_filter_class = DCHostFilterSet

def get_serializer_class(self, *args, **kwargs):
if self.request.method not in SAFE_METHODS:
obj_ = self.get_object()
if isinstance(obj_, VirtualServer):
from ralph.virtual.api import VirtualServerSaveSerializer
return VirtualServerSaveSerializer
elif isinstance(obj_, DataCenterAsset):
from ralph.data_center.api.serializers import DataCenterAssetSaveSerializer
return DataCenterAssetSaveSerializer
elif isinstance(obj_, CloudHost):
from ralph.virtual.api import SaveCloudHostSerializer
return SaveCloudHostSerializer
elif isinstance(obj_, Cluster):
from ralph.data_center.api.serializers import ClusterSerializer
return ClusterSerializer
return serializers.DCHostSerializer

def get_queryset(self):
return (
self.queryset.dc_hosts()
self.queryset
.select_related(*self.select_related)
.polymorphic_select_related(Cluster=['type'])
.polymorphic_prefetch_related(
Expand Down
25 changes: 25 additions & 0 deletions src/ralph/assets/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,31 @@ def test_filter_by_env_name(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['count'], 1)

def test_patch_dchost_virtual_server(self):
new_hypervisor = DataCenterAssetFullFactory()
url = reverse('dchost-detail', args=(self.virtual.id,))
data = {
'hostname': 'new-hostname',
'hypervisor': new_hypervisor.id,
}
response = self.client.patch(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.virtual.refresh_from_db()
self.assertEqual(self.virtual.hostname, 'new-hostname')
self.assertEqual(self.virtual.parent.id, new_hypervisor.id)

def test_patch_dchost_cloudhost(self):
new_hypervisor = DataCenterAssetFullFactory()
url = reverse('dchost-detail', args=(self.cloud_host.id,))
data = {
'hostname': 'new-hostname',
'hypervisor': new_hypervisor.id,
}
response = self.client.patch(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.cloud_host.refresh_from_db()
self.assertEqual(self.cloud_host.hostname, 'new-hostname')
self.assertEqual(self.cloud_host.hypervisor.id, new_hypervisor.id)

class ConfigurationModuleAPITests(RalphAPITestCase):
def setUp(self):
Expand Down

0 comments on commit 9cf0ac0

Please sign in to comment.