Add ability to pass in user_agent
keystoneauth supports adding a user_agent info to the Session and Adapter via app_name. Allow users to add app_name/app_name and versions as desired. Also, add os-client-config into additional_user_agent. As an example, once this is landed and plumbed through shade, nodepool will set app_name='nodepool' and we'll have: User-Agent: nodepool/0.4.0 os-client-config/1.26.1 shade/1.19.1 keystoneauth1/2.18.0 python-requests/2.13.0 CPython/2.7.12 Change-Id: I1eb4dbd2587dcbe297b5c060c3c34b68ef51ef5e
This commit is contained in:
parent
451ec8daad
commit
64b28d42ed
|
@ -23,9 +23,14 @@ from os_client_config.config import OpenStackConfig # noqa
|
||||||
__version__ = pbr.version.VersionInfo('os_client_config').version_string()
|
__version__ = pbr.version.VersionInfo('os_client_config').version_string()
|
||||||
|
|
||||||
|
|
||||||
def get_config(service_key=None, options=None, **kwargs):
|
def get_config(
|
||||||
|
service_key=None, options=None,
|
||||||
|
app_name=None, app_version=None,
|
||||||
|
**kwargs):
|
||||||
load_yaml_config = kwargs.pop('load_yaml_config', True)
|
load_yaml_config = kwargs.pop('load_yaml_config', True)
|
||||||
config = OpenStackConfig(load_yaml_config=load_yaml_config)
|
config = OpenStackConfig(
|
||||||
|
load_yaml_config=load_yaml_config,
|
||||||
|
app_name=app_name, app_version=app_version)
|
||||||
if options:
|
if options:
|
||||||
config.register_argparse_arguments(options, sys.argv, service_key)
|
config.register_argparse_arguments(options, sys.argv, service_key)
|
||||||
parsed_options = options.parse_known_args(sys.argv)
|
parsed_options = options.parse_known_args(sys.argv)
|
||||||
|
@ -35,7 +40,10 @@ def get_config(service_key=None, options=None, **kwargs):
|
||||||
return config.get_one_cloud(options=parsed_options, **kwargs)
|
return config.get_one_cloud(options=parsed_options, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def make_rest_client(service_key, options=None, **kwargs):
|
def make_rest_client(
|
||||||
|
service_key, options=None,
|
||||||
|
app_name=None, app_version=None,
|
||||||
|
**kwargs):
|
||||||
"""Simple wrapper function. It has almost no features.
|
"""Simple wrapper function. It has almost no features.
|
||||||
|
|
||||||
This will get you a raw requests Session Adapter that is mounted
|
This will get you a raw requests Session Adapter that is mounted
|
||||||
|
@ -48,7 +56,10 @@ def make_rest_client(service_key, options=None, **kwargs):
|
||||||
get_session_client on it. This function is to make it easy to poke
|
get_session_client on it. This function is to make it easy to poke
|
||||||
at OpenStack REST APIs with a properly configured keystone session.
|
at OpenStack REST APIs with a properly configured keystone session.
|
||||||
"""
|
"""
|
||||||
cloud = get_config(service_key=service_key, options=options, **kwargs)
|
cloud = get_config(
|
||||||
|
service_key=service_key, options=options,
|
||||||
|
app_name=app_name, app_version=app_version,
|
||||||
|
**kwargs)
|
||||||
return cloud.get_session_client(service_key)
|
return cloud.get_session_client(service_key)
|
||||||
# Backwards compat - simple_client was a terrible name
|
# Backwards compat - simple_client was a terrible name
|
||||||
simple_client = make_rest_client
|
simple_client = make_rest_client
|
||||||
|
|
|
@ -21,6 +21,7 @@ import keystoneauth1.exceptions.catalog
|
||||||
from keystoneauth1 import session
|
from keystoneauth1 import session
|
||||||
import requestsexceptions
|
import requestsexceptions
|
||||||
|
|
||||||
|
import os_client_config
|
||||||
from os_client_config import _log
|
from os_client_config import _log
|
||||||
from os_client_config import constructors
|
from os_client_config import constructors
|
||||||
from os_client_config import exceptions
|
from os_client_config import exceptions
|
||||||
|
@ -71,7 +72,8 @@ def _make_key(key, service_type):
|
||||||
class CloudConfig(object):
|
class CloudConfig(object):
|
||||||
def __init__(self, name, region, config,
|
def __init__(self, name, region, config,
|
||||||
force_ipv4=False, auth_plugin=None,
|
force_ipv4=False, auth_plugin=None,
|
||||||
openstack_config=None, session_constructor=None):
|
openstack_config=None, session_constructor=None,
|
||||||
|
app_name=None, app_version=None):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.region = region
|
self.region = region
|
||||||
self.config = config
|
self.config = config
|
||||||
|
@ -81,6 +83,8 @@ class CloudConfig(object):
|
||||||
self._openstack_config = openstack_config
|
self._openstack_config = openstack_config
|
||||||
self._keystone_session = None
|
self._keystone_session = None
|
||||||
self._session_constructor = session_constructor or session.Session
|
self._session_constructor = session_constructor or session.Session
|
||||||
|
self._app_name = app_name
|
||||||
|
self._app_version = app_version
|
||||||
|
|
||||||
def __getattr__(self, key):
|
def __getattr__(self, key):
|
||||||
"""Return arbitrary attributes."""
|
"""Return arbitrary attributes."""
|
||||||
|
@ -211,9 +215,14 @@ class CloudConfig(object):
|
||||||
requestsexceptions.squelch_warnings(insecure_requests=not verify)
|
requestsexceptions.squelch_warnings(insecure_requests=not verify)
|
||||||
self._keystone_session = self._session_constructor(
|
self._keystone_session = self._session_constructor(
|
||||||
auth=self._auth,
|
auth=self._auth,
|
||||||
|
app_name=self._app_name,
|
||||||
|
app_version=self._app_version,
|
||||||
verify=verify,
|
verify=verify,
|
||||||
cert=cert,
|
cert=cert,
|
||||||
timeout=self.config['api_timeout'])
|
timeout=self.config['api_timeout'])
|
||||||
|
if hasattr(self._keystone_session, 'additional_user_agent'):
|
||||||
|
self._keystone_session.additional_user_agent.append(
|
||||||
|
('os-client-config', os_client_config.__version__))
|
||||||
return self._keystone_session
|
return self._keystone_session
|
||||||
|
|
||||||
def get_session_client(self, service_key):
|
def get_session_client(self, service_key):
|
||||||
|
|
|
@ -174,9 +174,12 @@ class OpenStackConfig(object):
|
||||||
override_defaults=None, force_ipv4=None,
|
override_defaults=None, force_ipv4=None,
|
||||||
envvar_prefix=None, secure_files=None,
|
envvar_prefix=None, secure_files=None,
|
||||||
pw_func=None, session_constructor=None,
|
pw_func=None, session_constructor=None,
|
||||||
|
app_name=None, app_version=None,
|
||||||
load_yaml_config=True):
|
load_yaml_config=True):
|
||||||
self.log = _log.setup_logging(__name__)
|
self.log = _log.setup_logging(__name__)
|
||||||
self._session_constructor = session_constructor
|
self._session_constructor = session_constructor
|
||||||
|
self._app_name = app_name
|
||||||
|
self._app_version = app_version
|
||||||
|
|
||||||
if load_yaml_config:
|
if load_yaml_config:
|
||||||
self._config_files = config_files or CONFIG_FILES
|
self._config_files = config_files or CONFIG_FILES
|
||||||
|
@ -1088,6 +1091,8 @@ class OpenStackConfig(object):
|
||||||
auth_plugin=auth_plugin,
|
auth_plugin=auth_plugin,
|
||||||
openstack_config=self,
|
openstack_config=self,
|
||||||
session_constructor=self._session_constructor,
|
session_constructor=self._session_constructor,
|
||||||
|
app_name=self._app_name,
|
||||||
|
app_version=self._app_version,
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_one_cloud_osc(
|
def get_one_cloud_osc(
|
||||||
|
|
|
@ -212,6 +212,8 @@ class TestCase(base.BaseTestCase):
|
||||||
self.secure_yaml = _write_yaml(SECURE_CONF)
|
self.secure_yaml = _write_yaml(SECURE_CONF)
|
||||||
self.vendor_yaml = _write_yaml(VENDOR_CONF)
|
self.vendor_yaml = _write_yaml(VENDOR_CONF)
|
||||||
self.no_yaml = _write_yaml(NO_CONF)
|
self.no_yaml = _write_yaml(NO_CONF)
|
||||||
|
self.useFixture(fixtures.MonkeyPatch(
|
||||||
|
'os_client_config.__version__', '1.2.3'))
|
||||||
|
|
||||||
# Isolate the test runs from the environment
|
# Isolate the test runs from the environment
|
||||||
# Do this as two loops because you can't modify the dict in a loop
|
# Do this as two loops because you can't modify the dict in a loop
|
||||||
|
|
|
@ -179,15 +179,25 @@ class TestCloudConfig(base.TestCase):
|
||||||
def test_get_session(self, mock_session):
|
def test_get_session(self, mock_session):
|
||||||
config_dict = defaults.get_defaults()
|
config_dict = defaults.get_defaults()
|
||||||
config_dict.update(fake_services_dict)
|
config_dict.update(fake_services_dict)
|
||||||
|
fake_session = mock.Mock()
|
||||||
|
fake_session.additional_user_agent = []
|
||||||
|
mock_session.return_value = fake_session
|
||||||
cc = cloud_config.CloudConfig(
|
cc = cloud_config.CloudConfig(
|
||||||
"test1", "region-al", config_dict, auth_plugin=mock.Mock())
|
"test1", "region-al", config_dict, auth_plugin=mock.Mock())
|
||||||
cc.get_session()
|
cc.get_session()
|
||||||
mock_session.assert_called_with(
|
mock_session.assert_called_with(
|
||||||
auth=mock.ANY,
|
auth=mock.ANY,
|
||||||
verify=True, cert=None, timeout=None)
|
verify=True, cert=None, timeout=None,
|
||||||
|
app_name=None, app_version=None)
|
||||||
|
self.assertEqual(
|
||||||
|
fake_session.additional_user_agent,
|
||||||
|
[('os-client-config', '1.2.3')])
|
||||||
|
|
||||||
@mock.patch.object(ksa_session, 'Session')
|
@mock.patch.object(ksa_session, 'Session')
|
||||||
def test_get_session_with_timeout(self, mock_session):
|
def test_get_session_with_timeout(self, mock_session):
|
||||||
|
fake_session = mock.Mock()
|
||||||
|
fake_session.additional_user_agent = []
|
||||||
|
mock_session.return_value = fake_session
|
||||||
config_dict = defaults.get_defaults()
|
config_dict = defaults.get_defaults()
|
||||||
config_dict.update(fake_services_dict)
|
config_dict.update(fake_services_dict)
|
||||||
config_dict['api_timeout'] = 9
|
config_dict['api_timeout'] = 9
|
||||||
|
@ -196,7 +206,11 @@ class TestCloudConfig(base.TestCase):
|
||||||
cc.get_session()
|
cc.get_session()
|
||||||
mock_session.assert_called_with(
|
mock_session.assert_called_with(
|
||||||
auth=mock.ANY,
|
auth=mock.ANY,
|
||||||
verify=True, cert=None, timeout=9)
|
verify=True, cert=None, timeout=9,
|
||||||
|
app_name=None, app_version=None)
|
||||||
|
self.assertEqual(
|
||||||
|
fake_session.additional_user_agent,
|
||||||
|
[('os-client-config', '1.2.3')])
|
||||||
|
|
||||||
@mock.patch.object(ksa_session, 'Session')
|
@mock.patch.object(ksa_session, 'Session')
|
||||||
def test_override_session_endpoint_override(self, mock_session):
|
def test_override_session_endpoint_override(self, mock_session):
|
||||||
|
|
Loading…
Reference in New Issue