<commit_before>#!/usr/bin/env python
#
# <EMAIL>
# <EMAIL>
# 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
#

import os
import os.path
import subprocess
import sys
import time

from cinderclient import client as cinderclient
import functest.utils.functest_logger as ft_logger
from glanceclient import client as glanceclient
from keystoneclient.v2_0 import client as keystoneclient
from neutronclient.v2_0 import client as neutronclient
from novaclient import client as novaclient

logger = ft_logger.Logger("openstack_utils").getLogger()


# *********************************************
#   CREDENTIALS
# *********************************************
def check_credentials():
    """
    Check if the OpenStack credentials (openrc) are sourced
    """
    env_vars = ['OS_AUTH_URL', 'OS_USERNAME', 'OS_PASSWORD', 'OS_TENANT_NAME']
    return all(map(lambda v: v in os.environ and os.environ[v], env_vars))


def get_credentials(service):
    """Returns a creds dictionary filled with the following keys:
    * username
    * password/api_key (depending on the service)
    * tenant_name/project_id (depending on the service)
    * auth_url
    :param service: a string indicating the name of the service
                    requesting the credentials.
    """
    creds = {}

    # Check that the env vars exists:
    envvars = ('OS_USERNAME', 'OS_PASSWORD', 'OS_AUTH_URL', 'OS_TENANT_NAME')
    for envvar in envvars:
        if os.getenv(envvar) is None:
            logger.error("'%s' is not exported as an env variable." % envvar)
            exit(-1)

    # Unfortunately, each of the OpenStack client will request slightly
    # different entries in their credentials dict.
    if service.lower() in ("nova", "cinder"):
        password = "api_key"
        tenant = "project_id"
    else:
        password = "password"
        tenant = "tenant_name"

    # The most common way to pass these info to the script is to do it through
    # environment variables.
    creds.update({
        "username": os.environ.get("OS_USERNAME"),
        password: os.environ.get("OS_PASSWORD"),
        "auth_url": os.environ.get("OS_AUTH_URL"),
        tenant: os.environ.get("OS_TENANT_NAME")
    })
    if os.getenv('OS_ENDPOINT_TYPE') is not None:
        creds.update({
            "endpoint_type": os.environ.get("OS_ENDPOINT_TYPE")
        })
    cacert = os.environ.get("OS_CACERT")
    if cacert is not None:
        # each openstack client uses differnt kwargs for this
        creds.update({"cacert": cacert,
                      "ca_cert": cacert,
                      "https_ca_cert": cacert,
                      "https_cacert": cacert,
                      "ca_file": cacert})
        creds.update({"insecure": "True", "https_insecure": "True"})
        if not os.path.isfile(cacert):
            logger.info("WARNING: The 'OS_CACERT' environment variable is "
                        "set to %s but the file does not exist." % cacert)
    return creds


def source_credentials(rc_file):
    pipe = subprocess.Popen(". %s; env" % rc_file, stdout=subprocess.PIPE,
                            shell=True)
    output = pipe.communicate()[0]
    env = dict((line.split("=", 1) for line in output.splitlines()))
    os.environ.update(env)
    return env
<commit_msg>Add region name to OpenStack credentials<commit_after>#!/usr/bin/env python
#
# <EMAIL>
# <EMAIL>
# 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
#

import os
import os.path
import subprocess
import sys
import time

from cinderclient import client as cinderclient
import functest.utils.functest_logger as ft_logger
from glanceclient import client as glanceclient
from keystoneclient.v2_0 import client as keystoneclient
from neutronclient.v2_0 import client as neutronclient
from novaclient import client as novaclient

logger = ft_logger.Logger("openstack_utils").getLogger()


# *********************************************
#   CREDENTIALS
# *********************************************
def check_credentials():
    """
    Check if the OpenStack credentials (openrc) are sourced
    """
    env_vars = ['OS_AUTH_URL', 'OS_USERNAME', 'OS_PASSWORD', 'OS_TENANT_NAME']
    return all(map(lambda v: v in os.environ and os.environ[v], env_vars))


def get_credentials(service):
    """Returns a creds dictionary filled with the following keys:
    * username
    * password/api_key (depending on the service)
    * tenant_name/project_id (depending on the service)
    * auth_url
    :param service: a string indicating the name of the service
                    requesting the credentials.
    """
    creds = {}

    # Check that the env vars exists:
    envvars = ('OS_USERNAME', 'OS_PASSWORD', 'OS_AUTH_URL', 'OS_TENANT_NAME')
    for envvar in envvars:
        if os.getenv(envvar) is None:
            logger.error("'%s' is not exported as an env variable." % envvar)
            exit(-1)

    # Unfortunately, each of the OpenStack client will request slightly
    # different entries in their credentials dict.
    if service.lower() in ("nova", "cinder"):
        password = "<PASSWORD>"
        tenant = "project_id"
    else:
        password = "password"
        tenant = "tenant_name"

    # The most common way to pass these info to the script is to do it through
    # environment variables.
    creds.update({
        "username": os.environ.get("OS_USERNAME"),
        password: os.environ.get("OS_PASSWORD"),
        "auth_url": os.environ.get("OS_AUTH_URL"),
        tenant: os.environ.get("OS_TENANT_NAME")
    })
    if os.getenv('OS_ENDPOINT_TYPE') is not None:
        creds.update({
            "endpoint_type": os.environ.get("OS_ENDPOINT_TYPE")
        })
    if os.getenv('OS_REGION_NAME') is not None:
        creds.update({
            "region_name": os.environ.get("OS_REGION_NAME")
        })
    cacert = os.environ.get("OS_CACERT")
    if cacert is not None:
        # each openstack client uses differnt kwargs for this
        creds.update({"cacert": cacert,
                      "ca_cert": cacert,
                      "https_ca_cert": cacert,
                      "https_cacert": cacert,
                      "ca_file": cacert})
        creds.update({"insecure": "True", "https_insecure": "True"})
        if not os.path.isfile(cacert):
            logger.info("WARNING: The 'OS_CACERT' environment variable is "
                        "set to %s but the file does not exist." % cacert)
    return creds


def source_credentials(rc_file):
    pipe = subprocess.Popen(". %s; env" % rc_file, stdout=subprocess.PIPE,
                            shell=True)
    output = pipe.communicate()[0]
    env = dict((line.split("=", 1) for line in output.splitlines()))
    os.environ.update(env)
    return env

{'fraction_non_alphanumeric': 0.12588003379329765, 'fraction_numerical': 0.0030977189524077726, 'mean_word_length': 2.8963247394404825, 'pattern_counts': {'":': 21, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2326945', 'n_tokens_mistral': 2133, 'n_tokens_neox': 2111, 'n_words': 625}