puppetboard/app.py: Simplifying the Inventory Code (#289)

* puppetboard/app.py: Simplifying the code generating and rendering the Inventory

This resolves #275

This update eliminates one iteration over the resulting inventory facts
that generates a multidimensional dictionary keyed by the node's certname
to another dictionary of key-value pairs of the fact name and fact value.

* puppetboard/templates/inventory.html: Wrapping the fact values in links to the Node page

This comes as a request from #280
This commit is contained in:
Corey Hammerton
2016-09-03 17:10:33 -04:00
committed by GitHub
parent 0c0a15bdf2
commit 5048662861
2 changed files with 21 additions and 29 deletions

View File

@@ -307,14 +307,11 @@ def inventory(env):
envs = environments()
check_env(env, envs)
fact_desc = [] # a list of fact descriptions to go
headers = [] # a list of fact descriptions to go
# in the table header
fact_names = [] # a list of inventory fact names
factvalues = {} # values of the facts for all the nodes
# indexed by node name and fact name
nodedata = {} # a dictionary containing list of inventoried
# facts indexed by node name
nodelist = set() # a set of node names
fact_data = {} # a multidimensional dict for node and
# fact data
# load the list of items/facts we want in our inventory
try:
@@ -328,8 +325,8 @@ def inventory(env):
# generate a list of descriptions and a list of fact names
# from the list of tuples inv_facts.
for description, name in inv_facts:
fact_desc.append(description)
for desc, name in inv_facts:
headers.append(desc)
fact_names.append(name)
query = AndOperator()
@@ -344,26 +341,21 @@ def inventory(env):
# get all the facts from PuppetDB
facts = puppetdb.facts(query=query)
# convert the json in easy to access data structure
for fact in facts:
factvalues[fact.node, fact.name] = fact.value
nodelist.add(fact.node)
if fact.node not in fact_data:
fact_data[fact.node] = {}
# generate the per-host data
for node in nodelist:
nodedata[node] = []
for fact_name in fact_names:
try:
nodedata[node].append(factvalues[node, fact_name])
except KeyError:
nodedata[node].append("undef")
fact_data[fact.node][fact.name] = fact.value
return Response(stream_with_context(
stream_template('inventory.html',
nodedata=nodedata,
fact_desc=fact_desc,
envs=envs,
current_env=env)))
stream_template(
'inventory.html',
headers=headers,
fact_names=fact_names,
fact_data=fact_data,
envs=envs,
current_env=env
)))
@app.route('/node/<node_name>',

View File

@@ -6,16 +6,16 @@
<table class='ui compact very basic sortable table'>
<thead>
<tr>
{% for description in fact_desc %}
<th>{{description}}</th>
{% for head in headers %}
<th{% if loop.index == 1 %} class="default-sort"{% endif %}>{{head}}</th>
{% endfor %}
</tr>
</thead>
<tbody class="searchable">
{% for nodename in nodedata %}
{% for node, facts in fact_data.iteritems() %}
<tr>
{% for item in nodedata[nodename] %}
<td>{{item}}</td>
{% for name in fact_names %}
<td><a href="{{url_for('node', env=current_env, node_name=node)}}">{{facts.get(name, 'undef')}}</a></td>
{% endfor %}
</tr>
{% endfor %}