diff --git a/changelogs/fragments/257_pretty_printed_dashboard_export.yml b/changelogs/fragments/257_pretty_printed_dashboard_export.yml new file mode 100644 index 00000000..3ea9ca4f --- /dev/null +++ b/changelogs/fragments/257_pretty_printed_dashboard_export.yml @@ -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) diff --git a/plugins/modules/grafana_dashboard.py b/plugins/modules/grafana_dashboard.py index 2492f9cc..c759197a 100644 --- a/plugins/modules/grafana_dashboard.py +++ b/plugins/modules/grafana_dashboard.py @@ -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']), diff --git a/tests/integration/targets/grafana_dashboard/tasks/dashboard-export.yml b/tests/integration/targets/grafana_dashboard/tasks/dashboard-export.yml new file mode 100644 index 00000000..04b17f72 --- /dev/null +++ b/tests/integration/targets/grafana_dashboard/tasks/dashboard-export.yml @@ -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] == '}'" diff --git a/tests/integration/targets/grafana_dashboard/tasks/main.yml b/tests/integration/targets/grafana_dashboard/tasks/main.yml index 2221219d..4d9138fc 100644 --- a/tests/integration/targets/grafana_dashboard/tasks/main.yml +++ b/tests/integration/targets/grafana_dashboard/tasks/main.yml @@ -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