Skip to content

Commit

Permalink
Support alertmanager Grafana datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
jgosmann committed Nov 6, 2022
1 parent 324fcf7 commit 9f8a264
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
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:
- ``community.grafana.grafana_datasource`` supports alertmanager.
21 changes: 20 additions & 1 deletion plugins/modules/grafana_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- camptocamp-prometheus-alertmanager-datasource
- loki
- redis-datasource
- alertmanager
type: str
ds_url:
description:
Expand Down Expand Up @@ -209,6 +210,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
Expand Down Expand Up @@ -590,6 +602,10 @@ def get_datasource_payload(data):
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']
Expand Down Expand Up @@ -730,7 +746,8 @@ def setup_module_object():
'camptocamp-prometheus-alertmanager-datasource',
'sni-thruk-datasource',
'redis-datasource',
'loki']),
'loki',
'alertmanager']),
ds_url=dict(type='str'),
access=dict(default='proxy', choices=['proxy', 'direct']),
database=dict(type='str', default=""),
Expand All @@ -756,6 +773,8 @@ def setup_module_object():
tsdb_resolution=dict(type='str', default='second', choices=['second', 'millisecond']),
sslmode=dict(default='disable', 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']),
aws_default_region=dict(default='us-east-1', choices=['ap-northeast-1', 'ap-northeast-2', 'ap-southeast-1', 'ap-southeast-2', 'ap-south-1',
'ca-central-1',
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.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

0 comments on commit 9f8a264

Please sign in to comment.