diff options
Diffstat (limited to 'api/resources')
-rw-r--r-- | api/resources/v1/env.py | 20 | ||||
-rw-r--r-- | api/resources/v2/environments.py | 35 | ||||
-rw-r--r-- | api/resources/v2/tasks.py | 24 |
3 files changed, 67 insertions, 12 deletions
diff --git a/api/resources/v1/env.py b/api/resources/v1/env.py index 7c831fd74..75c981a96 100644 --- a/api/resources/v1/env.py +++ b/api/resources/v1/env.py @@ -22,6 +22,8 @@ import collections from six.moves import configparser from oslo_serialization import jsonutils from docker import Client +from docker.errors import APIError +from requests.exceptions import HTTPError from api.database.v1.handlers import AsyncTaskHandler from api.utils import influx @@ -44,7 +46,7 @@ class V1Env(ApiResource): def post(self): return self._dispatch_post() - def create_grafana(self, args): + def create_grafana(self, *args): task_id = str(uuid.uuid4()) thread = threading.Thread(target=self._create_grafana, args=(task_id,)) @@ -82,7 +84,7 @@ class V1Env(ApiResource): self._update_task_status(task_id) LOG.info('Finished') - except Exception as e: + except (APIError, HTTPError) as e: self._update_task_error(task_id, str(e)) LOG.exception('Create grafana failed') @@ -117,7 +119,7 @@ class V1Env(ApiResource): "isDefault": True, } try: - HttpClient().post(url, data, timeout=10) + HttpClient().post(url, data, timeout=60) except Exception: LOG.exception('Create datasources failed') raise @@ -145,7 +147,7 @@ class V1Env(ApiResource): return any(t in a['RepoTags'][0] for a in client.images() if a['RepoTags']) - def create_influxdb(self, args): + def create_influxdb(self, *args): task_id = str(uuid.uuid4()) thread = threading.Thread(target=self._create_influxdb, args=(task_id,)) @@ -185,7 +187,7 @@ class V1Env(ApiResource): self._update_task_status(task_id) LOG.info('Finished') - except Exception as e: + except APIError as e: self._update_task_error(task_id, str(e)) LOG.exception('Creating influxdb failed') @@ -217,7 +219,7 @@ class V1Env(ApiResource): consts.INFLUXDB_DB_NAME) client.create_database(consts.INFLUXDB_DB_NAME) LOG.info('Success to config influxDB') - except Exception: + except HTTPError: LOG.exception('Config influxdb failed') def _change_output_to_influxdb(self, ip): @@ -236,7 +238,7 @@ class V1Env(ApiResource): with open(consts.CONF_FILE, 'w') as f: parser.write(f) - def prepare_env(self, args): + def prepare_env(self, *args): task_id = str(uuid.uuid4()) thread = threading.Thread(target=self._prepare_env_daemon, @@ -287,7 +289,7 @@ class V1Env(ApiResource): self._update_task_status(task_id) LOG.info('Finished') - except Exception as e: + except (subprocess.CalledProcessError, OSError) as e: self._update_task_error(task_id, str(e)) LOG.exception('Prepare env failed') @@ -373,7 +375,7 @@ class V1Env(ApiResource): LOG.info('Source openrc: Sourcing') try: self._source_file(consts.OPENRC) - except Exception as e: + except subprocess.CalledProcessError as e: LOG.exception('Failed to source openrc') return result_handler(consts.API_ERROR, str(e)) LOG.info('Source openrc: Done') 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/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}) |