From c7d2e49837044d24410a5501877e3d67750fd56b Mon Sep 17 00:00:00 2001 From: Jan Gosmann Date: Tue, 25 Oct 2022 20:20:46 +0200 Subject: [PATCH 1/7] Support alertmanager Grafana datasource --- .../fragments/287-alertmanager-datasource.yml | 2 + plugins/modules/grafana_datasource.py | 22 +++++ .../grafana_datasource/tasks/alertmanager.yml | 84 +++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 changelogs/fragments/287-alertmanager-datasource.yml create mode 100644 tests/integration/targets/grafana_datasource/tasks/alertmanager.yml diff --git a/changelogs/fragments/287-alertmanager-datasource.yml b/changelogs/fragments/287-alertmanager-datasource.yml new file mode 100644 index 00000000..3fc50698 --- /dev/null +++ b/changelogs/fragments/287-alertmanager-datasource.yml @@ -0,0 +1,2 @@ +minor_changes: + - ``community.grafana.grafana_datasource`` supports alertmanager. diff --git a/plugins/modules/grafana_datasource.py b/plugins/modules/grafana_datasource.py index a4f59ab8..fa1550eb 100644 --- a/plugins/modules/grafana_datasource.py +++ b/plugins/modules/grafana_datasource.py @@ -49,6 +49,7 @@ - redis-datasource - tempo - quickwit-quickwit-datasource + - alertmanager type: str ds_url: description: @@ -233,6 +234,17 @@ - Use trends or not for zabbix datasource type. type: bool default: false + alertmanager_implementation: + description: + - The implementation to set for the alertmanager datasource type. + choices: + - mimir + - cortex + - prometheus + type: str + alertmanager_handle_grafana_alerts: + description: + - Whether Grafana should sent alerts to this alertmanager. aws_auth_type: description: - Type for AWS authentication for CloudWatch datasource type (authType of grafana @@ -644,6 +656,12 @@ def get_datasource_payload(data, org_id=None): json_data["tlsSkipVerify"] = True # datasource type related parameters + if data["ds_type"] == "alertmanager": + json_data["implementation"] = data["alertmanager_implementation"] + json_data["handleGrafanaManagedAlerts"] = data[ + "alertmanager_handle_grafana_alerts" + ] + if data["ds_type"] == "elasticsearch": json_data["maxConcurrentShardRequests"] = data["max_concurrent_shard_requests"] json_data["timeField"] = data["time_field"] @@ -818,6 +836,8 @@ def setup_module_object(): "loki", "tempo", "quickwit-quickwit-datasource", + "loki", + "alertmanager", ] ), ds_url=dict(type="str"), @@ -858,6 +878,8 @@ def setup_module_object(): choices=["disable", "require", "verify-ca", "verify-full"], ), trends=dict(default=False, type="bool"), + alertmanager_implementation=dict(choices=["mimir", "cortex", "prometheus"]), + alertmanager_handle_grafana_alerts=dict(default=False, type="bool"), aws_auth_type=dict( default="keys", choices=["keys", "credentials", "arn", "default"] ), diff --git a/tests/integration/targets/grafana_datasource/tasks/alertmanager.yml b/tests/integration/targets/grafana_datasource/tasks/alertmanager.yml new file mode 100644 index 00000000..a2ac550d --- /dev/null +++ b/tests/integration/targets/grafana_datasource/tasks/alertmanager.yml @@ -0,0 +1,84 @@ +- name: Create alertmanager datasource + register: result + grafana_datasource: + name: datasource-alertmanager + grafana_url: "{{ grafana_url }}" + grafana_user: "{{ grafana_username }}" + grafana_password: "{{ grafana_password }}" + org_id: '1' + ds_type: alertmanager + ds_url: alertmanager.company.com:9093 + alertmanager_implementation: prometheus + alertmanager_handle_grafana_alerts: false + +- debug: + var: result + +- assert: + that: + - result.changed + - "result.msg == 'Datasource datasource-alertmanager created'" + +- name: Check alertmanager datasource creation idempotency + register: result + grafana_datasource: + name: datasource-alertmanager + grafana_url: "{{ grafana_url }}" + grafana_user: "{{ grafana_username }}" + grafana_password: "{{ grafana_password }}" + org_id: '1' + ds_type: alertmanager + ds_url: alertmanager.company.com:9093 + alertmanager_implementation: prometheus + alertmanager_handle_grafana_alerts: false + +- debug: + var: result + +- assert: + that: + - not result.changed + - result.datasource.basicAuth == false + - result.datasource.isDefault == false + - result.datasource.jsonData.sslmode == 'verify-full' + - result.datasource.jsonData.tlsAuth == false + - result.datasource.jsonData.tlsAuthWithCACert == false + - result.datasource.jsonData.implementation == 'prometheus' + - result.datasource.jsonData.handleGrafanaManagedAlerts == false + - result.datasource.name == 'datasource-alertmanager' + - result.datasource.orgId == 1 + - result.datasource.type == 'alertmanager' + - result.datasource.url == 'alertmanager.company.com:9093' + - result.datasource.withCredentials == false + +- name: Delete alertmanager datasource + register: result + grafana_datasource: + name: datasource-alertmanager + grafana_url: "{{ grafana_url }}" + grafana_user: "{{ grafana_username }}" + grafana_password: "{{ grafana_password }}" + state: absent + +- debug: + var: result + +- assert: + that: + - result.changed + +- name: Delete alertmanager datasource + register: result + grafana_datasource: + name: datasource-alertmanager + grafana_url: "{{ grafana_url }}" + grafana_user: "{{ grafana_username }}" + grafana_password: "{{ grafana_password }}" + state: absent + +- debug: + var: result + +- assert: + that: + - not result.changed From 2c73d8f636a9300baab3c24a27098adec948a77a Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Fri, 9 Aug 2024 22:25:49 +0200 Subject: [PATCH 2/7] fix: rm duplicate loki item from type choices --- plugins/modules/grafana_datasource.py | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/modules/grafana_datasource.py b/plugins/modules/grafana_datasource.py index fa1550eb..50259250 100644 --- a/plugins/modules/grafana_datasource.py +++ b/plugins/modules/grafana_datasource.py @@ -836,7 +836,6 @@ def setup_module_object(): "loki", "tempo", "quickwit-quickwit-datasource", - "loki", "alertmanager", ] ), From 35581217c799e286c722dbe056a0721da1e7a7c6 Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Fri, 9 Aug 2024 22:30:04 +0200 Subject: [PATCH 3/7] docs: update changelog fragment --- changelogs/fragments/287-alertmanager-datasource.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/287-alertmanager-datasource.yml b/changelogs/fragments/287-alertmanager-datasource.yml index 3fc50698..bac977cf 100644 --- a/changelogs/fragments/287-alertmanager-datasource.yml +++ b/changelogs/fragments/287-alertmanager-datasource.yml @@ -1,2 +1,2 @@ minor_changes: - - ``community.grafana.grafana_datasource`` supports alertmanager. + - Support `alertmanager` as type for `grafana_datasource` From ebb7c9d593ed78aac8067561521d4a75cd745eea Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Fri, 9 Aug 2024 22:33:47 +0200 Subject: [PATCH 4/7] docs: define missing type and default for argument --- plugins/modules/grafana_datasource.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/modules/grafana_datasource.py b/plugins/modules/grafana_datasource.py index 50259250..bcdf3048 100644 --- a/plugins/modules/grafana_datasource.py +++ b/plugins/modules/grafana_datasource.py @@ -245,6 +245,8 @@ alertmanager_handle_grafana_alerts: description: - Whether Grafana should sent alerts to this alertmanager. + type: bool + default: false aws_auth_type: description: - Type for AWS authentication for CloudWatch datasource type (authType of grafana From 68a78648b287390f92aa6ce290b65bd0fbf431d8 Mon Sep 17 00:00:00 2001 From: Moritz <15136847+Nemental@users.noreply.github.com> Date: Fri, 16 Aug 2024 08:59:32 +0200 Subject: [PATCH 5/7] chore: typo Co-authored-by: Sebastian Gumprich --- plugins/modules/grafana_datasource.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/grafana_datasource.py b/plugins/modules/grafana_datasource.py index bcdf3048..89c9bfd0 100644 --- a/plugins/modules/grafana_datasource.py +++ b/plugins/modules/grafana_datasource.py @@ -244,7 +244,7 @@ type: str alertmanager_handle_grafana_alerts: description: - - Whether Grafana should sent alerts to this alertmanager. + - Whether Grafana should send alerts to this alertmanager. type: bool default: false aws_auth_type: From d25619d71d4c92ba4265d0b2e3c0bdf7fc973467 Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Fri, 16 Aug 2024 09:11:28 +0200 Subject: [PATCH 6/7] test: add test task file to main --- tests/integration/targets/grafana_datasource/tasks/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/integration/targets/grafana_datasource/tasks/main.yml b/tests/integration/targets/grafana_datasource/tasks/main.yml index 0b3ea4cd..a9d1d49e 100644 --- a/tests/integration/targets/grafana_datasource/tasks/main.yml +++ b/tests/integration/targets/grafana_datasource/tasks/main.yml @@ -1,5 +1,7 @@ --- - block: + - ansible.builtin.include_tasks: + file: alertmanager.yml - ansible.builtin.include_tasks: file: errors.yml - ansible.builtin.include_tasks: From a27f300f626ea2df17409fc982a23f6518a46722 Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Fri, 16 Aug 2024 09:21:07 +0200 Subject: [PATCH 7/7] test: remove sslmode from assert --- .../targets/grafana_datasource/tasks/alertmanager.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/targets/grafana_datasource/tasks/alertmanager.yml b/tests/integration/targets/grafana_datasource/tasks/alertmanager.yml index a2ac550d..154e9c61 100644 --- a/tests/integration/targets/grafana_datasource/tasks/alertmanager.yml +++ b/tests/integration/targets/grafana_datasource/tasks/alertmanager.yml @@ -40,7 +40,6 @@ - not result.changed - result.datasource.basicAuth == false - result.datasource.isDefault == false - - result.datasource.jsonData.sslmode == 'verify-full' - result.datasource.jsonData.tlsAuth == false - result.datasource.jsonData.tlsAuthWithCACert == false - result.datasource.jsonData.implementation == 'prometheus' @@ -66,6 +65,7 @@ - assert: that: - result.changed + - "result.msg == 'Datasource datasource-alertmanager deleted.'" - name: Delete alertmanager datasource register: result