From 95f842483f355deddb9e367e658121657218f324 Mon Sep 17 00:00:00 2001 From: Yaroslav Lobankov Date: Fri, 11 Dec 2015 14:52:46 +0300 Subject: [PATCH] Moving "os.environ" things to envutils.py module New get_creds_from_env_vars() function was created that creates a dict with credentials, using the following environment variables: OS_AUTH_URL OS_USERNAME OS_PASSWORD OS_TENANT_NAME OS_ENDPOINT OS_REGION_NAME OS_CACERT OS_INSECURE The corresponding unit tests were added. Change-Id: I38bea5b59b7c8225a248f10fce66efdb32bc5eca --- rally/cli/commands/deployment.py | 30 ++-------------------------- rally/cli/envutils.py | 27 +++++++++++++++++++++++++ rally/osclients.py | 20 +++++++++---------- tests/unit/cli/test_envutils.py | 34 ++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 38 deletions(-) diff --git a/rally/cli/commands/deployment.py b/rally/cli/commands/deployment.py index 58440af16e..d7915f3c75 100644 --- a/rally/cli/commands/deployment.py +++ b/rally/cli/commands/deployment.py @@ -87,34 +87,8 @@ class DeploymentCommands(object): """ if fromenv: - required_env_vars = ["OS_USERNAME", "OS_PASSWORD", "OS_AUTH_URL", - "OS_TENANT_NAME"] - - unavailable_vars = [v for v in required_env_vars - if v not in os.environ] - if unavailable_vars: - print("The following environment variables are required but " - "not set: %s" % " ".join(unavailable_vars)) - return(1) - - config = { - "type": "ExistingCloud", - "auth_url": os.environ["OS_AUTH_URL"], - "endpoint": os.environ.get("OS_ENDPOINT"), - "admin": { - "username": os.environ["OS_USERNAME"], - "password": os.environ["OS_PASSWORD"], - "tenant_name": os.environ["OS_TENANT_NAME"] - }, - "https_cacert": os.environ.get("OS_CACERT", ""), - "https_insecure": False - } - region_name = os.environ.get("OS_REGION_NAME") - if region_name and region_name != "None": - config["region_name"] = region_name - https_insecure = os.environ.get("OS_INSECURE") - if https_insecure and https_insecure.lower() == "true": - config["https_insecure"] = True + config = {"type": "ExistingCloud"} + config.update(envutils.get_creds_from_env_vars()) else: if not filename: print("Either --filename or --fromenv is required") diff --git a/rally/cli/envutils.py b/rally/cli/envutils.py index 51aa3e7668..7cc6890cb0 100644 --- a/rally/cli/envutils.py +++ b/rally/cli/envutils.py @@ -16,6 +16,7 @@ import os import decorator +from oslo_utils import strutils from rally.common import fileutils from rally.common.i18n import _ @@ -80,3 +81,29 @@ def with_default_deployment(cli_arg_name="uuid"): with_default_task_id = default_from_global("task_id", ENV_TASK, "uuid") with_default_verification_id = default_from_global( "verification_uuid", ENV_VERIFICATION, "uuid") + + +def get_creds_from_env_vars(): + required_env_vars = ["OS_AUTH_URL", "OS_USERNAME", + "OS_PASSWORD", "OS_TENANT_NAME"] + missing_env_vars = [v for v in required_env_vars if v not in os.environ] + if missing_env_vars: + msg = ("The following environment variables are " + "required but not set: %s" % " ".join(missing_env_vars)) + raise exceptions.ValidationError(message=msg) + + creds = { + "auth_url": os.environ["OS_AUTH_URL"], + "admin": { + "username": os.environ["OS_USERNAME"], + "password": os.environ["OS_PASSWORD"], + "tenant_name": os.environ["OS_TENANT_NAME"] + }, + "endpoint": os.environ.get("OS_ENDPOINT"), + "region_name": os.environ.get("OS_REGION_NAME", ""), + "https_cacert": os.environ.get("OS_CACERT", ""), + "https_insecure": strutils.bool_from_string( + os.environ.get("OS_INSECURE")) + } + + return creds diff --git a/rally/osclients.py b/rally/osclients.py index 7d1b631efe..ee4a55f99f 100644 --- a/rally/osclients.py +++ b/rally/osclients.py @@ -14,10 +14,10 @@ # under the License. import abc -import os from oslo_config import cfg +from rally.cli import envutils from rally.common.i18n import _ from rally.common import log as logging from rally.common import objects @@ -634,17 +634,17 @@ class Clients(object): @classmethod def create_from_env(cls): - https_insecure = os.environ.get("OS_INSECURE") + creds = envutils.get_creds_from_env_vars() return cls( objects.Credential( - os.environ["OS_AUTH_URL"], - os.environ["OS_USERNAME"], - os.environ["OS_PASSWORD"], - os.environ.get("OS_TENANT_NAME"), - region_name=os.environ.get("OS_REGION_NAME"), - https_cacert=os.environ.get("OS_CACERT", ""), - https_insecure=(True if https_insecure and - https_insecure.lower() == "true" else False) + creds["auth_url"], + creds["admin"]["username"], + creds["admin"]["password"], + creds["admin"]["tenant_name"], + endpoint=creds["endpoint"], + region_name=creds["region_name"], + https_cacert=creds["https_cacert"], + https_insecure=creds["https_insecure"] )) def clear(self): diff --git a/tests/unit/cli/test_envutils.py b/tests/unit/cli/test_envutils.py index 63e91e2fdb..66c6d40c05 100644 --- a/tests/unit/cli/test_envutils.py +++ b/tests/unit/cli/test_envutils.py @@ -103,3 +103,37 @@ class EnvUtilsTestCase(test.TestCase): def test_clear_env(self, mock_update_env_file, mock_path_exists): envutils.clear_env() self.assertEqual(os.environ, {}) + + @mock.patch.dict(os.environ, {"OS_AUTH_URL": "fake_auth_url", + "OS_USERNAME": "fake_username", + "OS_PASSWORD": "fake_password", + "OS_TENANT_NAME": "fake_tenant_name", + "OS_REGION_NAME": "fake_region_name", + "OS_ENDPOINT": "fake_endpoint", + "OS_INSECURE": "True", + "OS_CACERT": "fake_cacert"}) + def test_get_creds_from_env_vars(self): + expected_creds = { + "auth_url": "fake_auth_url", + "admin": { + "username": "fake_username", + "password": "fake_password", + "tenant_name": "fake_tenant_name" + }, + "endpoint": "fake_endpoint", + "region_name": "fake_region_name", + "https_cacert": "fake_cacert", + "https_insecure": True + } + creds = envutils.get_creds_from_env_vars() + self.assertEqual(expected_creds, creds) + + @mock.patch.dict(os.environ, {"OS_AUTH_URL": "fake_auth_url", + "OS_PASSWORD": "fake_password", + "OS_REGION_NAME": "fake_region_name", + "OS_ENDPOINT": "fake_endpoint", + "OS_INSECURE": "True", + "OS_CACERT": "fake_cacert"}) + def test_get_creds_from_env_vars_when_required_vars_missing(self): + self.assertRaises(exceptions.ValidationError, + envutils.get_creds_from_env_vars)