Scott Baker | c3ce3e7 | 2016-06-20 17:35:19 -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 viewsets |
| 7 | from rest_framework.decorators import detail_route, list_route |
| 8 | from rest_framework.views import APIView |
| 9 | from core.models import * |
| 10 | from services.vtn.models import VTNService |
| 11 | from django.forms import widgets |
| 12 | from django.conf.urls import patterns, url |
| 13 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| 14 | from django.shortcuts import get_object_or_404 |
| 15 | from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 16 | from xos.exceptions import * |
| 17 | import json |
| 18 | import subprocess |
| 19 | |
| 20 | class VTNServiceSerializer(PlusModelSerializer): |
| 21 | id = ReadOnlyField() |
| 22 | |
| 23 | privateGatewayMac = serializers.CharField(required=False) |
| 24 | localManagementIp = serializers.CharField(required=False) |
| 25 | ovsdbPort = serializers.IntegerField(required=False) |
| 26 | sshPort = serializers.IntegerField(required=False) |
| 27 | sshUser = serializers.CharField(required=False) |
| 28 | sshKeyFile = serializers.CharField(required=False) |
| 29 | mgmtSubnetBits = serializers.IntegerField(required=False) |
| 30 | xosEndpoint = serializers.CharField(required=False) |
| 31 | xosUser = serializers.CharField(required=False) |
| 32 | xosPassword = serializers.CharField(required=False) |
| 33 | |
| 34 | |
| 35 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 36 | class Meta: |
| 37 | model = VTNService |
| 38 | fields = ('humanReadableName', 'id', 'privateGatewayMac', 'localManagementIp', 'ovsdbPort', 'sshPort', 'sshUser', 'sshKeyFile', |
| 39 | 'mgmtSubnetBits', 'xosEndpoint', 'xosUser', 'xosPassword') |
| 40 | |
| 41 | def getHumanReadableName(self, obj): |
| 42 | return obj.__unicode__() |
| 43 | |
| 44 | class VTNViewSet(XOSViewSet): |
| 45 | base_name = "vtn" |
| 46 | method_name = "vtn" |
| 47 | method_kind = "viewset" |
| 48 | |
| 49 | # these are just because ViewSet needs some queryset and model, even if we don't use the |
| 50 | # default endpoints |
| 51 | queryset = VTNService.get_service_objects().all() |
| 52 | model = VTNService |
| 53 | serializer_class = VTNServiceSerializer |
| 54 | |
| 55 | @classmethod |
| 56 | def get_urlpatterns(self, api_path="^"): |
| 57 | patterns = [] |
| 58 | |
| 59 | patterns.append( self.detail_url("services/$", {"get": "get_services"}, "services") ) |
| 60 | patterns.append( self.detail_url("services_names/$", {"get": "get_services_names"}, "services") ) |
| 61 | patterns.append( self.detail_url("services/(?P<service>[a-zA-Z0-9\-_]+)/$", {"get": "get_service"}, "get_service") ) |
| 62 | |
| 63 | # Not as RESTful as it could be, but maintain these endpoints for compability |
| 64 | patterns.append( self.list_url("services/$", {"get": "get_services"}, "rootvtn_services") ) |
| 65 | patterns.append( self.list_url("services_names/$", {"get": "get_services_names"}, "rootvtn_services") ) |
| 66 | patterns.append( self.list_url("services/(?P<service>[a-zA-Z0-9\-_]+)/$", {"get": "get_service"}, "rootvtn_get_service") ) |
| 67 | |
| 68 | patterns = patterns + super(VTNViewSet,self).get_urlpatterns(api_path) |
| 69 | |
| 70 | return patterns |
| 71 | |
| 72 | def get_services_names(self, request, pk=None): |
| 73 | result = {} |
| 74 | for service in Service.objects.all(): |
| 75 | for id in service.get_vtn_src_names(): |
| 76 | dependencies = service.get_vtn_dependencies_names() |
| 77 | if dependencies: |
| 78 | result[id] = dependencies |
| 79 | return Response(result) |
| 80 | |
| 81 | def get_services(self, request, pk=None): |
| 82 | result = {} |
| 83 | for service in Service.objects.all(): |
| 84 | for id in service.get_vtn_src_ids(): |
| 85 | dependencies = service.get_vtn_dependencies_ids() |
| 86 | if dependencies: |
| 87 | result[id] = dependencies |
| 88 | return Response(result) |
| 89 | |
| 90 | def get_service(self, request, pk=None, service=None): |
| 91 | for xos_service in Service.objects.all(): |
| 92 | if service in xos_service.get_vtn_src_ids(): |
| 93 | return Response(xos_service.get_vtn_dependencies_ids()) |
| 94 | return Response([]) |
| 95 | |
| 96 | |