diff options
author | fengyuanxing <feng.yuanxing@zte.com.cn> | 2018-01-24 09:30:17 +0800 |
---|---|---|
committer | fengyuanxing <feng.yuanxing@zte.com.cn> | 2018-01-24 13:37:03 +0800 |
commit | b90ab808e40f2b8feedffafea563bc65867b7de8 (patch) | |
tree | 5b8cb810daa16689464eb46d9cd9eacc517f13e8 | |
parent | 3c86cfe7354fe55ba98d35757b5ba0fa59e70d53 (diff) |
Modify code of jobs for swagger
Change-Id: I7eae1478f692e52280850d5d85383b33c9fcff6b
Issue-ID: VFC-677
Signed-off-by: fengyuanxing <feng.yuanxing@zte.com.cn>
-rw-r--r-- | catalog/jobs/views.py | 20 | ||||
-rw-r--r-- | catalog/serializers.py | 151 | ||||
-rw-r--r-- | catalog/urls.py | 40 |
3 files changed, 211 insertions, 0 deletions
diff --git a/catalog/jobs/views.py b/catalog/jobs/views.py index febcfcec..20278e4a 100644 --- a/catalog/jobs/views.py +++ b/catalog/jobs/views.py @@ -14,22 +14,42 @@ import logging import traceback +from drf_yasg import openapi +from drf_yasg.utils import swagger_auto_schema from rest_framework.response import Response from rest_framework.views import APIView from catalog.jobs.job_get import GetJobInfoService from catalog.pub.utils.jobutil import JobUtil from catalog.pub.utils.values import ignore_case_get +from catalog.serializers import JobResponseSerializer +from catalog.serializers import PostJobResponseSerializer logger = logging.getLogger(__name__) class JobView(APIView): + + input_job_id = openapi.Parameter('job_id', openapi.IN_QUERY, description="job id", type=openapi.TYPE_STRING) + input_response_id = openapi.Parameter('responseId', openapi.IN_QUERY, description="response id", type=openapi.TYPE_STRING) + + @swagger_auto_schema( + operation_description="Get job status", + manual_parameters=[input_job_id, input_response_id], + responses={200: JobResponseSerializer()}) def get(self, request, job_id): response_id = ignore_case_get(request.META, 'responseId') ret = GetJobInfoService(job_id, response_id).do_biz() return Response(data=ret) + @swagger_auto_schema( + operation_description="Update job status", + manual_parameters=[input_job_id, input_response_id], + responses={ + 200: PostJobResponseSerializer(), + 500: PostJobResponseSerializer() + } + ) def post(self, request, job_id): try: logger.debug("Enter JobView:post, %s, %s ", job_id, request.data) diff --git a/catalog/serializers.py b/catalog/serializers.py new file mode 100644 index 00000000..53a4d83f --- /dev/null +++ b/catalog/serializers.py @@ -0,0 +1,151 @@ +# Copyright 2017 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 + +from catalog.pub.utils.toscaparser.nsdmodel import EtsiNsdInfoModel +from catalog.pub.utils.toscaparser.vnfdmodel import EtsiVnfdInfoModel + + +class JobResponseSerializer(serializers.Serializer): + progress = serializers.CharField(help_text="progress", required=False) + desc = serializers.CharField(help_text="desc", required=False) + errcode = serializers.CharField(help_text="errcode", required=False) + + +class JobResponseHistoryListSerializer(serializers.Serializer): + status = serializers.CharField(help_text="status", required=False) + progress = serializers.CharField(help_text="progress", required=False) + statusDescription = serializers.CharField( + help_text="statusDescription", required=False) + errorCode = serializers.CharField(help_text="errcode", required=False) + responseId = serializers.CharField(help_text="responseId", required=False) + + +class JobResponseDescriptorSerializer(serializers.Serializer): + status = serializers.CharField(help_text="status", required=False) + progress = serializers.CharField(help_text="progress", required=False) + statusDescription = serializers.CharField( + help_text="statusDescription", required=False) + errorCode = serializers.CharField(help_text="errcode", required=False) + responseId = serializers.CharField(help_text="responseId", required=False) + responseHistoryList = JobResponseHistoryListSerializer( + many=True, help_text="responseHistoryList", required=False) + + +class JobRequestSerializer(serializers.Serializer): + jobId = serializers.CharField( + help_text="this field is generated from a query_serializer", + required=False) + responseDescriptor = JobResponseDescriptorSerializer( + help_text="this one too!", required=False) + + +class PostJobResponseResultSerializer(serializers.Serializer): + result = serializers.CharField(help_text="result", required=True) + msg = serializers.CharField(help_text="msg", required=False) + + +class NsPackageDistributeRequestSerializer(serializers.Serializer): + csarId = serializers.CharField(help_text="csarId", required=True) + + +class NsPackageInfoSerializer(serializers.Serializer): + nsdId = serializers.CharField(help_text="csarId", required=True) + nsPackageId = serializers.CharField(help_text="csarId", required=True) + nsdProvider = serializers.CharField(help_text="csarId", required=True) + nsdVersion = serializers.CharField(help_text="csarId", required=True) + csarName = serializers.CharField(help_text="csarId", required=True) + nsdModel = serializers.CharField(help_text="csarId", required=True) + downloadUrl = serializers.CharField(help_text="csarId", required=True) + + +class NsPackageSerializer(serializers.Serializer): + csarId = serializers.CharField(help_text="csarId", required=True) + package_info = NsPackageInfoSerializer( + help_text="package_info", required=True) + + +class NsPackagesSerializer(serializers.ListSerializer): + child = NsPackageSerializer(many=True) + + +class NfPackageDistributeRequestSerializer(serializers.Serializer): + csar_id = serializers.CharField(help_text="csarId", required=True) + vim_ids = serializers.ListField( + help_text="vim_ids", + child=serializers.CharField(), + required=False) + lab_vim_id = serializers.CharField(help_text="lab_vim_id", required=False) + + +class NfPackageInfoSerializer(serializers.Serializer): + vnfdId = serializers.CharField(required=True) + vnfPackageId = serializers.CharField(required=True) + vnfdProvider = serializers.CharField(required=True) + vnfdVersion = serializers.CharField(required=True) + vnfVersion = serializers.CharField(required=True) + csarName = serializers.CharField(required=True) + vnfdModel = serializers.CharField(required=True) + downloadUrl = serializers.CharField(required=True) + + +class NfImageInfoSerializer(serializers.Serializer): + index = serializers.CharField(required=True) + fileName = serializers.CharField(required=True) + imageId = serializers.CharField(required=True) + vimId = serializers.CharField(required=True) + vimUser = serializers.CharField(required=True) + tenant = serializers.CharField(required=True) + status = serializers.CharField(required=True) + + +class NfPackageSerializer(serializers.Serializer): + csarId = serializers.CharField(help_text="csarId", required=True) + packageInfo = NfPackageInfoSerializer( + help_text="packageInfo", required=True) + imageInfo = NfImageInfoSerializer(help_text="imageInfo", required=False) + + +class NfPackagesSerializer(serializers.ListSerializer): + child = NfPackageSerializer(many=True) + + +class PostJobResponseSerializer(serializers.Serializer): + jobId = serializers.CharField(help_text="jobId", required=True) + + +class ParseModelRequestSerializer(serializers.Serializer): + csarId = serializers.CharField(help_text="csarId", required=True) + inputs = serializers.JSONField(help_text="inputs", required=False) + + +class EtsiNsdInfoModelSerializer(serializers.ModelSerializer): + + class Meta: + model = EtsiNsdInfoModel + + +class EtsiVnfdInfoModelSerializer(serializers.ModelSerializer): + + class Meta: + model = EtsiVnfdInfoModel + + +class ParseNSPackageResponseSerializer(serializers.Serializer): + model = EtsiNsdInfoModelSerializer(help_text="model", required=True) + + +class ParseNfPackageResponseSerializer(serializers.Serializer): + model = EtsiVnfdInfoModelSerializer(help_text="model", required=True) diff --git a/catalog/urls.py b/catalog/urls.py index fbdf7e4b..0732f484 100644 --- a/catalog/urls.py +++ b/catalog/urls.py @@ -15,7 +15,47 @@ from django.conf.urls import include, url from catalog.pub.config.config import REG_TO_MSB_WHEN_START, REG_TO_MSB_REG_URL, REG_TO_MSB_REG_PARAM +import user_agents +from django.shortcuts import redirect +from rest_framework import permissions +from drf_yasg import openapi +from drf_yasg.views import get_schema_view + + +# Add code for generating swagger automatically. +swagger_info = openapi.Info( + title="Snippets API", + default_version='v1', + description="""This is a VFC project for the catalog.""" +) + +SchemaView = get_schema_view( + validators=['ssv', 'flex'], + public=True, + permission_classes=(permissions.AllowAny,), +) + + +def root_redirect(request): + user_agent_string = request.META.get('HTTP_USER_AGENT', '') + user_agent = user_agents.parse(user_agent_string) + if user_agent.is_mobile: + schema_view = 'cschema-redoc' + else: + schema_view = 'cschema-swagger-ui' + return redirect(schema_view, permanent=True) + + urlpatterns = [ + + # Add code for generating swagger automatically. + url(r'^swagger(?P<format>.json|.yaml)$', SchemaView.without_ui(cache_timeout=0), name='schema-json'), + url(r'^swagger/$', SchemaView.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), + url(r'^redoc/$', SchemaView.with_ui('redoc', cache_timeout=0), name='schema-redoc'), + url(r'^cached/swagger(?P<format>.json|.yaml)$', SchemaView.without_ui(cache_timeout=None), name='cschema-json'), + url(r'^cached/swagger/$', SchemaView.with_ui('swagger', cache_timeout=None), name='cschema-swagger-ui'), + url(r'^cached/redoc/$', SchemaView.with_ui('redoc', cache_timeout=None), name='cschema-redoc'), + url(r'^$', root_redirect), url(r'^', include('catalog.samples.urls')), url(r'^', include('catalog.packages.urls')), url(r'^', include('catalog.jobs.urls')), |