use of com.walmartlabs.concord.runtime.v2.sdk.Variables in project concord by walmartlabs.
the class OrDefaultFunction method getValue.
@SuppressWarnings("unchecked")
private static Object getValue(String variableName) {
Variables variables = ThreadLocalEvalContext.get().variables();
String[] path = variableName.split("\\.");
if (path.length == 1) {
return variables.get(variableName);
} else {
Object maybeMap = variables.get(path[0]);
if (!(maybeMap instanceof Map)) {
throw new IllegalStateException("Expected a map. This is most likely a bug");
}
return ConfigurationUtils.get((Map<String, Object>) maybeMap, Arrays.copyOfRange(path, 1, path.length));
}
}
use of com.walmartlabs.concord.runtime.v2.sdk.Variables in project concord by walmartlabs.
the class FlowCallCommand method execute.
@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
state.peekFrame(threadId).pop();
Context ctx = runtime.getService(Context.class);
ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
EvalContext evalCtx = EvalContextFactory.global(ctx);
FlowCall call = getStep();
// the called flow's name
String flowName = ee.eval(evalCtx, call.getFlowName(), String.class);
// the called flow's steps
Compiler compiler = runtime.getService(Compiler.class);
ProcessDefinition pd = runtime.getService(ProcessDefinition.class);
ProcessConfiguration pc = runtime.getService(ProcessConfiguration.class);
Command steps = CompilerUtils.compile(compiler, pc, pd, flowName);
FlowCallOptions opts = Objects.requireNonNull(call.getOptions());
Map<String, Object> input = VMUtils.prepareInput(ee, ctx, opts.input(), opts.inputExpression());
// the call's frame should be a "root" frame
// all local variables will have this frame as their base
Frame innerFrame = Frame.builder().root().commands(steps).locals(input).build();
// an "out" handler:
// grab the out variable from the called flow's frame
// and put it into the callee's frame
Command processOutVars;
if (!opts.outExpr().isEmpty()) {
processOutVars = new EvalVariablesCommand(ctx, opts.outExpr(), innerFrame);
} else {
processOutVars = new CopyVariablesCommand(opts.out(), innerFrame, VMUtils::assertNearestRoot);
}
// push the out handler first so it executes after the called flow's frame is done
state.peekFrame(threadId).push(processOutVars);
state.pushFrame(threadId, innerFrame);
}
use of com.walmartlabs.concord.runtime.v2.sdk.Variables in project concord by walmartlabs.
the class Run method call.
@Override
public Integer call() throws Exception {
sourceDir = sourceDir.normalize().toAbsolutePath();
Path targetDir;
if (Files.isRegularFile(sourceDir)) {
Path src = sourceDir.toAbsolutePath();
System.out.println("Running a single Concord file: " + src);
targetDir = Files.createTempDirectory("payload");
Files.copy(src, targetDir.resolve("concord.yml"), StandardCopyOption.REPLACE_EXISTING);
} else if (Files.isDirectory(sourceDir)) {
targetDir = sourceDir.resolve("target");
if (cleanup && Files.exists(targetDir)) {
if (verbose) {
System.out.println("Cleaning target directory");
}
IOUtils.deleteRecursively(targetDir);
}
// copy everything into target except target
IOUtils.copy(sourceDir, targetDir, "^target$", new CopyNotifier(verbose ? 0 : 100), StandardCopyOption.REPLACE_EXISTING);
} else {
throw new IllegalArgumentException("Not a directory or single Concord YAML file: " + sourceDir);
}
DependencyManager dependencyManager = initDependencyManager();
ImportManager importManager = new ImportManagerFactory(dependencyManager, new CliRepositoryExporter(repoCacheDir), Collections.emptySet()).create();
ProjectLoaderV2.Result loadResult;
try {
loadResult = new ProjectLoaderV2(importManager).load(targetDir, new CliImportsNormalizer(importsSource, verbose, defaultVersion), verbose ? new CliImportsListener() : null);
} catch (ImportProcessingException e) {
ObjectMapper om = new ObjectMapper();
System.err.println("Error while processing import " + om.writeValueAsString(e.getImport()) + ": " + e.getMessage());
return -1;
} catch (Exception e) {
System.err.println("Error while loading " + targetDir);
e.printStackTrace();
return -1;
}
ProcessDefinition processDefinition = loadResult.getProjectDefinition();
UUID instanceId = UUID.randomUUID();
if (verbose && !extraVars.isEmpty()) {
System.out.println("Additional variables: " + extraVars);
}
if (verbose && !profiles.isEmpty()) {
System.out.println("Active profiles: " + profiles);
}
ProcessConfiguration cfg = from(processDefinition.configuration()).entryPoint(entryPoint).instanceId(instanceId).build();
RunnerConfiguration runnerCfg = RunnerConfiguration.builder().dependencies(new DependencyResolver(dependencyManager, verbose).resolveDeps(processDefinition)).debug(cfg.debug()).build();
Map<String, Object> profileArgs = getProfilesArguments(processDefinition, profiles);
Map<String, Object> args = ConfigurationUtils.deepMerge(cfg.arguments(), profileArgs, extraVars);
if (verbose) {
System.out.println("Process arguments: " + args);
}
args.put(Constants.Context.TX_ID_KEY, instanceId.toString());
args.put(Constants.Context.WORK_DIR_KEY, targetDir.toAbsolutePath().toString());
if (effectiveYaml) {
Map<String, List<Step>> flows = new HashMap<>(processDefinition.flows());
for (String ap : profiles) {
Profile p = processDefinition.profiles().get(ap);
if (p != null) {
flows.putAll(p.flows());
}
}
ProcessDefinition pd = ProcessDefinition.builder().from(processDefinition).configuration(ProcessDefinitionConfiguration.builder().from(processDefinition.configuration()).arguments(args).build()).flows(flows).imports(Imports.builder().build()).profiles(Collections.emptyMap()).build();
ProjectSerializerV2 serializer = new ProjectSerializerV2();
serializer.write(pd, System.out);
return 0;
}
System.out.println("Starting...");
Injector injector = new InjectorFactory(new WorkingDirectory(targetDir), runnerCfg, () -> cfg, new ProcessDependenciesModule(targetDir, runnerCfg.dependencies(), cfg.debug()), new CliServicesModule(secretStoreDir, targetDir, new VaultProvider(vaultDir, vaultId), dependencyManager)).create();
Runner runner = injector.getInstance(Runner.class);
if (cfg.debug()) {
System.out.println("Available tasks: " + injector.getInstance(TaskProviders.class).names());
}
try {
runner.start(cfg, processDefinition, args);
} catch (Exception e) {
if (verbose) {
System.err.print("Error: ");
e.printStackTrace(System.err);
} else {
System.err.println("Error: " + e.getMessage());
}
return 1;
}
System.out.println("...done!");
return 0;
}
use of com.walmartlabs.concord.runtime.v2.sdk.Variables in project concord by walmartlabs.
the class SlackTaskParams method of.
public static SlackTaskParams of(Variables input, Map<String, Object> defaults) {
Variables variables = Utils.merge(input, defaults);
SlackTaskParams p = new SlackTaskParams(variables);
switch(p.action()) {
case SENDMESSAGE:
case UPDATEMESSAGE:
{
return new SendMessageParams(variables);
}
case ADDREACTION:
{
return new AddReactionParams(variables);
}
default:
{
throw new IllegalArgumentException("Unsupported action type: " + p.action());
}
}
}
use of com.walmartlabs.concord.runtime.v2.sdk.Variables in project concord by walmartlabs.
the class TaskPolicy method paramMatches.
@SuppressWarnings("unchecked")
private static boolean paramMatches(String[] names, int nameIndex, List<Object> values, Object param, boolean isProtected) {
if (param == null) {
return values.contains(null);
}
if (param instanceof Map) {
if (names == null) {
return false;
}
Map<String, Object> m = (Map<String, Object>) param;
String name = names[nameIndex];
nameIndex += 1;
return paramMatches(names, nameIndex, values, m.get(name), isProtected);
} else if (param instanceof Context) {
if (names == null) {
return false;
}
Context ctx = (Context) param;
String name = names[nameIndex];
nameIndex += 1;
Object v = isProtected ? ctx.getProtectedVariable(name) : ctx.getVariable(name);
return paramMatches(names, nameIndex, values, v, isProtected);
} else if (param instanceof Variables) {
Variables vars = (Variables) param;
String name = names[nameIndex];
nameIndex += 1;
Object v = vars.get(name);
return paramMatches(names, nameIndex, values, v, isProtected);
} else if (param instanceof com.walmartlabs.concord.runtime.v2.sdk.Context) {
com.walmartlabs.concord.runtime.v2.sdk.Context ctx = (com.walmartlabs.concord.runtime.v2.sdk.Context) param;
String name = names[nameIndex];
nameIndex += 1;
Object v = ctx.variables().get(name);
return paramMatches(names, nameIndex, values, v, isProtected);
} else if (param instanceof String) {
return Utils.matchAny(values.stream().map(Object::toString).collect(Collectors.toList()), param.toString());
} else {
for (Object v : values) {
if (v != null && v.equals(param)) {
return true;
}
}
}
return false;
}
Aggregations