Skip to content

Commit 605797a

Browse files
committed
Add AWS to cloud platforms
This commit adds support for detecting AWS to `CloudPlatform`. The detection is based on presence of `AWS_DEFAULT_REGION` and `AWS_EXECUTION_ENV` environment variables. Signed-off-by: Vedran Pavic <vedran@vedranpavic.com>
1 parent 5afbec7 commit 605797a

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

core/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,21 @@ public boolean isDetected(Environment environment) {
164164
return this.azureEnvVariables.stream().allMatch(environment::containsProperty);
165165
}
166166

167+
},
168+
169+
/**
170+
* Amazon Web Services (AWS) platform.
171+
* @since 4.0.0
172+
*/
173+
AWS {
174+
175+
private final List<String> awsEnvVariables = Arrays.asList("AWS_DEFAULT_REGION", "AWS_EXECUTION_ENV");
176+
177+
@Override
178+
public boolean isDetected(Environment environment) {
179+
return this.awsEnvVariables.stream().allMatch(environment::containsProperty);
180+
}
181+
167182
};
168183

169184
private static final String PROPERTY_NAME = "spring.main.cloud-platform";

core/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.stream.Stream;
2323

2424
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.ValueSource;
2527

2628
import org.springframework.boot.context.properties.bind.Binder;
2729
import org.springframework.boot.context.properties.source.MockConfigurationPropertySource;
@@ -205,6 +207,28 @@ void getActiveWhenHasMissingWebsiteSkuShouldNotReturnAzureAppService() {
205207
assertThat(platform).isNull();
206208
}
207209

210+
@Test
211+
void getActiveWhenHasAllAwsEnvVariablesShouldReturnAws() {
212+
Map<String, Object> envVars = new HashMap<>();
213+
envVars.put("AWS_DEFAULT_REGION", "eu-west-1");
214+
envVars.put("AWS_EXECUTION_ENV", "AWS_ECS_FARGATE");
215+
Environment environment = getEnvironmentWithEnvVariables(envVars);
216+
CloudPlatform platform = CloudPlatform.getActive(environment);
217+
assertThat(platform).isNotNull().isEqualTo(CloudPlatform.AWS);
218+
assertThat(platform.isActive(environment)).isTrue();
219+
}
220+
221+
@ParameterizedTest
222+
@ValueSource(strings = { "AWS_DEFAULT_REGION", "AWS_EXECUTION_ENV" })
223+
void getActiveWhenHasMissingAwsVariableShouldNotReturnAws(String missingEnvVar) {
224+
Map<String, Object> envVars = new HashMap<>();
225+
envVars.compute("AWS_DEFAULT_REGION", (k, v) -> k.equals(missingEnvVar) ? null : "eu-west-1");
226+
envVars.compute("AWS_EXECUTION_ENV", (k, v) -> k.equals(missingEnvVar) ? null : "AWS_ECS_FARGATE");
227+
Environment environment = getEnvironmentWithEnvVariables(envVars);
228+
CloudPlatform platform = CloudPlatform.getActive(environment);
229+
assertThat(platform).isNull();
230+
}
231+
208232
@Test
209233
void getActiveWhenHasEnforcedCloudPlatform() {
210234
Environment environment = getEnvironmentWithEnvVariables(

0 commit comments

Comments
 (0)