Search in sources :

Example 51 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class ConfigurationProcessorTest method testAllCfg.

@Test
public void testAllCfg() throws Exception {
    Path workDir = Files.createTempDirectory("testAllCfg_workDir");
    UUID instanceId = UUID.randomUUID();
    UUID orgId = UUID.randomUUID();
    UUID prjId = UUID.randomUUID();
    Map<String, Object> req = new HashMap<>();
    req.put("a", "a-req");
    req.put("req", "req-value");
    Map<String, Object> orgCfg = new HashMap<>();
    orgCfg.put("a", "a-org");
    orgCfg.put("org", "org-value");
    Map<String, Object> prjCfg = new HashMap<>();
    prjCfg.put("a", "a-prj");
    prjCfg.put("project", "prj-value");
    ProjectEntry projectEntry = new ProjectEntry(prjId, null, null, null, null, null, prjCfg, null, null, null, null, null, null);
    Map<String, Object> processCfgPolicy = new HashMap<>();
    processCfgPolicy.put("a", "a-process-cfg-policy");
    processCfgPolicy.put("process-cfg-policy", "process-cfg-policy-value");
    Map<String, Object> defaultProcessCfgPolicy = new HashMap<>();
    defaultProcessCfgPolicy.put("a", "default");
    defaultProcessCfgPolicy.put("process-cfg-policy", "default-2");
    PolicyEngineRules policy = new PolicyEngineRules(null, null, null, null, null, null, null, null, processCfgPolicy, null, defaultProcessCfgPolicy, null, null, null, null);
    // ---
    when(orgDao.getConfiguration(eq(orgId))).thenReturn(orgCfg);
    when(projectDao.get(eq(prjId))).thenReturn(projectEntry);
    Payload payload = new Payload(new ProcessKey(instanceId, OffsetDateTime.now()));
    payload = payload.putHeader(Payload.CONFIGURATION, req).putHeader(Payload.ORGANIZATION_ID, orgId).putHeader(Payload.PROJECT_ID, prjId).putHeader(Payload.WORKSPACE_DIR, workDir).putHeader(Payload.POLICY, new PolicyEngine("test", policy));
    // ---
    Map<String, Object> expected = new HashMap<>();
    expected.put("activeProfiles", Collections.singletonList("default"));
    // orgCfg < prjCfg < req < org-policy < prj-policy
    expected.put("a", "a-process-cfg-policy");
    expected.put("org", "org-value");
    expected.put("project", "prj-value");
    expected.put("req", "req-value");
    expected.put("process-cfg-policy", "process-cfg-policy-value");
    Map<String, Object> result = process(payload);
    // don't care about arguments and other stuff here
    result.remove(Constants.Request.ARGUMENTS_KEY);
    result.remove(Constants.Request.PROCESS_INFO_KEY);
    result.remove(Constants.Request.PROJECT_INFO_KEY);
    assertEquals(expected, result);
}
Also used : Path(java.nio.file.Path) ProjectEntry(com.walmartlabs.concord.server.org.project.ProjectEntry) PolicyEngineRules(com.walmartlabs.concord.policyengine.PolicyEngineRules) HashMap(java.util.HashMap) PolicyEngine(com.walmartlabs.concord.policyengine.PolicyEngine) Payload(com.walmartlabs.concord.server.process.Payload) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test)

Example 52 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class ConfigurationProcessorTest method testWithoutPolicy.

