add get and patch operations
This commit is contained in:
parent
7c7f315e52
commit
a4850683c9
3 changed files with 72 additions and 2 deletions
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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<instance_id>[^/.]+)",
|
||||
)
|
||||
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<instance_id>[^/.]+)",
|
||||
)
|
||||
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}
|
||||
),
|
||||
}
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue