Search in sources :

Example 1 with Context

use of com.walmartlabs.concord.runtime.v2.sdk.Context in project concord-plugins by walmartlabs.

the class HashiVaultTaskV2Test method getTask.

private HashiVaultTask getTask(boolean setDefaults) {
    Map<String, Object> vars = new HashMap<>();
    if (setDefaults) {
        Map<String, Object> defaults = new HashMap<>();
        defaults.put("baseUrl", getBaseUrl());
        defaults.put("apiToken", getApiToken());
        vars.put("hashivaultParams", defaults);
    }
    Context ctx = new MockContextV2(vars, null);
    SecretService s = new MockSecretServiceV2(new MockSecretServiceDelegate() {

        @Override
        public String exportString(String o, String n, String p) {
            return getApiToken();
        }
    });
    return new HashiVaultTask(ctx, s);
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) MockSecretServiceV2(com.walmartlabs.concord.plugins.hashivault.model.MockSecretServiceV2) MockSecretServiceDelegate(com.walmartlabs.concord.plugins.hashivault.model.MockSecretServiceDelegate) MockContextV2(com.walmartlabs.concord.plugins.hashivault.model.MockContextV2) HashMap(java.util.HashMap) HashiVaultTask(com.walmartlabs.concord.plugins.hashivault.v2.HashiVaultTask) SecretService(com.walmartlabs.concord.runtime.v2.sdk.SecretService)

Example 2 with Context

use of com.walmartlabs.concord.runtime.v2.sdk.Context in project concord-plugins by walmartlabs.

the class HashiVaultTaskV2Test method writeAndRead.

private void writeAndRead(String path, String prefix) throws Exception {
    HashiVaultTask task = getTask(true);
    Map<String, Object> vars1 = new HashMap<>();
    vars1.put("action", "writeKV");
    vars1.put("path", path);
    Map<String, Object> kvPairs = new HashMap<>(2);
    kvPairs.put("key1", prefix + "Value1");
    kvPairs.put("key2", prefix + "Value2");
    vars1.put("kvPairs", kvPairs);
    Variables input1 = new MapBackedVariables(vars1);
    SimpleResult writeResult = task.execute(input1);
    assertTrue(writeResult.ok());
    // -- now get the values back
    // resets context
    task = getTask(true);
    Map<String, Object> vars2 = new HashMap<>();
    vars2.put("action", "readKV");
    vars2.put("path", path);
    Variables input2 = new MapBackedVariables(vars2);
    SimpleResult readResult = task.execute(input2);
    assertTrue(readResult.ok());
    Map<String, Object> data = MapUtils.getMap(readResult.values(), "data", Collections.emptyMap());
    assertEquals(prefix + "Value1", MapUtils.getString(data, "key1"));
    assertEquals(prefix + "Value2", MapUtils.getString(data, "key2"));
}
Also used : MapBackedVariables(com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables) Variables(com.walmartlabs.concord.runtime.v2.sdk.Variables) MapBackedVariables(com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables) HashiVaultTask(com.walmartlabs.concord.plugins.hashivault.v2.HashiVaultTask) HashMap(java.util.HashMap) SimpleResult(com.walmartlabs.concord.runtime.v2.sdk.TaskResult.SimpleResult)

Example 3 with Context

use of com.walmartlabs.concord.runtime.v2.sdk.Context in project concord-plugins by walmartlabs.

the class JiraTask method getStatus.

public String getStatus(@InjectVariable("context") Context ctx, String issueKey) {
    Variables vars = TaskParams.merge(new ContextVariables(ctx), defaults);
    Map<String, Object> result = delegate(ctx).execute(new TaskParams.CurrentStatusParams(vars) {

        @Override
        public String issueKey() {
            return issueKey;
        }
    });
    return MapUtils.getString(result, JIRA_ISSUE_STATUS_KEY);
}
Also used : Variables(com.walmartlabs.concord.runtime.v2.sdk.Variables)

Example 4 with Context

use of com.walmartlabs.concord.runtime.v2.sdk.Context in project concord by walmartlabs.

the class RunPlaybookTask2 method run.

