Add API to upload pod.yaml file 73/36373/5
authorchenjiankun <chenjiankun1@huawei.com>
Thu, 22 Jun 2017 13:00:40 +0000 (13:00 +0000)
committerchenjiankun <chenjiankun1@huawei.com>
Sat, 24 Jun 2017 06:25:50 +0000 (06:25 +0000)
JIRA: YARDSTICK-687

We need a API to upload a pod.yaml file to /etc/yardstick/pod.yaml.

API: /yardstick/env/action
method: POST
param:
{
    'action': 'upload_pod_file',
    'file': file object
}

Change-Id: I3d25df364da10aaf34f995e948e1704235a40f6f
Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
api/base.py
api/resources/env_action.py
yardstick/common/constants.py

index 5270085..6fa2777 100644 (file)
@@ -23,9 +23,16 @@ logger.setLevel(logging.DEBUG)
 class ApiResource(Resource):
 
     def _post_args(self):
-        params = common_utils.translate_to_str(request.json)
-        action = params.get('action', '')
+        data = request.json if request.json else {}
+        params = common_utils.translate_to_str(data)
+        action = params.get('action', request.form.get('action', ''))
         args = params.get('args', {})
+
+        try:
+            args['file'] = request.files['file']
+        except KeyError:
+            pass
+
         logger.debug('Input args is: action: %s, args: %s', action, args)
 
         return action, args
index 42bef85..3536559 100644 (file)
@@ -16,12 +16,12 @@ import threading
 import time
 import uuid
 import glob
+import yaml
 import collections
 
 from six.moves import configparser
 from oslo_serialization import jsonutils
 from docker import Client
-from docker.utils import create_host_config
 
 from api.database.handler import AsyncTaskHandler
 from api.utils import influx
@@ -343,3 +343,22 @@ def update_openrc(args):
     logger.info('Source openrc: Done')
 
     return result_handler(consts.API_SUCCESS, {'openrc': openrc_vars})
+
+
+def upload_pod_file(args):
+    try:
+        pod_file = args['file']
+    except KeyError:
+        return result_handler(consts.API_ERROR, 'file must be provided')
+
+    logger.info('Checking file')
+    data = yaml.load(pod_file.read())
+    if not isinstance(data, collections.Mapping):
+        return result_handler(consts.API_ERROR, 'invalid yaml file')
+
+    logger.info('Writing file')
+    with open(consts.POD_FILE, 'w') as f:
+        yaml.dump(data, f, default_flow_style=False)
+    logger.info('Writing finished')
+
+    return result_handler(consts.API_SUCCESS, {'pod_info': data})
index 4d65aff..47a5199 100644 (file)
@@ -43,6 +43,7 @@ TESTSUITE_DIR = join(YARDSTICK_ROOT_PATH, 'tests/opnfv/test_suites/')
 # file
 OPENRC = get_param('file.openrc', '/etc/yardstick/openstack.creds')
 CONF_FILE = join(CONF_DIR, 'yardstick.conf')
+POD_FILE = join(CONF_DIR, 'pod.yaml')
 CONF_SAMPLE_FILE = join(CONF_SAMPLE_DIR, 'yardstick.conf.sample')
 FETCH_SCRIPT = get_param('file.fetch_script', 'utils/fetch_os_creds.sh')
 FETCH_SCRIPT = join(RELENG_DIR, FETCH_SCRIPT)