summaryrefslogtreecommitdiff
path: root/keystoneclient/_discover.py
diff options
context:
space:
mode:
authorJamie Lennox <jamielennox@redhat.com>2014-04-28 12:20:46 +1000
committerJamie Lennox <jamielennox@redhat.com>2014-09-16 10:41:55 +1000
commitec57b35bc8edb933fe259db2b96c393874166dc0 (patch)
tree13d73119da90341a28ab2b340b0de3e95e3a1624 /keystoneclient/_discover.py
parent3305c7be4b726de4dcc889006d0be30eb46d3ad9 (diff)
downloadpython-keystoneclient-ec57b35bc8edb933fe259db2b96c393874166dc0.tar.gz
Versioned Endpoint hack for Sessions
To maintain compatibility we must allow people to specify a versioned URL in the service catalog but allow the plugins to return a different URL to users. We need this to be a general approach as other services will likely have a similar problem with their catalog. The expectation here is that a client will register the catalog hack at import time rather than for every request. Closes-Bug: #1335726 Change-Id: I244f0ec3acca39fd1b2a2c5883abc06ec10eddc7
Diffstat (limited to 'keystoneclient/_discover.py')
-rw-r--r--keystoneclient/_discover.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/keystoneclient/_discover.py b/keystoneclient/_discover.py
index 7ea396f..c9c9792 100644
--- a/keystoneclient/_discover.py
+++ b/keystoneclient/_discover.py
@@ -22,6 +22,7 @@ raw data specified in version discovery responses.
"""
import logging
+import re
from keystoneclient import exceptions
from keystoneclient import utils
@@ -262,3 +263,56 @@ class Discover(object):
"""
data = self.data_for(version, **kwargs)
return data['url'] if data else None
+
+
+class _VersionHacks(object):
+ """A container to abstract the list of version hacks.
+
+ This could be done as simply a dictionary but is abstracted like this to
+ make for easier testing.
+ """
+
+ def __init__(self):
+ self._discovery_data = {}
+
+ def add_discover_hack(self, service_type, old, new=''):
+ """Add a new hack for a service type.
+
+ :param str service_type: The service_type in the catalog.
+ :param re.RegexObject old: The pattern to use.
+ :param str new: What to replace the pattern with.
+ """
+ hacks = self._discovery_data.setdefault(service_type, [])
+ hacks.append((old, new))
+
+ def get_discover_hack(self, service_type, url):
+ """Apply the catalog hacks and figure out an unversioned endpoint.
+
+ :param str service_type: the service_type to look up.
+ :param str url: The original url that came from a service_catalog.
+
+ :return: Either the unversioned url or the one from the catalog to try.
+ """
+ for old, new in self._discovery_data.get(service_type, []):
+ new_string, number_of_subs_made = old.subn(new, url)
+ if number_of_subs_made > 0:
+ return new_string
+
+ return url
+
+
+_VERSION_HACKS = _VersionHacks()
+_VERSION_HACKS.add_discover_hack('identity', re.compile('/v2.0/?$'), '/')
+
+
+def get_catalog_discover_hack(service_type, url):
+ """Apply the catalog hacks and figure out an unversioned endpoint.
+
+ This function is internal to keystoneclient.
+
+ :param str service_type: the service_type to look up.
+ :param str url: The original url that came from a service_catalog.
+
+ :return: Either the unversioned url or the one from the catalog to try.
+ """
+ return _VERSION_HACKS.get_discover_hack(service_type, url)