Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for specifying custom profiles in Spring Cloud Config client #32130

Merged
merged 1 commit into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.nio.file.Path;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -110,6 +111,11 @@ public interface SpringCloudConfigClientConfig {
*/
Map<String, String> headers();

/**
* The profiles to use for lookup
*/
Optional<List<String>> profiles();

/** */
default boolean usernameAndPasswordSet() {
return username().isPresent() && password().isPresent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Iterable<ConfigSource> getConfigSources(final ConfigSourceContext context
VertxSpringCloudConfigGateway client = new VertxSpringCloudConfigGateway(config);
try {
List<Response> responses = new ArrayList<>();
for (String profile : context.getProfiles()) {
for (String profile : determineProfiles(context, config)) {
Response response;
if (connectionTimeoutIsGreaterThanZero || readTimeoutIsGreaterThanZero) {
response = client.exchange(applicationName.getValue(), profile).await()
Expand Down Expand Up @@ -88,6 +88,13 @@ public Iterable<ConfigSource> getConfigSources(final ConfigSourceContext context
}
}

private static List<String> determineProfiles(ConfigSourceContext context, SpringCloudConfigClientConfig config) {
if (config.profiles().isPresent()) {
return config.profiles().get();
}
return context.getProfiles();
}

private static class SpringCloudPropertySource extends MapBackedConfigSource {
private SpringCloudPropertySource(final String name, final Map<String, String> propertyMap, final int defaultOrdinal) {
super(name, propertyMap, defaultOrdinal);
Expand Down
29 changes: 29 additions & 0 deletions integration-tests/spring-cloud-config-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,35 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>default-test</id>
<configuration>
<!-- include tags -->
<groups>common,test</groups>
<!-- exclude tags -->
<excludedGroups>test-only</excludedGroups>
</configuration>
</execution>
<!-- This configuration is used in order to test that 'quarkus.spring-cloud-config.profiles' is taken into account -->
<execution>
<id>profiles-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<systemPropertyVariables>
<quarkus.spring-cloud-config.profiles>test</quarkus.spring-cloud-config.profiles>
</systemPropertyVariables>
<!-- include tags -->
<groups>test-only</groups>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import static jakarta.ws.rs.core.Response.Status.OK;
import static org.hamcrest.Matchers.equalTo;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
@QuarkusTestResource(SpringCloudConfigServerResource.class)
public class SpringCloudConfigClientTest {
@Tag("common")
@Tag("test")
public class CommonAndTestProfilesTest {
@Test
void config() {
given()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.quarkus.spring.cloud.config.client.runtime;

import static io.restassured.RestAssured.given;
import static jakarta.ws.rs.core.Response.Status.OK;
import static org.hamcrest.Matchers.equalTo;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
@QuarkusTestResource(SpringCloudConfigServerResource.class)
@Tag("test")
@Tag("test-only")
public class OnlyTestProfileTest {
@Test
void config() {
given()
.get("/config/{name}", "greeting.message")
.then()
.statusCode(OK.getStatusCode())
.body("value", equalTo("hello from spring cloud config server"));
}

@Test
void ordinal() {
given()
.get("/config/{name}", "foo")
.then()
.statusCode(OK.getStatusCode())
.body("value", equalTo("from foo development"))
.body("sourceName", equalTo("https://github.com/spring-cloud-samples/config-repo/testapp-prod.yml"));

given()
.get("/config/{name}", "info.description")
.then()
.statusCode(OK.getStatusCode())
.body("value", equalTo("Sample"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class SpringCloudConfigClientIT extends SpringCloudConfigClientTest {
public class SpringCloudConfigClientIT extends CommonAndTestProfilesTest {

}