diff options
author | 2018-01-30 14:10:55 +0800 | |
---|---|---|
committer | 2018-01-30 14:10:55 +0800 | |
commit | f30925c952e7a9e2dc5f81c0dfc3dbfdf2c812f5 (patch) | |
tree | f93d6da3607fcf7bbe7e2c3e6330e5d6bbe52fda | |
parent | 25b4a690e673d9155d4ef478f65a03c74e3cf3d1 (diff) |
Add vfc-vnflcm instantiation auto-swagger
Change-Id: I14a392be766496720b76b736314193e89f6b9e53
Issue-ID: VFC-682
Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
-rw-r--r-- | lcm/lcm/nf/vnfs/const.py | 5 | ||||
-rw-r--r-- | lcm/lcm/nf/vnfs/serializers.py | 45 | ||||
-rw-r--r-- | lcm/lcm/nf/vnfs/tests/test_vnf_create.py | 7 | ||||
-rw-r--r-- | lcm/lcm/nf/vnfs/views.py | 28 |
4 files changed, 57 insertions, 28 deletions
diff --git a/lcm/lcm/nf/vnfs/const.py b/lcm/lcm/nf/vnfs/const.py index 02edce7e..da338f67 100644 --- a/lcm/lcm/nf/vnfs/const.py +++ b/lcm/lcm/nf/vnfs/const.py @@ -66,7 +66,7 @@ inst_req_data = { "resourceSubnetId": '55_subnet', "cpdId": 'cpId1', "vim": { - "vimid": '55_vimid' + "vimId": '55_vimid' } } ], @@ -83,13 +83,14 @@ inst_req_data = { "resourceSubnetId": '55_subnet', "cpdId": 'cpId1', "vim": { - "vimid": '55_vimid' + "vimId": '55_vimid' } } ] } } + vnfd_model_dict = { 'metadata': { 'vendor': u'zte', diff --git a/lcm/lcm/nf/vnfs/serializers.py b/lcm/lcm/nf/vnfs/serializers.py index df9bd7e5..817d5619 100644 --- a/lcm/lcm/nf/vnfs/serializers.py +++ b/lcm/lcm/nf/vnfs/serializers.py @@ -56,11 +56,11 @@ class InterfaceInfoSerializer(serializers.Serializer): class vimInfoSerializer(serializers.Serializer): - vimInfoId = serializers.CharField(help_text="vimInfoId", required=True) - vimId = serializers.CharField(help_text="vimId", required=True) - interfaceEndpoint = serializers.CharField(help_text="interfaceEndpoint", required=True) - interfaceInfo = InterfaceInfoSerializer(help_text="vimInfoId", required=True) - accessInfo = accessInfoSerializer(help_text="accessInfo", required=True) + vimInfoId = serializers.CharField(help_text="vimInfoId", required=False, allow_null=True) + vimId = serializers.CharField(help_text="vimId", required=False, allow_null=True) + interfaceEndpoint = serializers.CharField(help_text="interfaceEndpoint", required=False, allow_null=True) + interfaceInfo = InterfaceInfoSerializer(help_text="vimInfoId", required=False, allow_null=True) + accessInfo = accessInfoSerializer(help_text="accessInfo", required=False, allow_null=True) class monitoringParametersSerializer(serializers.Serializer): @@ -96,7 +96,7 @@ class NetworkAddressSerializer(serializers.Serializer): class extCpInfoSerializer(serializers.Serializer): - cpInstanceId = serializers.CharField(help_text="cpInstanceId", required=True) + cpInstanceId = serializers.CharField(help_text="cpInstanceId", required=False, allow_null=True) cpdId = serializers.IntegerField(help_text="cpdId", required=True) numDynamicAddresses = serializers.IntegerField(help_text="numDynamicAddresses", required=False, allow_null=True) addresses = NetworkAddressSerializer(help_text="addresses", many=True, allow_null=True) @@ -135,25 +135,36 @@ class VnfsInfoSerializer(serializers.ListSerializer): child = VnfInfoSerializer() -class emptySerializer(serializers.Serializer): - pass +class extVirtualLinkSerizlizer(serializers.Serializer): + vlInstanceId = serializers.CharField(help_text="vlInstanceId", required=False, allow_null=True) + resourceSubnetId = serializers.CharField(help_text="resourceSubnetId", required=False, allow_null=True) + cpdId = serializers.CharField(help_text="cpdId", required=False, allow_null=True) + resourceId = serializers.CharField(help_text="resourceId", required=False, allow_null=True) + vim = vimInfoSerializer(help_text="vim", required=False, allow_null=True) + extCps = extCpInfoSerializer(help_text="extCps", many=True, required=False, allow_null=True) -class extVirtualLinksSerializer(serializers.Serializer): - vlInstanceId = serializers.CharField(help_text="vlInstanceId", required=True) - resourceId = serializers.CharField(help_text="resourceId", required=True) - vim = vimInfoSerializer(help_text="vim", required=True) - extCps = extCpInfoSerializer(help_text="extCps", many=True) +class extVirtualLinksSerializer(serializers.ListSerializer): + child = extVirtualLinkSerizlizer(help_text="extVirtualLink", required=False, allow_null=True) class additionalParamsSerializer(serializers.Serializer): - inputs = emptySerializer(help_text="inputs", required=True) - extVirtualLinks = extVirtualLinksSerializer(help_text="extVirtualLinks", many=True, allow_null=True) + 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 + ) + extVirtualLinks = extVirtualLinksSerializer(help_text="extVirtualLinks", required=False, allow_null=True) class InstantiateVnfRequestSerializer(serializers.Serializer): flavourId = serializers.CharField(help_text="flavourId", required=True) instantiationLevelId = serializers.CharField(help_text="instantiationLevelId", required=True, allow_null=True) localizationLanguage = serializers.CharField(help_text="localizationLanguage", required=True, allow_null=True) - extVirtualLinks = extVirtualLinksSerializer(help_text="extVirtualLinks", many=True, allow_null=True) - additionalParams = additionalParamsSerializer(help_text="additionalParams", required=True, allow_null=True) + extVirtualLinks = extVirtualLinksSerializer(help_text="extVirtualLinks", required=False, allow_null=True) + additionalParams = additionalParamsSerializer(help_text="additionalParams", required=False, allow_null=True) + + +class InstantiateVnfResponseSerializer(serializers.Serializer): + jobId = serializers.CharField(help_text="jobId", required=True) diff --git a/lcm/lcm/nf/vnfs/tests/test_vnf_create.py b/lcm/lcm/nf/vnfs/tests/test_vnf_create.py index 2c4a2ea9..f1592732 100644 --- a/lcm/lcm/nf/vnfs/tests/test_vnf_create.py +++ b/lcm/lcm/nf/vnfs/tests/test_vnf_create.py @@ -16,8 +16,9 @@ import json import uuid import mock -from django.test import TestCase, Client +from django.test import TestCase from rest_framework import status +from rest_framework.test import APIClient from lcm.nf.vnfs.const import c1_data_get_tenant_id, c4_data_create_network, c2_data_create_volume, \ c5_data_create_subnet, c3_data_get_volume, c6_data_create_port, c7_data_create_flavor, c8_data_list_image, \ @@ -32,7 +33,7 @@ from lcm.pub.vimapi import api class TestNFInstantiate(TestCase): def setUp(self): - self.client = Client() + self.client = APIClient() self.grant_result = { "vim": { "vimid": 'vimid_1', @@ -90,7 +91,7 @@ class TestNFInstantiate(TestCase): @mock.patch.object(InstVnf, 'run') def test_instantiate_vnf(self, mock_run): mock_run.re.return_value = None - response = self.client.post("/api/vnflcm/v1/vnf_instances/12/instantiate", data={}, format='json') + response = self.client.post("/api/vnflcm/v1/vnf_instances/12/instantiate", data=inst_req_data, format='json') self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code) def test_instantiate_vnf_when_inst_id_not_exist(self): diff --git a/lcm/lcm/nf/vnfs/views.py b/lcm/lcm/nf/vnfs/views.py index 171de9ce..c8f6b1b7 100644 --- a/lcm/lcm/nf/vnfs/views.py +++ b/lcm/lcm/nf/vnfs/views.py @@ -20,7 +20,8 @@ from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView -from lcm.nf.vnfs.serializers import CreateVnfReqSerializer, CreateVnfRespSerializer, VnfsInfoSerializer +from lcm.nf.vnfs.serializers import CreateVnfReqSerializer, CreateVnfRespSerializer, VnfsInfoSerializer, \ + InstantiateVnfResponseSerializer, InstantiateVnfRequestSerializer from lcm.nf.vnfs.vnf_cancel.delete_vnf_identifier import DeleteVnf from lcm.nf.vnfs.vnf_cancel.term_vnf import TermVnf from lcm.nf.vnfs.vnf_create.create_vnf_identifier import CreateVnf @@ -91,12 +92,31 @@ class CreateVnfAndQueryVnfs(APIView): class InstantiateVnf(APIView): + @swagger_auto_schema( + request_body=InstantiateVnfRequestSerializer(), + responses={ + status.HTTP_202_ACCEPTED: InstantiateVnfResponseSerializer(), + status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" + } + ) def post(self, request, instanceid): logger.debug("InstantiateVnf--post::> %s" % request.data) + instantiateVnfRequestSerializer = InstantiateVnfRequestSerializer(data=request.data) + req_isValid = instantiateVnfRequestSerializer.is_valid() try: + if not req_isValid: + raise NFLCMException(instantiateVnfRequestSerializer.errors) + job_id = JobUtil.create_job('NF', 'INSTANTIATE', instanceid) JobUtil.add_job_status(job_id, 0, "INST_VNF_READY") - InstVnf(request.data, instanceid, job_id).start() + InstVnf(instantiateVnfRequestSerializer.data, instanceid, job_id).start() + + instantiateVnfResponseSerializer = InstantiateVnfResponseSerializer(data={"jobId": job_id}) + resp_isValid = instantiateVnfResponseSerializer.is_valid() + if not resp_isValid: + raise NFLCMException(instantiateVnfResponseSerializer.errors) + + return Response(data=instantiateVnfResponseSerializer.data, status=status.HTTP_202_ACCEPTED) except NFLCMException as e: logger.error(e.message) return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) @@ -104,10 +124,6 @@ class InstantiateVnf(APIView): logger.error(e.message) logger.error(traceback.format_exc()) 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 DeleteVnfAndQueryVnf(APIView): |