From 1b8c3b75b7cf466dc641377a95871b794bd90630 Mon Sep 17 00:00:00 2001 From: Akash Anand Date: Tue, 17 Aug 2021 09:47:27 +0530 Subject: [PATCH 1/2] Defect : 1394815 | can't dynamically populate a variable from defined system property based on user input. --- .../transformers/AbstractInOutForTransformer.java | 5 +++++ .../lang/entities/bindings/ScriptFunction.java | 4 +++- .../cloudslang/lang/entities/constants/Regex.java | 3 +++ .../lang/entities/utils/ExpressionUtils.java | 7 +++++++ .../runtime/bindings/scripts/ScriptEvaluator.java | 14 ++++++++++++-- .../lang/runtime/services/ScriptsService.java | 3 ++- .../src/main/resources/scripts/get_sp_var.py | 8 ++++++++ 7 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 cloudslang-runtime/src/main/resources/scripts/get_sp_var.py diff --git a/cloudslang-compiler/src/main/java/io/cloudslang/lang/compiler/modeller/transformers/AbstractInOutForTransformer.java b/cloudslang-compiler/src/main/java/io/cloudslang/lang/compiler/modeller/transformers/AbstractInOutForTransformer.java index 2843ddde33..4b776b14c2 100644 --- a/cloudslang-compiler/src/main/java/io/cloudslang/lang/compiler/modeller/transformers/AbstractInOutForTransformer.java +++ b/cloudslang-compiler/src/main/java/io/cloudslang/lang/compiler/modeller/transformers/AbstractInOutForTransformer.java @@ -37,6 +37,11 @@ protected Accumulator extractFunctionData(Serializable... values) { functionDependencies.add(ScriptFunction.GET); } + boolean getSpVarFunctionFound = ExpressionUtils.matchGetSystemPropertyVariableFunction(expression); + if (getSpVarFunctionFound) { + functionDependencies.add(ScriptFunction.GET_SP_VAR); + } + for (ScriptFunction function : ScriptFunction.values()) { if (ExpressionUtils.matchesFunction(function, expression)) { functionDependencies.add(function); diff --git a/cloudslang-entities/src/main/java/io/cloudslang/lang/entities/bindings/ScriptFunction.java b/cloudslang-entities/src/main/java/io/cloudslang/lang/entities/bindings/ScriptFunction.java index eb223e6c71..77c6c37104 100644 --- a/cloudslang-entities/src/main/java/io/cloudslang/lang/entities/bindings/ScriptFunction.java +++ b/cloudslang-entities/src/main/java/io/cloudslang/lang/entities/bindings/ScriptFunction.java @@ -41,7 +41,9 @@ public enum ScriptFunction { CS_XPATH_QUERY("cs_xpath_query"), - CS_JSON_QUERY("cs_json_query"); + CS_JSON_QUERY("cs_json_query"), + + GET_SP_VAR("get_sp_var"); private final String value; diff --git a/cloudslang-entities/src/main/java/io/cloudslang/lang/entities/constants/Regex.java b/cloudslang-entities/src/main/java/io/cloudslang/lang/entities/constants/Regex.java index 17f8750f05..21f20ed5b4 100644 --- a/cloudslang-entities/src/main/java/io/cloudslang/lang/entities/constants/Regex.java +++ b/cloudslang-entities/src/main/java/io/cloudslang/lang/entities/constants/Regex.java @@ -98,5 +98,8 @@ public class Regex { public static final String DESCRIPTION_TOKEN = "#!"; public static final String DESCRIPTION_EMPTY_LINE = "(\\s*)(#!)(\\s*)"; + + // get_sp_var(flow_input) + public static final String GET_SP_VAR_REGEX = "get_sp_var\\(\\s*(.+)\\s*\\)"; //////////////// description end } diff --git a/cloudslang-entities/src/main/java/io/cloudslang/lang/entities/utils/ExpressionUtils.java b/cloudslang-entities/src/main/java/io/cloudslang/lang/entities/utils/ExpressionUtils.java index 658ab3919c..b699ce5d7e 100644 --- a/cloudslang-entities/src/main/java/io/cloudslang/lang/entities/utils/ExpressionUtils.java +++ b/cloudslang-entities/src/main/java/io/cloudslang/lang/entities/utils/ExpressionUtils.java @@ -36,6 +36,7 @@ import static io.cloudslang.lang.entities.constants.Regex.SYSTEM_PROPERTY_REGEX_WITH_DEFAULT_DOUBLE_QUOTE; import static io.cloudslang.lang.entities.constants.Regex.SYSTEM_PROPERTY_REGEX_WITH_DEFAULT_SINGLE_QUOTE; import static java.util.regex.Pattern.compile; +import static io.cloudslang.lang.entities.constants.Regex.GET_SP_VAR_REGEX; /** * @author Bonczidai Levente @@ -65,6 +66,8 @@ private ExpressionUtils() { private static final Map patternsMap = new HashMap<>(); + private static final Pattern GET_SP_VAR_PATTERN = compile(GET_SP_VAR_REGEX, Pattern.UNICODE_CHARACTER_CLASS); + static { addPattern(ScriptFunction.CHECK_EMPTY, CHECK_EMPTY_REGEX); addPattern(ScriptFunction.CS_APPEND, CS_APPEND_REGEX); @@ -131,4 +134,8 @@ public static boolean matchesFunction(ScriptFunction function, String expression return false; } + + public static boolean matchGetSystemPropertyVariableFunction(String text) { + return matchPattern(GET_SP_VAR_PATTERN, text); + } } diff --git a/cloudslang-runtime/src/main/java/io/cloudslang/lang/runtime/bindings/scripts/ScriptEvaluator.java b/cloudslang-runtime/src/main/java/io/cloudslang/lang/runtime/bindings/scripts/ScriptEvaluator.java index 974ab8372b..05702b322d 100644 --- a/cloudslang-runtime/src/main/java/io/cloudslang/lang/runtime/bindings/scripts/ScriptEvaluator.java +++ b/cloudslang-runtime/src/main/java/io/cloudslang/lang/runtime/bindings/scripts/ScriptEvaluator.java @@ -80,7 +80,13 @@ private Value doEvaluateExpressionJython(String expr, Set systemProperties, Set functionDependencies) { Map jythonContext = createJythonContext(context); - boolean systemPropertiesDefined = functionDependencies.contains(ScriptFunction.GET_SYSTEM_PROPERTY); + + boolean systemPropertiesDefined = false; + if (functionDependencies.contains(ScriptFunction.GET_SYSTEM_PROPERTY) || + functionDependencies.contains(ScriptFunction.GET_SP_VAR)) { + systemPropertiesDefined = true; + } + if (systemPropertiesDefined) { jythonContext.put(SYSTEM_PROPERTIES_MAP, (Serializable) prepareSystemPropertiesForJython(systemProperties)); @@ -93,7 +99,11 @@ private Value doEvaluateExpressionExternalPython(String expr, Set systemProperties, Set functionDependencies) { Map pythonContext = createExternalPythonContext(context); - boolean systemPropertiesDefined = functionDependencies.contains(ScriptFunction.GET_SYSTEM_PROPERTY); + boolean systemPropertiesDefined = false; + if (functionDependencies.contains(ScriptFunction.GET_SYSTEM_PROPERTY) || + functionDependencies.contains(ScriptFunction.GET_SP_VAR)) { + systemPropertiesDefined = true; + } if (systemPropertiesDefined) { pythonContext.put(SYSTEM_PROPERTIES_MAP, (Serializable) prepareSystemPropertiesForExternalPython(systemProperties)); diff --git a/cloudslang-runtime/src/main/java/io/cloudslang/lang/runtime/services/ScriptsService.java b/cloudslang-runtime/src/main/java/io/cloudslang/lang/runtime/services/ScriptsService.java index b3ad05e51d..cc82a8d75d 100644 --- a/cloudslang-runtime/src/main/java/io/cloudslang/lang/runtime/services/ScriptsService.java +++ b/cloudslang-runtime/src/main/java/io/cloudslang/lang/runtime/services/ScriptsService.java @@ -45,7 +45,8 @@ public void init() throws IOException { ScriptFunction.CS_ROUND, ScriptFunction.CS_SUBSTRING, ScriptFunction.CS_TO_UPPER, - ScriptFunction.CS_TO_LOWER + ScriptFunction.CS_TO_LOWER, + ScriptFunction.GET_SP_VAR ); for (ScriptFunction function: list) { diff --git a/cloudslang-runtime/src/main/resources/scripts/get_sp_var.py b/cloudslang-runtime/src/main/resources/scripts/get_sp_var.py new file mode 100644 index 0000000000..ef9446c5e3 --- /dev/null +++ b/cloudslang-runtime/src/main/resources/scripts/get_sp_var.py @@ -0,0 +1,8 @@ +def get_sp_var(key): + try: + value1 = get_from_smaller_context(key) + except NameError: + value1 = globals().get(key) + accessed(value1) + property_value = sys_prop.get(value1) + return property_value \ No newline at end of file From bdb0cc8e3c0ca9b31f4ecc7b10915ef04d8a99bc Mon Sep 17 00:00:00 2001 From: Akash Anand Date: Wed, 18 Aug 2021 08:39:58 +0530 Subject: [PATCH 2/2] Reviewed Changes : 1) Added || statement in new line 2) Extra new line at the end of the file get_sp_var.py --- .../lang/runtime/bindings/scripts/ScriptEvaluator.java | 8 ++++---- .../src/main/resources/scripts/get_sp_var.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cloudslang-runtime/src/main/java/io/cloudslang/lang/runtime/bindings/scripts/ScriptEvaluator.java b/cloudslang-runtime/src/main/java/io/cloudslang/lang/runtime/bindings/scripts/ScriptEvaluator.java index 05702b322d..f825154056 100644 --- a/cloudslang-runtime/src/main/java/io/cloudslang/lang/runtime/bindings/scripts/ScriptEvaluator.java +++ b/cloudslang-runtime/src/main/java/io/cloudslang/lang/runtime/bindings/scripts/ScriptEvaluator.java @@ -82,8 +82,8 @@ private Value doEvaluateExpressionJython(String expr, Map jythonContext = createJythonContext(context); boolean systemPropertiesDefined = false; - if (functionDependencies.contains(ScriptFunction.GET_SYSTEM_PROPERTY) || - functionDependencies.contains(ScriptFunction.GET_SP_VAR)) { + if (functionDependencies.contains(ScriptFunction.GET_SYSTEM_PROPERTY) + || functionDependencies.contains(ScriptFunction.GET_SP_VAR)) { systemPropertiesDefined = true; } @@ -100,8 +100,8 @@ private Value doEvaluateExpressionExternalPython(String expr, Set functionDependencies) { Map pythonContext = createExternalPythonContext(context); boolean systemPropertiesDefined = false; - if (functionDependencies.contains(ScriptFunction.GET_SYSTEM_PROPERTY) || - functionDependencies.contains(ScriptFunction.GET_SP_VAR)) { + if (functionDependencies.contains(ScriptFunction.GET_SYSTEM_PROPERTY) + || functionDependencies.contains(ScriptFunction.GET_SP_VAR)) { systemPropertiesDefined = true; } if (systemPropertiesDefined) { diff --git a/cloudslang-runtime/src/main/resources/scripts/get_sp_var.py b/cloudslang-runtime/src/main/resources/scripts/get_sp_var.py index ef9446c5e3..3e6cda5b9c 100644 --- a/cloudslang-runtime/src/main/resources/scripts/get_sp_var.py +++ b/cloudslang-runtime/src/main/resources/scripts/get_sp_var.py @@ -5,4 +5,4 @@ def get_sp_var(key): value1 = globals().get(key) accessed(value1) property_value = sys_prop.get(value1) - return property_value \ No newline at end of file + return property_value