Search in sources :

Example 6 with MapBackedVariables

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

the class UnitTest method invalidActionTest.

@Test(expected = IllegalArgumentException.class)
public void invalidActionTest() {
    Map<String, Object> vars = new HashMap<>();
    vars.put("action", "not-an-action");
    TaskParams.of(new MapBackedVariables(vars), null, exporter);
}
Also used : MapBackedVariables(com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 7 with MapBackedVariables

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

the class UnitTest method mapDataTest.

/**
 * The data returned by the task may be either a Map containing all of the
 * key/value pairs in the Vault secret OR a single String value from the
 * Vault secret when a specific secret "key" is specified.
 */
@Test
public void mapDataTest() {
    Variables vars = new MapBackedVariables(getMap("path", "secret/mysecret"));
    TaskParams params = TaskParams.of(vars, null, exporter);
    HashiVaultTaskResult result = HashiVaultTaskResult.of(true, getMap("top_secret", "value"), null, params);
    try {
        String s = result.data();
        fail("data should be Map when key param is not given");
    } catch (ClassCastException e) {
    // that's expected
    }
    // this should work
    Map<String, Object> m = result.data();
    assertEquals(1, m.size());
}
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) Test(org.junit.Test)

Example 8 with MapBackedVariables

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

the class TaskParams method of.

public static TaskParams of(Variables input, Map<String, Object> defaults, SecretExporter secretExporter) {
    Map<String, Object> variablesMap = new HashMap<>(defaults != null ? defaults : Collections.emptyMap());
    variablesMap.putAll(input.toMap());
    if (variablesMap.containsKey(API_TOKEN_SECRET_KEY)) {
        Map<String, Object> tokenSecret = MapUtils.assertMap(variablesMap, API_TOKEN_SECRET_KEY);
        variablesMap.put(API_TOKEN_KEY, exportToken(secretExporter, tokenSecret));
    }
    Variables variables = new MapBackedVariables(variablesMap);
    TaskParams p = new TaskParams(variables);
    switch(p.action()) {
        case READKV:
        case WRITEKV:
            return new TaskParams(variables);
        default:
            throw new IllegalArgumentException("Unsupported action type: " + p.action());
    }
}
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) HashMap(java.util.HashMap)

Example 9 with MapBackedVariables

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

the class SmtpTaskV2Test method testWithBothDefaults.

@Test
public void testWithBothDefaults() throws Exception {
    Map<String, Object> policyDefaults = new HashMap<>();
    policyDefaults.put("host", "badserver");
    policyDefaults.put("port", -1);
    // Process arg defaults override policy defaults
    Map<String, Object> processArgsDefaults = new HashMap<>();
    processArgsDefaults.put("host", "localhost");
    processArgsDefaults.put("port", mailServer.getSmtp().getPort());
    Map<String, Object> mail = new HashMap<>();
    mail.put("from", "me@localhost");
    mail.put("to", "you@localhost");
    mail.put("message", "Default vars from process arguments.");
    Context ctx = mock(Context.class);
    when(ctx.workingDirectory()).thenReturn(Paths.get(System.getProperty("user.dir")));
    when(ctx.variables()).thenReturn(new MapBackedVariables(Collections.singletonMap("smtpParams", processArgsDefaults)));
    when(ctx.defaultVariables()).thenReturn(new MapBackedVariables(policyDefaults));
    SmtpTaskV2 t = new SmtpTaskV2(ctx);
    t.execute(new MapBackedVariables(Collections.singletonMap("mail", mail)));
    MimeMessage[] messages = mailServer.getReceivedMessages();
    assertEquals(1, messages.length);
    assertEquals("Default vars from process arguments.\r\n", messages[0].getContent());
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) MapBackedVariables(com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables) HashMap(java.util.HashMap) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 10 with MapBackedVariables

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

the class SmtpTaskV2Test method testWithPolicyDefaults.

@Test
public void testWithPolicyDefaults() throws Exception {
    Map<String, Object> smtpParams = new HashMap<>();
    smtpParams.put("host", "localhost");
    smtpParams.put("port", mailServer.getSmtp().getPort());
    Map<String, Object> mail = new HashMap<>();
    mail.put("from", "me@localhost");
    mail.put("to", "you@localhost");
    mail.put("message", "Default vars from policy.");
    Context ctx = mock(Context.class);
    when(ctx.workingDirectory()).thenReturn(Paths.get(System.getProperty("user.dir")));
    when(ctx.variables()).thenReturn(new MapBackedVariables(Collections.emptyMap()));
    when(ctx.defaultVariables()).thenReturn(new MapBackedVariables(smtpParams));
    SmtpTaskV2 t = new SmtpTaskV2(ctx);
    t.execute(new MapBackedVariables(Collections.singletonMap("mail", mail)));
    MimeMessage[] messages = mailServer.getReceivedMessages();
    assertEquals(1, messages.length);
    assertEquals("Default vars from policy.\r\n", messages[0].getContent());
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) MapBackedVariables(com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables) HashMap(java.util.HashMap) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Aggregations

MapBackedVariables (com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables)21 HashMap (java.util.HashMap)14 Test (org.junit.Test)10 Variables (com.walmartlabs.concord.runtime.v2.sdk.Variables)9 Test (org.junit.jupiter.api.Test)6 SimpleResult (com.walmartlabs.concord.runtime.v2.sdk.TaskResult.SimpleResult)5 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)3 Context (com.walmartlabs.concord.runtime.v2.sdk.Context)3 MimeMessage (javax.mail.internet.MimeMessage)3 ForkParams (com.walmartlabs.concord.client.ConcordTaskParams.ForkParams)1 ForkStartParams (com.walmartlabs.concord.client.ConcordTaskParams.ForkStartParams)1 HashiVaultTask (com.walmartlabs.concord.plugins.hashivault.v2.HashiVaultTask)1 TaskCall (com.walmartlabs.concord.runtime.v2.model.TaskCall)1 TaskCallOptions (com.walmartlabs.concord.runtime.v2.model.TaskCallOptions)1 TaskContext (com.walmartlabs.concord.runtime.v2.runner.context.TaskContext)1 ExpressionEvaluator (com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)1 TaskCallInterceptor (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallInterceptor)1 CallContext (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallInterceptor.CallContext)1 TaskException (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskException)1 TaskProviders (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders)1