Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,57 @@
process the expression and returns the processed values
"""
import ast
import operator
import re
from processor.logging.log_handler import getlogger

logger = getlogger()

# Supported comparison operators for condition evaluation
_COMPARISON_OPS = {
"<=": operator.le,
">=": operator.ge,
"!=": operator.ne,
"==": operator.eq,
"<": operator.lt,
">": operator.gt,
}

def _evaluate_condition(condition):
"""
Evaluate a condition string that may contain comparison operators.
Returns the boolean result of the condition.
"""
# First try literal eval for simple true/false/numeric values
try:
return ast.literal_eval(condition)
except (ValueError, SyntaxError):
pass

# Try to evaluate comparison expressions like "2 < 1", "0 >= 1", etc.
for op_str, op_func in _COMPARISON_OPS.items():
parts = condition.split(op_str)
if len(parts) == 2:
left, right = parts[0].strip(), parts[1].strip()
try:
left_val = ast.literal_eval(left)
right_val = ast.literal_eval(right)
return op_func(left_val, right_val)
except (ValueError, SyntaxError):
continue

# Fallback: treat non-empty string as truthy
return bool(condition)


def conditional_expression(expression):
"""
perform the condition operation on provided expression and returns the result
"""
expression_list = expression.split(" ? ")
expression_list = expression.split(" ? ", 1)
condition = expression_list[0]
true_value = expression_list[1].split(" : ")[0]
false_value = expression_list[1].split(" : ")[1]
rest = expression_list[1]
true_value, false_value = rest.split(" : ", 1)
try:
ast.literal_eval(true_value)
except (ValueError, SyntaxError):
Expand All @@ -23,10 +61,7 @@ def conditional_expression(expression):
ast.literal_eval(false_value)
except (ValueError, SyntaxError):
false_value = f'"{false_value}"'
try:
condition_result = ast.literal_eval(condition)
except (ValueError, SyntaxError):
condition_result = bool(condition)
condition_result = _evaluate_condition(condition)
try:
response = ast.literal_eval(true_value) if condition_result else ast.literal_eval(false_value)
return response, True
Expand Down
9 changes: 5 additions & 4 deletions src/processor/templates/terraform/terraform_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,10 +880,11 @@ def process_resource(self, resource, count=None, nested_string_params=None):
else:
parameters.append(processed_param)

args = inspect.getargspec(func['method']).args
varargs = inspect.getargspec(func['method']).varargs
keywords = inspect.getargspec(func['method']).keywords
defaults = inspect.getargspec(func['method']).defaults
argspec = inspect.getfullargspec(func['method'])
args = argspec.args
varargs = argspec.varargs
keywords = argspec.varkw
defaults = argspec.defaults

if process and ((len(args) == len(parameters)) or \
(defaults and len(parameters) <= len(args) and len(parameters) >= (len(args) - len(defaults))) or \
Expand Down
Loading