Skip to content

Commit

Permalink
dashboard: Fail if the target directory does not exist
Browse files Browse the repository at this point in the history
Previously, this would 'succeed' but the dashboard would not be added.
  • Loading branch information
BenjaminSchubert authored and rrey committed Apr 27, 2021
1 parent e132eaf commit 5f72a4d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
4 changes: 4 additions & 0 deletions changelogs/fragments/1.2.2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bugfixes:
- grafana_dashboard now explicitely fails if the folder doesn't exist upon creation.
It would previously silently pass but not create the dashboard.
(https://github.com/ansible-collections/community.grafana/issues/153)
9 changes: 1 addition & 8 deletions plugins/modules/grafana_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,7 @@ def grafana_create_dashboard(module, data):
if grafana_version >= 5:
folder_exists, folder_id = grafana_folder_exists(module, data['grafana_url'], data['folder'], headers)
if folder_exists is False:
result['msg'] = "Dashboard folder '%s' does not exist." % data['folder']
result['uid'] = uid
result['changed'] = False
return result
raise GrafanaAPIException("Dashboard folder '%s' does not exist." % data['folder'])

payload['folderId'] = folder_id

Expand Down Expand Up @@ -385,10 +382,6 @@ def grafana_create_dashboard(module, data):
result['msg'] = "Dashboard %s unchanged." % payload['dashboard']['title']
result['changed'] = False
else:
# create
if folder_exists is True:
payload['folderId'] = folder_id

# Ensure there is no id in payload
if 'id' in payload['dashboard']:
del payload['dashboard']['id']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
- name: copy dashboard file
copy:
src: "files/dashboard.json"
dest: "/tmp/dashboard.json"

- block:
- name: Check import grafana dashboard from file to unknown folder fails
grafana_dashboard:
grafana_url: "{{ grafana_url }}"
grafana_user: "{{ grafana_username }}"
grafana_password: "{{ grafana_password }}"
state: present
commit_message: Updated by ansible
path: /tmp/dashboard.json
overwrite: true
folder: inexistent
register: result
ignore_errors: true

- debug:
var: result

- set_fact:
# XXX: Too many quotes of different types to do inline.
# I did not manage to find a good way of having it inline.
expected_error: "error : Dashboard folder 'inexistent' does not exist."

- assert:
that:
- "result.changed == false"
- "result.failed == true"
- "result.msg == expected_error"
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,3 +3,4 @@
- include: delete-dashboard.yml
- include: dashboard-from-id.yml
- include: dashboard-from-file.yml
- include: dashboard-folder-destination.yml

0 comments on commit 5f72a4d

Please sign in to comment.