diff options
author | Olivia2047 <zhan.jie1@zte.com.cn> | 2018-02-06 15:31:41 +0800 |
---|---|---|
committer | Olivia2047 <zhan.jie1@zte.com.cn> | 2018-02-06 15:31:41 +0800 |
commit | e368947988e064462d0e2465610074a1c36b6c3f (patch) | |
tree | dbaff8642f801b54e5333db17eec2e2534d8f287 /gvnfmadapter | |
parent | 9910f563a5c74072ce03e65e699b7bd49e16af62 (diff) |
Add auto swagger code for instantiate_vnf
Change-Id: Ied422c9d0b33cecfe6ca63238fc8247141d7156d
Issue-ID: VFC-737
Signed-off-by: Olivia2047 <zhan.jie1@zte.com.cn>
Diffstat (limited to 'gvnfmadapter')
-rw-r--r-- | gvnfmadapter/driver/interfaces/serializers.py | 57 | ||||
-rw-r--r-- | gvnfmadapter/driver/interfaces/views.py | 25 | ||||
-rw-r--r-- | gvnfmadapter/driver/settings.py | 26 | ||||
-rw-r--r-- | gvnfmadapter/driver/swagger/urls.py | 32 | ||||
-rw-r--r-- | gvnfmadapter/requirements.txt | 11 |
5 files changed, 142 insertions, 9 deletions
diff --git a/gvnfmadapter/driver/interfaces/serializers.py b/gvnfmadapter/driver/interfaces/serializers.py new file mode 100644 index 0000000..4c0916a --- /dev/null +++ b/gvnfmadapter/driver/interfaces/serializers.py @@ -0,0 +1,57 @@ +# Copyright 2018 ZTE Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from rest_framework import serializers + + +class AdditionalParams(serializers.Serializer): + sdncontroller = serializers.CharField(help_text="sdncontroller", required=False) + NatIpRange = serializers.CharField(help_text="NatIpRange", required=False) + m6000_mng_ip = serializers.CharField(help_text="m6000_mng_ip", required=False) + externalPluginManageNetworkName = serializers.CharField(help_text="externalPluginManageNetworkName", required=False) + location = serializers.CharField(help_text="location", required=False) + externalManageNetworkName = serializers.CharField(help_text="externalManageNetworkName", required=False) + sfc_data_network = serializers.CharField(help_text="sfc_data_network", required=False) + externalDataNetworkName = serializers.CharField(help_text="externalDataNetworkName", required=False) + inputs = serializers.DictField( + help_text="inputs", + child=serializers.CharField(help_text="but i needed to test these 2 fields somehow", allow_blank=True), + required=False, + allow_null=True + ) + + +class VnfRequestParamsSerializer(serializers.Serializer): + vnfDescriptorId = serializers.CharField( + help_text="Identifier that identifies the VNFD which defines the VNF instance to be created.", + required=True + ) + vnfInstanceName = serializers.CharField( + help_text="Human-readable name of the VNF instance to be created.", required=True) + vnfInstanceDescription = serializers.CharField( + help_text="Human-readable description of the VNF instance to be created.", required=False) + additionalParam = AdditionalParams( + help_text="Additional input parameters for the instantiation process," + " specific to the VNF being instantiated.", + required=True + ) + + +class ResponseSerializer(serializers.Serializer): + vnfInstanceId = serializers.CharField(help_text="VNF instance identifier", required=True) + jobId = serializers.CharField(help_text="Job ID", required=True) + + +class ErrorSerializer(serializers.Serializer): + error = serializers.CharField(help_text="error message", required=True) diff --git a/gvnfmadapter/driver/interfaces/views.py b/gvnfmadapter/driver/interfaces/views.py index de7d496..609d6ba 100644 --- a/gvnfmadapter/driver/interfaces/views.py +++ b/gvnfmadapter/driver/interfaces/views.py @@ -18,6 +18,7 @@ import logging import time import traceback +from drf_yasg.utils import swagger_auto_schema from rest_framework import status from rest_framework.decorators import api_view from rest_framework.response import Response @@ -25,19 +26,31 @@ from rest_framework.response import Response from driver.pub.exceptions import GvnfmDriverException from driver.pub.utils import restcall from driver.pub.utils.restcall import req_by_msb +from driver.interfaces.serializers import VnfRequestParamsSerializer, ResponseSerializer, ErrorSerializer logger = logging.getLogger(__name__) +@swagger_auto_schema(method='post', + request_body=VnfRequestParamsSerializer(), + responses={ + status.HTTP_201_CREATED: ResponseSerializer(), + status.HTTP_500_INTERNAL_SERVER_ERROR: ErrorSerializer()}) @api_view(http_method_names=['POST']) def instantiate_vnf(request, *args, **kwargs): try: logger.debug("instantiate_vnf--post::> %s" % request.data) logger.debug("Create vnf begin!") + requestSerializer = VnfRequestParamsSerializer(data=request.data) + request_isValid = requestSerializer.is_valid() + if not request_isValid: + raise Exception(requestSerializer.errors) + + requestData = requestSerializer.data input_data = { - "vnfdId": ignorcase_get(request.data, "vnfDescriptorId"), - "vnfInstanceName": ignorcase_get(request.data, "vnfInstanceName"), - "vnfInstanceDescription": ignorcase_get(request.data, "vnfInstanceDescription") + "vnfdId": ignorcase_get(requestData, "vnfDescriptorId"), + "vnfInstanceName": ignorcase_get(requestData, "vnfInstanceName"), + "vnfInstanceDescription": ignorcase_get(requestData, "vnfInstanceDescription") } vnfm_id = ignorcase_get(kwargs, "vnfmid") logger.debug("do_createvnf: request data=[%s],input_data=[%s],vnfm_id=[%s]", request.data, input_data, vnfm_id) @@ -48,9 +61,9 @@ def instantiate_vnf(request, *args, **kwargs): logger.debug("Instantiate vnf start!") vnfInstanceId = resp["vnfInstanceId"] input_data = { - "flavourId": ignorcase_get(request.data, "flavourId"), - "extVirtualLinks": ignorcase_get(request.data, "extVirtualLink"), - "additionalParams": ignorcase_get(request.data, "additionalParam") + "flavourId": ignorcase_get(requestData, "flavourId"), + "extVirtualLinks": ignorcase_get(requestData, "extVirtualLink"), + "additionalParams": ignorcase_get(requestData, "additionalParam") } logger.debug("do_instvnf: vnfInstanceId=[%s],request data=[%s],input_data=[%s],vnfm_id=[%s]", vnfInstanceId, request.data, input_data, vnfm_id) diff --git a/gvnfmadapter/driver/settings.py b/gvnfmadapter/driver/settings.py index bf49cf9..d1599e1 100644 --- a/gvnfmadapter/driver/settings.py +++ b/gvnfmadapter/driver/settings.py @@ -40,7 +40,8 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', - 'driver.interfaces' + 'driver.interfaces', + 'drf_yasg' ] MIDDLEWARE_CLASSES = [ @@ -68,6 +69,29 @@ REST_FRAMEWORK = { 'rest_framework.parsers.JSONParser') } +# drf-yasg +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +SWAGGER_SETTINGS = { + 'LOGIN_URL': '/admin/login', + 'LOGOUT_URL': '/admin/logout', + 'DEFAULT_INFO': 'driver.swagger.urls.swagger_info' +} + DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', diff --git a/gvnfmadapter/driver/swagger/urls.py b/gvnfmadapter/driver/swagger/urls.py index 5e8dec7..6c5a4e4 100644 --- a/gvnfmadapter/driver/swagger/urls.py +++ b/gvnfmadapter/driver/swagger/urls.py @@ -15,6 +15,38 @@ from django.conf.urls import url from driver.swagger import views +from drf_yasg import openapi +from drf_yasg.views import get_schema_view +from rest_framework import permissions + +swagger_info = openapi.Info( + title="vnfmgr API", + default_version='v1', + description=""" + +The `swagger-ui` view can be found [here](/api/gvnfmdriver/v1/cached/swagger). +The `ReDoc` view can be found [here](/api/gvnfmdriver/v1/redoc). +The swagger YAML document can be found [here](/api/gvnfmdriver/v1/cached/swagger.yaml).""" +) + +SchemaView = get_schema_view( + validators=['ssv', 'flex'], + public=True, + permission_classes=(permissions.AllowAny,), +) + urlpatterns = [ url(r'^api/gvnfmdriver/v1/swagger.json$', views.SwaggerView.as_view()), + url(r'^api/gvnfmdriver/v1/swagger(?P<format>.json|.yaml)$', SchemaView.without_ui(cache_timeout=0), + name='schema-json'), + url(r'^api/gvnfmdriver/v1/swagger/$', SchemaView.with_ui('swagger', cache_timeout=0), + name='schema-swagger-ui'), + url(r'^api/gvnfmdriver/v1/redoc/$', SchemaView.with_ui('redoc', cache_timeout=0), name='schema-redoc'), + url(r'^api/gvnfmdriver/v1/cached/swagger(?P<format>.json|.yaml)$', SchemaView.without_ui(cache_timeout=None), + name='cschema-json'), + url(r'^api/gvnfmdriver/v1/cached/swagger/$', SchemaView.with_ui('swagger', cache_timeout=None), + name='cschema-swagger-ui'), + url(r'^api/gvnfmdriver/v1/cached/redoc/$', SchemaView.with_ui('redoc', cache_timeout=None), + name='cschema-redoc'), + ] diff --git a/gvnfmadapter/requirements.txt b/gvnfmadapter/requirements.txt index 49f0e42..6458169 100644 --- a/gvnfmadapter/requirements.txt +++ b/gvnfmadapter/requirements.txt @@ -1,6 +1,6 @@ # rest framework -Django==1.9.6 -djangorestframework==3.3.3 +Django==1.11.9 +djangorestframework==3.7.7 # for call rest api httplib2==0.9.2 @@ -9,3 +9,10 @@ httplib2==0.9.2 coverage==4.2 mock==2.0.0 unittest_xml_reporting==1.12.0 + +# for auto swagger +drf-yasg>=1.2.2 + +# for the validation feature +flex>=6.11.1 +swagger-spec-validator>=2.1.0 |