From e78a3221f033075fc168105fde4ac2115aca8795 Mon Sep 17 00:00:00 2001 From: Alexander Goussas Date: Fri, 26 Dec 2025 09:30:44 -0500 Subject: [PATCH] add andes configuration source --- .idea/vcs.xml | 6 + lib/build.gradle.kts | 2 + .../AndesConfigurationVariableSource.java | 104 ++++++++++++++++++ .../AndesConfigurationVariableSourceTest.java | 34 ++++++ 4 files changed, 146 insertions(+) create mode 100644 .idea/vcs.xml create mode 100644 lib/src/main/java/io/github/aloussase/configvalidator/sources/AndesConfigurationVariableSource.java create mode 100644 lib/src/test/java/io/github/aloussase/configvalidator/sources/AndesConfigurationVariableSourceTest.java diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts index 0d96185..dc0bd47 100644 --- a/lib/build.gradle.kts +++ b/lib/build.gradle.kts @@ -16,6 +16,8 @@ repositories { dependencies { implementation("org.yaml:snakeyaml:2.2") + implementation("com.squareup.okhttp3:okhttp:5.3.0") + implementation("com.fasterxml.jackson.core:jackson-databind:2.20.0-rc1") testImplementation(libs.junit.jupiter) testRuntimeOnly("org.junit.platform:junit-platform-launcher") } diff --git a/lib/src/main/java/io/github/aloussase/configvalidator/sources/AndesConfigurationVariableSource.java b/lib/src/main/java/io/github/aloussase/configvalidator/sources/AndesConfigurationVariableSource.java new file mode 100644 index 0000000..821e806 --- /dev/null +++ b/lib/src/main/java/io/github/aloussase/configvalidator/sources/AndesConfigurationVariableSource.java @@ -0,0 +1,104 @@ +package io.github.aloussase.configvalidator.sources; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.aloussase.configvalidator.ConfigurationVariable; +import io.github.aloussase.configvalidator.ConfigurationVariableSource; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class AndesConfigurationVariableSource implements ConfigurationVariableSource { + + private final OkHttpClient httpClient; + + private final String apiUrl; + + private final String bluePrint; + + private final String authToken; + + private final String deploymentId; + + private final ObjectMapper objectMapper; + + public AndesConfigurationVariableSource(OkHttpClient httpClient, String apiUrl, String bluePrint, String authToken, String deploymentId, ObjectMapper objectMapper) { + this.httpClient = httpClient; + this.apiUrl = apiUrl; + this.bluePrint = bluePrint; + this.authToken = authToken; + this.deploymentId = deploymentId; + this.objectMapper = objectMapper; + } + + // TODO: Better error handling to let the user know what went wrong. + + @Override + public List readConfigurationVariables(String env) { + final var requestBody = """ + { + "query": { + "combinator": "and", + "rules": [ + { + "combinator": "and", + "rules": [ + { + "property": "deployment_id", + "operator": "=", + "value": "%s" + }, + { + "property": "entorno_name", + "operator": "=", + "value": "%s" + } + ] + } + ] + }, + "include": ["$title", "value"] + } + """ + .formatted(deploymentId, env) + .trim(); + + final var request = new Request.Builder() + .post(RequestBody.create(requestBody.getBytes())) + .url("%s/v1/blueprints/%s/entities/search".formatted(apiUrl, bluePrint)) + .header("Authorization", "Bearer " + authToken) + .header("Accept", "application/json") + .header("Content-Type", "application/json") + .build(); + + Response response; + + try { + response = httpClient.newCall(request).execute(); + } catch (IOException ignored) { + return new ArrayList<>(); + } + + if (!response.isSuccessful()) { + return new ArrayList<>(); + } + + try (final var inputStream = response.body().byteStream()) { + final var rootNode = objectMapper.readTree(inputStream); + return rootNode + .get("entities") + .valueStream() + .map(node -> new ConfigurationVariable( + node.get("title").asText(), + node.get("properties").get("value").asText() + )) + .toList(); + } catch (IOException ignored) { + return new ArrayList<>(); + } + } +} diff --git a/lib/src/test/java/io/github/aloussase/configvalidator/sources/AndesConfigurationVariableSourceTest.java b/lib/src/test/java/io/github/aloussase/configvalidator/sources/AndesConfigurationVariableSourceTest.java new file mode 100644 index 0000000..a7a4aaa --- /dev/null +++ b/lib/src/test/java/io/github/aloussase/configvalidator/sources/AndesConfigurationVariableSourceTest.java @@ -0,0 +1,34 @@ +package io.github.aloussase.configvalidator.sources; + +import com.fasterxml.jackson.databind.ObjectMapper; +import okhttp3.OkHttpClient; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class AndesConfigurationVariableSourceTest { + + @Test + public void givenRealDataSourceParametersWhenReadingConfigurationVariablesCallReturnsWithSuccess() { + final var apiUrl = System.getenv("ANDES_API_URL"); + final var bluePrint = System.getenv("ANDES_BLUEPRINT"); + final var authToken = System.getenv("ANDES_AUTH_TOKEN"); + final var deploymentId = System.getenv("ANDES_DEPLOYMENT_ID"); + + if (apiUrl == null || authToken == null || deploymentId == null || bluePrint == null) { + return; + } + + final var httpClient = new OkHttpClient.Builder().build(); + final var objectMapper = new ObjectMapper(); + + final var dataSource = new AndesConfigurationVariableSource(httpClient, apiUrl, bluePrint, authToken, deploymentId, objectMapper); + + final var variables = dataSource.readConfigurationVariables("DEV"); + + assertNotNull(variables); + assertFalse(variables.isEmpty()); + } + +} \ No newline at end of file -- 2.43.0