Skip to content

Commit

Permalink
Merge pull request #1421 from CloudSlang/magic-cs-var
Browse files Browse the repository at this point in the history
add support for cs magic var
  • Loading branch information
pentektamas authored Nov 7, 2023
2 parents 3a703b6 + feef84e commit a712a36
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public enum ScriptFunction {

CS_JSON_QUERY("cs_json_query"),

GET_SP_VAR("get_sp_var");
GET_SP_VAR("get_sp_var"),

GET_USER_ID("get_user_id"),

GET_WORKER_GROUP("get_worker_group");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class Regex {
public static final String CS_SUBSTRING_REGEX = "cs_substring\\((.+?),(.+?)(,(.+?))?\\)";
public static final String CS_TO_UPPER_REGEX = "cs_to_upper\\((.+?)\\)";
public static final String CS_TO_LOWER_REGEX = "cs_to_lower\\((.+?)\\)";
public static final String GET_USER_ID_REGEX = "get_user_id()";
public static final String GET_WORKER_GROUP_REGEX = "get_worker_group()";

//////////////// description
public static final String DESCRIPTION_START_LINE = "(\\s*)(#!!)(([^#])(.*))*";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import static io.cloudslang.lang.entities.constants.Regex.GET_SP_VAR_REGEX_WITHOUT_QUOTES;
import static io.cloudslang.lang.entities.constants.Regex.GET_SP_VAR_REGEX_WITH_DOUBLE_QUOTES;
import static io.cloudslang.lang.entities.constants.Regex.GET_SP_VAR_REGEX_WITH_SINGLE_QUOTES;
import static io.cloudslang.lang.entities.constants.Regex.GET_USER_ID_REGEX;
import static io.cloudslang.lang.entities.constants.Regex.GET_WORKER_GROUP_REGEX;
import static io.cloudslang.lang.entities.constants.Regex.SYSTEM_PROPERTY_REGEX_DOUBLE_QUOTE;
import static io.cloudslang.lang.entities.constants.Regex.SYSTEM_PROPERTY_REGEX_SINGLE_QUOTE;
import static io.cloudslang.lang.entities.constants.Regex.SYSTEM_PROPERTY_REGEX_WITHOUT_QUOTES;
Expand Down Expand Up @@ -95,6 +97,8 @@ private ExpressionUtils() {
addPattern(ScriptFunction.CS_SUBSTRING, CS_SUBSTRING_REGEX);
addPattern(ScriptFunction.CS_TO_LOWER, CS_TO_LOWER_REGEX);
addPattern(ScriptFunction.CS_TO_UPPER, CS_TO_UPPER_REGEX);
addPattern(ScriptFunction.GET_USER_ID, GET_USER_ID_REGEX);
addPattern(ScriptFunction.GET_WORKER_GROUP, GET_WORKER_GROUP_REGEX);
}

private static void addPattern(ScriptFunction function, String regex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ public interface RuntimeConstants {
String BRANCH_RETURN_VALUES_KEY = "branchReturnValues";

String EXECUTION_ID = "run_id";
String USER_ID = "get_user_id()";
String WORKER_GROUP = "get_worker_group()";

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public void init() throws IOException {
ScriptFunction.CS_SUBSTRING,
ScriptFunction.CS_TO_UPPER,
ScriptFunction.CS_TO_LOWER,
ScriptFunction.GET_SP_VAR
ScriptFunction.GET_SP_VAR,
ScriptFunction.GET_USER_ID,
ScriptFunction.GET_WORKER_GROUP
);

for (ScriptFunction function: list) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@
@Component("csMagicVariableHelper")
public class CsMagicVariableHelper {

private static final String RAS_OPERATOR_PATH = "RAS_Operator_Path";

public Map<String, Value> getGlobalContext(ExecutionRuntimeServices executionRuntimeServices) {
Map<String, Value> globalContext = new HashMap<>();
String executionId = String.valueOf(executionRuntimeServices.getExecutionId());
String userId = executionRuntimeServices.getEffectiveRunningUser();
String workerGroup = executionRuntimeServices.getWorkerGroupName();
globalContext.put(RuntimeConstants.EXECUTION_ID, ValueFactory.create(executionId));
globalContext.put(RuntimeConstants.USER_ID, ValueFactory.create(userId));
globalContext.put(RuntimeConstants.WORKER_GROUP, ValueFactory.create(workerGroup == null ?
RAS_OPERATOR_PATH : workerGroup));
return Collections.unmodifiableMap(globalContext);
}
}
6 changes: 6 additions & 0 deletions cloudslang-runtime/src/main/resources/scripts/get_user_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def get_user_id():
try:
value = get_from_smaller_context("get_user_id()")
except NameError:
value = globals().get("get_user_id()")
return "" if value is None else value
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def get_worker_group():
try:
value = get_from_smaller_context("get_worker_group()")
except NameError:
value = globals().get("get_worker_group()")
return "" if value is None else value

0 comments on commit a712a36

Please sign in to comment.