From 6407e77988ea97b995b095af225660cd38c5b590 Mon Sep 17 00:00:00 2001 From: Linda Wang Date: Fri, 18 Aug 2017 07:08:12 +0000 Subject: Create API to update openrc API: /api/v1/functest/openstack/action METHOD: POST PARAMS: { "action": "update_openrc", "args": { "openrc": { "OS_AUTH_URL": "http://192.168.30.222:5000/v3", "OS_IDENTITY_API_VERSION": "3", "OS_IMAGE_API_VERSION": "2", "OS_PASSWORD": "console", "OS_PROJECT_DOMAIN_NAME": "default", "OS_PROJECT_NAME": "admin", "OS_TENANT_NAME": "admin", "OS_USERNAME": "admin", "OS_USER_DOMAIN_NAME": "default" } } } JIRA: FUNCTEST-855 Change-Id: I7c935483c264f2b1b47239684392c8a37dc23d26 Signed-off-by: Linda Wang --- functest/api/base.py | 4 ++-- functest/api/common/api_utils.py | 10 ++++++++++ functest/api/common/error.py | 24 ------------------------ functest/api/resources/v1/creds.py | 38 ++++++++++++++++++++++++++++++++++++++ functest/api/urls.py | 4 ++++ 5 files changed, 54 insertions(+), 26 deletions(-) delete mode 100644 functest/api/common/error.py diff --git a/functest/api/base.py b/functest/api/base.py index efeab824..ffc56786 100644 --- a/functest/api/base.py +++ b/functest/api/base.py @@ -17,7 +17,7 @@ import logging from flask import request from flask_restful import Resource -from functest.api.common import api_utils, error +from functest.api.common import api_utils LOGGER = logging.getLogger(__name__) @@ -58,7 +58,7 @@ class ApiResource(Resource): try: return getattr(self, action)(args) except AttributeError: - error.result_handler(status=1, data='No such action') + api_utils.result_handler(status=1, data='No such action') # Import modules from package "functest.api.resources" diff --git a/functest/api/common/api_utils.py b/functest/api/common/api_utils.py index f518e777..d85acf92 100644 --- a/functest/api/common/api_utils.py +++ b/functest/api/common/api_utils.py @@ -18,6 +18,7 @@ import os import sys from oslo_utils import importutils +from flask import jsonify import six import functest @@ -89,3 +90,12 @@ def change_obj_to_dict(obj): for key, value in vars(obj).items(): dic.update({key: value}) return dic + + +def result_handler(status, data): + """ Return the json format of result in dict """ + result = { + 'status': status, + 'result': data + } + return jsonify(result) diff --git a/functest/api/common/error.py b/functest/api/common/error.py deleted file mode 100644 index d0045225..00000000 --- a/functest/api/common/error.py +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env python - -# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. -# -# 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 - -""" -Used to handle results - -""" - -from flask import jsonify - - -def result_handler(status, data): - """ Return the json format of result in dict """ - result = { - 'status': status, - 'result': data - } - return jsonify(result) 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 ca45b4be..40af98d6 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'), -- cgit 1.2.3-korg