Skip to content

Commit

Permalink
Merge pull request #1322 from CloudSlang/akash/cpe_1394815
Browse files Browse the repository at this point in the history
Adding support for retrieving dynamic system properties
  • Loading branch information
sashutosh authored Aug 18, 2021
2 parents cd29a83 + bdb0cc8 commit b6d2b60
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -65,6 +66,8 @@ private ExpressionUtils() {

private static final Map<ScriptFunction, Pattern> 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);
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ private Value doEvaluateExpressionJython(String expr,
Set<SystemProperty> systemProperties,
Set<ScriptFunction> functionDependencies) {
Map<String, Serializable> 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));
Expand All @@ -93,7 +99,11 @@ private Value doEvaluateExpressionExternalPython(String expr,
Set<SystemProperty> systemProperties,
Set<ScriptFunction> functionDependencies) {
Map<String, Serializable> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions cloudslang-runtime/src/main/resources/scripts/get_sp_var.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b6d2b60

Please sign in to comment.