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 viewsets |
| 23 | from rest_framework import status |
| 24 | from rest_framework.decorators import detail_route, list_route |
| 25 | from rest_framework.views import APIView |
| 26 | from core.models import * |
| 27 | from django.forms import widgets |
| 28 | from django.conf.urls import patterns, url |
| 29 | from services.veg.models import VEGService |
| 30 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| 31 | from django.shortcuts import get_object_or_404 |
| 32 | from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 33 | from xos.exceptions import * |
| 34 | import json |
| 35 | import subprocess |
| 36 | from django.views.decorators.csrf import ensure_csrf_cookie |
| 37 | |
| 38 | class VEGServiceForApi(VEGService): |
| 39 | class Meta: |
| 40 | proxy = True |
| 41 | app_label = "cord" |
| 42 | |
| 43 | def __init__(self, *args, **kwargs): |
| 44 | super(VEGServiceForApi, self).__init__(*args, **kwargs) |
| 45 | |
| 46 | def save(self, *args, **kwargs): |
| 47 | super(VEGServiceForApi, self).save(*args, **kwargs) |
| 48 | |
| 49 | def __init__(self, *args, **kwargs): |
| 50 | super(VEGService, self).__init__(*args, **kwargs) |
| 51 | |
| 52 | class VEGServiceSerializer(PlusModelSerializer): |
| 53 | id = ReadOnlyField() |
| 54 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 55 | wan_container_gateway_ip = serializers.CharField(required=False) |
| 56 | wan_container_gateway_mac = serializers.CharField(required=False) |
| 57 | dns_servers = serializers.CharField(required=False) |
| 58 | url_filter_kind = serializers.CharField(required=False) |
| 59 | node_label = serializers.CharField(required=False) |
| 60 | |
| 61 | class Meta: |
| 62 | model = VEGServiceForApi |
| 63 | fields = ('humanReadableName', |
| 64 | 'id', |
| 65 | 'wan_container_gateway_ip', |
| 66 | 'wan_container_gateway_mac', |
| 67 | 'dns_servers', |
| 68 | 'url_filter_kind', |
| 69 | 'node_label') |
| 70 | |
| 71 | def getHumanReadableName(self, obj): |
| 72 | return obj.__unicode__() |
| 73 | |
| 74 | # @ensure_csrf_cookie |
| 75 | class VEGServiceViewSet(XOSViewSet): |
| 76 | base_name = "VEGservice" |
| 77 | method_name = None # use the api endpoint /api/service/veg/ |
| 78 | method_kind = "viewset" |
| 79 | queryset = VEGService.get_service_objects().select_related().all() |
| 80 | serializer_class = VEGServiceSerializer |
| 81 | |
| 82 | @classmethod |
| 83 | def get_urlpatterns(self, api_path="^"): |
| 84 | patterns = super(VEGServiceViewSet, self).get_urlpatterns(api_path=api_path) |
| 85 | |
| 86 | return patterns |
| 87 | |
| 88 | def list(self, request): |
| 89 | object_list = self.filter_queryset(self.get_queryset()) |
| 90 | |
| 91 | serializer = self.get_serializer(object_list, many=True) |
| 92 | |
| 93 | return Response(serializer.data) |
| 94 | |