Search in sources :

Example 6 with ApiClient

use of com.walmartlabs.concord.ApiClient 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 7 with ApiClient

use of com.walmartlabs.concord.ApiClient in project concord by walmartlabs.

the class ApiClientFactoryImpl method create.

@Override
public ApiClient create(ApiClientConfiguration overrides) {
    String baseUrl = overrides.baseUrl() != null ? overrides.baseUrl() : cfg.baseUrl();
    String sessionToken = null;
    if (overrides.apiKey() == null) {
        sessionToken = overrides.sessionToken();
    }
    String apiKey = overrides.apiKey();
    if (apiKey != null) {
        sessionToken = null;
    }
    if (sessionToken == null && apiKey == null) {
        throw new IllegalArgumentException("Session token or an API key is required");
    }
    ApiClient client = new ConcordApiClient(baseUrl, httpClient).setSessionToken(sessionToken).setApiKey(apiKey).addDefaultHeader("Accept", "*/*").setTempFolderPath(tmpDir.toString());
    UUID txId = instanceId.getValue();
    if (txId != null) {
        client = client.setUserAgent("Concord-Runner-v2: txId=" + txId);
    }
    return client;
}
Also used : ConcordApiClient(com.walmartlabs.concord.client.ConcordApiClient) ConcordApiClient(com.walmartlabs.concord.client.ConcordApiClient) ApiClient(com.walmartlabs.concord.ApiClient) UUID(java.util.UUID)

Example 8 with ApiClient

use of com.walmartlabs.concord.ApiClient in project concord by walmartlabs.

the class SecretClientTest method testInvalidSecretType.

@Test
public void testInvalidSecretType(WireMockRuntimeInfo wmRuntimeInfo) throws Exception {
    String orgName = "org_" + System.currentTimeMillis();
    String secretName = "secret_" + System.currentTimeMillis();
    stubFor(post(urlEqualTo("/api/v1/org/" + orgName + "/secret/" + secretName + "/data")).willReturn(aResponse().withStatus(200).withHeader(Constants.Headers.SECRET_TYPE, SecretEntry.TypeEnum.DATA.name()).withBody("Hello!")));
    ApiClient apiClient = new ConcordApiClient("http://localhost:" + wmRuntimeInfo.getHttpPort());
    SecretClient secretClient = new SecretClient(apiClient);
    try {
        secretClient.getData(orgName, secretName, null, SecretEntry.TypeEnum.KEY_PAIR);
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage().contains("Unexpected type of " + orgName + "/" + secretName));
    }
}
Also used : ApiClient(com.walmartlabs.concord.ApiClient) Test(org.junit.jupiter.api.Test) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest)

Example 9 with ApiClient

use of com.walmartlabs.concord.ApiClient in project concord by walmartlabs.

the class ServerClient method createClient.

private static ApiClient createClient(String baseUrl, String apiKey, String gitHubKey) {
    ApiClient c = new ConcordApiClient(baseUrl, new OkHttpClient());
    c.setReadTimeout(60000);
    c.setConnectTimeout(10000);
    c.setWriteTimeout(60000);
    c.addDefaultHeader("X-Concord-Trace-Enabled", "true");
    if (apiKey != null) {
        c.setApiKey(apiKey);
    }
    if (gitHubKey != null) {
        c.addDefaultHeader("X-Hub-Signature", gitHubKey);
    }
    return c;
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) ApiClient(com.walmartlabs.concord.ApiClient)

Example 10 with ApiClient

use of com.walmartlabs.concord.ApiClient in project concord by walmartlabs.

the class ProcessMetadataIT method list.

private List<ProcessEntry> list(String orgName, String projectName, String... kv) throws Exception {
    ApiClient apiClient = getApiClient();
    HttpUrl.Builder b = HttpUrl.parse(apiClient.getBasePath() + "/api/v2/process").newBuilder().addQueryParameter("orgName", orgName).addQueryParameter("projectName", projectName);
    for (int i = 0; i < kv.length - 1; i += 2) {
        b.addQueryParameter(kv[i], kv[i + 1]);
    }
    Request req = new Request.Builder().url(b.build()).header("Authorization", ServerClient.DEFAULT_API_KEY).build();
    OkHttpClient httpClient = apiClient.getHttpClient();
    Response resp = null;
    try {
        resp = httpClient.newCall(req).execute();
        if (!resp.isSuccessful()) {
            throw new RuntimeException("Request failed: " + resp);
        }
        ObjectMapper om = new ObjectMapper();
        // to parse timestamps
        om.registerModule(new ServerCompatModule());
        return om.readValue(resp.body().byteStream(), LIST_OF_PROCESS_ENTRIES);
    } finally {
        if (resp != null && resp.body() != null) {
            resp.body().close();
        }
    }
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) ServerCompatModule(com.walmartlabs.concord.it.common.ServerCompatModule) Request(com.squareup.okhttp.Request) ApiClient(com.walmartlabs.concord.ApiClient) HttpUrl(com.squareup.okhttp.HttpUrl) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ApiClient (com.walmartlabs.concord.ApiClient)27 Test (org.junit.jupiter.api.Test)7 ITUtils.randomString (com.walmartlabs.concord.it.common.ITUtils.randomString)4 ConcordProcess (ca.ibodrov.concord.testcontainers.ConcordProcess)3 Payload (ca.ibodrov.concord.testcontainers.Payload)3 TaskResult (com.walmartlabs.concord.runtime.v2.sdk.TaskResult)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 OkHttpClient (com.squareup.okhttp.OkHttpClient)2 ApiException (com.walmartlabs.concord.ApiException)2 ConcordApiClient (com.walmartlabs.concord.client.ConcordApiClient)2 ProcessHeartbeat (com.walmartlabs.concord.runtime.common.ProcessHeartbeat)2 Path (java.nio.file.Path)2 UUID (java.util.UUID)2 WebElement (org.openqa.selenium.WebElement)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 WireMockTest (com.github.tomakehurst.wiremock.junit5.WireMockTest)1 Injector (com.google.inject.Injector)1 HttpUrl (com.squareup.okhttp.HttpUrl)1 Request (com.squareup.okhttp.Request)1