Search in sources :

Example 1 with AsyncHttpClientHelper

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;
    });
}
Also used : AppInfoImpl(com.nike.riposte.server.config.impl.AppInfoImpl) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LoggerFactory(org.slf4j.LoggerFactory) HttpMethod(io.netty.handler.codec.http.HttpMethod) CompletableFuture(java.util.concurrent.CompletableFuture) UnknownHostException(java.net.UnknownHostException) InetAddress(java.net.InetAddress) Map(java.util.Map) AppInfo(com.nike.riposte.server.config.AppInfo) TypeReference(com.fasterxml.jackson.core.type.TypeReference) AsyncHttpClientHelper(com.nike.riposte.client.asynchttp.ning.AsyncHttpClientHelper) Response(com.ning.http.client.Response) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AsyncHttpClientHelper (com.nike.riposte.client.asynchttp.ning.AsyncHttpClientHelper)1 AppInfo (com.nike.riposte.server.config.AppInfo)1 AppInfoImpl (com.nike.riposte.server.config.impl.AppInfoImpl)1 Response (com.ning.http.client.Response)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1