Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop modifying the query response for showing as JSON #696

Merged
merged 2 commits into from Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 20 additions & 18 deletions puppetboard/templates/query.html
Expand Up @@ -130,24 +130,26 @@ <h2 id="results_header">Result</h2>
{% endblock content %}

{% block onload_script %}
{% macro extra_options(caller) %}
'columns': [
{% for column in columns %}
{
"title": "{{ quote_columns_data(column) }}",
{% if column in ['node', 'certname'] %}
"render": function (data, type, full, meta) {
return `<a href='{{ url_for("node", node_name="") }}${data}'>${data}</a>`
{% if not form.rawjson.data %}
{% macro extra_options(caller) %}
'columns': [
{% for column in columns %}
{
"title": "{{ quote_columns_data(column) }}",
{% if column in ['node', 'certname'] %}
"render": function (data, type, full, meta) {
return `<a href='{{ url_for("node", node_name="") }}${data}'>${data}</a>`
},
{% endif %}
},
{% endfor %}
],
'serverSide': false,
{% endmacro %}

{% if not result %}
{% set result = [] %}
{% endif %}
{{ macros.datatable_init(table_html_id="query_table", ajax_url=None, data=result|tojson, default_length=config.NORMAL_TABLE_COUNT, length_selector=config.TABLE_COUNT_SELECTOR, extra_options=extra_options) }}
{% endif %}
},
{% endfor %}
],
'serverSide': false,
{% endmacro %}

{% if not result %}
{% set result = [] %}
{% endif %}
{{ macros.datatable_init(table_html_id="query_table", ajax_url=None, data=result|tojson, default_length=config.NORMAL_TABLE_COUNT, length_selector=config.TABLE_COUNT_SELECTOR, extra_options=extra_options) }}
{% endblock onload_script %}
39 changes: 25 additions & 14 deletions puppetboard/views/query.py
Expand Up @@ -61,21 +61,32 @@ def query(env):
zero_results = (len(result) == 0)
result = result if not zero_results else None

output = []
if not zero_results:
columns = result[0].keys()
for items in result:
output.append(list(items.values()))
if form.rawjson.data:
# for JSON view pass the response from PuppetDB as-is
return render_template('query.html',
form=form,
zero_results=zero_results,
result=result,
columns=None,
envs=envs,
current_env=env)
else:
columns = []

return render_template('query.html',
form=form,
zero_results=zero_results,
result=output,
columns=columns,
envs=envs,
current_env=env)
# for table view separate the columns and the rows
rows = []
if not zero_results:
columns = result[0].keys()
for items in result:
rows.append(list(items.values()))
else:
columns = []

return render_template('query.html',
form=form,
zero_results=zero_results,
result=rows,
columns=columns,
envs=envs,
current_env=env)

except HTTPError as e:
error_text = e.response.text
Expand Down
60 changes: 51 additions & 9 deletions test/views/test_query.py
Expand Up @@ -18,23 +18,23 @@ def test_query_view(client, mocker,
assert len(vals) == 0


def test_query__some_response(client, mocker,
mock_puppetdb_environments,
mock_puppetdb_default_nodes):
def test_query__some_response__table(client, mocker,
mock_puppetdb_environments,
mock_puppetdb_default_nodes):
app.app.config['WTF_CSRF_ENABLED'] = False

query_data = [
{'certname': 'foobar'},
result = [
{'certname': 'foobar', 'catalog_environment': 'qa'},
]
mocker.patch.object(app.puppetdb, '_query', return_value=query_data)
mocker.patch.object(app.puppetdb, '_query', return_value=result)

data = {
query_data = {
'query': 'nodes[certname] { certname = "foobar" }',
'endpoints': 'pql',
}
rv = client.post(
'/query',
data=data,
data=query_data,
content_type='application/x-www-form-urlencoded',
)

Expand All @@ -48,7 +48,49 @@ def test_query__some_response(client, mocker,

vals = soup.find_all('p', {"id": "number_of_results"})
assert len(vals) == 1
assert str(len(query_data)) in vals[0].string
assert 'Number of results: 1' in vals[0].string

vals = soup.find_all('table', {"id": "query_table"})
assert len(vals) == 1
# we can't test more here as the content of this table is generated with JavaScript...


def test_query__some_response__json(client, mocker,
mock_puppetdb_environments,
mock_puppetdb_default_nodes):
app.app.config['WTF_CSRF_ENABLED'] = False

result = [
{'certname': 'foobar', 'catalog_environment': 'qa'},
]
mocker.patch.object(app.puppetdb, '_query', return_value=result)

query_data = {
'query': 'nodes[certname] { certname = "foobar" }',
'endpoints': 'pql',
'rawjson': 'y',
}
rv = client.post(
'/query',
data=query_data,
content_type='application/x-www-form-urlencoded',
)

assert rv.status_code == 200

soup = BeautifulSoup(rv.data, 'html.parser')
assert soup.title.contents[0] == 'Puppetboard'

vals = soup.find_all('h2', {"id": "results_header"})
assert len(vals) == 1

vals = soup.find_all('p', {"id": "number_of_results"})
assert len(vals) == 1
assert 'Number of results: 1' in vals[0].string

vals = soup.find_all('pre', {"id": "result"})
assert len(vals) == 1
# we can't test more here as the content of this tag is generated with JavaScript...


def test_query__empty_response(client, mocker,
Expand Down