From 52da36dd6ed776b1e836dbe4153f596f77fc3c0d Mon Sep 17 00:00:00 2001 From: fujinhua Date: Wed, 31 Jan 2018 14:38:05 +0800 Subject: [PATCH] Refactor get sfc swagger generate logic Change-Id: Ie93a29f031e6797d030547dd72f861a1879df73d Issue-ID: VFC-694 Signed-off-by: fujinhua --- lcm/ns/sfcs/detail_views.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/lcm/ns/sfcs/detail_views.py b/lcm/ns/sfcs/detail_views.py index b5646f5f..dfbed985 100644 --- a/lcm/ns/sfcs/detail_views.py +++ b/lcm/ns/sfcs/detail_views.py @@ -11,24 +11,46 @@ # 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 logging from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView +from drf_yasg.utils import swagger_auto_schema from lcm.ns.sfcs.delete_sfcs import DeleteSfcs from lcm.ns.sfcs.get_sfcs import GetSfcs +from lcm.ns.sfcs.serializers import GetSfcRespSerializer + +logger = logging.getLogger(__name__) class SfcDetailView(APIView): + @swagger_auto_schema( + request_body=None, + responses={ + status.HTTP_200_OK: GetSfcRespSerializer(), + status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error", + status.HTTP_404_NOT_FOUND: "SFC not found" + } + ) def get(self, request, sfc_inst_id): - print "SfcCreateView--get::> %s" % sfc_inst_id + logger.debug("SfcCreateView--get::> %s", sfc_inst_id) sfc_inst_info = GetSfcs(sfc_inst_id).do() if not sfc_inst_info: return Response(status=status.HTTP_404_NOT_FOUND) - return Response(status=status.HTTP_200_OK, data={'sfcInstId': sfc_inst_id, - 'sfcName': "xxx", - 'sfcStatus': sfc_inst_info[0].status}) + + resp_data = {'sfcInstId': sfc_inst_id, + 'sfcName': "xxx", + 'sfcStatus': sfc_inst_info[0].status} + + resp_serializer = GetSfcRespSerializer(data=resp_data) + if not resp_serializer.is_valid(): + logger.error(resp_serializer.errors) + return Response(data={'error': resp_serializer.errors}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + return Response(status=status.HTTP_200_OK, data=resp_serializer.data) def delete(self, request_paras, sfc_inst_id): resp = DeleteSfcs(sfc_inst_id).do() -- 2.16.6