use of com.nike.riposte.client.asynchttp.ning.AsyncHttpClientHelper 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