def format_url(url, substitutions, silent_keyerror_failures=None): """Formats a user-defined URL with the given substitutions. :param string url: the URL to be formatted :param dict substitutions: the dictionary used for substitution :param list silent_keyerror_failures: keys for which we should be silent if there is a KeyError exception on substitution attempt :returns: a formatted URL """ WHITELISTED_PROPERTIES = [ 'tenant_id', 'user_id', 'public_bind_host', 'admin_bind_host', 'compute_host', 'compute_port', 'admin_port', 'public_port', 'public_endpoint', 'admin_endpoint', ] substitutions = utils.WhiteListedItemFilter(WHITELISTED_PROPERTIES, substitutions) allow_keyerror = silent_keyerror_failures or [] try: result = url.replace('$(', '%(') % substitutions except AttributeError: LOG.error(_LE('Malformed endpoint - %(url)r is not a string'), {"url": url}) raise exception.MalformedEndpoint(endpoint=url) except KeyError as e: if not e.args or e.args[0] not in allow_keyerror: LOG.error( _LE("Malformed endpoint %(url)s - unknown key " "%(keyerror)s"), { "url": url, "keyerror": e }) raise exception.MalformedEndpoint(endpoint=url) else: result = None except TypeError as e: LOG.error( _LE("Malformed endpoint '%(url)s'. The following type error " "occurred during string substitution: %(typeerror)s"), { "url": url, "typeerror": e }) raise exception.MalformedEndpoint(endpoint=url) except ValueError as e: LOG.error( _LE("Malformed endpoint %s - incomplete format " "(are you missing a type notifier ?)"), url) raise exception.MalformedEndpoint(endpoint=url) return result
def format_url(url, substitutions): """Formats a user-defined URL with the given substitutions. :param string url: the URL to be formatted :param dict substitutions: the dictionary used for substitution :returns: a formatted URL """ substitutions = utils.WhiteListedItemFilter( CONF.catalog.endpoint_substitution_whitelist, substitutions) try: result = url.replace('$(', '%(') % substitutions except AttributeError: LOG.error(_('Malformed endpoint - %(url)r is not a string'), {"url": url}) raise exception.MalformedEndpoint(endpoint=url) except KeyError as e: LOG.error(_LE("Malformed endpoint %(url)s - unknown key %(keyerror)s"), { "url": url, "keyerror": e }) raise exception.MalformedEndpoint(endpoint=url) except TypeError as e: LOG.error( _LE("Malformed endpoint '%(url)s'. The following type error " "occurred during string substitution: %(typeerror)s"), { "url": url, "typeerror": e }) raise exception.MalformedEndpoint(endpoint=url) except ValueError as e: LOG.error( _LE("Malformed endpoint %s - incomplete format " "(are you missing a type notifier ?)"), url) raise exception.MalformedEndpoint(endpoint=url) return result