diff options
author | Linda Wang <wangwulin@huawei.com> | 2017-08-21 14:36:49 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2017-08-21 14:36:49 +0000 |
commit | 9515e36a056ad7f16f39564ae2eeca955054997d (patch) | |
tree | 82baae98430e2744e6a11eff7d1dcad65dab3b25 /functest/api | |
parent | 2cfa22a0d0aa86cee5d77707c2f06ef3c4434a14 (diff) | |
parent | 6407e77988ea97b995b095af225660cd38c5b590 (diff) |
Merge "Create API to update openrc"
Diffstat (limited to 'functest/api')
-rw-r--r-- | functest/api/resources/v1/creds.py | 38 | ||||
-rw-r--r-- | functest/api/urls.py | 4 |
2 files changed, 42 insertions, 0 deletions
diff --git a/functest/api/resources/v1/creds.py b/functest/api/resources/v1/creds.py index e402d7e3..45e4559f 100644 --- a/functest/api/resources/v1/creds.py +++ b/functest/api/resources/v1/creds.py @@ -11,13 +11,19 @@ Resources to handle openstack related requests """ +import collections +import logging + from flask import jsonify from functest.api.base import ApiResource +from functest.api.common import api_utils from functest.cli.commands.cli_os import OpenStack from functest.utils import openstack_utils as os_utils from functest.utils.constants import CONST +LOGGER = logging.getLogger(__name__) + class V1Creds(ApiResource): """ V1Creds Resource class""" @@ -27,3 +33,35 @@ class V1Creds(ApiResource): os_utils.source_credentials(CONST.__getattribute__('openstack_creds')) credentials_show = OpenStack.show_credentials() return jsonify(credentials_show) + + def post(self): + """ Used to handle post request """ + return self._dispatch_post() + + def update_openrc(self, args): # pylint: disable=no-self-use + """ Used to update the OpenStack RC file """ + try: + openrc_vars = args['openrc'] + except KeyError: + return api_utils.result_handler( + status=0, data='openrc must be provided') + else: + if not isinstance(openrc_vars, collections.Mapping): + return api_utils.result_handler( + status=0, data='args should be a dict') + + lines = ['export {}={}\n'.format(k, v) for k, v in openrc_vars.items()] + + rc_file = CONST.__getattribute__('openstack_creds') + with open(rc_file, 'w') as creds_file: + creds_file.writelines(lines) + + LOGGER.info("Sourcing the OpenStack RC file...") + try: + os_utils.source_credentials(rc_file) + except Exception as err: # pylint: disable=broad-except + LOGGER.exception('Failed to source the OpenStack RC file') + return api_utils.result_handler(status=0, data=str(err)) + + return api_utils.result_handler( + status=0, data='Update openrc successfully') diff --git a/functest/api/urls.py b/functest/api/urls.py index 160a3c1d..f7bcae38 100644 --- a/functest/api/urls.py +++ b/functest/api/urls.py @@ -32,6 +32,10 @@ URLPATTERNS = [ # GET /api/v1/functest/openstack/credentials => GET credentials Url('/api/v1/functest/openstack/credentials', 'v1_creds'), + # POST /api/v1/functest/openstack/action + # {"action":"update_openrc", "args": {"openrc": {}}} => Update openrc + Url('/api/v1/functest/openstack/action', 'v1_creds'), + # GET /api/v1/functest/testcases => GET all testcases Url('/api/v1/functest/testcases', 'v1_test_cases'), |