puppetboard/app: Reducing code redundancy for environment retreival and checking
Moving the envs variable out of the functions scope to the global scope, this enables each function to reference and use these values. Adding a new function check_env() to standardize the way to ensure that the request environment exists, if it doesn't then abort with a 404. This reduces 16 blocks of code and is in line with @daenney's suggestions
This commit is contained in:
@@ -78,9 +78,15 @@ def environments():
|
|||||||
|
|
||||||
return x
|
return x
|
||||||
|
|
||||||
|
def check_env(env):
|
||||||
|
if env not in envs:
|
||||||
|
abort(404)
|
||||||
|
|
||||||
app.jinja_env.globals['url_for_pagination'] = url_for_pagination
|
app.jinja_env.globals['url_for_pagination'] = url_for_pagination
|
||||||
app.jinja_env.globals['url_for_environments'] = url_for_environments
|
app.jinja_env.globals['url_for_environments'] = url_for_environments
|
||||||
|
|
||||||
|
envs = environments()
|
||||||
|
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
def utility_processor():
|
def utility_processor():
|
||||||
def now(format='%m/%d/%Y %H:%M:%S'):
|
def now(format='%m/%d/%Y %H:%M:%S'):
|
||||||
@@ -91,29 +97,29 @@ def utility_processor():
|
|||||||
|
|
||||||
@app.errorhandler(400)
|
@app.errorhandler(400)
|
||||||
def bad_request(e):
|
def bad_request(e):
|
||||||
return render_template('400.html'), 400
|
return render_template('400.html', envs=envs), 400
|
||||||
|
|
||||||
|
|
||||||
@app.errorhandler(403)
|
@app.errorhandler(403)
|
||||||
def forbidden(e):
|
def forbidden(e):
|
||||||
return render_template('403.html'), 400
|
return render_template('403.html', envs=envs), 400
|
||||||
|
|
||||||
|
|
||||||
@app.errorhandler(404)
|
@app.errorhandler(404)
|
||||||
def not_found(e):
|
def not_found(e):
|
||||||
return render_template('404.html'), 404
|
return render_template('404.html', envs=envs), 404
|
||||||
|
|
||||||
|
|
||||||
@app.errorhandler(412)
|
@app.errorhandler(412)
|
||||||
def precond_failed(e):
|
def precond_failed(e):
|
||||||
"""We're slightly abusing 412 to handle missing features
|
"""We're slightly abusing 412 to handle missing features
|
||||||
depending on the API version."""
|
depending on the API version."""
|
||||||
return render_template('412.html'), 412
|
return render_template('412.html', envs=envs), 412
|
||||||
|
|
||||||
|
|
||||||
@app.errorhandler(500)
|
@app.errorhandler(500)
|
||||||
def server_error(e):
|
def server_error(e):
|
||||||
return render_template('500.html'), 500
|
return render_template('500.html', envs=envs), 500
|
||||||
|
|
||||||
|
|
||||||
@app.route('/', defaults={'env': 'production'})
|
@app.route('/', defaults={'env': 'production'})
|
||||||
@@ -125,10 +131,7 @@ def index(env):
|
|||||||
:param env: Search for nodes in this (Catalog and Fact) environment
|
:param env: Search for nodes in this (Catalog and Fact) environment
|
||||||
:type env: :obj:`string`
|
:type env: :obj:`string`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
# TODO: Would be great if we could parallelize this somehow, doing these
|
# TODO: Would be great if we could parallelize this somehow, doing these
|
||||||
# requests in sequence is rather pointless.
|
# requests in sequence is rather pointless.
|
||||||
@@ -204,10 +207,7 @@ def nodes(env):
|
|||||||
:param env: Search for nodes in this (Catalog and Fact) environment
|
:param env: Search for nodes in this (Catalog and Fact) environment
|
||||||
:type env: :obj:`string`
|
:type env: :obj:`string`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
status_arg = request.args.get('status', '')
|
status_arg = request.args.get('status', '')
|
||||||
nodelist = puppetdb.nodes(
|
nodelist = puppetdb.nodes(
|
||||||
@@ -245,10 +245,7 @@ def inventory(env):
|
|||||||
:param env: Search for facts in this environment
|
:param env: Search for facts in this environment
|
||||||
:type env: :obj:`string`
|
:type env: :obj:`string`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
fact_desc = [] # a list of fact descriptions to go
|
fact_desc = [] # a list of fact descriptions to go
|
||||||
# in the table header
|
# in the table header
|
||||||
@@ -315,10 +312,7 @@ def node(env, node_name):
|
|||||||
:param env: Ensure that the node, facts and reports are in this environment
|
:param env: Ensure that the node, facts and reports are in this environment
|
||||||
:type env: :obj:`string`
|
:type env: :obj:`string`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
node = get_or_abort(puppetdb.node, node_name)
|
node = get_or_abort(puppetdb.node, node_name)
|
||||||
facts = node.facts()
|
facts = node.facts()
|
||||||
@@ -366,10 +360,7 @@ def reports(env, page):
|
|||||||
and this value
|
and this value
|
||||||
:type page: :obj:`int`
|
:type page: :obj:`int`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
reports = get_or_abort(puppetdb.reports,
|
reports = get_or_abort(puppetdb.reports,
|
||||||
query='["=", "environment", "{0}"]'.format(env),
|
query='["=", "environment", "{0}"]'.format(env),
|
||||||
@@ -427,10 +418,7 @@ def reports_node(env, node_name, page):
|
|||||||
and this value
|
and this value
|
||||||
:type page: :obj:`int`
|
:type page: :obj:`int`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
reports = get_or_abort(puppetdb.reports,
|
reports = get_or_abort(puppetdb.reports,
|
||||||
query='["and",' \
|
query='["and",' \
|
||||||
@@ -483,10 +471,7 @@ def report_latest(env, node_name):
|
|||||||
:param node_name: Find the reports whose certname match this value
|
:param node_name: Find the reports whose certname match this value
|
||||||
:type node_name: :obj:`string`
|
:type node_name: :obj:`string`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
reports = get_or_abort(puppetdb.reports,
|
reports = get_or_abort(puppetdb.reports,
|
||||||
query='["and",' \
|
query='["and",' \
|
||||||
@@ -522,10 +507,7 @@ def report(env, node_name, report_id):
|
|||||||
report
|
report
|
||||||
:type report_id: :obj:`string`
|
:type report_id: :obj:`string`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
query = '["and", ["=", "environment", "{0}"], ["=", "certname", "{1}"],' \
|
query = '["and", ["=", "environment", "{0}"], ["=", "certname", "{1}"],' \
|
||||||
'["or", ["=", "hash", "{2}"], ["=", "configuration_version", "{2}"]]]'.format(
|
'["or", ["=", "hash", "{2}"], ["=", "configuration_version", "{2}"]]]'.format(
|
||||||
@@ -557,10 +539,7 @@ def facts(env):
|
|||||||
sake
|
sake
|
||||||
:type env: :obj:`string`
|
:type env: :obj:`string`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
facts_dict = collections.defaultdict(list)
|
facts_dict = collections.defaultdict(list)
|
||||||
facts = get_or_abort(puppetdb.fact_names)
|
facts = get_or_abort(puppetdb.fact_names)
|
||||||
@@ -588,10 +567,7 @@ def fact(env, fact):
|
|||||||
:param fact: Find all facts with this name
|
:param fact: Find all facts with this name
|
||||||
:type fact: :obj:`string`
|
:type fact: :obj:`string`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
# we can only consume the generator once, lists can be doubly consumed
|
# we can only consume the generator once, lists can be doubly consumed
|
||||||
# om nom nom
|
# om nom nom
|
||||||
@@ -622,10 +598,7 @@ def fact_value(env, fact, value):
|
|||||||
:param value: Filter facts whose value is equal to this
|
:param value: Filter facts whose value is equal to this
|
||||||
:type value: :obj:`string`
|
:type value: :obj:`string`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
facts = get_or_abort(puppetdb.facts,
|
facts = get_or_abort(puppetdb.facts,
|
||||||
name=fact,
|
name=fact,
|
||||||
@@ -654,10 +627,7 @@ def query(env):
|
|||||||
select field in the environment block
|
select field in the environment block
|
||||||
:type env: :obj:`string`
|
:type env: :obj:`string`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
if app.config['ENABLE_QUERY']:
|
if app.config['ENABLE_QUERY']:
|
||||||
form = QueryForm()
|
form = QueryForm()
|
||||||
@@ -693,10 +663,7 @@ def metrics(env):
|
|||||||
for the environments template block
|
for the environments template block
|
||||||
:type env: :obj:`string`
|
:type env: :obj:`string`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
metrics = get_or_abort(puppetdb._query, 'mbean')
|
metrics = get_or_abort(puppetdb._query, 'mbean')
|
||||||
for key, value in metrics.items():
|
for key, value in metrics.items():
|
||||||
@@ -716,10 +683,7 @@ def metric(env, metric):
|
|||||||
for the environments template block
|
for the environments template block
|
||||||
:type env: :obj:`string`
|
:type env: :obj:`string`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
name = unquote(metric)
|
name = unquote(metric)
|
||||||
metric = puppetdb.metric(metric)
|
metric = puppetdb.metric(metric)
|
||||||
@@ -738,10 +702,7 @@ def catalogs(env):
|
|||||||
:param env: Find the nodes with this catalog_environment value
|
:param env: Find the nodes with this catalog_environment value
|
||||||
:type env: :obj:`string`
|
:type env: :obj:`string`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
if app.config['ENABLE_CATALOG']:
|
if app.config['ENABLE_CATALOG']:
|
||||||
nodenames = []
|
nodenames = []
|
||||||
@@ -792,10 +753,7 @@ def catalog_node(env, node_name):
|
|||||||
:param env: Find the catalog with this environment value
|
:param env: Find the catalog with this environment value
|
||||||
:type env: :obj:`string`
|
:type env: :obj:`string`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
if app.config['ENABLE_CATALOG']:
|
if app.config['ENABLE_CATALOG']:
|
||||||
catalog = get_or_abort(puppetdb.catalog,
|
catalog = get_or_abort(puppetdb.catalog,
|
||||||
@@ -820,10 +778,7 @@ def catalog_submit(env):
|
|||||||
catalogs page with the right environment.
|
catalogs page with the right environment.
|
||||||
:type env: :obj:`string`
|
:type env: :obj:`string`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
if app.config['ENABLE_CATALOG']:
|
if app.config['ENABLE_CATALOG']:
|
||||||
form = CatalogForm(request.form)
|
form = CatalogForm(request.form)
|
||||||
@@ -851,10 +806,7 @@ def catalog_compare(env, compare, against):
|
|||||||
:param env: Ensure that the 2 catalogs are in the same environment
|
:param env: Ensure that the 2 catalogs are in the same environment
|
||||||
:type env: :obj:`string`
|
:type env: :obj:`string`
|
||||||
"""
|
"""
|
||||||
envs = environments()
|
check_env(env)
|
||||||
|
|
||||||
if env not in envs:
|
|
||||||
return redirect(url_for_environments(envs[0]))
|
|
||||||
|
|
||||||
if app.config['ENABLE_CATALOG']:
|
if app.config['ENABLE_CATALOG']:
|
||||||
compare_cat = get_or_abort(puppetdb.catalog,
|
compare_cat = get_or_abort(puppetdb.catalog,
|
||||||
|
|||||||
@@ -42,7 +42,6 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="four column row">
|
<div class="four column row">
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<span>Global Metrics:</span>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<h1 class="ui header darkblue no-margin-bottom">{{metrics['num_nodes']}}</h1>
|
<h1 class="ui header darkblue no-margin-bottom">{{metrics['num_nodes']}}</h1>
|
||||||
|
|||||||
@@ -40,7 +40,6 @@
|
|||||||
<a {% if endpoint == request.endpoint %} class="active item" {% else %} class="item" {% endif %}
|
<a {% if endpoint == request.endpoint %} class="active item" {% else %} class="item" {% endif %}
|
||||||
href="{{ url_for(endpoint, env=current_env) }}">{{ caption }}</a>
|
href="{{ url_for(endpoint, env=current_env) }}">{{ caption }}</a>
|
||||||
{%- endfor %}
|
{%- endfor %}
|
||||||
{% if envs|length > 0 %}
|
|
||||||
<div class="ui item dropdown">
|
<div class="ui item dropdown">
|
||||||
Environments
|
Environments
|
||||||
<i class="dropdown icon"></i>
|
<i class="dropdown icon"></i>
|
||||||
@@ -50,7 +49,6 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
|
||||||
<div class="item right"><a href="https://github.com/puppet-community/puppetboard" target="_blank">v0.1.0</a></div>
|
<div class="item right"><a href="https://github.com/puppet-community/puppetboard" target="_blank">v0.1.0</a></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="ui grid padding-bottom">
|
<div class="ui grid padding-bottom">
|
||||||
|
|||||||
Reference in New Issue
Block a user