Add jinja exceptions support
An exception support allows to use some kind of template-level validation. Its may be helpful for openstack services configs files templates. Usage: {% raise_exception("exception message") %} Change-Id: Id7d51916572da8bcb9161739240805f5f7cc80cc
This commit is contained in:
parent
5893d2fb44
commit
558cb0271a
|
@ -218,7 +218,7 @@ def address(service, port=None, external=False, with_scheme=False):
|
|||
scheme = 'http'
|
||||
if external:
|
||||
if not port:
|
||||
raise RuntimeError('Port config is required for external address')
|
||||
ee RuntimeError('Port config is required for external address')
|
||||
if VARIABLES['ingress']['enabled'] and port.get('ingress'):
|
||||
scheme = 'https'
|
||||
addr = "%s:%s" % (get_ingress_host(port['ingress']),
|
||||
|
@ -238,12 +238,17 @@ def address(service, port=None, external=False, with_scheme=False):
|
|||
return addr
|
||||
|
||||
|
||||
def j2raise(msg):
|
||||
raise AssertionError(msg)
|
||||
|
||||
|
||||
def jinja_render_file(path, lookup_paths=None):
|
||||
file_loaders = [jinja2.FileSystemLoader(os.path.dirname(path))]
|
||||
for p in lookup_paths:
|
||||
file_loaders.append(jinja2.FileSystemLoader(p))
|
||||
env = jinja2.Environment(loader=jinja2.ChoiceLoader(loaders=file_loaders))
|
||||
env.globals['address'] = address
|
||||
env.globals['raise_exception'] = j2raise
|
||||
env.filters['gethostbyname'] = socket.gethostbyname
|
||||
content = env.get_template(os.path.basename(path)).render(VARIABLES)
|
||||
|
||||
|
|
Loading…
Reference in New Issue