Leverage on Xtesting
[functest.git] / functest / api / resources / v1 / creds.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
4 #
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
9
10 """
11 Resources to handle openstack related requests
12 """
13
14 import collections
15 import logging
16 import socket
17
18 from flask import jsonify
19 from flasgger.utils import swag_from
20 import pkg_resources
21 from xtesting.ci import run_tests
22
23 from functest.api.base import ApiResource
24 from functest.api.common import api_utils
25 from functest.cli.commands.cli_os import OpenStack
26 from functest.utils import constants
27
28 LOGGER = logging.getLogger(__name__)
29
30 ADDRESS = socket.gethostbyname(socket.gethostname())
31 ENDPOINT_CREDS = ('http://{}:5000/api/v1/functest/openstack'.format(ADDRESS))
32
33
34 class V1Creds(ApiResource):
35     """ V1Creds Resource class"""
36
37     @swag_from(
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(constants.ENV_FILE)
43         credentials_show = OpenStack.show_credentials()
44         return jsonify(credentials_show)
45
46     @swag_from(
47         pkg_resources.resource_filename('functest',
48                                         'api/swagger/creds_action.yaml'),
49         endpoint='{0}/action'.format(ENDPOINT_CREDS))
50     def post(self):
51         """ Used to handle post request """
52         return self._dispatch_post()
53
54     def update_openrc(self, args):  # pylint: disable=no-self-use
55         """ Used to update the OpenStack RC file """
56         try:
57             openrc_vars = args['openrc']
58         except KeyError:
59             return api_utils.result_handler(
60                 status=0, data='openrc must be provided')
61         else:
62             if not isinstance(openrc_vars, collections.Mapping):
63                 return api_utils.result_handler(
64                     status=0, data='args should be a dict')
65
66         lines = ['export {}={}\n'.format(k, v) for k, v in openrc_vars.items()]
67
68         rc_file = constants.ENV_FILE
69         with open(rc_file, 'w') as creds_file:
70             creds_file.writelines(lines)
71
72         LOGGER.info("Sourcing the OpenStack RC file...")
73         try:
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))
78
79         return api_utils.result_handler(
80             status=0, data='Update openrc successfully')