diff options
author | Bin Sun <bins@vmware.com> | 2018-03-23 17:29:26 +0800 |
---|---|---|
committer | Bin Sun <bins@vmware.com> | 2018-03-23 17:30:41 +0800 |
commit | dab42d91cc09da3b4193ee32853e15fbaef7b61b (patch) | |
tree | fdf1922b7787e34972726aa74d66243a7ab07da2 /vio | |
parent | afef883e9373ffc7d278244c82b8a58c5262f13a (diff) |
image upload service will return detailed response
Change-Id: Ie4ecb51c3f1dd85acfacdaa75e66b54db3ea7666
Issue-ID: MULTICLOUD-155
Signed-off-by: Bin Sun <bins@vmware.com>
Diffstat (limited to 'vio')
-rw-r--r-- | vio/vio/swagger/views/image/views.py | 48 |
1 files changed, 28 insertions, 20 deletions
diff --git a/vio/vio/swagger/views/image/views.py b/vio/vio/swagger/views/image/views.py index f77ed85..52d14ef 100644 --- a/vio/vio/swagger/views/image/views.py +++ b/vio/vio/swagger/views/image/views.py @@ -150,34 +150,42 @@ class CreateImageFileView(APIView): except VimDriverVioException as e: return Response(data={'error': str(e)}, status=e.status_code) + vim_rsp = image_utils.vim_formatter(vim_info, tenantid) image_instance = OperateImage.OperateImage(vim_info) - image_file = request.FILES['file'] + try: + image_file = request.FILES['file'] + + random_name = ''.join(random.sample( + string.ascii_letters + + string.digits, 4)) + file_name = image_file.name[:image_file.name.rfind('.')] + + "_" + random_name + + image_file.name[image_file.name.find('.'):] - random_name = ''.join(random.sample( - string.ascii_letters - + string.digits, 4)) - file_name = image_file.name[:image_file.name.rfind('.')] - + "_" + random_name - + image_file.name[image_file.name.find('.'):] + file_dest = sys.path[0] + '/images/' + file_name + with open(file_dest, 'wb+') as temp_file: + for chunk in image_file.chunks(): + temp_file.write(chunk) + temp_file.close() - file_dest = sys.path[0] + '/images/' + file_name - with open(file_dest, 'wb+') as temp_file: - for chunk in image_file.chunks(): - temp_file.write(chunk) - temp_file.close() + image_type = image_file.name[image_file.name.find('.') + 1:] - image_type = image_file.name[image_file.name.find('.') + 1:] + except Exception as e: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) try: - image_instance.create_vim_image_file(vimid, tenantid, - file_name[: - file_name.rfind( - '.')], - file_dest, - image_type) + image = image_instance.create_vim_image_file( + vimid, tenantid, + file_name[:file_name.rfind('.')], + file_dest, + image_type) + rsp = image_utils.image_formatter(image) + rsp.update(vim_rsp) + rsp['returnCode'] = '1' - return Response(data={'status': 'upload OK'}, + return Response(data={'status': rsp}, status=status.HTTP_201_CREATED) except Exception as e: if hasattr(e, "http_status"): |