Skip to content

Commit 36c7152

Browse files
Merge pull request #135 from jinyoungmoonDEV/master
fix: fix alarm actions logic
2 parents d9c184f + 4d168ad commit 36c7152

File tree

1 file changed

+40
-28
lines changed
  • src/spaceone/inventory/connector/aws_cloud_watch_connector

1 file changed

+40
-28
lines changed

src/spaceone/inventory/connector/aws_cloud_watch_connector/connector.py

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@
55
from spaceone.core.utils import *
66

77
from spaceone.inventory.connector.aws_cloud_watch_connector.schema.data import Alarms
8-
from spaceone.inventory.connector.aws_cloud_watch_connector.schema.resource import AlarmsResource, AlarmsResponse
9-
from spaceone.inventory.connector.aws_cloud_watch_connector.schema.service_type import CLOUD_SERVICE_TYPES
8+
from spaceone.inventory.connector.aws_cloud_watch_connector.schema.resource import (
9+
AlarmsResource,
10+
AlarmsResponse,
11+
)
12+
from spaceone.inventory.connector.aws_cloud_watch_connector.schema.service_type import (
13+
CLOUD_SERVICE_TYPES,
14+
)
1015
from spaceone.inventory.connector.aws_dynamodb_connector.schema.data import Table
11-
from spaceone.inventory.connector.aws_dynamodb_connector.schema.resource import TableResource
16+
from spaceone.inventory.connector.aws_dynamodb_connector.schema.resource import (
17+
TableResource,
18+
)
1219
from spaceone.inventory.libs.connector import SchematicAWSConnector
1320

1421

1522
_LOGGER = logging.getLogger(__name__)
1623

24+
1725
class CloudWatchConnector(SchematicAWSConnector):
18-
service_name = 'cloudwatch'
19-
cloud_service_group = 'CloudWatch'
26+
service_name = "cloudwatch"
27+
cloud_service_group = "CloudWatch"
2028
cloud_service_types = CLOUD_SERVICE_TYPES
2129
ComparisonOperator = {
2230
"GreaterThanOrEqualToThreshold": ">=",
@@ -25,11 +33,13 @@ class CloudWatchConnector(SchematicAWSConnector):
2533
"LessThanOrEqualToThreshold": "<=",
2634
"LessThanLowerOrGreaterThanUpperThreshold": "<>",
2735
"LessThanLowerThreshold": "<",
28-
"GreaterThanUpperThreshold": ">"
36+
"GreaterThanUpperThreshold": ">",
2937
}
3038

3139
def get_resources(self) -> List[TableResource]:
32-
_LOGGER.debug(f"[get_resources][account_id: {self.account_id}] START: CloudWatch")
40+
_LOGGER.debug(
41+
f"[get_resources][account_id: {self.account_id}] START: CloudWatch"
42+
)
3343
resources = []
3444
start_time = time.time()
3545

@@ -95,26 +105,28 @@ def request_alarms_data(self, region_name: str) -> List[Table]:
95105
"data": alarms_vo,
96106
"name": alarms_vo.name,
97107
"account": self.account_id,
98-
"tags": self.convert_tags_to_dict_type(tags)
108+
"tags": self.convert_tags_to_dict_type(tags),
99109
}
100110

101111
except Exception as e:
102-
error_resource_response = self.generate_error(
103-
region_name, "alarms", e
104-
)
112+
error_resource_response = self.generate_error(region_name, "alarms", e)
105113
yield {"data": error_resource_response}
106114

107115
def _set_alarm_conditions(self, raw_alarm: Alarms) -> None:
108116
metric_name = raw_alarm.get("MetricName", "?")
109117
period = raw_alarm["Period"]
110-
evaluation_periods = self._convert_int_type(raw_alarm.get("EvaluationPeriods", "?"))
118+
evaluation_periods = self._convert_int_type(
119+
raw_alarm.get("EvaluationPeriods", "?")
120+
)
111121
threshold = self._convert_int_type(raw_alarm.get("Threshold", "?"))
112122
comparison_operator = raw_alarm.get("ComparisonOperator", "?")
113123

114124
period_minutes = period // 60 if isinstance(period, int) else "?"
115125
operator = self.ComparisonOperator.get(comparison_operator, "?")
116126

117-
raw_alarm["conditions"] = f"{metric_name} {operator} {threshold} for {evaluation_periods} datapoionts within {period_minutes} minutes"
127+
raw_alarm["conditions"] = (
128+
f"{metric_name} {operator} {threshold} for {evaluation_periods} datapoionts within {period_minutes} minutes"
129+
)
118130

119131
def _set_alarm_actions(self, raw_alarm: Alarms) -> None:
120132
raw_alarm["actions"] = []
@@ -126,25 +138,25 @@ def _set_alarm_actions(self, raw_alarm: Alarms) -> None:
126138

127139
for action in alarm_actions:
128140
action_service = self._extract_arn_service(action)
129-
action_type = None
130-
action_description = None
131-
action_config = None
132141

133142
if action_service == "sns":
134143
action_type = "Notification"
135144
arn = action.split(":")[-1]
136-
action_description = f"When in alarm, send message to topic \"{arn}\""
145+
action_description = f'When in alarm, send message to topic "{arn}"'
137146
action_config = ""
138-
elif action_service == "lambda":
139-
# lambda_client = boto3.client("lambda", region_name=self.region_name, verify=BOTO3_HTTPS_VERIFIED)
140-
# lambda_response = lambda_client.get_function(FunctionName=action.split(':')[-1])
141-
pass
142-
elif action_service == "ec2":
143-
# ec2_client = boto3.client("ec2", region_name=self.region_name, verify=BOTO3_HTTPS_VERIFIED)
144-
# ec2_response = ec2_client.describe_instances(InstanceIds=action.split(':')[-1])
145-
pass
146-
147-
actions.append({"type": action_type, "description": action_description, "config": action_config})
147+
else:
148+
_LOGGER.debug(f"Other Actions Type Detected : {action}")
149+
action_type = action_service
150+
action_description = action.split(":")[-1]
151+
action_config = ""
152+
153+
actions.append(
154+
{
155+
"type": action_type,
156+
"description": action_description,
157+
"config": action_config,
158+
}
159+
)
148160
else:
149161
raw_alarm["actions_enabled"] = "No actions"
150162

@@ -175,7 +187,7 @@ def _set_alarm_history(self, raw_alarm: Alarms) -> None:
175187
{
176188
"date": alarm_history["Timestamp"],
177189
"type": alarm_history["HistoryItemType"],
178-
"description": alarm_history["HistorySummary"]
190+
"description": alarm_history["HistorySummary"],
179191
}
180192
)
181193

0 commit comments

Comments
 (0)