diff options
author | Bin Sun <bins@vmware.com> | 2018-03-23 16:49:03 +0800 |
---|---|---|
committer | Bin Sun <bins@vmware.com> | 2018-03-23 16:58:31 +0800 |
commit | afef883e9373ffc7d278244c82b8a58c5262f13a (patch) | |
tree | 36913edf93a2fb080c3043321f45fd29cc9bcf9d /vio | |
parent | 5d459650895007d8062369f2d9d4d4734832cdef (diff) |
image download service
Change-Id: I6de9574ea035a2a6efb390fd32e5e448107bf99e
Issue-ID: MULTICLOUD-155
Signed-off-by: Bin Sun <bins@vmware.com>
Diffstat (limited to 'vio')
-rw-r--r-- | vio/vio/pub/vim/drivers/vimsdk/image_v2.py | 5 | ||||
-rw-r--r-- | vio/vio/pub/vim/vimapi/glance/OperateImage.py | 5 | ||||
-rw-r--r-- | vio/vio/swagger/urls.py | 5 | ||||
-rw-r--r-- | vio/vio/swagger/views/image/views.py | 45 |
4 files changed, 60 insertions, 0 deletions
diff --git a/vio/vio/pub/vim/drivers/vimsdk/image_v2.py b/vio/vio/pub/vim/drivers/vimsdk/image_v2.py index 9eacdff..a7034cf 100644 --- a/vio/vio/pub/vim/drivers/vimsdk/image_v2.py +++ b/vio/vio/pub/vim/drivers/vimsdk/image_v2.py @@ -72,3 +72,8 @@ class GlanceClient(base.DriverBase): img = self._proxy._create(_image.Image, disk_format=image_type, container_format='bare', name=file_name) return img + + @sdk.translate_exception + def download_image(self, image): + img = image.download(self.session, stream=True) + return img diff --git a/vio/vio/pub/vim/vimapi/glance/OperateImage.py b/vio/vio/pub/vim/vimapi/glance/OperateImage.py index 55c900f..b26a184 100644 --- a/vio/vio/pub/vim/vimapi/glance/OperateImage.py +++ b/vio/vio/pub/vim/vimapi/glance/OperateImage.py @@ -110,3 +110,8 @@ class OperateImage(baseclient): image_type) self.glance(self.param).upload_image(open(file_dest), image) return image + + def download_vim_image(self, image): + + image_data = self.glance(self.param).download_image(image) + return image_data diff --git a/vio/vio/swagger/urls.py b/vio/vio/swagger/urls.py index 41db785..3871599 100644 --- a/vio/vio/swagger/urls.py +++ b/vio/vio/swagger/urls.py @@ -21,6 +21,7 @@ from vio.swagger.views.tenant.views import ListTenantsView from vio.swagger.views.image.views import CreateListImagesView from vio.swagger.views.image.views import GetDeleteImageView from vio.swagger.views.image.views import CreateImageFileView +from vio.swagger.views.image.views import GetImageFileView from vio.swagger.views.volume.views import CreateListVolumeView from vio.swagger.views.volume.views import GetDeleteVolumeView from vio.swagger.views.server.views import ListServersView, GetServerView @@ -103,6 +104,10 @@ urlpatterns = [ r'(?P<tenantid>[0-9a-zA-Z_-]+)/images/file$', CreateImageFileView.as_view()), url(r'^api/multicloud-vio/v0/(?P<vimid>[0-9a-zA-Z_-]+)/' + r'(?P<tenantid>[0-9a-zA-Z_-]+)/images/file/' + r'(?P<imageid>[0-9a-zA-Z_-]+)$', + GetImageFileView.as_view()), + url(r'^api/multicloud-vio/v0/(?P<vimid>[0-9a-zA-Z_-]+)/' r'(?P<tenantid>[0-9a-zA-Z_-]+)/volumes$', CreateListVolumeView.as_view()), url(r'^api/multicloud-vio/v0/(?P<vimid>[0-9a-zA-Z_-]+)/' diff --git a/vio/vio/swagger/views/image/views.py b/vio/vio/swagger/views/image/views.py index 87b2f99..f77ed85 100644 --- a/vio/vio/swagger/views/image/views.py +++ b/vio/vio/swagger/views/image/views.py @@ -185,3 +185,48 @@ class CreateImageFileView(APIView): else: return Response(data={'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + +class GetImageFileView(APIView): + + def post(self, request, vimid, tenantid, imageid): + try: + vim_info = extsys.get_vim_by_id(vimid) + vim_info['tenant'] = tenantid + except VimDriverVioException as e: + return Response(data={'error': str(e)}, status=e.status_code) + + try: + req_body = json.loads(request.body) + except Exception as e: + return Response(data={'error': 'Fail to decode request body.'}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + image_instance = OperateImage.OperateImage(vim_info) + try: + image = image_instance.find_vim_image(imageid) + except Exception as e: + return Response(data={'error': 'the image does not exist'}, + status=status.HTTP_404_NOT_FOUND) + + try: + image_data = image_instance.download_vim_image(image) + + imagePath = req_body.get('imagePath') + if imagePath[-1:] is not '/': + imagePath += '/' + file_name = "%s%s.%s" % (imagePath, image.name, image.disk_format) + image_file = open(file_name, 'w+') + + for chunk in image_data: + image_file.write(chunk) + image_file.close() + + return Response(data={'status': 'donwload OK'}, + status=status.HTTP_200_OK) + + except Exception as e: + if hasattr(e, "http_status"): + return Response(data={'error': str(e)}, status=e.http_status) + else: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) |