Skip to content

Commit

Permalink
refactor: silence payload with new id args
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemental committed Jul 3, 2024
1 parent 7788147 commit b1cecfd
Showing 1 changed file with 43 additions and 53 deletions.
96 changes: 43 additions & 53 deletions plugins/modules/grafana_silence.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,8 @@ def get_version(self):
return {"major": int(major), "minor": int(minor), "rev": int(rev)}
raise GrafanaError("Failed to retrieve version from '%s'" % url)

def create_silence(self, comment, created_by, starts_at, ends_at, matchers):
def create_silence(self, silence):
url = "/api/alertmanager/grafana/api/v2/silences"
silence = dict(
comment=comment,
createdBy=created_by,
endsAt=ends_at,
matchers=matchers,
startsAt=starts_at,
)
response = self._send_request(
url, data=silence, headers=self.headers, method="POST"
)
Expand All @@ -343,33 +336,25 @@ def create_silence(self, comment, created_by, starts_at, ends_at, matchers):
response.pop("id", None)
return response

def get_silence(self, comment, created_by, starts_at, ends_at, matchers):
url = "/api/alertmanager/grafana/api/v2/silences"

responses = self._send_request(url, headers=self.headers, method="GET")

for response in responses:
if (
response["comment"] == comment
and response["createdBy"] == created_by
and response["startsAt"] == starts_at
and response["endsAt"] == ends_at
and response["matchers"] == matchers
):
return response
return None

def get_silence_by_id(self, silence_id):
url = "/api/alertmanager/grafana/api/v2/silence/{SilenceId}".format(
SilenceId=silence_id
)
response = self._send_request(url, headers=self.headers, method="GET")
return response

def get_silences(self):
url = "/api/alertmanager/grafana/api/v2/silences"
response = self._send_request(url, headers=self.headers, method="GET")
return response
def get_silence(self, silence):
if silence["silenceID"]:
url = "/api/alertmanager/grafana/api/v2/silence/%s" % silence["silenceID"]
response = self._send_request(url, headers=self.headers, method="GET")
return response
else:
url = "/api/alertmanager/grafana/api/v2/silences"
response = self._send_request(url, headers=self.headers, method="GET")

for resp in response:
if (
resp["comment"] == silence["comment"]
and resp["createdBy"] == silence["createdBy"]
and resp["startsAt"] == silence["startsAt"]
and resp["endsAt"] == silence["endsAt"]
and resp["matchers"] == silence["matchers"]
):
return resp
return None

def delete_silence(self, silence_id):
url = "/api/alertmanager/grafana/api/v2/silence/{SilenceId}".format(
Expand All @@ -384,7 +369,10 @@ def setup_module_object():
argument_spec=argument_spec,
supports_check_mode=False,
required_together=base.grafana_required_together(),
mutually_exclusive=base.grafana_mutually_exclusive(),
mutually_exclusive=base.grafana_mutually_exclusive()
+ [
["ends_at", "duration"],
],
)
return module

Expand All @@ -407,29 +395,31 @@ def setup_module_object():

def main():
module = setup_module_object()
comment = module.params["comment"]
created_by = module.params["created_by"]
duration = module.params.get("duration")
ends_at = module.params.get("ends_at")
matchers = module.params["matchers"]
silence_id = module.params.get("id")
starts_at = module.params["starts_at"]
state = module.params["state"]

grafana_iface = GrafanaSilenceInterface(module)
changed = False
failed = False
grafana_iface = GrafanaSilenceInterface(module)

silence = grafana_iface.get_silence(
comment, created_by, starts_at, ends_at, matchers
)
silence_payload = {
"comment": module.params.get("comment"),
"createdBy": module.params.get("created_by"),
"matchers": module.params.get("matchers"),
"startsAt": module.params.get("starts_at"),
"silenceID": module.params.get("silence_id"),
}

if module.params.get("ends_at"):
silence_payload["endsAt"] = module.params.get("ends_at")
elif module.params.get("duration"):
silence_payload["duration"] = module.params.get("duration")

silence = grafana_iface.get_silence(silence_payload)

state = module.params.get("state")

if state == "present":
if not silence:
silence = grafana_iface.create_silence(
comment, created_by, starts_at, ends_at, matchers
)
silence = grafana_iface.get_silence_by_id(silence["silenceID"])
silence = grafana_iface.create_silence(silence_payload)
silence = grafana_iface.get_silence(silence_payload)
changed = True
else:
module.exit_json(
Expand Down

0 comments on commit b1cecfd

Please sign in to comment.