Skip to content

Commit

Permalink
feat: export dashboard with pretty printed JSON
Browse files Browse the repository at this point in the history
Resolves: #257
  • Loading branch information
kongslund authored and rrey committed Jul 17, 2022
1 parent f52324b commit a3eb949
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/257_pretty_printed_dashboard_export.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- Export dashboard with pretty printed JSON so that it becomes easier to compare changes with the previous version (#257)
2 changes: 1 addition & 1 deletion plugins/modules/grafana_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ def grafana_export_dashboard(module, data):
if dashboard_exists is True:
try:
with open(data['path'], 'w') as f:
f.write(json.dumps(dashboard))
f.write(json.dumps(dashboard, indent=2))
except Exception as e:
raise GrafanaExportException("Can't write json file : %s" % to_native(e))
result = {'msg': "Dashboard %s exported to %s" % (uid, data['path']),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
- set_fact:
dashboard_uid: "{{ result.uid }}"

- name: Check export grafana dashboard to file
grafana_dashboard:
grafana_url: "{{ grafana_url }}"
grafana_user: "{{ grafana_username }}"
grafana_password: "{{ grafana_password }}"
state: export
path: /tmp/dashboard.json
overwrite: true
uid: "{{ dashboard_uid }}"
register: result

- debug:
var: result

- assert:
that:
- "result.changed == true"
- "result.msg == 'Dashboard {{ dashboard_uid }} exported to /tmp/dashboard.json'"

- name: Load /tmp/dashboard.json or fail if missing
set_fact:
exported_dashboard_lines: "{{ lookup('file', '/tmp/dashboard.json').splitlines() }}"

- name: Assert that exported dashboard contains formatted JSON
assert:
that:
- "exported_dashboard_lines | length >= 2"
- "exported_dashboard_lines[0] == '{'"
- "exported_dashboard_lines[-1] == '}'"
1 change: 1 addition & 0 deletions tests/integration/targets/grafana_dashboard/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
- include: delete-dashboard.yml
- include: dashboard-from-id.yml
- include: dashboard-from-file.yml
- include: dashboard-export.yml
- include: dashboard-folder-destination.yml

0 comments on commit a3eb949

Please sign in to comment.