summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzhang ab <zhanganbing@chinamobile.com>2018-03-23 08:35:35 +0000
committerGerrit Code Review <gerrit@onap.org>2018-03-23 08:35:35 +0000
commit8044056b750cd275af2f21ae368dcaf18a6ed226 (patch)
tree5389bbf1f59a3a55891a0e3facb5a26e296192b9
parentca332a45025aec920020e88f6c59af02ddd6d91c (diff)
parent5d459650895007d8062369f2d9d4d4734832cdef (diff)
Merge "image upload service"
-rw-r--r--vio/images/empty.txt0
-rw-r--r--vio/vio/pub/vim/drivers/vimsdk/image_v2.py7
-rw-r--r--vio/vio/pub/vim/vimapi/glance/OperateImage.py8
-rw-r--r--vio/vio/swagger/urls.py4
-rw-r--r--vio/vio/swagger/views/image/views.py50
5 files changed, 69 insertions, 0 deletions
diff --git a/vio/images/empty.txt b/vio/images/empty.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/vio/images/empty.txt
diff --git a/vio/vio/pub/vim/drivers/vimsdk/image_v2.py b/vio/vio/pub/vim/drivers/vimsdk/image_v2.py
index f185e35..9eacdff 100644
--- a/vio/vio/pub/vim/drivers/vimsdk/image_v2.py
+++ b/vio/vio/pub/vim/drivers/vimsdk/image_v2.py
@@ -65,3 +65,10 @@ class GlanceClient(base.DriverBase):
def upload_image(self, data, image):
image.data = data
image.upload(self.session)
+
+ @sdk.translate_exception
+ def create_image_file(self, file_name, image_type):
+
+ img = self._proxy._create(_image.Image, disk_format=image_type,
+ container_format='bare', name=file_name)
+ return img
diff --git a/vio/vio/pub/vim/vimapi/glance/OperateImage.py b/vio/vio/pub/vim/vimapi/glance/OperateImage.py
index 35deb4c..55c900f 100644
--- a/vio/vio/pub/vim/vimapi/glance/OperateImage.py
+++ b/vio/vio/pub/vim/vimapi/glance/OperateImage.py
@@ -102,3 +102,11 @@ class OperateImage(baseclient):
except Exception:
pass
return image
+
+ def create_vim_image_file(self, vimid, tenantid, file_name,
+ file_dest, image_type):
+
+ image = self.glance(self.param).create_image_file(file_name,
+ image_type)
+ self.glance(self.param).upload_image(open(file_dest), image)
+ return image
diff --git a/vio/vio/swagger/urls.py b/vio/vio/swagger/urls.py
index 2dcae22..41db785 100644
--- a/vio/vio/swagger/urls.py
+++ b/vio/vio/swagger/urls.py
@@ -20,6 +20,7 @@ from vio.swagger.views.swagger_json import SwaggerJsonView
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.volume.views import CreateListVolumeView
from vio.swagger.views.volume.views import GetDeleteVolumeView
from vio.swagger.views.server.views import ListServersView, GetServerView
@@ -99,6 +100,9 @@ urlpatterns = [
r'(?P<tenantid>[0-9a-zA-Z_-]+)/images/(?P<imageid>[0-9a-zA-Z_-]+)$',
GetDeleteImageView.as_view()),
url(r'^api/multicloud-vio/v0/(?P<vimid>[0-9a-zA-Z_-]+)/'
+ 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_-]+)/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 26914cf..87b2f99 100644
--- a/vio/vio/swagger/views/image/views.py
+++ b/vio/vio/swagger/views/image/views.py
@@ -11,6 +11,9 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import json
+import random
+import string
+import sys
from rest_framework import status
from rest_framework.response import Response
@@ -135,3 +138,50 @@ class CreateListImagesView(APIView):
else:
return Response(data={'error': str(e)},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+
+class CreateImageFileView(APIView):
+
+ def post(self, request, vimid, tenantid):
+
+ 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)
+
+ image_instance = OperateImage.OperateImage(vim_info)
+
+ 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('.'):]
+
+ 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:]
+
+ try:
+ image_instance.create_vim_image_file(vimid, tenantid,
+ file_name[:
+ file_name.rfind(
+ '.')],
+ file_dest,
+ image_type)
+
+ return Response(data={'status': 'upload OK'},
+ status=status.HTTP_201_CREATED)
+ 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)