Skip to content

Commit 963a300

Browse files
committed
feat: Add if condition(Conditional Resource Evaluation)
1 parent 6828e9f commit 963a300

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

stackql_deploy/cmd/build.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
run_ext_script,
66
get_type
77
)
8-
from ..lib.config import get_full_context
8+
from ..lib.config import get_full_context, render_value
99
from ..lib.templating import get_queries
1010
from .base import StackQLBase
1111

@@ -49,6 +49,20 @@ def run(self, dry_run, show_queries, on_failure):
4949
# get full context
5050
full_context = get_full_context(self.env, self.global_context, resource, self.logger)
5151

52+
# Check if the resource has an 'if' condition and evaluate it
53+
if 'if' in resource:
54+
condition = resource['if']
55+
try:
56+
# Render the condition with the full context to resolve any template variables
57+
rendered_condition = render_value(self.env, condition, full_context, self.logger)
58+
# Evaluate the condition
59+
condition_result = eval(rendered_condition)
60+
if not condition_result:
61+
self.logger.info(f"skipping resource [{resource['name']}] due to condition: {condition}")
62+
continue
63+
except Exception as e:
64+
catch_error_and_exit(f"error evaluating condition for resource [{resource['name']}]: {e}", self.logger)
65+
5266
if type == 'script':
5367
self.process_script_resource(resource, dry_run, full_context)
5468
continue

stackql_deploy/cmd/teardown.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
catch_error_and_exit,
44
get_type
55
)
6-
from ..lib.config import get_full_context
6+
from ..lib.config import get_full_context, render_value
77
from ..lib.templating import get_queries
88
from .base import StackQLBase
99

@@ -63,6 +63,19 @@ def run(self, dry_run, show_queries, on_failure):
6363
# get full context
6464
full_context = get_full_context(self.env, self.global_context, resource, self.logger)
6565

66+
# Check if the resource has an 'if' condition and evaluate it
67+
if 'if' in resource:
68+
condition = resource['if']
69+
try:
70+
# Render the condition with the full context to resolve any template variables
71+
rendered_condition = render_value(self.env, condition, full_context, self.logger)
72+
# Evaluate the condition
73+
condition_result = eval(rendered_condition)
74+
if not condition_result:
75+
self.logger.info(f"skipping resource [{resource['name']}] due to condition: {condition}")
76+
continue
77+
except Exception as e:
78+
catch_error_and_exit(f"error evaluating condition for resource [{resource['name']}]: {e}", self.logger)
6679
# add reverse export map variable to full context
6780
if 'exports' in resource:
6881
for export in resource['exports']:

0 commit comments

Comments
 (0)