From 0f5fe6fee93764eed7f6af28eb7f2cd5627d9712 Mon Sep 17 00:00:00 2001 From: Brian Curtin Date: Fri, 15 Jan 2016 12:46:03 -0500 Subject: [PATCH] Send keystoneauth a better user-agent string When we moved to keystoneauth sessions from our own handwritten session class, we lost the nice user-agent string we built up. That was then added to keystoneauth and has now been released in version 2.2.0, which we picked up. That implementation provides a good base for us to build on, and we should now send our own version string as well as make way for callers to submit their application's name/version string as well. Change-Id: I439b0912b27a37c4ddfc91447a5f3394df2d0385 --- openstack/session.py | 17 +++++++++++++++-- openstack/tests/unit/test_session.py | 8 ++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/openstack/session.py b/openstack/session.py index 40f64763..5eae6e4a 100644 --- a/openstack/session.py +++ b/openstack/session.py @@ -20,7 +20,9 @@ from six.moves.urllib import parse from keystoneauth1 import session as _session +import openstack +DEFAULT_USER_AGENT = "openstacksdk/%s" % openstack.__version__ VERSION_PATTERN = re.compile('/v\d[\d.]*') @@ -40,16 +42,27 @@ def parse_url(filt, url): class Session(_session.Session): - def __init__(self, profile, **kwargs): + def __init__(self, profile, user_agent=None, **kwargs): """Create a new Keystone auth session with a profile. :param profile: If the user has any special profiles such as the service name, region, version or interface, they may be provided in the profile object. If no profiles are provided, the services that appear first in the service catalog will be used. + :param user_agent: A User-Agent header string to use for the + request. If not provided, a default of + :attr:`~openstack.session.DEFAULT_USER_AGENT` + is used, which contains the openstacksdk version + When a non-None value is passed, it will be + prepended to the default. :type profile: :class:`~openstack.profile.Profile` """ - super(Session, self).__init__(**kwargs) + if user_agent is not None: + self.user_agent = "%s %s" % (user_agent, DEFAULT_USER_AGENT) + else: + self.user_agent = DEFAULT_USER_AGENT + super(Session, self).__init__(user_agent=self.user_agent, **kwargs) + self.profile = profile def get_endpoint(self, auth=None, interface=None, **kwargs): diff --git a/openstack/tests/unit/test_session.py b/openstack/tests/unit/test_session.py index c1421d8e..b5a4018c 100644 --- a/openstack/tests/unit/test_session.py +++ b/openstack/tests/unit/test_session.py @@ -33,3 +33,11 @@ class TestSession(testtools.TestCase): self.assertEqual( "http://127.0.0.1:9292/wot/v1/mytenant", session.parse_url(filt, "http://127.0.0.1:9292/wot/v2.0/mytenant")) + + def test_user_agent_none(self): + sot = session.Session(None) + self.assertTrue(sot.user_agent.startswith("openstacksdk")) + + def test_user_agent_set(self): + sot = session.Session(None, user_agent="testing/123") + self.assertTrue(sot.user_agent.startswith("testing/123 openstacksdk"))