From a4850683c97b493e8f35d6e141baa0719f28668e Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Fri, 31 Jan 2025 16:35:33 +0100 Subject: [PATCH] add get and patch operations --- hub/broker/serializers.py | 17 +++++++++++++ hub/broker/urls.py | 6 ++++- hub/broker/views.py | 51 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/hub/broker/serializers.py b/hub/broker/serializers.py index d92953c..3d20e22 100644 --- a/hub/broker/serializers.py +++ b/hub/broker/serializers.py @@ -127,3 +127,20 @@ class ServiceOfferingSerializer(serializers.ModelSerializer): class CatalogSerializer(serializers.Serializer): services = ServiceOfferingSerializer(many=True) + + +class ServiceInstanceUpdateSerializer(serializers.Serializer): + plan_id = serializers.CharField() + + def validate_plan_id(self, value): + try: + plan = Plan.objects.get(pk=value) + # Get instance from context + instance = self.context.get("instance") + if str(plan.offering.id) != str(instance.offering.id): + raise serializers.ValidationError( + "Plan does not belong to the current offering" + ) + return plan + except Plan.DoesNotExist: + raise serializers.ValidationError("Invalid plan_id") diff --git a/hub/broker/urls.py b/hub/broker/urls.py index aee3d55..f980846 100644 --- a/hub/broker/urls.py +++ b/hub/broker/urls.py @@ -9,9 +9,13 @@ class ServiceBrokerRouter(DefaultRouter): def get_method_map(self, viewset, method_map): result = super().get_method_map(viewset, method_map) - # Add PUT method for provision_instance + # Add methods for service instance operations if hasattr(viewset, "provision_instance"): result.update({"put": "provision_instance"}) + if hasattr(viewset, "get_instance"): + result.update({"get": "get_instance"}) + if hasattr(viewset, "update_instance"): + result.update({"patch": "update_instance"}) return result diff --git a/hub/broker/views.py b/hub/broker/views.py index 8e33ef6..4f7cf53 100644 --- a/hub/broker/views.py +++ b/hub/broker/views.py @@ -8,7 +8,11 @@ from django.shortcuts import get_object_or_404 from hub.services.models import Lead from hub.services.odoo import OdooAPI from .models import ServiceInstance, ServiceBrokerUser -from .serializers import ServiceInstanceProvisioningSerializer, CatalogSerializer +from .serializers import ( + ServiceInstanceProvisioningSerializer, + CatalogSerializer, + ServiceInstanceUpdateSerializer, +) from .authentication import ServiceBrokerAuthentication logger = logging.getLogger(__name__) @@ -137,3 +141,48 @@ class ServiceBrokerViewSet(viewsets.ViewSet): instance = get_object_or_404(ServiceInstance, instance_id=instance_id) instance.delete() return Response(status=status.HTTP_200_OK) + + @action( + detail=False, + methods=["get"], + url_path="service_instances/(?P[^/.]+)", + ) + def get_instance(self, request, instance_id): + instance = get_object_or_404(ServiceInstance, instance_id=instance_id) + return Response( + { + "service_id": str(instance.offering.id), + "plan_id": str(instance.plan.id), + "dashboard_url": settings.WEBSITE_URL + + reverse( + "services:offering_detail", kwargs={"slug": instance.offering.slug} + ), + } + ) + + @action( + detail=False, + methods=["patch"], + url_path="service_instances/(?P[^/.]+)", + ) + def update_instance(self, request, instance_id): + instance = get_object_or_404(ServiceInstance, instance_id=instance_id) + serializer = ServiceInstanceUpdateSerializer( + data=request.data, context={"instance": instance} + ) + + if not serializer.is_valid(): + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + instance.plan = serializer.validated_data["plan_id"] + instance.save() + + return Response( + { + "operation": "update", + "dashboard_url": settings.WEBSITE_URL + + reverse( + "services:offering_detail", kwargs={"slug": instance.offering.slug} + ), + } + )