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

View File

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