sliceplus support for site_allocation and network_ports for tenant view
Scott Baker [Sun, 11 Jan 2015 21:45:19 +0000 (13:45 -0800)]
planetstack/core/xoslib/methods/plus.py
planetstack/core/xoslib/methods/sliceplus.py
planetstack/core/xoslib/objects/sliceplus.py

index 9ace688..ca89c79 100644 (file)
@@ -1,4 +1,7 @@
+from rest_framework import generics
 from rest_framework import serializers
+from rest_framework.response import Response
+from rest_framework import status
 
 """ PlusSerializerMixin
 
@@ -22,3 +25,53 @@ class PlusSerializerMixin():
     def getBackendHtml(self, obj):
         return obj.getBackendHtml()
 
+# XXX this is taken from genapi.py
+# XXX find a better way to re-use the code
+class PlusRetrieveUpdateDestroyAPIView(generics.RetrieveUpdateDestroyAPIView):
+
+    # To handle fine-grained field permissions, we have to check can_update
+    # the object has been updated but before it has been saved.
+
+    def update(self, request, *args, **kwargs):\r
+        partial = kwargs.pop('partial', False)\r
+        self.object = self.get_object_or_none()\r
+\r
+        serializer = self.get_serializer(self.object, data=request.DATA,\r
+                                         files=request.FILES, partial=partial)\r
+\r
+        if not serializer.is_valid():\r
+            response = {"error": "validation",\r
+                        "specific_error": "not serializer.is_valid()",\r
+                        "reasons": serializer.errors}\r
+            return Response(response, status=status.HTTP_400_BAD_REQUEST)\r
+\r
+        try:\r
+            self.pre_save(serializer.object)\r
+        except ValidationError as err:\r
+            # full_clean on model instance may be called in pre_save,\r
+            # so we have to handle eventual errors.\r
+            response = {"error": "validation",\r
+                         "specific_error": "ValidationError in pre_save",\r
+                         "reasons": err.message_dict}\r
+            return Response(response, status=status.HTTP_400_BAD_REQUEST)\r
+\r
+        if serializer.object is not None:\r
+            if not serializer.object.can_update(request.user):\r
+                return Response(status=status.HTTP_400_BAD_REQUEST)\r
+\r
+        if self.object is None:\r
+            self.object = serializer.save(force_insert=True)\r
+            self.post_save(self.object, created=True)\r
+            return Response(serializer.data, status=status.HTTP_201_CREATED)\r
+\r
+        self.object = serializer.save(force_update=True)\r
+        self.post_save(self.object, created=False)\r
+        return Response(serializer.data, status=status.HTTP_200_OK)
+
+    def destroy(self, request, *args, **kwargs):
+        obj = self.get_object()
+        if obj.can_update(request.user):
+            return super(generics.RetrieveUpdateDestroyAPIView, self).destroy(request, *args, **kwargs)
+        else:
+            return Response(status=status.HTTP_400_BAD_REQUEST)
+
index 25e4d1e..f29e200 100644 (file)
@@ -6,7 +6,7 @@ from rest_framework import generics
 from core.models import *\r
 from django.forms import widgets
 from core.xoslib.objects.sliceplus import SlicePlus
-from plus import PlusSerializerMixin
+from plus import PlusSerializerMixin, PlusRetrieveUpdateDestroyAPIView
 
 if hasattr(serializers, "ReadOnlyField"):
     # rest_framework 3.x
@@ -22,12 +22,20 @@ class NetworkPortsField(serializers.WritableField):   # note: maybe just Field i
     def to_internal_value(self, data):
         return data
 
+class SiteAllocationField(serializers.WritableField):   # note: maybe just Field in rest_framework 3.x instead of WritableField
+    def to_representation(self, obj):
+        return json.dumps(obj)
+
+    def to_internal_value(self, data):
+        return json.loads(data)
+
 class SlicePlusIdSerializer(serializers.ModelSerializer, PlusSerializerMixin):
         id = IdField()
 \r
         sliceInfo = serializers.SerializerMethodField("getSliceInfo")\r
         humanReadableName = serializers.SerializerMethodField("getHumanReadableName")\r
-        network_ports = NetworkPortsField()\r
+        network_ports = NetworkPortsField(required=False)\r
+        site_allocation = SiteAllocationField(required=False)\r
 \r
         def getSliceInfo(self, slice):\r
             return slice.getSliceInfo(user=self.context['request'].user)\r
@@ -40,7 +48,8 @@ class SlicePlusIdSerializer(serializers.ModelSerializer, PlusSerializerMixin):
 \r
         class Meta:\r
             model = SlicePlus\r
-            fields = ('humanReadableName', 'id','created','updated','enacted','name','enabled','omf_friendly','description','slice_url','site','max_slivers','image_preference','service','network','mount_data_sets','serviceClass','creator','networks','sliceInfo','network_ports','backendIcon','backendHtml')
+            fields = ('humanReadableName', 'id','created','updated','enacted','name','enabled','omf_friendly','description','slice_url','site','max_slivers','image_preference','service','network','mount_data_sets',
+                      'serviceClass','creator','networks','sliceInfo','network_ports','backendIcon','backendHtml','site_allocation')
 
 class SlicePlusList(generics.ListCreateAPIView):
     queryset = SlicePlus.objects.select_related().all()
@@ -60,7 +69,7 @@ class SlicePlusList(generics.ListCreateAPIView):
         else:
             return Response(status=status.HTTP_400_BAD_REQUEST)
 
-class SlicePlusDetail(generics.RetrieveUpdateDestroyAPIView):
+class SlicePlusDetail(PlusRetrieveUpdateDestroyAPIView):
     queryset = SlicePlus.objects.select_related().all()
     serializer_class = SlicePlusIdSerializer
 
index efb6b2a..7ec8e27 100644 (file)
@@ -27,6 +27,14 @@ class SlicePlus(Slice, PlusObjectMixin):
                 "roles": roles}
 
     @property
+    def site_allocation(self):
+        return self.getSliceInfo()["sitesUsed"]
+
+    @site_allocation.setter
+    def site_allocation(self, value):
+        print "XXX set sitesUsed to", value
+
+    @property
     def network_ports(self):
         # XXX this assumes there is only one network that can have ports bound
         # to it for a given slice. This is intended for the tenant view, which