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

Support alertmanager Grafana datasource #287

Merged
merged 7 commits into from
Aug 16, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/287-alertmanager-datasource.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- Support `alertmanager` as type for `grafana_datasource`
23 changes: 23 additions & 0 deletions plugins/modules/grafana_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
- redis-datasource
- tempo
- quickwit-quickwit-datasource
- alertmanager
type: str
ds_url:
description:
Expand Down Expand Up @@ -233,6 +234,19 @@
- 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 send alerts to this alertmanager.
type: bool
default: false
aws_auth_type:
description:
- Type for AWS authentication for CloudWatch datasource type (authType of grafana
Expand Down Expand Up @@ -644,6 +658,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"]
Expand Down Expand Up @@ -818,6 +838,7 @@ def setup_module_object():
"loki",
"tempo",
"quickwit-quickwit-datasource",
"alertmanager",
]
),
ds_url=dict(type="str"),
Expand Down Expand Up @@ -858,6 +879,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"]
),
Expand Down
Original file line number Diff line number Diff line change
@@ -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.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
- "result.msg == 'Datasource datasource-alertmanager deleted.'"

- 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
2 changes: 2 additions & 0 deletions tests/integration/targets/grafana_datasource/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
- block:
- ansible.builtin.include_tasks:
file: alertmanager.yml
- ansible.builtin.include_tasks:
file: errors.yml
- ansible.builtin.include_tasks:
Expand Down
Loading