Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame^] | 1 | from rest_framework.decorators import api_view |
| 2 | from rest_framework.response import Response |
| 3 | from rest_framework.reverse import reverse |
| 4 | from rest_framework import serializers |
| 5 | from rest_framework import generics |
| 6 | from rest_framework import status |
| 7 | from core.models import * |
| 8 | from django.forms import widgets |
| 9 | from services.vsg.models import VSGTenant, VSGService |
| 10 | from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 11 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| 12 | |
| 13 | def get_default_vsg_service(): |
| 14 | vsg_services = VSGService.get_service_objects().all() |
| 15 | if vsg_services: |
| 16 | return vsg_services[0].id |
| 17 | return None |
| 18 | |
| 19 | class VSGTenantForAPI(VSGTenant): |
| 20 | class Meta: |
| 21 | proxy = True |
| 22 | app_label = "cord" |
| 23 | |
| 24 | @property |
| 25 | def related(self): |
| 26 | related = {} |
| 27 | if self.instance: |
| 28 | related["instance_id"] = self.instance.id |
| 29 | return related |
| 30 | |
| 31 | class VSGTenantSerializer(PlusModelSerializer): |
| 32 | id = ReadOnlyField() |
| 33 | wan_container_ip = serializers.CharField() |
| 34 | wan_container_mac = ReadOnlyField() |
| 35 | related = serializers.DictField(required=False) |
| 36 | |
| 37 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 38 | class Meta: |
| 39 | model = VSGTenantForAPI |
| 40 | fields = ('humanReadableName', 'id', 'wan_container_ip', 'wan_container_mac', 'related' ) |
| 41 | |
| 42 | def getHumanReadableName(self, obj): |
| 43 | return obj.__unicode__() |
| 44 | |
| 45 | class VSGTenantViewSet(XOSViewSet): |
| 46 | base_name = "vsg" |
| 47 | method_name = "vsg" |
| 48 | method_kind = "viewset" |
| 49 | queryset = VSGTenantForAPI.get_tenant_objects().all() |
| 50 | serializer_class = VSGTenantSerializer |
| 51 | |
| 52 | @classmethod |
| 53 | def get_urlpatterns(self, api_path="^"): |
| 54 | patterns = super(VSGTenantViewSet, self).get_urlpatterns(api_path=api_path) |
| 55 | |
| 56 | return patterns |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |