Skip to content

Commit 44057d8

Browse files
Merge pull request #507 from DrDroidLab/global_variable_condition
adds global variable conditions
2 parents c5d1fa0 + 0d4d2e1 commit 44057d8

29 files changed

+577
-49
lines changed

executor/playbook_result_conditional_evaluators/global_variable_evaluators/__init__.py

Whitespace-only changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from executor.playbook_result_conditional_evaluators.global_variable_evaluators.global_variable_evaluator import \
2+
GlobalVariableEvaluator
3+
from protos.base_pb2 import Operator
4+
from protos.playbooks.playbook_pb2 import PlaybookTaskExecutionLog
5+
from protos.playbooks.playbook_global_variable_evaluator_pb2 import GlobalVariableResultRule, CompareGlobalVariable
6+
from utils.proto_utils import proto_to_dict
7+
from utils.dict_utils import get_nested_value
8+
9+
class CompareGlobalVariableEvaluator(GlobalVariableEvaluator):
10+
11+
def evaluate(self, rule: GlobalVariableResultRule, playbook_task_execution_log: [PlaybookTaskExecutionLog]) -> bool:
12+
if rule.type != GlobalVariableResultRule.Type.COMPARE_GLOBAL_VARIABLE:
13+
raise ValueError(f'Rule type {GlobalVariableResultRule.Type.Name(rule.type)} not supported')
14+
15+
compare_global_variable: CompareGlobalVariable = rule.compare_global_variable
16+
operator = compare_global_variable.operator
17+
variable_name = compare_global_variable.variable_name.value
18+
threshold = compare_global_variable.threshold.value
19+
20+
global_variable_set = next(
21+
(tr.execution_global_variable_set for tr in playbook_task_execution_log), None)
22+
global_variable_set_dict = proto_to_dict(global_variable_set) if global_variable_set else {}
23+
value = get_nested_value(global_variable_set_dict, variable_name)
24+
25+
# compare current time with first member of cron schedules
26+
if operator == Operator.EQUAL_O:
27+
return value == threshold
28+
elif operator == Operator.GREATER_THAN_O:
29+
try:
30+
value = int(value)
31+
threshold = int(threshold)
32+
except ValueError:
33+
return False
34+
return value > threshold
35+
elif operator == Operator.GREATER_THAN_EQUAL_O:
36+
try:
37+
value = int(value)
38+
threshold = int(threshold)
39+
except ValueError:
40+
return False
41+
return value >= threshold
42+
elif operator == Operator.LESS_THAN_O:
43+
try:
44+
value = int(value)
45+
threshold = int(threshold)
46+
except ValueError:
47+
return False
48+
return value < threshold
49+
elif operator == Operator.LESS_THAN_EQUAL_O:
50+
try:
51+
value = int(value)
52+
threshold = int(threshold)
53+
except ValueError:
54+
return False
55+
return value <= threshold
56+
else:
57+
raise ValueError(f'Operator {Operator.Name(operator)} not supported')
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from protos.playbooks.playbook_pb2 import PlaybookTaskExecutionLog
2+
from protos.playbooks.playbook_global_variable_evaluator_pb2 import GlobalVariableResultRule
3+
4+
5+
class GlobalVariableEvaluator:
6+
7+
def evaluate(self, rule: GlobalVariableResultRule, playbook_task_execution_log: [PlaybookTaskExecutionLog]) -> bool:
8+
pass

executor/playbook_result_conditional_evaluators/step_condition_evaluator.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@
1212
TimeseriesResultEvaluator
1313
from executor.playbook_result_conditional_evaluators.task_result_evalutors.bash_command_result_evaluator import \
1414
BashCommandOutputResultEvaluator
15+
from executor.playbook_result_conditional_evaluators.global_variable_evaluators.compare_global_variable_evaluator import \
16+
CompareGlobalVariableEvaluator
1517
from protos.base_pb2 import LogicalOperator
1618
from protos.playbooks.playbook_commons_pb2 import PlaybookTaskResultType
1719
from protos.playbooks.playbook_pb2 import PlaybookStepResultCondition, PlaybookTaskResultRule, \
1820
PlaybookTaskExecutionLog
1921
from protos.playbooks.playbook_step_result_evaluator_pb2 import PlaybookStepResultRule
22+
from protos.playbooks.playbook_global_variable_evaluator_pb2 import GlobalVariableResultRule
2023

2124

2225
class StepConditionEvaluator:
2326

2427
def __init__(self):
2528
self._task_rule_map = {}
2629
self._step_rule_map = {}
30+
self._variable_rule_map = {}
2731

2832
def register_task_result_evaluator(self, result_type: PlaybookTaskResultType,
2933
task_result_evaluator: TaskResultEvaluator):
@@ -33,6 +37,10 @@ def register_step_result_evaluator(self, step_rule_type: PlaybookStepResultRule.
3337
step_result_evaluator: StepResultEvaluator):
3438
self._step_rule_map[step_rule_type] = step_result_evaluator
3539

