use of com.nike.riposte.server.config.AppInfo in project riposte by Nike-Inc.
the class AwsUtil method getAppInfoFutureWithAwsInfo.
/**
* @param appId The app ID for the running application.
* @param environment The environment the application is running in (local, test, prod, etc).
* @param asyncHttpClientHelper The async HTTP client you want this method to use to make the AWS metadata calls.
*
* @return A {@link CompletableFuture} that will eventually yield an {@link AppInfo} with the values coming from the
* given arguments for app ID and environment, and coming from the AWS metadata services for datacenter and instance
* ID. If the given environment is "local" then {@link AppInfoImpl#createLocalInstance(String)} will be returned
* (see the javadocs of that method for more information on what values it will contain). Otherwise the AWS metadata
* services will be used to determine {@link AppInfo#dataCenter()} and {@link AppInfo#instanceId()}. If those AWS
* metadata calls fail for any reason then {@link AppInfo#UNKNOWN_VALUE} will be used instead.
*/
public static CompletableFuture<AppInfo> getAppInfoFutureWithAwsInfo(String appId, String environment, AsyncHttpClientHelper asyncHttpClientHelper) {
if ("local".equalsIgnoreCase(environment) || "compiletimetest".equalsIgnoreCase(environment)) {
AppInfo localAppInfo = AppInfoImpl.createLocalInstance(appId);
logger.info("Local environment. Using the following data for AppInfo. " + "appId={}, environment={}, dataCenter={}, instanceId={}", localAppInfo.appId(), localAppInfo.environment(), localAppInfo.dataCenter(), localAppInfo.instanceId());
return CompletableFuture.completedFuture(AppInfoImpl.createLocalInstance(appId));
}
// Not local, so assume AWS.
CompletableFuture<String> dataCenterFuture = getAwsRegion(asyncHttpClientHelper);
CompletableFuture<String> instanceIdFuture = getAwsInstanceId(asyncHttpClientHelper);
return CompletableFuture.allOf(dataCenterFuture, instanceIdFuture).thenApply((aVoid) -> {
String dataCenter = dataCenterFuture.join();
String instanceId = instanceIdFuture.join();
if (AppInfo.UNKNOWN_VALUE.equals(instanceId)) {
try {
instanceId = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
logger.error("An error occurred trying to use local hostname as fallback. " + "Using 'unknown' as the fallback's fallback.", e);
}
}
logger.info("Non-local environment. Using the following data for AppInfo. " + "appId={}, environment={}, dataCenter={}, instanceId={}", appId, environment, dataCenter, instanceId);
return new AppInfoImpl(appId, environment, dataCenter, instanceId);
});
}
use of com.nike.riposte.server.config.AppInfo in project riposte by Nike-Inc.
the class AwsUtilTest method getAppInfoFutureWithAwsInfo_with_all_args_uses_InetAddress_local_hostname_if_getAwsInstanceId_returns_unknown.
@Test
public void getAppInfoFutureWithAwsInfo_with_all_args_uses_InetAddress_local_hostname_if_getAwsInstanceId_returns_unknown() throws IOException {
// given
String appId = "appid-" + UUID.randomUUID().toString();
String environment = "environment-" + UUID.randomUUID().toString();
String expectedDataCenter = AwsUtil.getAwsRegion(asyncClientMock).join();
doReturn(null).when(responseMockForAwsInstanceId).getResponseBody();
String expectedInstanceId = InetAddress.getLocalHost().getHostName();
// when
AppInfo result = AwsUtil.getAppInfoFutureWithAwsInfo(appId, environment, asyncClientMock).join();
// then
assertThat(result.appId()).isEqualTo(appId);
assertThat(result.environment()).isEqualTo(environment);
assertThat(result.dataCenter()).isEqualTo(expectedDataCenter);
assertThat(result.instanceId()).isEqualTo(expectedInstanceId);
}
use of com.nike.riposte.server.config.AppInfo in project riposte by Nike-Inc.
the class AwsUtilTest method getAppInfoFutureWithAwsInfo_with_all_args_uses_data_from_getAwsRegion_and_getAwsInstanceId_to_build_result.
@Test
public void getAppInfoFutureWithAwsInfo_with_all_args_uses_data_from_getAwsRegion_and_getAwsInstanceId_to_build_result() {
// given
String appId = "appid-" + UUID.randomUUID().toString();
String environment = "environment-" + UUID.randomUUID().toString();
String expectedDataCenter = AwsUtil.getAwsRegion(asyncClientMock).join();
String expectedInstanceId = AwsUtil.getAwsInstanceId(asyncClientMock).join();
// when
AppInfo result = AwsUtil.getAppInfoFutureWithAwsInfo(appId, environment, asyncClientMock).join();
// then
assertThat(result.appId()).isEqualTo(appId);
assertThat(result.environment()).isEqualTo(environment);
assertThat(result.dataCenter()).isEqualTo(expectedDataCenter);
assertThat(result.instanceId()).isEqualTo(expectedInstanceId);
}
use of com.nike.riposte.server.config.AppInfo in project riposte by Nike-Inc.
the class AwsUtilTest method getAppInfoFutureWithAwsInfo_with_minimal_args_delegates_to_kitchen_sink_overload_method.
@Test
public void getAppInfoFutureWithAwsInfo_with_minimal_args_delegates_to_kitchen_sink_overload_method() {
// given
String appId = "appid-" + UUID.randomUUID().toString();
String environment = "environment-" + UUID.randomUUID().toString();
String expectedDataCenter = AwsUtil.getAwsRegion(asyncClientMock).join();
String expectedInstanceId = AwsUtil.getAwsInstanceId(asyncClientMock).join();
setAppIdAndEnvironemntSystemProperties(appId, environment);
// when
AppInfo result = AwsUtil.getAppInfoFutureWithAwsInfo(asyncClientMock).join();
// then
assertThat(result.appId()).isEqualTo(appId);
assertThat(result.environment()).isEqualTo(environment);
assertThat(result.dataCenter()).isEqualTo(expectedDataCenter);
assertThat(result.instanceId()).isEqualTo(expectedInstanceId);
}
use of com.nike.riposte.server.config.AppInfo in project riposte by Nike-Inc.
the class AwsUtil method getAwsRegion.
/**
* @param asyncHttpClientHelper The async HTTP client you want this method to use to make the AWS metadata call.
*
* @return A {@link CompletableFuture} that will contain the AWS region this app is running in (assuming it
* completes successfully). If an error occurs retrieving the region from AWS then the error will be logged and
* {@link AppInfo#UNKNOWN_VALUE} returned as the value.
*/
public static CompletableFuture<String> getAwsRegion(AsyncHttpClientHelper asyncHttpClientHelper) {
return asyncHttpClientHelper.executeAsyncHttpRequest(asyncHttpClientHelper.getRequestBuilder(AMAZON_METADATA_DOCUMENT_URL, HttpMethod.GET), response -> {
String region = null;
try {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, String> resultMap = objectMapper.readValue(response.getResponseBody(), new TypeReference<Map<String, String>>() {
});
region = resultMap.get("region");
} catch (Throwable t) {
logger.error("Error retrieving region from AWS", t);
}
if (region == null) {
logger.error("AWS metadata service returned null for region. Using 'unknown' as fallback.");
region = AppInfo.UNKNOWN_VALUE;
}
return region;
}).handle((region, error) -> {
if (error != null) {
logger.error("Unable to get region info from AWS metadata service.", error);
return AppInfo.UNKNOWN_VALUE;
}
if (region == null) {
logger.error("AWS metadata service returned null for region. Using 'unknown' as fallback.");
region = AppInfo.UNKNOWN_VALUE;
}
return region;
});
}
Aggregations