Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for retrieving dynamic system properties #1322

Merged
merged 2 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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)) {
nehaagarwal1 marked this conversation as resolved.
Show resolved Hide resolved
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)) {
nehaagarwal1 marked this conversation as resolved.
Show resolved Hide resolved
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
nehaagarwal1 marked this conversation as resolved.
Show resolved Hide resolved