Merge "Add configuration function using os-client-config"
This commit is contained in:
@@ -21,27 +21,12 @@ For example:
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import os_client_config
|
|
||||||
|
|
||||||
from examples import common
|
from examples import common
|
||||||
from openstack import connection
|
from openstack import connection
|
||||||
|
|
||||||
|
|
||||||
def make_connection(opts):
|
def make_connection(opts):
|
||||||
occ = os_client_config.OpenStackConfig()
|
return connection.from_config(opts)
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def run_connection(opts):
|
def run_connection(opts):
|
||||||
|
@@ -60,14 +60,73 @@ try to find it and if that fails, you would create it::
|
|||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import os_client_config
|
||||||
|
|
||||||
from openstack import module_loader
|
from openstack import module_loader
|
||||||
|
from openstack import profile
|
||||||
from openstack import proxy
|
from openstack import proxy
|
||||||
from openstack import session
|
from openstack import session
|
||||||
from openstack import transport as xport
|
from openstack import transport as xport
|
||||||
|
from openstack import utils
|
||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
_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):
|
class Connection(object):
|
||||||
|
|
||||||
def __init__(self, transport=None, authenticator=None, profile=None,
|
def __init__(self, transport=None, authenticator=None, profile=None,
|
||||||
|
@@ -11,17 +11,12 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
import time
|
import time
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import os_client_config
|
|
||||||
|
|
||||||
from openstack.auth import service_filter
|
from openstack.auth import service_filter
|
||||||
from openstack import connection
|
from openstack import connection
|
||||||
from openstack import exceptions
|
from openstack import exceptions
|
||||||
from openstack import profile
|
|
||||||
from openstack import utils
|
|
||||||
|
|
||||||
|
|
||||||
def requires_service(**kwargs):
|
def requires_service(**kwargs):
|
||||||
@@ -55,26 +50,14 @@ def requires_service(**kwargs):
|
|||||||
|
|
||||||
|
|
||||||
class BaseFunctionalTest(unittest.TestCase):
|
class BaseFunctionalTest(unittest.TestCase):
|
||||||
|
class Opts(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.cloud = os.getenv('OS_CLOUD', 'test_cloud')
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
name = os.getenv('OS_CLOUD', 'test_cloud')
|
opts = cls.Opts()
|
||||||
test_cloud = os_client_config.OpenStackConfig().get_one_cloud(name)
|
cls.conn = connection.from_config(opts)
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def assertIs(cls, expected, actual):
|
def assertIs(cls, expected, actual):
|
||||||
|
@@ -6,3 +6,4 @@ requests>=2.5.2
|
|||||||
six>=1.9.0
|
six>=1.9.0
|
||||||
stevedore>=1.5.0 # Apache-2.0
|
stevedore>=1.5.0 # Apache-2.0
|
||||||
oslo.utils>=1.9.0 # Apache-2.0
|
oslo.utils>=1.9.0 # Apache-2.0
|
||||||
|
os-client-config>=1.4.0
|
||||||
|
@@ -9,7 +9,6 @@ fixtures>=1.3.1
|
|||||||
mock>=1.2
|
mock>=1.2
|
||||||
python-subunit>=0.0.18
|
python-subunit>=0.0.18
|
||||||
oslosphinx>=2.5.0 # Apache-2.0
|
oslosphinx>=2.5.0 # Apache-2.0
|
||||||
os-client-config>=1.4.0
|
|
||||||
os-testr>=0.1.0
|
os-testr>=0.1.0
|
||||||
requests-mock>=0.6.0 # Apache-2.0
|
requests-mock>=0.6.0 # Apache-2.0
|
||||||
sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2
|
sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2
|
||||||
|
Reference in New Issue
Block a user