summaryrefslogtreecommitdiffstats
path: root/.gitignore
blob: e7c25c17e33be0fd69a2a96edbad0924e1b83d61 (plain)
1
2
3
/docs_build/
/docs_output/
/releng/
Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
#!/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

"""
Resources to handle openstack related requests
"""

import collections
import logging
import socket

from flask import jsonify
from flasgger.utils import swag_from
import pkg_resources

from functest.api.base import ApiResource
from functest.api.common import api_utils
from functest.ci import run_tests
from functest.cli.commands.cli_os import OpenStack
from functest.utils.constants import CONST

LOGGER = logging.getLogger(__name__)

ADDRESS = socket.gethostbyname(socket.gethostname())
ENDPOINT_CREDS = ('http://{}:5000/api/v1/functest/openstack'.format(ADDRESS))


class V1Creds(ApiResource):
    """ V1Creds Resource class"""

    @swag_from(
        pkg_resources.resource_filename('functest', 'api/swagger/creds.yaml'),
        endpoint='{0}/credentials'.format(ENDPOINT_CREDS))
    def get(self):  # pylint: disable=no-self-use
        """ Get credentials """
        run_tests.Runner.source_envfile(getattr(CONST, 'env_file'))
        credentials_show = OpenStack.show_credentials()
        return jsonify(credentials_show)

    @swag_from(
        pkg_resources.resource_filename('functest',
                                        'api/swagger/creds_action.yaml'),
        endpoint='{0}/action'.format(ENDPOINT_CREDS))
    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 = getattr(CONST, 'env_file')
        with open(rc_file, 'w') as creds_file:
            creds_file.writelines(lines)

        LOGGER.info("Sourcing the OpenStack RC file...")
        try:
            run_tests.Runner.source_envfile(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')