aboutsummaryrefslogtreecommitdiffstats
path: root/api/resources/v2
diff options
context:
space:
mode:
Diffstat (limited to 'api/resources/v2')
-rw-r--r--api/resources/v2/environments.py35
-rw-r--r--api/resources/v2/images.py69
-rw-r--r--api/resources/v2/openrcs.py11
-rw-r--r--api/resources/v2/tasks.py24
4 files changed, 85 insertions, 54 deletions
diff --git a/api/resources/v2/environments.py b/api/resources/v2/environments.py
index 158e98be7..7e587be85 100644
--- a/api/resources/v2/environments.py
+++ b/api/resources/v2/environments.py
@@ -11,6 +11,7 @@ import logging
from oslo_serialization import jsonutils
from docker import Client
+from docker.errors import APIError
from api import ApiResource
from api.database.v2.handlers import V2EnvironmentHandler
@@ -20,6 +21,7 @@ from api.database.v2.handlers import V2ContainerHandler
from yardstick.common.utils import result_handler
from yardstick.common.utils import change_obj_to_dict
from yardstick.common import constants as consts
+from yardstick.service.environment import Environment
LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)
@@ -124,10 +126,41 @@ class V2Environment(ApiResource):
LOG.debug('container name: %s', container.name)
try:
client.remove_container(container.name, force=True)
- except Exception:
+ except APIError:
LOG.exception('remove container failed')
container_handler.delete_by_uuid(v)
environment_handler.delete_by_uuid(environment_id)
return result_handler(consts.API_SUCCESS, {'environment': environment_id})
+
+
+class V2SUT(ApiResource):
+
+ def get(self, environment_id):
+ try:
+ uuid.UUID(environment_id)
+ except ValueError:
+ return result_handler(consts.API_ERROR, 'invalid environment id')
+
+ environment_handler = V2EnvironmentHandler()
+ try:
+ environment = environment_handler.get_by_uuid(environment_id)
+ except ValueError:
+ return result_handler(consts.API_ERROR, 'no such environment id')
+
+ if not environment.pod_id:
+ return result_handler(consts.API_SUCCESS, {'sut': {}})
+
+ pod_handler = V2PodHandler()
+ try:
+ pod = pod_handler.get_by_uuid(environment.pod_id)
+ except ValueError:
+ return result_handler(consts.API_ERROR, 'no such pod id')
+ else:
+ pod_content = pod.content
+
+ env = Environment(pod=pod_content)
+ sut_info = env.get_sut_info()
+
+ return result_handler(consts.API_SUCCESS, {'sut': sut_info})
diff --git a/api/resources/v2/images.py b/api/resources/v2/images.py
index 0c36a0a26..c3e5ee73e 100644
--- a/api/resources/v2/images.py
+++ b/api/resources/v2/images.py
@@ -18,8 +18,7 @@ from api.database.v2.handlers import V2ImageHandler
from api.database.v2.handlers import V2EnvironmentHandler
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 openstack_utils
from yardstick.common.openstack_utils import get_glance_client
from yardstick.common import constants as consts
@@ -47,39 +46,21 @@ class V2Images(ApiResource):
def get(self):
try:
source_env(consts.OPENRC)
- except Exception:
+ except OSError:
return result_handler(consts.API_ERROR, 'source openrc error')
- nova_client = get_nova_client()
- try:
- images_list = nova_client.images.list()
- except Exception:
+ image_list = openstack_utils.list_images()
+
+ if image_list is False:
return result_handler(consts.API_ERROR, 'get images error')
- else:
- images = {i.name: self.get_info(change_obj_to_dict(i)) for i in images_list}
+
+ images = {i.name: format_image_info(i) for i in image_list}
return result_handler(consts.API_SUCCESS, {'status': 1, 'images': images})
def post(self):
return self._dispatch_post()
- def get_info(self, data):
- try:
- size = data['OS-EXT-IMG-SIZE:size']
- except KeyError:
- size = None
- else:
- size = float(size) / 1024 / 1024
-
- result = {
- 'name': data.get('name', ''),
- 'discription': data.get('description', ''),
- 'size': size,
- 'status': data.get('status'),
- 'time': data.get('updated')
- }
- return result
-
def load_image(self, args):
try:
image_name = args['name']
@@ -268,7 +249,7 @@ class V2Images(ApiResource):
r = requests.head(url)
try:
file_size = int(r.headers['content-length'])
- except Exception:
+ except (TypeError, ValueError):
return
with open(path, 'wb') as f:
@@ -303,14 +284,13 @@ class V2Image(ApiResource):
except ValueError:
return result_handler(consts.API_ERROR, 'no such image id')
- nova_client = get_nova_client()
- images = nova_client.images.list()
+ images = openstack_utils.list_images()
try:
image = next((i for i in images if i.name == image.name))
except StopIteration:
pass
- return_image = self.get_info(change_obj_to_dict(image))
+ return_image = format_image_info(image)
return_image['id'] = image_id
return result_handler(consts.API_SUCCESS, {'image': return_image})
@@ -349,19 +329,16 @@ class V2Image(ApiResource):
return result_handler(consts.API_SUCCESS, {'image': image_id})
- def get_info(self, data):
- try:
- size = data['OS-EXT-IMG-SIZE:size']
- except KeyError:
- size = None
- else:
- size = float(size) / 1024 / 1024
-
- result = {
- 'name': data.get('name', ''),
- 'description': data.get('description', ''),
- 'size': size,
- 'status': data.get('status'),
- 'time': data.get('updated')
- }
- return result
+
+def format_image_info(image):
+ image_dict = {}
+
+ if image is None:
+ return image_dict
+
+ image_dict['name'] = image.name
+ image_dict['size'] = float(image.size) / 1024 / 1024
+ image_dict['status'] = image.status.upper()
+ image_dict['time'] = image.updated_at
+
+ return image_dict
diff --git a/api/resources/v2/openrcs.py b/api/resources/v2/openrcs.py
index cb506d0e8..4706b856a 100644
--- a/api/resources/v2/openrcs.py
+++ b/api/resources/v2/openrcs.py
@@ -21,6 +21,7 @@ from yardstick.common import constants as consts
from yardstick.common.utils import result_handler
from yardstick.common.utils import makedirs
from yardstick.common.utils import source_env
+from yardstick.common import exceptions as y_exc
LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)
@@ -57,7 +58,7 @@ class V2Openrcs(ApiResource):
openrc_data = self._get_openrc_dict()
except Exception:
LOG.exception('parse openrc failed')
- return result_handler(consts.API_ERROR, 'parse openrc failed')
+ raise y_exc.UploadOpenrcError()
openrc_id = str(uuid.uuid4())
self._write_into_database(environment_id, openrc_id, openrc_data)
@@ -67,7 +68,7 @@ class V2Openrcs(ApiResource):
self._generate_ansible_conf_file(openrc_data)
except Exception:
LOG.exception('write cloud conf failed')
- return result_handler(consts.API_ERROR, 'genarate ansible conf failed')
+ raise y_exc.UploadOpenrcError()
LOG.info('finish writing ansible cloud conf')
return result_handler(consts.API_SUCCESS, {'openrc': openrc_data, 'uuid': openrc_id})
@@ -102,7 +103,7 @@ class V2Openrcs(ApiResource):
source_env(consts.OPENRC)
except Exception:
LOG.exception('source openrc failed')
- return result_handler(consts.API_ERROR, 'source openrc failed')
+ raise y_exc.UpdateOpenrcError()
LOG.info('source openrc: Done')
openrc_id = str(uuid.uuid4())
@@ -113,7 +114,7 @@ class V2Openrcs(ApiResource):
self._generate_ansible_conf_file(openrc_vars)
except Exception:
LOG.exception('write cloud conf failed')
- return result_handler(consts.API_ERROR, 'genarate ansible conf failed')
+ raise y_exc.UpdateOpenrcError()
LOG.info('finish writing ansible cloud conf')
return result_handler(consts.API_SUCCESS, {'openrc': openrc_vars, 'uuid': openrc_id})
@@ -174,7 +175,7 @@ class V2Openrcs(ApiResource):
makedirs(consts.OPENSTACK_CONF_DIR)
with open(consts.CLOUDS_CONF, 'w') as f:
- yaml.dump(ansible_conf, f, default_flow_style=False)
+ yaml.safe_dump(ansible_conf, f, default_flow_style=False)
class V2Openrc(ApiResource):
diff --git a/api/resources/v2/tasks.py b/api/resources/v2/tasks.py
index 25a9cf109..17241ed63 100644
--- a/api/resources/v2/tasks.py
+++ b/api/resources/v2/tasks.py
@@ -38,6 +38,8 @@ class V2Tasks(ApiResource):
for t in tasks:
result = t['result']
t['result'] = jsonutils.loads(result) if result else None
+ params = t['params']
+ t['params'] = jsonutils.loads(params) if params else None
return result_handler(consts.API_SUCCESS, {'tasks': tasks})
@@ -94,6 +96,9 @@ class V2Task(ApiResource):
result = task_info['result']
task_info['result'] = jsonutils.loads(result) if result else None
+ params = task_info['params']
+ task_info['params'] = jsonutils.loads(params) if params else None
+
return result_handler(consts.API_SUCCESS, {'task': task_info})
def delete(self, task_id):
@@ -127,7 +132,6 @@ class V2Task(ApiResource):
return result_handler(consts.API_SUCCESS, {'task': task_id})
def put(self, task_id):
-
try:
uuid.UUID(task_id)
except ValueError:
@@ -166,6 +170,21 @@ class V2Task(ApiResource):
return result_handler(consts.API_SUCCESS, {'uuid': task_id})
+ def add_params(self, args):
+ task_id = args['task_id']
+ try:
+ params = args['params']
+ except KeyError:
+ return result_handler(consts.API_ERROR, 'params must be provided')
+
+ LOG.info('update params info in task')
+
+ task_handler = V2TaskHandler()
+ task_update_data = {'params': jsonutils.dumps(params)}
+ task_handler.update_attr(task_id, task_update_data)
+
+ return result_handler(consts.API_SUCCESS, {'uuid': task_id})
+
def add_case(self, args):
task_id = args['task_id']
try:
@@ -243,7 +262,8 @@ class V2Task(ApiResource):
data = {
'inputfile': ['/tmp/{}.yaml'.format(task.case_name)],
- 'task_id': task_id
+ 'task_id': task_id,
+ 'task-args': task.params
}
if task.suite:
data.update({'suite': True})