From 03fda2d5ca97a7202b4e7953282b4156e763f183 Mon Sep 17 00:00:00 2001 From: "ying.yunlong" Date: Thu, 23 Nov 2017 15:12:00 +0800 Subject: [PATCH] Refactor vfc-vnflcm swagger query logic Change-Id: Iac0287fb53a6f5635f9bafe0152cb5e6d95cf162 Issue-ID: VFC-589 Signed-off-by: ying.yunlong (cherry picked from commit 8caba28ed1db2c346185b2e7549cb4eb8be0778e) --- lcm/lcm/nf/vnfs/tests/test_vnf_create.py | 4 ---- lcm/lcm/nf/vnfs/urls.py | 3 +-- lcm/lcm/nf/vnfs/views.py | 11 ----------- lcm/lcm/settings.py | 3 ++- lcm/lcm/swagger/__init__.py | 13 +++++++++++++ lcm/lcm/{nf/vnfs => swagger}/swagger.json | 0 lcm/lcm/swagger/tests.py | 30 ++++++++++++++++++++++++++++++ lcm/lcm/swagger/urls.py | 20 ++++++++++++++++++++ lcm/lcm/swagger/views.py | 26 ++++++++++++++++++++++++++ lcm/lcm/urls.py | 1 + 10 files changed, 93 insertions(+), 18 deletions(-) create mode 100644 lcm/lcm/swagger/__init__.py rename lcm/lcm/{nf/vnfs => swagger}/swagger.json (100%) create mode 100644 lcm/lcm/swagger/tests.py create mode 100644 lcm/lcm/swagger/urls.py create mode 100644 lcm/lcm/swagger/views.py diff --git a/lcm/lcm/nf/vnfs/tests/test_vnf_create.py b/lcm/lcm/nf/vnfs/tests/test_vnf_create.py index 3260c2db..537171e3 100644 --- a/lcm/lcm/nf/vnfs/tests/test_vnf_create.py +++ b/lcm/lcm/nf/vnfs/tests/test_vnf_create.py @@ -43,10 +43,6 @@ class TestNFInstantiate(TestCase): descp=job_detail) self.assertEqual(1, len(jobs)) - def test_swagger_ok(self): - response = self.client.get("/api/vnflcm/v1/swagger.json", format='json') - self.assertEqual(response.status_code, status.HTTP_200_OK) - @mock.patch.object(restcall, 'call_req') def test_create_vnf_identifier(self, mock_call_req): r1_get_csarid_by_vnfdid = [0, json.JSONEncoder().encode( diff --git a/lcm/lcm/nf/vnfs/urls.py b/lcm/lcm/nf/vnfs/urls.py index 5be0073e..7e88f30a 100644 --- a/lcm/lcm/nf/vnfs/urls.py +++ b/lcm/lcm/nf/vnfs/urls.py @@ -15,7 +15,7 @@ from django.conf.urls import patterns, url from rest_framework.urlpatterns import format_suffix_patterns -from lcm.nf.vnfs.views import InstantiateVnf, TerminateVnf, SwaggerJsonView, DeleteVnfAndQueryVnf, CreateVnfAndQueryVnfs +from lcm.nf.vnfs.views import InstantiateVnf, TerminateVnf, DeleteVnfAndQueryVnf, CreateVnfAndQueryVnfs urlpatterns = patterns('', url(r'^api/vnflcm/v1/vnf_instances$', CreateVnfAndQueryVnfs.as_view()), @@ -25,7 +25,6 @@ urlpatterns = patterns('', DeleteVnfAndQueryVnf.as_view()), url(r'^api/vnflcm/v1/vnf_instances/(?P[0-9a-zA-Z_-]+)/terminate$', TerminateVnf.as_view()), - url(r'^api/vnflcm/v1/swagger.json$', SwaggerJsonView.as_view()) ) urlpatterns = format_suffix_patterns(urlpatterns) diff --git a/lcm/lcm/nf/vnfs/views.py b/lcm/lcm/nf/vnfs/views.py index 1062d3a1..2330fa2c 100644 --- a/lcm/lcm/nf/vnfs/views.py +++ b/lcm/lcm/nf/vnfs/views.py @@ -12,9 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json import logging -import os import traceback from rest_framework import status @@ -119,12 +117,3 @@ class TerminateVnf(APIView): return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) rsp = {"jobId": job_id} return Response(data=rsp, status=status.HTTP_202_ACCEPTED) - - -class SwaggerJsonView(APIView): - def get(self, request): - json_file = os.path.join(os.path.dirname(__file__), 'swagger.json') - f = open(json_file) - json_data = json.JSONDecoder().decode(f.read()) - f.close() - return Response(json_data) diff --git a/lcm/lcm/settings.py b/lcm/lcm/settings.py index 5583c4cd..f4b7826b 100644 --- a/lcm/lcm/settings.py +++ b/lcm/lcm/settings.py @@ -44,7 +44,8 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', 'rest_framework', 'lcm.pub.database', - 'lcm.samples' + 'lcm.samples', + 'lcm.swagger' ] MIDDLEWARE_CLASSES = [ diff --git a/lcm/lcm/swagger/__init__.py b/lcm/lcm/swagger/__init__.py new file mode 100644 index 00000000..c7b6818e --- /dev/null +++ b/lcm/lcm/swagger/__init__.py @@ -0,0 +1,13 @@ +# 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. diff --git a/lcm/lcm/nf/vnfs/swagger.json b/lcm/lcm/swagger/swagger.json similarity index 100% rename from lcm/lcm/nf/vnfs/swagger.json rename to lcm/lcm/swagger/swagger.json diff --git a/lcm/lcm/swagger/tests.py b/lcm/lcm/swagger/tests.py new file mode 100644 index 00000000..f65f6990 --- /dev/null +++ b/lcm/lcm/swagger/tests.py @@ -0,0 +1,30 @@ +# 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. + +import unittest + +from django.test import Client +from rest_framework import status + + +class SwaggerViewTest(unittest.TestCase): + def setUp(self): + self.client = Client() + + def tearDown(self): + pass + + def test_swagger(self): + response = self.client.get("/api/vnflcm/v1/swagger.json") + self.assertEqual(status.HTTP_200_OK, response.status_code, response.content) diff --git a/lcm/lcm/swagger/urls.py b/lcm/lcm/swagger/urls.py new file mode 100644 index 00000000..6764a1d4 --- /dev/null +++ b/lcm/lcm/swagger/urls.py @@ -0,0 +1,20 @@ +# 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 django.conf.urls import url +from lcm.swagger import views + +urlpatterns = [ + url(r'^api/vnflcm/v1/swagger.json$', views.SwaggerView.as_view()) +] diff --git a/lcm/lcm/swagger/views.py b/lcm/lcm/swagger/views.py new file mode 100644 index 00000000..400d6dd1 --- /dev/null +++ b/lcm/lcm/swagger/views.py @@ -0,0 +1,26 @@ +# 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. +import os +import json +from rest_framework.views import APIView +from rest_framework.response import Response + + +class SwaggerView(APIView): + def get(self, request, format=None): + json_file = os.path.join(os.path.dirname(__file__), 'swagger.json') + f = open(json_file) + json_data = json.JSONDecoder().decode(f.read()) + f.close() + return Response(json_data) diff --git a/lcm/lcm/urls.py b/lcm/lcm/urls.py index e60108b8..a4ebda98 100644 --- a/lcm/lcm/urls.py +++ b/lcm/lcm/urls.py @@ -19,6 +19,7 @@ urlpatterns = [ url(r'^', include('lcm.samples.urls')), url(r'^', include('lcm.nf.vnfs.urls')), url(r'^', include('lcm.jobs.urls')), + url(r'^', include('lcm.swagger.urls')), ] # regist to MSB when startup -- 2.16.6