@Test
public void testWithoutPolicy() throws Exception {
    Path workDir = Files.createTempDirectory("testWithoutPoliy_workDir");
    UUID instanceId = UUID.randomUUID();
    UUID orgId = UUID.randomUUID();
    UUID prjId = UUID.randomUUID();
    Map<String, Object> req = new HashMap<>();
    req.put("a", "a-req");
    req.put("req", "req-value");
    Map<String, Object> orgCfg = new HashMap<>();
    orgCfg.put("a", "a-org");
    orgCfg.put("org", "org-value");
    Map<String, Object> prjCfg = new HashMap<>();
    prjCfg.put("a", "a-prj");
    prjCfg.put("project", "prj-value");
    ProjectEntry projectEntry = new ProjectEntry(prjId, null, null, null, null, null, prjCfg, null, null, null, null, null, null);
    // ---
    when(orgDao.getConfiguration(eq(orgId))).thenReturn(orgCfg);
    when(projectDao.get(eq(prjId))).thenReturn(projectEntry);
    Payload payload = new Payload(new ProcessKey(instanceId, OffsetDateTime.now()));
    payload = payload.putHeader(Payload.CONFIGURATION, req).putHeader(Payload.ORGANIZATION_ID, orgId).putHeader(Payload.PROJECT_ID, prjId).putHeader(Payload.WORKSPACE_DIR, workDir);
    // ---
    Map<String, Object> expected = new HashMap<>();
    expected.put("activeProfiles", Collections.singletonList("default"));
    // orgCfg < prjCfg < req
    expected.put("a", "a-req");
    expected.put("org", "org-value");
    expected.put("project", "prj-value");
    expected.put("req", "req-value");
    Map<String, Object> result = process(payload);
    // don't care about arguments and other stuff here
    result.remove(Constants.Request.ARGUMENTS_KEY);
    result.remove(Constants.Request.PROCESS_INFO_KEY);
    result.remove(Constants.Request.PROJECT_INFO_KEY);
    assertEquals(expected, result);
}
Also used : Path(java.nio.file.Path) ProjectEntry(com.walmartlabs.concord.server.org.project.ProjectEntry) HashMap(java.util.HashMap) Payload(com.walmartlabs.concord.server.process.Payload) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test)

Example 53 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class ProcessKeyCacheTest method testNotFound.

@Test
public void testNotFound() {
    ProcessQueueDao dao = new ProcessQueueDao(getConfiguration(), new ConcordObjectMapper(TestObjectMapper.INSTANCE));
    ProcessKeyCache keyCache = new ProcessKeyCache(dao);
    ProcessKey key = keyCache.get(UUID.randomUUID());
    assertNull(key);
}
Also used : ConcordObjectMapper(com.walmartlabs.concord.server.ConcordObjectMapper) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) Test(org.junit.jupiter.api.Test) AbstractDaoTest(com.walmartlabs.concord.server.AbstractDaoTest)

Example 54 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class ProcessAnsibleResource method listEvents.

/**
 * Lists Ansible events of a specific process.
 */
@GET
@ApiOperation("List Ansible events of a specific process")
@Path("/{processInstanceId}/ansible/events")
@Produces(MediaType.APPLICATION_JSON)
@WithTimer
public List<ProcessEventEntry> listEvents(@PathParam("processInstanceId") UUID processInstanceId, @QueryParam("host") String host, @QueryParam("hostGroup") String hostGroup, @QueryParam("status") String status, @QueryParam("playbookId") UUID playbookId) {
    ProcessKey key = processKeyCache.get(processInstanceId);
    if (key == null) {
        return Collections.emptyList();
    }
    Map<String, String> eventFilter = new HashMap<>();
    if (host != null) {
        eventFilter.put("host", host);
    }
    if (hostGroup != null) {
        eventFilter.put("hostGroup", hostGroup);
    }
    if (status != null) {
        eventFilter.put("status", status);
    }
    if (playbookId != null) {
        eventFilter.put("playbookId", playbookId.toString());
    }
    return eventDao.list(key, eventFilter);
}
Also used : ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer) ApiOperation(io.swagger.annotations.ApiOperation)

Example 55 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class FormServiceV1 method submit.

