-
Notifications
You must be signed in to change notification settings - Fork 83
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
Feature: Add silence datasource #403
base: main
Are you sure you want to change the base?
Changes from all commits
4c7340c
a13d474
88b46d0
539d502
fd004e1
e59c96a
d29a845
1dcea66
4e0cbc1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
minor_changes: | ||
- Add the ability to define which alertmanager datasource to target in grafana_silence |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -69,6 +69,12 @@ | |||||||||||||||||
type: list | ||||||||||||||||||
elements: dict | ||||||||||||||||||
required: true | ||||||||||||||||||
alertmanager_datasource: | ||||||||||||||||||
description: | ||||||||||||||||||
- Which alertmanager datasource to target | ||||||||||||||||||
type: str | ||||||||||||||||||
required: false | ||||||||||||||||||
default: grafana | ||||||||||||||||||
state: | ||||||||||||||||||
description: | ||||||||||||||||||
- Delete the first occurrence of a silence with the same settings. Can be "absent" or "present". | ||||||||||||||||||
|
@@ -104,6 +110,22 @@ | |||||||||||||||||
value: test | ||||||||||||||||||
state: present | ||||||||||||||||||
|
||||||||||||||||||
- name: Create a silence against a specific datasource | ||||||||||||||||||
community.grafana.grafana_silence: | ||||||||||||||||||
grafana_url: "https://grafana.example.com" | ||||||||||||||||||
grafana_api_key: "{{ some_api_token_value }}" | ||||||||||||||||||
comment: "a testcomment" | ||||||||||||||||||
created_by: "me" | ||||||||||||||||||
starts_at: "2029-07-29T08:45:45.000Z" | ||||||||||||||||||
ends_at: "2029-07-29T08:55:45.000Z" | ||||||||||||||||||
matchers: | ||||||||||||||||||
- isEqual: true | ||||||||||||||||||
isRegex: true | ||||||||||||||||||
name: environment | ||||||||||||||||||
value: test | ||||||||||||||||||
alertmanager_datasource: exampleDS | ||||||||||||||||||
state: present | ||||||||||||||||||
|
||||||||||||||||||
- name: Delete a silence | ||||||||||||||||||
community.grafana.grafana_silence: | ||||||||||||||||||
grafana_url: "https://grafana.example.com" | ||||||||||||||||||
|
@@ -196,6 +218,8 @@ | |||||||||||||||||
self._module = module | ||||||||||||||||||
self.grafana_url = base.clean_url(module.params.get("url")) | ||||||||||||||||||
self.org_id = None | ||||||||||||||||||
# Default here because you can't look up "grafana" DS via API | ||||||||||||||||||
self.alertmanager_path = "grafana" | ||||||||||||||||||
Comment on lines
+221
to
+222
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
# {{{ Authentication header | ||||||||||||||||||
self.headers = {"Content-Type": "application/json"} | ||||||||||||||||||
if module.params.get("grafana_api_key", None): | ||||||||||||||||||
|
@@ -225,6 +249,11 @@ | |||||||||||||||||
msg="Silences API is available starting with Grafana v8", | ||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
if module.params.get("alertmanager_datasource", None): | ||||||||||||||||||
self.alertmanager_path = self.datasource_by_name( | ||||||||||||||||||
module.params["alertmanager_datasource"] | ||||||||||||||||||
) | ||||||||||||||||||
Comment on lines
+252
to
+255
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
def _send_request(self, url, data=None, headers=None, method="GET"): | ||||||||||||||||||
if data is not None: | ||||||||||||||||||
data = json.dumps(data) | ||||||||||||||||||
|
@@ -268,6 +297,16 @@ | |||||||||||||||||
failed=True, msg="Current user isn't member of organization: %s" % org_name | ||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
def datasource_by_name(self, datasource_name): | ||||||||||||||||||
url = f"/api/datasources/name/{datasource_name}" | ||||||||||||||||||
datasource = self._send_request(url, headers=self.headers, method="GET") | ||||||||||||||||||
if datasource: | ||||||||||||||||||
return datasource["uid"] | ||||||||||||||||||
|
||||||||||||||||||
return self._module.fail_json( | ||||||||||||||||||
failed=True, msg=f"Datasource not found: {datasource_name}" | ||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
def get_version(self): | ||||||||||||||||||
url = "/api/health" | ||||||||||||||||||
response = self._send_request( | ||||||||||||||||||
|
@@ -280,7 +319,7 @@ | |||||||||||||||||
raise GrafanaError("Failed to retrieve version from '%s'" % url) | ||||||||||||||||||
|
||||||||||||||||||
def create_silence(self, comment, created_by, starts_at, ends_at, matchers): | ||||||||||||||||||
url = "/api/alertmanager/grafana/api/v2/silences" | ||||||||||||||||||
url = f"/api/alertmanager/{self.alertmanager_path}/api/v2/silences" | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
silence = dict( | ||||||||||||||||||
comment=comment, | ||||||||||||||||||
createdBy=created_by, | ||||||||||||||||||
|
@@ -297,7 +336,7 @@ | |||||||||||||||||
return response | ||||||||||||||||||
|
||||||||||||||||||
def get_silence(self, comment, created_by, starts_at, ends_at, matchers): | ||||||||||||||||||
url = "/api/alertmanager/grafana/api/v2/silences" | ||||||||||||||||||
url = f"/api/alertmanager/{self.alertmanager_path}/api/v2/silences" | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
responses = self._send_request(url, headers=self.headers, method="GET") | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -313,21 +352,17 @@ | |||||||||||||||||
return None | ||||||||||||||||||
|
||||||||||||||||||
def get_silence_by_id(self, silence_id): | ||||||||||||||||||
url = "/api/alertmanager/grafana/api/v2/silence/{SilenceId}".format( | ||||||||||||||||||
SilenceId=silence_id | ||||||||||||||||||
) | ||||||||||||||||||
url = f"/api/alertmanager/{self.alertmanager_path}/api/v2/silence/{silence_id}" | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
response = self._send_request(url, headers=self.headers, method="GET") | ||||||||||||||||||
return response | ||||||||||||||||||
|
||||||||||||||||||
def get_silences(self): | ||||||||||||||||||
url = "/api/alertmanager/grafana/api/v2/silences" | ||||||||||||||||||
url = f"/api/alertmanager/{self.alertmanager_path}/api/v2/silences" | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
response = self._send_request(url, headers=self.headers, method="GET") | ||||||||||||||||||
return response | ||||||||||||||||||
|
||||||||||||||||||
def delete_silence(self, silence_id): | ||||||||||||||||||
url = "/api/alertmanager/grafana/api/v2/silence/{SilenceId}".format( | ||||||||||||||||||
SilenceId=silence_id | ||||||||||||||||||
) | ||||||||||||||||||
url = f"/api/alertmanager/{self.alertmanager_path}/api/v2/silence/{silence_id}" | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
response = self._send_request(url, headers=self.headers, method="DELETE") | ||||||||||||||||||
return response | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -352,6 +387,7 @@ | |||||||||||||||||
org_name=dict(type="str"), | ||||||||||||||||||
skip_version_check=dict(type="bool", default=False), | ||||||||||||||||||
starts_at=dict(type="str", required=True), | ||||||||||||||||||
alertmanager_datasource=dict(type=str), | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
state=dict(type="str", choices=["present", "absent"], default="present"), | ||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.