private void run(Context ctx, Path workDir, Map<String, Object> args) throws Exception {
    ApiClient apiClient = apiClientFactory.create(ApiClientConfiguration.builder().context(context).build());
    AnsibleSecretServiceV1 ansibleSecretService = new AnsibleSecretServiceV1(context, secretService);
    AnsibleTask task = new AnsibleTask(apiClient, new AnsibleAuthFactory(ansibleSecretService), ansibleSecretService);
    Map<String, Object> projectInfo = getMap(context, Constants.Request.PROJECT_INFO_KEY, null);
    String orgName = projectInfo != null ? (String) projectInfo.get("orgName") : null;
    AnsibleContext context = AnsibleContext.builder().apiBaseUrl(apiClient.getBasePath()).instanceId(UUID.fromString(txId)).workDir(workDir).tmpDir(createTmpDir(workDir)).defaults(defaults != null ? defaults : Collections.emptyMap()).args(args).sessionToken(apiCfg.getSessionToken(ctx)).eventCorrelationId(ctx.getEventCorrelationId()).orgName(orgName).retryCount((Integer) ctx.getVariable(Constants.Context.CURRENT_RETRY_COUNTER)).build();
    PlaybookProcessRunner runner = new PlaybookProcessRunnerFactory(new AnsibleDockerServiceV1(ctx, dockerService), workDir).create(args);
    TaskResult.SimpleResult result = task.run(context, runner);
    result.values().forEach(ctx::setVariable);
    if (!result.ok()) {
        throw new IllegalStateException("Process finished with exit code " + result.values().get("exitCode"));
    }
}
Also used : ApiClient(com.walmartlabs.concord.ApiClient) TaskResult(com.walmartlabs.concord.runtime.v2.sdk.TaskResult)

Example 5 with Context

use of com.walmartlabs.concord.runtime.v2.sdk.Context in project concord by walmartlabs.

the class AnsibleTaskV2 method execute.

@Override
public TaskResult execute(Variables input) throws Exception {
    Map<String, Object> in = input.toMap();
    Path workDir = context.workingDirectory();
    PlaybookProcessRunner runner = new PlaybookProcessRunnerFactory(new AnsibleDockerServiceV2(context.dockerService()), workDir).create(in);
    AnsibleSecretService secretService = new AnsibleSecretServiceV2(context.secretService());
    AnsibleTask task = new AnsibleTask(apiClient, new AnsibleAuthFactory(secretService), secretService);
    UUID instanceId = Objects.requireNonNull(context.processInstanceId());
    Path tmpDir = context.fileService().createTempDirectory("ansible");
    ProjectInfo projectInfo = context.processConfiguration().projectInfo();
    AnsibleContext ctx = AnsibleContext.builder().apiBaseUrl(apiClient.getBasePath()).instanceId(instanceId).workDir(workDir).tmpDir(tmpDir).defaults(defaults).args(in).sessionToken(context.processConfiguration().processInfo().sessionToken()).eventCorrelationId(context.execution().correlationId()).orgName(projectInfo != null ? projectInfo.orgName() : null).retryCount(ContextUtils.getCurrentRetryAttemptNumber(context)).build();
    TaskResult.SimpleResult result = task.run(ctx, runner);
    if (!result.ok()) {
        throw new IllegalStateException("Process finished with exit code " + result.values().get("exitCode"));
    }
    return result;
}
Also used : Path(java.nio.file.Path) AnsibleSecretService(com.walmartlabs.concord.plugins.ansible.secrets.AnsibleSecretService) ProjectInfo(com.walmartlabs.concord.runtime.v2.sdk.ProjectInfo) UUID(java.util.UUID)

Aggregations

Context (com.walmartlabs.concord.runtime.v2.sdk.Context)18 ExpressionEvaluator (com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)15 MapBackedVariables (com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables)6 HashMap (java.util.HashMap)6 ContextFactory (com.walmartlabs.concord.runtime.v2.runner.context.ContextFactory)5 EvalContext (com.walmartlabs.concord.runtime.v2.runner.el.EvalContext)4 EvalContextFactory (com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory)4 TaskResult (com.walmartlabs.concord.runtime.v2.sdk.TaskResult)4 Map (java.util.Map)4 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)3 ApiClient (com.walmartlabs.concord.ApiClient)3 ProcessDefinition (com.walmartlabs.concord.runtime.v2.model.ProcessDefinition)3 BlockCommand (com.walmartlabs.concord.runtime.v2.runner.vm.BlockCommand)3 Variables (com.walmartlabs.concord.runtime.v2.sdk.Variables)3 Command (com.walmartlabs.concord.svm.Command)3 MimeMessage (javax.mail.internet.MimeMessage)3 Test (org.junit.jupiter.api.Test)3 HashiVaultTask (com.walmartlabs.concord.plugins.hashivault.v2.HashiVaultTask)2 Step (com.walmartlabs.concord.runtime.v2.model.Step)2 LogContext (com.walmartlabs.concord.runtime.v2.runner.logging.LogContext)2