From 549e7ded6a440f12704704a40894fee54bf2da00 Mon Sep 17 00:00:00 2001 From: TerryHowe Date: Tue, 14 Jul 2015 10:10:18 -0600 Subject: [PATCH] Add configuration function using os-client-config Create a factory function that takes an object like a argparse Namespace and returns a valid SDK connection using os-client-config. Change-Id: I120c4ec49013a4c92a37971dea4dda67c4bd26e2 --- examples/connection.py | 17 +-------- openstack/connection.py | 59 ++++++++++++++++++++++++++++++ openstack/tests/functional/base.py | 29 +++------------ requirements.txt | 1 + test-requirements.txt | 1 - 5 files changed, 67 insertions(+), 40 deletions(-) diff --git a/examples/connection.py b/examples/connection.py index 44f97d82..4ce82609 100644 --- a/examples/connection.py +++ b/examples/connection.py @@ -21,27 +21,12 @@ For example: import sys -import os_client_config - from examples import common from openstack import connection def make_connection(opts): - occ = os_client_config.OpenStackConfig() - cloud = occ.get_one_cloud(opts.cloud, opts) - opts.preferences.set_region(opts.preferences.ALL, cloud.region) - # TODO(thowe): There is a general smell here that this code is - # repeated in two places at that we flatten the auth structure. - # The connection class should take OCC config and just deal, but - # I'd just like to get cacert working for now. - auth = cloud.config['auth'] - if 'cacert' in cloud.config: - auth['verify'] = cloud.config['cacert'] - if 'insecure' in cloud.config: - auth['verify'] = not bool(cloud.config['insecure']) - conn = connection.Connection(profile=opts.preferences, **auth) - return conn + return connection.from_config(opts) def run_connection(opts): diff --git a/openstack/connection.py b/openstack/connection.py index b8f6300d..fcd3fb48 100644 --- a/openstack/connection.py +++ b/openstack/connection.py @@ -60,14 +60,73 @@ try to find it and if that fails, you would create it:: import logging import sys +import os_client_config + from openstack import module_loader +from openstack import profile from openstack import proxy from openstack import session from openstack import transport as xport +from openstack import utils _logger = logging.getLogger(__name__) +def from_config(opts): + """Create a connection from a configuration. + + Create a :class:`~openstack.connection.Connection` from a configuration + similar to a os-client-config CloudConfig. + + :param opts: Options class like the argparse Namespace object. + """ + + # TODO(thowe): I proposed that service name defaults to None in OCC + defaults = {} + prof = profile.Profile() + services = [service.service_type for service in prof.get_services()] + for service in services: + defaults[service + '_service_name'] = None + # TODO(thowe): default is 2 which turns into v2 which doesn't work + # this stuff needs to be fixed where we keep version and path separated. + defaults['network_api_version'] = 'v2.0' + + # Get the cloud_config + occ = os_client_config.OpenStackConfig(override_defaults=defaults) + cloud_config = occ.get_one_cloud(opts.cloud, opts) + + if cloud_config.debug: + utils.enable_logging(True, stream=sys.stdout) + + # TODO(mordred) we need to add service_type setting to openstacksdk. + # Some clouds have type overridden as well as name. + prof = profile.Profile() + services = [service.service_type for service in prof.get_services()] + for service in cloud_config.get_services(): + if service in services: + version = cloud_config.get_api_version(service) + if version: + version = str(version) + if not version.startswith("v"): + version = "v" + version + prof.set_version(service, version) + prof.set_name(service, cloud_config.get_service_name(service)) + prof.set_visibility( + service, cloud_config.get_endpoint_type(service)) + prof.set_region(service, cloud_config.get_region_name(service)) + + # Auth + auth = cloud_config.config['auth'] + # TODO(thowe) We should be using auth_type + auth['auth_plugin'] = cloud_config.config['auth_type'] + if 'cacert' in cloud_config.config: + auth['verify'] = cloud_config.config['cacert'] + if 'insecure' in cloud_config.config: + auth['verify'] = not bool(cloud_config.config['insecure']) + + return Connection(profile=prof, **auth) + + class Connection(object): def __init__(self, transport=None, authenticator=None, profile=None, diff --git a/openstack/tests/functional/base.py b/openstack/tests/functional/base.py index 7ed41f0d..2408e4cb 100644 --- a/openstack/tests/functional/base.py +++ b/openstack/tests/functional/base.py @@ -11,17 +11,12 @@ # under the License. import os -import sys import time import unittest -import os_client_config - from openstack.auth import service_filter from openstack import connection from openstack import exceptions -from openstack import profile -from openstack import utils def requires_service(**kwargs): @@ -55,26 +50,14 @@ def requires_service(**kwargs): class BaseFunctionalTest(unittest.TestCase): + class Opts(object): + def __init__(self): + self.cloud = os.getenv('OS_CLOUD', 'test_cloud') + @classmethod def setUpClass(cls): - name = os.getenv('OS_CLOUD', 'test_cloud') - test_cloud = os_client_config.OpenStackConfig().get_one_cloud(name) - - prof = profile.Profile() - prof.set_region(prof.ALL, test_cloud.region) - if test_cloud.debug: - utils.enable_logging(True, stream=sys.stdout) - - # TODO(thowe): There is a general smell here that this code is - # repeated in two places at that we flatten the auth structure. - # The connection class should take OCC config and just deal, but - # I'd just like to get cacert working for now. - auth = test_cloud.config['auth'] - if 'cacert' in test_cloud.config: - auth['verify'] = test_cloud.config['cacert'] - if 'insecure' in test_cloud.config: - auth['verify'] = not bool(test_cloud.config['insecure']) - cls.conn = connection.Connection(profile=prof, **auth) + opts = cls.Opts() + cls.conn = connection.from_config(opts) @classmethod def assertIs(cls, expected, actual): diff --git a/requirements.txt b/requirements.txt index 96b5d89e..1a8459a4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ requests>=2.5.2 six>=1.9.0 stevedore>=1.5.0 # Apache-2.0 oslo.utils>=1.9.0 # Apache-2.0 +os-client-config>=1.4.0 diff --git a/test-requirements.txt b/test-requirements.txt index 0b0ac370..16e558bf 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -10,7 +10,6 @@ mock>=1.1;python_version!='2.6' mock==1.0.1;python_version=='2.6' python-subunit>=0.0.18 oslosphinx>=2.5.0 # Apache-2.0 -os-client-config>=1.4.0 os-testr>=0.1.0 requests-mock>=0.6.0 # Apache-2.0 sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2