Skip to content

Commit

Permalink
terraform: remove okhttp dependency (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
benbroadaway authored Jul 19, 2024
1 parent 00e7af7 commit b7b68a3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 35 deletions.
6 changes: 0 additions & 6 deletions tasks/terraform/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@
<artifactId>wiremock-jre8</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.walmartlabs.concord.runtime.v2</groupId>
<artifactId>concord-runner-v2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@
import com.github.tomakehurst.wiremock.http.HttpHeader;
import com.github.tomakehurst.wiremock.http.RequestMethod;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.walmartlabs.concord.common.IOUtils;
import com.walmartlabs.concord.plugins.terraform.backend.BackendFactoryV1;
import com.walmartlabs.concord.plugins.terraform.docker.DockerService;
import com.walmartlabs.concord.sdk.*;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;

import java.io.*;
import java.lang.reflect.Field;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -60,14 +59,15 @@ public abstract class AbstractTerraformTest {
private String basedir;

private AWSCredentials awsCredentials;
@TempDir
protected Path workDir;
protected Path dstDir;
private Path testFile;
private Path downloadManagerCacheDir;
protected LockService lockService;
protected ObjectStorage objectStorage;
protected BackendFactoryV1 backendManager;
protected OKHttpDownloadManager dependencyManager;
protected HttpClientDownloadManager dependencyManager;
protected DockerService dockerService;

@RegisterExtension
Expand Down Expand Up @@ -100,7 +100,7 @@ public void abstractSetup() throws Exception {
Files.copy(testFile, dstDir.resolve("main.tf"));

lockService = mock(LockService.class);
dependencyManager = new OKHttpDownloadManager("terraform");
dependencyManager = new HttpClientDownloadManager("terraform");
dockerService = new DockerService(workDir, Collections.emptyList());
}

Expand Down Expand Up @@ -297,14 +297,8 @@ public Object put(Object key, Object value) {
//

private Path workDir() throws Exception {
String concordTmpDir = System.getenv(CONCORD_TMP_DIR_KEY);
if (concordTmpDir == null) {
// Grab the old environment and add the CONCORD_TMP_DIR value to it and reset it
Map<String, String> newEnvironment = new HashMap<>(System.getenv());
newEnvironment.put(CONCORD_TMP_DIR_KEY, CONCORD_TMP_DIR_VALUE);
setNewEnvironment(newEnvironment);
}
return IOUtils.createTempDir("test");

return Files.createDirectories(workDir.resolve("test"));
}

private Path terraformTestFile() {
Expand Down Expand Up @@ -361,11 +355,11 @@ private static void setNewEnvironment(Map<String, String> newEnvironment) throws
}
}

static class OKHttpDownloadManager implements DependencyManager, com.walmartlabs.concord.runtime.v2.sdk.DependencyManager {
protected static class HttpClientDownloadManager implements DependencyManager, com.walmartlabs.concord.runtime.v2.sdk.DependencyManager {

private final Path toolDir;

public OKHttpDownloadManager(String tool) {
public HttpClientDownloadManager(String tool) {
this.toolDir = Paths.get(System.getProperty("user.home"), ".m2/tools/", tool);

if (Files.exists(toolDir)) {
Expand Down Expand Up @@ -395,11 +389,17 @@ public Path resolve(URI uri) throws IOException {
String fileName = urlString.substring(urlString.lastIndexOf('/') + 1);
Path target = toolDir.resolve(fileName);
if (!Files.exists(target)) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(urlString).build();
Call call = client.newCall(request);
Response response = call.execute();
download(response.body().byteStream(), target.toFile());
HttpClient client = HttpClient.newHttpClient();
HttpRequest req = HttpRequest.newBuilder(URI.create(urlString))
.GET()
.build();

try {
var response = client.send(req, HttpResponse.BodyHandlers.ofInputStream());
download(response.body(), target.toFile());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
return target;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public class TerraformTaskIT {
private static final String CURRENT_VERSION = getCurrentVersion();

@Test
public void testWithRuntimeV1() throws Exception {
void testWithRuntimeV1() throws Exception {
test("runtimeV1/concord.yml");
}

@Test
public void testWithRuntimeV2() throws Exception {
void testWithRuntimeV2() throws Exception {
test("runtimeV2/concord.yml");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* =====
*/

import com.squareup.okhttp.OkHttpClient;
import com.walmartlabs.concord.ApiClient;
import com.walmartlabs.concord.client.ApiClientConfiguration;
import com.walmartlabs.concord.client.ApiClientFactory;
Expand All @@ -36,7 +35,6 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;

Expand Down Expand Up @@ -71,7 +69,7 @@
// TODO: split test apart to prepare for testing OCI/GCP
//
@Disabled
public class TerraformTaskV2Test extends AbstractTerraformTest {
class TerraformTaskV2Test extends AbstractTerraformTest {
private ApiClient apiClient;
private SecretService secretService;
private final LockService lockService = mock(LockService.class);
Expand All @@ -96,7 +94,7 @@ public void setup() throws Exception {
}

@Test
public void test() throws Exception {
void test() throws Exception {
Map<String, Object> args = baseArguments(workDir, dstDir, Action.PLAN.name());
args.put(TaskConstants.VARS_FILES, varFiles());
args.put(TaskConstants.EXTRA_VARS_KEY, extraVars());
Expand Down Expand Up @@ -235,7 +233,7 @@ private static SecretService createSecretService(Path workDir) throws Exception

ApiClientFactory getApiClientFactory() {
return cfg -> {
ApiClient apiClient = new ConcordApiClient(cfg.baseUrl(), new OkHttpClient());
ApiClient apiClient = new ConcordApiClient(cfg.baseUrl());
apiClient.setReadTimeout(60000);
apiClient.setConnectTimeout(10000);
apiClient.setWriteTimeout(60000);
Expand Down

0 comments on commit b7b68a3

Please sign in to comment.