Bugfix: openrc api dump should be safe_dump 61/57261/4
authorrexlee8776 <limingjiang@huawei.com>
Wed, 9 May 2018 09:52:27 +0000 (09:52 +0000)
committerrexlee8776 <limingjiang@huawei.com>
Tue, 22 May 2018 08:01:54 +0000 (08:01 +0000)
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 <limingjiang@huawei.com>
api/resources/v2/openrcs.py
api/server.py
yardstick/common/constants.py
yardstick/common/exceptions.py

index cb506d0..4706b85 100644 (file)
@@ -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):
index 37a1ab6..914fe84 100644 (file)
@@ -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()
 
 
index 8640afb..f6e4ab7 100644 (file)
@@ -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'
index c7ba562..8a0c52d 100644 (file)
@@ -247,3 +247,15 @@ class ScenarioDeleteVolumeError(YardstickException):
 
 class ScenarioDetachVolumeError(YardstickException):
     message = 'Cinder Detach Volume 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!'