diff options
author | maopeng zhang <zhang.maopeng1@zte.com.cn> | 2019-07-10 03:39:09 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-07-10 03:39:09 +0000 |
commit | 8211cd1235dd4526069265b47419b9fc100682f7 (patch) | |
tree | 05ff8e2264c831df6aa8bd6dba12ed9ee1135132 | |
parent | bc7bd1eb46a3d1e7bb70b0faaac728ab7f9d4f62 (diff) | |
parent | 1151093cdbfb543075a20460e5e076a0f89a87c0 (diff) |
Merge "Refactor codes for manual scale ns"
-rw-r--r-- | lcm/ns/tests/test_ns_manual_scale.py | 4 | ||||
-rw-r--r-- | lcm/ns/views/deprecated/scale_ns_views.py | 49 |
2 files changed, 28 insertions, 25 deletions
diff --git a/lcm/ns/tests/test_ns_manual_scale.py b/lcm/ns/tests/test_ns_manual_scale.py index 4c31611b..e4c3c71f 100644 --- a/lcm/ns/tests/test_ns_manual_scale.py +++ b/lcm/ns/tests/test_ns_manual_scale.py @@ -131,7 +131,7 @@ class TestNsManualScale(TestCase): mock_start.side_effect = NSLCMException("NS scale failed.") response = self.client.post("/api/nslcm/v1/ns/%s/scale" % self.ns_inst_id, data={}, format='json') self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) - self.assertIn("error", response.data) + self.assertIn("detail", response.data) @mock.patch.object(NSManualScaleService, 'start') def test_ns_manual_scale_when_ns_not_exist(self, mock_start): @@ -139,4 +139,4 @@ class TestNsManualScale(TestCase): scale_ns_json = SCALE_NS_DICT.copy() response = self.client.post("/api/nslcm/v1/ns/11/scale", data=scale_ns_json, format='json') self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) - self.assertIn("error", response.data) + self.assertIn("detail", response.data) diff --git a/lcm/ns/views/deprecated/scale_ns_views.py b/lcm/ns/views/deprecated/scale_ns_views.py index ed51e861..a260ce85 100644 --- a/lcm/ns/views/deprecated/scale_ns_views.py +++ b/lcm/ns/views/deprecated/scale_ns_views.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import logging -import traceback + from drf_yasg.utils import swagger_auto_schema from rest_framework import status from rest_framework.response import Response @@ -24,6 +24,7 @@ from lcm.ns.serializers.deprecated.ns_serializers import _ManualScaleNsReqSerial from lcm.pub.exceptions import NSLCMException from lcm.pub.utils.jobutil import JobUtil from lcm.jobs.enum import JOB_TYPE, JOB_ACTION +from .common import view_safe_call_with_log logger = logging.getLogger(__name__) @@ -36,27 +37,29 @@ class NSManualScaleView(APIView): status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error" } ) + @view_safe_call_with_log(logger=logger) def post(self, request, ns_instance_id): logger.debug("Enter NSManualScaleView::post %s, %s", request.data, ns_instance_id) - job_id = JobUtil.create_job(JOB_TYPE.NS, JOB_ACTION.MANUAL_SCALE, ns_instance_id) - try: - req_serializer = _ManualScaleNsReqSerializer(data=request.data) - if not req_serializer.is_valid(): - raise NSLCMException(req_serializer.errors) - req = request.data - scale_data = {} - scale_data['scaleType'] = req['scaleType'] - scaleNsData = req['scaleNsData'][0] - scale_data['scaleNsData'] = {"scaleNsByStepsData": scaleNsData['scaleNsByStepsData'][0]} - NSManualScaleService(ns_instance_id, scale_data, job_id).start() - - resp_serializer = _NsOperateJobSerializer(data={'jobId': job_id}) - if not resp_serializer.is_valid(): - raise NSLCMException(resp_serializer.errors) - - return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED) - except Exception as e: - logger.error(traceback.format_exc()) - JobUtil.add_job_status(job_id, 255, 'NS scale failed: %s' % e.args[0]) - return Response(data={'error': 'NS scale failed: %s' % ns_instance_id}, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) + req_serializer = _ManualScaleNsReqSerializer(data=request.data) + if not req_serializer.is_valid(): + raise NSLCMException(req_serializer.errors) + + req = request.data + scale_data = {} + scale_data['scaleType'] = req['scaleType'] + scaleNsData = req['scaleNsData'][0] + scale_data['scaleNsData'] = { + "scaleNsByStepsData": scaleNsData['scaleNsByStepsData'][0] + } + job_id = JobUtil.create_job( + JOB_TYPE.NS, + JOB_ACTION.MANUAL_SCALE, + ns_instance_id + ) + NSManualScaleService(ns_instance_id, scale_data, job_id).start() + + resp_serializer = _NsOperateJobSerializer(data={'jobId': job_id}) + if not resp_serializer.is_valid(): + raise NSLCMException(resp_serializer.errors) + + return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED) |