3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
11 Resources to handle openstack related requests
18 from flask import jsonify
19 from flasgger.utils import swag_from
22 from functest.api.base import ApiResource
23 from functest.api.common import api_utils
24 from functest.ci import run_tests
25 from functest.cli.commands.cli_os import OpenStack
26 from functest.utils.constants import CONST
28 LOGGER = logging.getLogger(__name__)
30 ADDRESS = socket.gethostbyname(socket.gethostname())
31 ENDPOINT_CREDS = ('http://{}:5000/api/v1/functest/openstack'.format(ADDRESS))
34 class V1Creds(ApiResource):
35 """ V1Creds Resource class"""
38 pkg_resources.resource_filename('functest', 'api/swagger/creds.yaml'),
39 endpoint='{0}/credentials'.format(ENDPOINT_CREDS))
40 def get(self): # pylint: disable=no-self-use
41 """ Get credentials """
42 run_tests.Runner.source_envfile(CONST.__getattribute__('env_file'))
43 credentials_show = OpenStack.show_credentials()
44 return jsonify(credentials_show)
47 pkg_resources.resource_filename('functest',
48 'api/swagger/creds_action.yaml'),
49 endpoint='{0}/action'.format(ENDPOINT_CREDS))
51 """ Used to handle post request """
52 return self._dispatch_post()
54 def update_openrc(self, args): # pylint: disable=no-self-use
55 """ Used to update the OpenStack RC file """
57 openrc_vars = args['openrc']
59 return api_utils.result_handler(
60 status=0, data='openrc must be provided')
62 if not isinstance(openrc_vars, collections.Mapping):
63 return api_utils.result_handler(
64 status=0, data='args should be a dict')
66 lines = ['export {}={}\n'.format(k, v) for k, v in openrc_vars.items()]
68 rc_file = CONST.__getattribute__('env_file')
69 with open(rc_file, 'w') as creds_file:
70 creds_file.writelines(lines)
72 LOGGER.info("Sourcing the OpenStack RC file...")
74 run_tests.Runner.source_envfile(rc_file)
75 except Exception as err: # pylint: disable=broad-except
76 LOGGER.exception('Failed to source the OpenStack RC file')
77 return api_utils.result_handler(status=0, data=str(err))
79 return api_utils.result_handler(
80 status=0, data='Update openrc successfully')