aboutsummaryrefslogtreecommitdiffstats
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/resources/v2/images.py69
-rw-r--r--api/resources/v2/openrcs.py11
-rw-r--r--api/server.py4
-rw-r--r--api/utils/influx.py41
4 files changed, 56 insertions, 69 deletions
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/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/api/utils/influx.py b/api/utils/influx.py
index 9bc6e9abe..8f3604745 100644
--- a/api/utils/influx.py
+++ b/api/utils/influx.py
@@ -1,53 +1,60 @@
##############################################################################
# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
+# Copyright (c) 2019 Intel Corporation
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
-from __future__ import absolute_import
import logging
-import six.moves.configparser as ConfigParser
-from six.moves.urllib.parse import urlsplit
-from influxdb import InfluxDBClient
+from six.moves import configparser as ConfigParser
+# NOTE(ralonsoh): pylint E0401 import error
+# https://github.com/PyCQA/pylint/issues/1640
+from six.moves.urllib.parse import urlsplit # pylint: disable=relative-import
+from influxdb import client as influxdb_client
from yardstick.common import constants as consts
+from yardstick.common import exceptions
+from yardstick import dispatcher
-logger = logging.getLogger(__name__)
+logger = logging.getLogger(__name__)
-def get_data_db_client():
+def get_data_db_client(db=None):
parser = ConfigParser.ConfigParser()
try:
parser.read(consts.CONF_FILE)
-
- if parser.get('DEFAULT', 'dispatcher') != 'influxdb':
- raise RuntimeError
-
- return _get_client(parser)
+ return _get_influxdb_client(parser, db)
except ConfigParser.NoOptionError:
- logger.error('can not find the key')
+ logger.error('Can not find the key')
raise
+def _get_influxdb_client(parser, db=None):
+ if dispatcher.INFLUXDB not in parser.get('DEFAULT', 'dispatcher'):
+ raise exceptions.InfluxDBConfigurationMissing()
-def _get_client(parser):
ip = _get_ip(parser.get('dispatcher_influxdb', 'target'))
user = parser.get('dispatcher_influxdb', 'username')
password = parser.get('dispatcher_influxdb', 'password')
- db_name = parser.get('dispatcher_influxdb', 'db_name')
- return InfluxDBClient(ip, consts.INFLUXDB_PORT, user, password, db_name)
+ if db is None:
+ db_name = parser.get('dispatcher_influxdb', 'db_name')
+ else:
+ db_name = db
+
+ return influxdb_client.InfluxDBClient(ip, consts.INFLUXDB_PORT, user,
+ password, db_name)
def _get_ip(url):
return urlsplit(url).hostname
-def query(query_sql):
+def query(query_sql, db=None):
try:
- client = get_data_db_client()
+ client = get_data_db_client(db)
logger.debug('Start to query: %s', query_sql)
return list(client.query(query_sql).get_points())
except RuntimeError: