Skip to content

Commit

Permalink
Merge pull request #121 from whdalsrnt/master
Browse files Browse the repository at this point in the history
feat: add change_escalation_policy action to EventRule
  • Loading branch information
whdalsrnt authored Mar 29, 2024
2 parents a568d7f + d384ce4 commit 47e87e5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/spaceone/monitoring/manager/event_rule_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ def _change_event_data_with_actions(self, event_data, actions, domain_id):
if action == "change_urgency":
event_data["urgency"] = value

if action == "change_escalation_policy":
event_data["escalation_policy_id"] = value

if action == "add_additional_info":
event_data["additional_info"] = event_data.get("additional_info", {})
event_data["additional_info"].update(value)
Expand Down
42 changes: 35 additions & 7 deletions src/spaceone/monitoring/service/event_rule_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from spaceone.monitoring.error.event_rule import *
from spaceone.monitoring.manager.event_rule_manager import EventRuleManager
from spaceone.monitoring.manager.identity_manager import IdentityManager
from spaceone.monitoring.manager.escalation_policy_manager import (
EscalationPolicyManager,
)
from spaceone.monitoring.model.event_rule_model import EventRule

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -38,6 +41,9 @@ def __init__(self, *args, **kwargs):
self.event_rule_mgr: EventRuleManager = self.locator.get_manager(
"EventRuleManager"
)
self.escalation_policy_manager: EscalationPolicyManager = (
self.locator.get_manager("EscalationPolicyManager")
)

@transaction(
permission="monitoring:EventRule.write",
Expand Down Expand Up @@ -91,7 +97,7 @@ def create(self, params: dict) -> EventRule:
project_id = params["project_id"]

self._check_conditions(params["conditions"])
self._check_actions(params["actions"])
self._check_actions(params["actions"], domain_id, workspace_id, identity_mgr)

params["order"] = (
self._get_highest_order(resource_group, project_id, domain_id, workspace_id)
Expand Down Expand Up @@ -131,15 +137,22 @@ def update(self, params: dict) -> EventRule:
workspace_id = params["workspace_id"]
user_projects = params.get("user_projects")

event_rule_vo = self.event_rule_mgr.get_event_rule(
event_rule_id, domain_id, workspace_id, user_projects
)

if "conditions" in params:
self._check_conditions(params["conditions"])

if "actions" in params:
self._check_actions(params["actions"])
identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager")
self._check_actions(
params["actions"],
domain_id,
workspace_id,
identity_mgr,
)

event_rule_vo = self.event_rule_mgr.get_event_rule(
event_rule_id, domain_id, workspace_id, user_projects
)
return self.event_rule_mgr.update_event_rule_by_vo(params, event_rule_vo)

@transaction(
Expand Down Expand Up @@ -382,14 +395,29 @@ def _check_conditions(conditions: list) -> None:
f'({" | ".join(_SUPPORTED_CONDITION_OPERATORS)})',
)

@staticmethod
def _check_actions(actions: dict) -> None:
def _check_actions(
self,
actions: dict,
domain_id: str,
workspace_id: str,
identity_mgr: IdentityManager,
) -> None:
if "change_urgency" in actions:
if actions["change_urgency"] not in ["HIGH", "LOW"]:
raise ERROR_INVALID_PARAMETER(
key="actions.change_urgency",
reason=f"Unsupported urgency. (HIGH | LOW)",
)
elif "change_assignee" in actions:
pass
elif "change_project" in actions:
change_project_id = actions["change_project"]
identity_mgr.get_project(change_project_id, domain_id)
elif "change_escalation_policy" in actions:
change_es_id = actions["change_escalation_policy"]
self.escalation_policy_manager.get_escalation_policy(
change_es_id, workspace_id, domain_id
)

@staticmethod
def _check_order(order: int) -> None:
Expand Down
39 changes: 31 additions & 8 deletions src/spaceone/monitoring/service/event_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
)
from spaceone.monitoring.manager.webhook_manager import WebhookManager
from spaceone.monitoring.manager.webhook_plugin_manager import WebhookPluginManager
from spaceone.monitoring.manager.escalation_policy_manager import (
EscalationPolicyManager,
)
from spaceone.monitoring.model.alert_model import Alert
from spaceone.monitoring.model.escalation_policy_model import EscalationPolicy
from spaceone.monitoring.model.event_model import Event
Expand Down Expand Up @@ -311,6 +314,7 @@ def _create_alert(self, event_data):
event_data["project_id"],
event_data["workspace_id"],
event_data["domain_id"],
event_data.get("escalation_policy_id", ""),
)

alert_data["escalation_policy_id"] = escalation_policy_id
Expand Down Expand Up @@ -340,14 +344,33 @@ def _get_urgency_from_severity(severity):
else:
return "LOW"

@cache.cacheable(key="escalation-policy-info:{domain_id}:{project_id}", expire=300)
def _get_escalation_policy_info(self, project_id, workspace_id, domain_id):
project_alert_config_vo: ProjectAlertConfig = self._get_project_alert_config(
project_id, workspace_id, domain_id
)
escalation_policy_vo: EscalationPolicy = (
project_alert_config_vo.escalation_policy
)
@cache.cacheable(
key="escalation-policy-info:{domain_id}:{workspace_id}:{project_id}:{escalation_policy_id}",
expire=300,
)
def _get_escalation_policy_info(
self,
project_id: str,
workspace_id: str,
domain_id: str,
escalation_policy_id: str,
):
if escalation_policy_id != "":
escalation_policy_mgr: EscalationPolicyManager = self.locator.get_manager(
"EscalationPolicyManager"
)
escalation_policy_vo: EscalationPolicy = (
escalation_policy_mgr.get_escalation_policy(
escalation_policy_id, workspace_id, domain_id
)
)
else:
project_alert_config_vo: ProjectAlertConfig = (
self._get_project_alert_config(project_id, workspace_id, domain_id)
)
escalation_policy_vo: EscalationPolicy = (
project_alert_config_vo.escalation_policy
)

return (
escalation_policy_vo.escalation_policy_id,
Expand Down

0 comments on commit 47e87e5

Please sign in to comment.