From 4bc3b756f0eefad98aeacf32865eda85a61fb157 Mon Sep 17 00:00:00 2001 From: rexlee8776 Date: Wed, 9 May 2018 09:52:27 +0000 Subject: Bugfix: openrc api dump should be safe_dump 1. fix safe_dump 2. fix pep8 problem, use flask_restapi custom error handling [1] [1] https://flask-restful.readthedocs.io/en/latest/extending.html#custom-error-handlers JIRA: YARDSTICK-1165 RestApi openrc dump clouds.yaml error it now use yarml.dump, should be yaml.safe_dump. dump would gererate !!python/unicode and cause error when upload openrc file in gui Change-Id: Id3e85f7ba7d4967277ef79109b07d7552179e5db Signed-off-by: rexlee8776 (cherry picked from commit f1a71b2889da3b49358b10b9a12ea5a512dd2743) --- api/resources/v2/openrcs.py | 11 ++++++----- api/server.py | 4 +++- yardstick/common/constants.py | 15 +++++++++++++++ yardstick/common/exceptions.py | 12 ++++++++++++ 4 files changed, 36 insertions(+), 6 deletions(-) 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/server.py b/api/server.py index 37a1ab6a6..914fe8457 100644 --- a/api/server.py +++ b/api/server.py @@ -39,11 +39,13 @@ app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024 * 1024 Swagger(app) -api = Api(app) +api = Api(app, errors=consts.API_ERRORS) @app.teardown_request def shutdown_session(exception=None): + if exception: + LOG.warning(exception.message) db_session.remove() diff --git a/yardstick/common/constants.py b/yardstick/common/constants.py index 153bd4bf4..3f5c37456 100644 --- a/yardstick/common/constants.py +++ b/yardstick/common/constants.py @@ -145,6 +145,21 @@ BASE_URL = 'http://localhost:5000' ENV_ACTION_API = BASE_URL + '/yardstick/env/action' ASYNC_TASK_API = BASE_URL + '/yardstick/asynctask' +API_ERRORS = { + 'UploadOpenrcError': { + 'message': "Upload openrc ERROR!", + 'status': API_ERROR, + }, + 'UpdateOpenrcError': { + 'message': "Update openrc ERROR!", + 'status': API_ERROR, + }, + 'ApiServerError': { + 'message': "An unkown exception happened to Api Server!", + 'status': API_ERROR, + }, +} + # flags IS_EXISTING = 'is_existing' IS_PUBLIC = 'is_public' diff --git a/yardstick/common/exceptions.py b/yardstick/common/exceptions.py index 439b9cb1b..fb4d33a59 100644 --- a/yardstick/common/exceptions.py +++ b/yardstick/common/exceptions.py @@ -166,3 +166,15 @@ class ScenarioCreateFloatingIPError(YardstickException): class ScenarioDeleteFloatingIPError(YardstickException): message = 'Delete Neutron Floating IP Scenario failed' + + +class ApiServerError(YardstickException): + message = 'An unkown exception happened to Api Server!' + + +class UploadOpenrcError(ApiServerError): + message = 'Upload openrc ERROR!' + + +class UpdateOpenrcError(ApiServerError): + message = 'Update openrc ERROR!' -- cgit 1.2.3-korg