From 68c2a5f10b7c1f5fc59579a5a3078c57c60a85a6 Mon Sep 17 00:00:00 2001 From: chenjiankun Date: Fri, 21 Jul 2017 02:30:24 +0000 Subject: Add API(v2) to get all images JIRA: YARDSTICK-752 API: /api/v2/yardstick/images METHOD: GET Change-Id: I7b0bba85d875575ee684cc42cad977af91d6f84e Signed-off-by: chenjiankun --- api/resources/v2/images.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'api') diff --git a/api/resources/v2/images.py b/api/resources/v2/images.py index dc996742f..701818493 100644 --- a/api/resources/v2/images.py +++ b/api/resources/v2/images.py @@ -5,6 +5,8 @@ import threading from api import ApiResource from yardstick.common.utils import result_handler from yardstick.common.utils import source_env +from yardstick.common.utils import change_obj_to_dict +from yardstick.common.openstack_utils import get_nova_client from yardstick.common import constants as consts LOG = logging.getLogger(__name__) @@ -13,9 +15,35 @@ LOG.setLevel(logging.DEBUG) class V2Images(ApiResource): + def get(self): + try: + source_env(consts.OPENRC) + except: + return result_handler(consts.API_ERROR, 'source openrc error') + + nova_client = get_nova_client() + try: + images_list = nova_client.images.list() + except: + return result_handler(consts.API_ERROR, 'get images error') + else: + images = [self.get_info(change_obj_to_dict(i)) for i in images_list] + status = 1 if all(i['status'] == 'ACTIVE' for i in images) else 0 + + return result_handler(consts.API_SUCCESS, {'status': status, 'images': images}) + def post(self): return self._dispatch_post() + def get_info(self, data): + result = { + 'name': data.get('name', ''), + 'size': data.get('OS-EXT-IMG-SIZE:size', ''), + 'status': data.get('status', ''), + 'time': data.get('updated', '') + } + return result + def load_image(self, args): thread = threading.Thread(target=self._load_images) thread.start() -- cgit 1.2.3-korg