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 b011e5db..b1a3a6e4 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: @@ -229,6 +230,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 @@ -637,6 +649,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"] @@ -811,6 +829,8 @@ def setup_module_object(): "loki", "tempo", "quickwit-quickwit-datasource", + "loki", + "alertmanager", ] ), ds_url=dict(type="str"), @@ -850,6 +870,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