summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorchenjiankun <chenjiankun1@huawei.com>2017-07-13 09:39:20 +0000
committerchenjiankun <chenjiankun1@huawei.com>2017-07-14 04:22:03 +0000
commit1a83219677fd0ce48f59e09f5a787d8296ba9261 (patch)
tree5651b4d9f6a2ae2a901a49b9e49a0eed879ff371
parente1d830c41c0f67b631b5b83f295f9dd1d6f6f0f1 (diff)
Add API(v2) to upload openrc
JIRA: YARDSTICK-719 API: /api/v2/yardstick/environments/openrcs/action METHOD: POST PARAMS: { 'action': 'upload_openrc', 'args': { 'file': file, 'environment_id': environment_id } } Change-Id: If367904a2d0c2d4a192fdc87a8da21ac6549269e Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
-rw-r--r--api/__init__.py1
-rw-r--r--api/resources/v2/openrcs.py118
-rw-r--r--api/urls.py2
-rw-r--r--yardstick/common/constants.py2
4 files changed, 123 insertions, 0 deletions
diff --git a/api/__init__.py b/api/__init__.py
index 4622802df..3195c971b 100644
--- a/api/__init__.py
+++ b/api/__init__.py
@@ -34,6 +34,7 @@ class ApiResource(Resource):
except KeyError:
pass
+ args.update({k: v for k, v in request.form.items()})
LOG.debug('Input args is: action: %s, args: %s', action, args)
return action, args
diff --git a/api/resources/v2/openrcs.py b/api/resources/v2/openrcs.py
new file mode 100644
index 000000000..b78c696df
--- /dev/null
+++ b/api/resources/v2/openrcs.py
@@ -0,0 +1,118 @@
+import uuid
+import logging
+import re
+import os
+
+import yaml
+from oslo_serialization import jsonutils
+
+from api import ApiResource
+from api.database.v2.handlers import V2OpenrcHandler
+from api.database.v2.handlers import V2EnvironmentHandler
+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
+
+LOG = logging.getLogger(__name__)
+LOG.setLevel(logging.DEBUG)
+
+
+class V2Openrc(ApiResource):
+
+ def post(self):
+ return self._dispatch_post()
+
+ def upload_openrc(self, args):
+ try:
+ upload_file = args['file']
+ except KeyError:
+ return result_handler(consts.API_ERROR, 'file must be provided')
+
+ try:
+ environment_id = args['environment_id']
+ except KeyError:
+ return result_handler(consts.API_ERROR, 'environment_id must be provided')
+
+ LOG.info('writing openrc: %s', consts.OPENRC)
+ makedirs(consts.CONF_DIR)
+ upload_file.save(consts.OPENRC)
+ source_env(consts.OPENRC)
+
+ LOG.info('parsing openrc')
+ try:
+ openrc_data = self._get_openrc_dict()
+ except Exception:
+ LOG.exception('parse openrc failed')
+ return result_handler(consts.API_ERROR, 'parse openrc failed')
+
+ openrc_id = str(uuid.uuid4())
+ self._write_into_database(environment_id, openrc_id, openrc_data)
+
+ LOG.info('writing ansible cloud conf')
+ try:
+ 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')
+ LOG.info('finish writing ansible cloud conf')
+
+ return result_handler(consts.API_SUCCESS, {'openrc': openrc_data, 'uuid': openrc_id})
+
+ def _write_into_database(self, environment_id, openrc_id, openrc_data):
+ LOG.info('writing openrc to database')
+ openrc_handler = V2OpenrcHandler()
+ openrc_init_data = {
+ 'uuid': openrc_id,
+ 'environment_id': environment_id,
+ 'content': jsonutils.dumps(openrc_data)
+ }
+ openrc_handler.insert(openrc_init_data)
+
+ LOG.info('binding openrc to environment: %s', environment_id)
+ environment_handler = V2EnvironmentHandler()
+ environment_handler.update_attr(environment_id, {'openrc_id': openrc_id})
+
+ def _get_openrc_dict(self):
+ with open(consts.OPENRC) as f:
+ content = f.readlines()
+
+ result = {}
+ for line in content:
+ m = re.search(r'(\ .*)=(.*)', line)
+ if m:
+ try:
+ value = os.environ[m.group(1).strip()]
+ except KeyError:
+ pass
+ else:
+ result.update({m.group(1).strip(): value})
+
+ return result
+
+ def _generate_ansible_conf_file(self, openrc_data):
+ ansible_conf = {
+ 'clouds': {
+ 'opnfv': {
+ 'auth': {
+ }
+ }
+ }
+ }
+ black_list = ['OS_IDENTITY_API_VERSION', 'OS_IMAGE_API_VERSION']
+
+ for k, v in openrc_data.items():
+ if k.startswith('OS') and k not in black_list:
+ key = k[3:].lower()
+ ansible_conf['clouds']['opnfv']['auth'][key] = v
+
+ try:
+ value = openrc_data['OS_IDENTITY_API_VERSION']
+ except KeyError:
+ pass
+ else:
+ ansible_conf['clouds']['opnfv']['identity_api_version'] = value
+
+ makedirs(consts.OPENSTACK_CONF_DIR)
+ with open(consts.CLOUDS_CONF, 'w') as f:
+ yaml.dump(ansible_conf, f, default_flow_style=False)
diff --git a/api/urls.py b/api/urls.py
index 70d69f881..efb7f8945 100644
--- a/api/urls.py
+++ b/api/urls.py
@@ -25,4 +25,6 @@ urlpatterns = [
Url('/api/v2/yardstick/environments', 'v2_environments'),
Url('/api/v2/yardstick/environments/action', 'v2_environments'),
Url('/api/v2/yardstick/environments/<environment_id>', 'v2_environment'),
+
+ Url('/api/v2/yardstick/environments/openrcs/action', 'v2_openrc'),
]
diff --git a/yardstick/common/constants.py b/yardstick/common/constants.py
index d71975af4..5cd34ba53 100644
--- a/yardstick/common/constants.py
+++ b/yardstick/common/constants.py
@@ -48,12 +48,14 @@ SAMPLE_CASE_DIR = join(REPOS_DIR, 'samples')
TESTCASE_DIR = join(YARDSTICK_ROOT_PATH, 'tests/opnfv/test_cases/')
TESTSUITE_DIR = join(YARDSTICK_ROOT_PATH, 'tests/opnfv/test_suites/')
DOCS_DIR = join(REPOS_DIR, 'docs/testing/user/userguide/')
+OPENSTACK_CONF_DIR = '/etc/openstack'
# file
OPENRC = get_param('file.openrc', '/etc/yardstick/openstack.creds')
ETC_HOSTS = get_param('file.etc_hosts', '/etc/hosts')
CONF_FILE = join(CONF_DIR, 'yardstick.conf')
POD_FILE = join(CONF_DIR, 'pod.yaml')
+CLOUDS_CONF = join(OPENSTACK_CONF_DIR, 'clouds.yml')
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)