public FormSubmitResult submit(ProcessKey processKey, String formName, Map<String, Object> data) {
    Form form = get(processKey, formName);
    if (form == null) {
        throw new ProcessException(processKey, "Form not found: " + formName);
    }
    ResumeHandler resumeHandler = (f, args) -> {
        String resource = path(Constants.Files.JOB_ATTACHMENTS_DIR_NAME, Constants.Files.JOB_STATE_DIR_NAME, Constants.Files.JOB_FORMS_DIR_NAME, formName);
        stateManager.deleteFile(processKey, resource);
        @SuppressWarnings("unchecked") Map<String, Object> clearedData = (Map<String, Object>) args.get(f.getFormDefinition().getName());
        args.put(f.getFormDefinition().getName(), clearedData);
        // TODO refactor into the process manager
        Map<String, Object> m = new HashMap<>();
        m.put(Constants.Request.ARGUMENTS_KEY, args);
        if (data != null) {
            m.put(Constants.Files.FORM_FILES, data.remove(Constants.Files.FORM_FILES));
        }
        Map<String, Object> opts = f.getOptions();
        Object runAs = opts != null ? opts.get(Constants.Forms.RUN_AS_KEY) : null;
        if (runAs != null) {
            m.put(INTERNAL_RUN_AS_KEY, runAs);
        }
        resume(processKey, f.getEventName(), m);
    };
    Map<String, Object> merged = merge(form, data);
    // optionally save the user who submitted the form
    boolean saveSubmittedBy = MapUtils.getBoolean(form.getOptions(), Constants.Forms.SAVE_SUBMITTED_BY_KEY, false);
    if (saveSubmittedBy) {
        UserInfo i = userManager.getCurrentUserInfo();
        merged.put(Constants.Forms.SUBMITTED_BY_KEY, i);
    }
    try {
        FormValidator validator = createFormValidator(processKey, formName);
        return toResult(processKey, form, DefaultFormService.submit(resumeHandler, validator, form, merged));
    } catch (ExecutionException e) {
        throw new ProcessException(processKey, "Form submit error: " + e.getMessage(), e);
    }
}
Also used : MapUtils(com.walmartlabs.concord.sdk.MapUtils) PayloadManager(com.walmartlabs.concord.server.process.PayloadManager) UserInfo(com.walmartlabs.concord.server.user.UserInfoProvider.UserInfo) java.util(java.util) ObjectInputStream(java.io.ObjectInputStream) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) ProcessKeyCache(com.walmartlabs.concord.server.process.queue.ProcessKeyCache) Singleton(javax.inject.Singleton) ConfigurationUtils(com.walmartlabs.concord.common.ConfigurationUtils) ResumeHandler(io.takari.bpm.form.DefaultFormService.ResumeHandler) Function(java.util.function.Function) ValidationError(com.walmartlabs.concord.forms.ValidationError) ExecutionException(io.takari.bpm.api.ExecutionException) Inject(javax.inject.Inject) Constants(com.walmartlabs.concord.sdk.Constants) FormValidatorLocale(io.takari.bpm.form.FormValidatorLocale) Named(javax.inject.Named) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ProcessException(com.walmartlabs.concord.server.process.ProcessException) UserManager(com.walmartlabs.concord.server.user.UserManager) Payload(com.walmartlabs.concord.server.process.Payload) ProcessStateManager(com.walmartlabs.concord.server.process.state.ProcessStateManager) ProcessManager(com.walmartlabs.concord.server.process.ProcessManager) FormValidator(io.takari.bpm.form.FormValidator) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Form(io.takari.bpm.form.Form) ConcordFormValidator(com.walmartlabs.concord.common.form.ConcordFormValidator) ProcessStateManager.path(com.walmartlabs.concord.server.process.state.ProcessStateManager.path) DefaultFormService(io.takari.bpm.form.DefaultFormService) InputStream(java.io.InputStream) ResumeHandler(io.takari.bpm.form.DefaultFormService.ResumeHandler) Form(io.takari.bpm.form.Form) UserInfo(com.walmartlabs.concord.server.user.UserInfoProvider.UserInfo) FormValidator(io.takari.bpm.form.FormValidator) ConcordFormValidator(com.walmartlabs.concord.common.form.ConcordFormValidator) ProcessException(com.walmartlabs.concord.server.process.ProcessException) ExecutionException(io.takari.bpm.api.ExecutionException)

Aggregations

ProcessKey (com.walmartlabs.concord.server.sdk.ProcessKey)69 WithTimer (com.walmartlabs.concord.server.sdk.metrics.WithTimer)28 Path (java.nio.file.Path)27 PartialProcessKey (com.walmartlabs.concord.server.sdk.PartialProcessKey)25 ApiOperation (io.swagger.annotations.ApiOperation)22 ProcessException (com.walmartlabs.concord.server.process.ProcessException)20 ConcordApplicationException (com.walmartlabs.concord.server.sdk.ConcordApplicationException)16 IOException (java.io.IOException)16 UUID (java.util.UUID)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 PolicyEngine (com.walmartlabs.concord.policyengine.PolicyEngine)8 Inject (javax.inject.Inject)8 Named (javax.inject.Named)8 ProcessKeyCache (com.walmartlabs.concord.server.process.queue.ProcessKeyCache)7 ProcessQueueDao (com.walmartlabs.concord.server.process.queue.ProcessQueueDao)7 ProcessStateManager (com.walmartlabs.concord.server.process.state.ProcessStateManager)7 UserPrincipal (com.walmartlabs.concord.server.security.UserPrincipal)7 ProcessLogManager (com.walmartlabs.concord.server.process.logs.ProcessLogManager)6 HttpUtils (com.walmartlabs.concord.server.HttpUtils)5 ResourceAccessLevel (com.walmartlabs.concord.server.org.ResourceAccessLevel)5