40+
def register_variable_evaluator(self, variable_rule_type: GlobalVariableResultRule.Type,
41+
variable_evaluator: TaskResultEvaluator):
42+
self._variable_rule_map[variable_rule_type] = variable_evaluator
43+
3644
def evaluate(self, condition: PlaybookStepResultCondition,
3745
playbook_task_execution_log: [PlaybookTaskExecutionLog]) -> (bool, Dict):
3846
if not condition.rule_sets:
@@ -63,6 +71,15 @@ def evaluate(self, condition: PlaybookStepResultCondition,
6371
raise ValueError(f"Step result type {PlaybookStepResultRule.Type.Name(sr.type)} not supported")
6472
evaluation = step_result_evaluator.evaluate(sr, playbook_task_execution_log)
6573
all_evaluations.append(evaluation)
74+
75+
variable_rules: [PlaybookTaskResultRule] = rs.variable_rules
76+
for vr in variable_rules:
77+
variable_evaluator = self._variable_rule_map.get(vr.type)
78+
if not variable_evaluator:
79+
raise ValueError(f"Task result type {task_result.type} not supported")
80+
evaluation = variable_evaluator.evaluate(vr, playbook_task_execution_log)
81+
all_evaluations.append(evaluation)
82+
6683
logical_operator = rs.logical_operator
6784
if logical_operator == LogicalOperator.AND_LO:
6885
all_rs_evaluations.append(all(all_evaluations))
@@ -99,3 +116,5 @@ def evaluate(self, condition: PlaybookStepResultCondition,
99116

100117
step_condition_evaluator.register_step_result_evaluator(PlaybookStepResultRule.Type.COMPARE_TIME_WITH_CRON,
101118
CompareTimeWithCronEvaluator())
119+
step_condition_evaluator.register_variable_evaluator(GlobalVariableResultRule.Type.COMPARE_GLOBAL_VARIABLE,
120+
CompareGlobalVariableEvaluator())

executor/tasks.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ def execute_playbook_step_impl(tr: TimeRange, account: Account, step: PlaybookSt
7777
execution_global_variable_set = Struct()
7878
if global_variable_set and global_variable_set.items():
7979
execution_global_variable_set.CopyFrom(global_variable_set)
80+
81+
task_execution_global_variable_set = Struct()
82+
if global_variable_set and global_variable_set.items():
83+
task_execution_global_variable_set.CopyFrom(global_variable_set)
8084
for task_proto in tasks:
8185
if not execution_global_variable_set or not execution_global_variable_set.items():
8286
if task_proto.global_variable_set and task_proto.global_variable_set.items():
@@ -123,9 +127,9 @@ def execute_playbook_step_impl(tr: TimeRange, account: Account, step: PlaybookSt
123127
continue
124128
for bev in bulk_execution_var_values:
125129
if is_bulk_execution and bulk_task_var:
126-
execution_global_variable_set[bulk_task_var] = bev
130+
task_execution_global_variable_set[bulk_task_var] = bev
127131
try:
128-
task_result = playbook_source_facade.execute_task(account.id, tr, execution_global_variable_set,
132+
task_result = playbook_source_facade.execute_task(account.id, tr, task_execution_global_variable_set,
129133
task_proto)
130134
task_interpretation: InterpretationProto = task_result_interpret(interpreter_type, task_proto,
131135
task_result)

protos/playbooks/playbook.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import "protos/playbooks/intelligence_layer/interpreter.proto";
99

1010
import "protos/playbooks/playbook_task_result_evaluator.proto";
1111
import "protos/playbooks/playbook_step_result_evaluator.proto";
12+
import "protos/playbooks/playbook_global_variable_evaluator.proto";
1213

1314
import "protos/playbooks/source_task_definitions/cloudwatch_task.proto";
1415
import "protos/playbooks/source_task_definitions/grafana_task.proto";
@@ -118,6 +119,7 @@ message PlaybookStepResultCondition {
118119
LogicalOperator logical_operator = 1;
119120
repeated PlaybookTaskResultRule rules = 2;
120121
repeated PlaybookStepResultRule step_rules = 3;
122+
repeated GlobalVariableResultRule variable_rules = 4;
121123
}
122124
LogicalOperator logical_operator = 1;
123125
repeated RuleSet rule_sets = 2;

protos/playbooks/playbook_commons.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ enum PlaybookTaskResultType {
2222
BASH_COMMAND_OUTPUT = 4;
2323
TEXT = 5;
2424
LOGS = 6;
25+
GLOBAL_VARIABLE = 7;
2526
}
2627

2728
message ExternalLink {

0 commit comments

Comments
 (0)