def format_url(url, data): """Safely string formats a user-defined URL with the given data.""" try: result = url.replace('$(', '%(') % data except AttributeError: return None except KeyError as e: LOG.error( _("Malformed endpoint %(url)s - unknown key %(keyerror)s") % { "url": url, "keyerror": str(e) }) raise exception.MalformedEndpoint(endpoint=url) except TypeError as e: LOG.error( _("Malformed endpoint %(url)s - unknown key %(keyerror)s" "(are you missing brackets ?)") % { "url": url, "keyerror": str(e) }) raise exception.MalformedEndpoint(endpoint=url) except ValueError as e: LOG.error( _("Malformed endpoint %s - incomplete format \ (are you missing a type notifier ?)") % url) raise exception.MalformedEndpoint(endpoint=url) return result
def format_url(url, data): """Safely string formats a user-defined URL with the given data.""" data = utils.WhiteListedFormatter( CONF.catalog.endpoint_substitution_whitelist, data) try: result = url.replace('$(', '%(') % data 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(_("Malformed endpoint %(url)s - unknown key %(keyerror)s"), {"url": url, "keyerror": e}) raise exception.MalformedEndpoint(endpoint=url) except TypeError as e: LOG.error(_("Malformed endpoint %(url)s - unknown key %(keyerror)s" "(are you missing brackets ?)"), {"url": url, "keyerror": e}) raise exception.MalformedEndpoint(endpoint=url) except ValueError as e: LOG.error(_("Malformed endpoint %s - incomplete format " "(are you missing a type notifier ?)"), url) raise exception.MalformedEndpoint(endpoint=url) return result
def format_url(url, data): """Helper Method for all Backend Catalog's to Deal with URLS""" try: result = url.replace('$(', '%(') % data except AttributeError: return None except KeyError as e: LOG.error( _("Malformed endpoint %(url)s - unknown key %(keyerror)s") % { "url": url, "keyerror": str(e) }) raise exception.MalformedEndpoint(endpoint=url) except TypeError as e: LOG.error( _("Malformed endpoint %(url)s - unknown key %(keyerror)s" "(are you missing brackets ?)") % { "url": url, "keyerror": str(e) }) raise exception.MalformedEndpoint(endpoint=url) except ValueError as e: LOG.error( _("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 """ 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(_("Malformed endpoint %(url)s - unknown key %(keyerror)s"), { "url": url, "keyerror": e }) raise exception.MalformedEndpoint(endpoint=url) except TypeError as e: LOG.error( _("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( _("Malformed endpoint %s - incomplete format " "(are you missing a type notifier ?)"), url) raise exception.MalformedEndpoint(endpoint=url) return result
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, data): """Helper Method for all Backend Catalog's to Deal with URLS""" try: result = url % data except KeyError as e: LOG.error("Malformed endpoint %s - unknown key %s" % (url, str(e))) raise exception.MalformedEndpoint(endpoint=url) except TypeError as e: LOG.error("Malformed endpoint %s - type mismatch %s \ (are you missing brackets ?)" % (url, str(e))) raise exception.MalformedEndpoint(endpoint=url) except ValueError as e: LOG.error("Malformed endpoint %s - incomplete format \ (are you missing a type notifier ?)" % url) raise exception.MalformedEndpoint(endpoint=url) return result
def format_url(url, data): """Safely string formats a user-defined URL with the given data.""" try: result = url.replace('$(', '%(') % data 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(_("Malformed endpoint %(url)s - unknown key %(keyerror)s"), {"url": url, "keyerror": e}) raise exception.MalformedEndpoint(endpoint=url) except TypeError as e: LOG.error(_("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(_("Malformed endpoint %s - incomplete format " "(are you missing a type notifier ?)"), url) raise exception.MalformedEndpoint(endpoint=url) return result