Skip to content

Commit

Permalink
fix(providers): check if provided sns_topic already exists or not (ke…
Browse files Browse the repository at this point in the history
  • Loading branch information
pehlicd authored Mar 18, 2024
1 parent 5bf86ad commit bbaed9f
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions keep/providers/cloudwatch_provider/cloudwatch_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def setup_webhook(
actions = alarm.get("AlarmActions", [])
# extract only SNS actions
topics = [action for action in actions if action.startswith("arn:aws:sns")]
# if we got explicitly SNS topic, add is as an action
# if we got explicitly SNS topic, add it as an action
if self.authentication_config.cloudwatch_sns_topic:
self.logger.warning(
"Cannot hook alarm without SNS topic, trying to add SNS action..."
Expand All @@ -418,22 +418,32 @@ def setup_webhook(
else:
sns_topic = self.authentication_config.cloudwatch_sns_topic
actions.append(sns_topic)
try:
alarm["AlarmActions"] = actions
# filter out irrelevant files
filtered_alarm = {
k: v
for k, v in alarm.items()
if k in CloudwatchProvider.VALID_ALARM_KEYS
}
cloudwatch_client.put_metric_alarm(**filtered_alarm)
# now it should contain the SNS topic
topics = [sns_topic]
except Exception:
self.logger.exception(
"Error adding SNS action to alarm %s", alarm.get("AlarmName")
# if the alarm already has the SNS topic as action, we don't need to add it again
if sns_topic in actions:
self.logger.info(
"SNS action already added to alarm %s, skipping...",
alarm.get("AlarmName"),
)
continue
else:
self.logger.info(
"Adding SNS action to alarm %s...", alarm.get("AlarmName")
)
try:
alarm["AlarmActions"] = actions
# filter out irrelevant files
filtered_alarm = {
k: v
for k, v in alarm.items()
if k in CloudwatchProvider.VALID_ALARM_KEYS
}
cloudwatch_client.put_metric_alarm(**filtered_alarm)
# now it should contain the SNS topic
topics = [sns_topic]
except Exception:
self.logger.exception(
"Error adding SNS action to alarm %s", alarm.get("AlarmName")
)
continue
self.logger.info(
"SNS action added to alarm %s!", alarm.get("AlarmName")
)
Expand Down

0 comments on commit bbaed9f

Please sign in to comment.