diff options
-rw-r--r-- | lcm/pub/msapi/activiti.py | 12 | ||||
-rw-r--r-- | lcm/pub/utils/restcall.py | 12 | ||||
-rw-r--r-- | lcm/workflows/tests.py | 14 | ||||
-rw-r--r-- | lcm/workflows/views.py | 28 |
4 files changed, 52 insertions, 14 deletions
diff --git a/lcm/pub/msapi/activiti.py b/lcm/pub/msapi/activiti.py index 69320d1c..b5ff96f5 100644 --- a/lcm/pub/msapi/activiti.py +++ b/lcm/pub/msapi/activiti.py @@ -14,7 +14,7 @@ import json from lcm.pub.exceptions import NSLCMException -from lcm.pub.utils.restcall import req_by_msb +from lcm.pub.utils import restcall """ Input: @@ -29,14 +29,16 @@ Output: "processId": "string" } """ -def deploy_workflow(content): - content_str = json.JSONEncoder().encode(content) - ret = req_by_msb("/api/workflow/v1/package", "POST", content_str) +def deploy_workflow(file_path): + file_name = file_path.split("/")[-1] + file_data = { + 'file': open(file_path, 'rb'), + 'filename': file_name} + ret = restcall.upload_by_msb("api/workflow/v1/package", "POST", file_data) if ret[0] != 0: raise NSLCMException("Status code is %s, detail is %s.", ret[2], ret[1]) return json.JSONDecoder().decode(ret[1]) - """ Input: None diff --git a/lcm/pub/utils/restcall.py b/lcm/pub/utils/restcall.py index bfb02d6b..0ef20561 100644 --- a/lcm/pub/utils/restcall.py +++ b/lcm/pub/utils/restcall.py @@ -81,6 +81,18 @@ def req_by_msb(resource, method, content=''): base_url = "http://%s:%s/" % (MSB_SERVICE_IP, MSB_SERVICE_PORT) return call_req(base_url, "", "", rest_no_auth, resource, method, content) +def upload_by_msb(resource, method, file_data={}): + headers = {'Content-Type': 'application/octet-stream'} + full_url = "http://%s:%s/%s" % (MSB_SERVICE_IP, MSB_SERVICE_PORT, resource) + http = httplib2.Http() + resp, resp_content = http.request(full_url, + method=method.upper(), body=file_data, headers=headers) + resp_status, resp_body = resp['status'], resp_content.decode('UTF-8') + if resp_status not in status_ok_list: + logger.error("Status code is %s, detail is %s.", resp_status, resp_body) + return [1, "Failed to upload file.", resp_status] + logger.debug("resp_body=%s", resp_body) + return [0, resp_body, resp_status] def combine_url(base_url, resource): full_url = None diff --git a/lcm/workflows/tests.py b/lcm/workflows/tests.py index 31651624..564472e8 100644 --- a/lcm/workflows/tests.py +++ b/lcm/workflows/tests.py @@ -14,9 +14,12 @@ import unittest import json +import mock +import os from django.test import Client from rest_framework import status +from lcm.pub.utils import restcall class WorkflowViewTest(unittest.TestCase): def setUp(self): @@ -25,7 +28,14 @@ class WorkflowViewTest(unittest.TestCase): def tearDown(self): pass - def test_deploy_workflow(self): + @mock.patch.object(restcall, 'upload_by_msb') + def test_deploy_workflow(self, mock_upload_by_msb): + mock_upload_by_msb.return_value = [0, json.JSONEncoder().encode({ + "status": "1", + "message": "2", + "deployedId": "3", + "processId": "4" + }), '202'] response = self.client.post("/api/nslcm/v1/workflow", - {"filePath": "/home/init.zip"}, format='json') + {"filePath": os.path.abspath(__file__)}, format='json') self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content) diff --git a/lcm/workflows/views.py b/lcm/workflows/views.py index 92f029fb..221cc5ff 100644 --- a/lcm/workflows/views.py +++ b/lcm/workflows/views.py @@ -14,14 +14,16 @@ import logging import traceback +import sys from rest_framework import status from rest_framework.decorators import api_view from rest_framework.response import Response -from lcm.pub.database import models +from lcm.pub.database.models import WFPlanModel from lcm.pub.utils.syscomm import fun_name from lcm.pub.utils.values import ignore_case_get +from lcm.pub.msapi import activiti logger = logging.getLogger(__name__) @@ -30,16 +32,28 @@ logger = logging.getLogger(__name__) @api_view(http_method_names=['POST']) def deploy_workflow(request, *args, **kwargs): logger.debug("Enter %s", fun_name()) - file_path = ignore_case_get(request.data, "filePath") - logger.debug("file_path is %s", file_path) - ret = None try: - ret = [0, "TODO"] + file_path = ignore_case_get(request.data, "filePath") + force_deploy = ignore_case_get(request.data, "forceDeploy") + logger.debug("file_path is %s, force_deploy is %s", file_path, force_deploy) + if force_deploy.upper() == "TRUE": + WFPlanModel.objects.filter().delete() + else: + if WFPlanModel.objects.filter(): + logger.warn("Already deployed.") + return Response(data={'msg': 'Already deployed.'}, status=status.HTTP_202_ACCEPTED) + deploy_info = activiti.deploy_workflow(file_path) + WFPlanModel( + deployed_id=deploy_info["deployedId"], + process_id=deploy_info["processId"], + status=deploy_info["status"], + message=deploy_info["message"], + plan_name="ns_instantiate").save() except: logger.error(traceback.format_exc()) return Response(data={'error': str(sys.exc_info())}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - logger.debug("Leave %s, Return value is %s", fun_name(), ret) - return Response(data=ret[1], status=status.HTTP_202_ACCEPTED) + logger.debug("Leave %s", fun_name()) + return Response(data={'msg': 'OK'}, status=status.HTTP_202_ACCEPTED) |