Matteo Scandolo | 6288d5a | 2017-08-08 13:05:26 -0700 | [diff] [blame^] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 17 | from rest_framework.decorators import api_view |
| 18 | from rest_framework.response import Response |
| 19 | from rest_framework.reverse import reverse |
| 20 | from rest_framework import serializers |
| 21 | from rest_framework import generics |
| 22 | from rest_framework import status |
| 23 | from core.models import * |
| 24 | from django.forms import widgets |
| 25 | from services.veg.models import VEGTenant, VEGService |
| 26 | from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 27 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| 28 | |
| 29 | def get_default_veg_service(): |
| 30 | VEG_services = VEGService.get_service_objects().all() |
| 31 | if VEG_services: |
| 32 | return VEG_services[0].id |
| 33 | return None |
| 34 | |
| 35 | class VEGTenantForAPI(VEGTenant): |
| 36 | class Meta: |
| 37 | proxy = True |
| 38 | app_label = "cord" |
| 39 | |
| 40 | @property |
| 41 | def related(self): |
| 42 | related = {} |
| 43 | if self.instance: |
| 44 | related["instance_id"] = self.instance.id |
| 45 | return related |
| 46 | |
| 47 | class VEGTenantSerializer(PlusModelSerializer): |
| 48 | id = ReadOnlyField() |
| 49 | wan_container_ip = serializers.CharField() |
| 50 | wan_container_mac = ReadOnlyField() |
| 51 | related = serializers.DictField(required=False) |
| 52 | |
| 53 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 54 | class Meta: |
| 55 | model = VEGTenantForAPI |
| 56 | fields = ('humanReadableName', 'id', 'wan_container_ip', 'wan_container_mac', 'related' ) |
| 57 | |
| 58 | def getHumanReadableName(self, obj): |
| 59 | return obj.__unicode__() |
| 60 | |
| 61 | class VEGTenantViewSet(XOSViewSet): |
| 62 | base_name = "veg" |
| 63 | method_name = "veg" |
| 64 | method_kind = "viewset" |
| 65 | queryset = VEGTenantForAPI.get_tenant_objects().all() |
| 66 | serializer_class = VEGTenantSerializer |
| 67 | |
| 68 | @classmethod |
| 69 | def get_urlpatterns(self, api_path="^"): |
| 70 | patterns = super(VEGTenantViewSet, self).get_urlpatterns(api_path=api_path) |
| 71 | |
| 72 | return patterns |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |