Search in sources :

Example 1 with AppInfo

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);
    });
}
Also used : UnknownHostException(java.net.UnknownHostException) AppInfoImpl(com.nike.riposte.server.config.impl.AppInfoImpl) AppInfo(com.nike.riposte.server.config.AppInfo)

Example 2 with AppInfo

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);
}
Also used : AppInfo(com.nike.riposte.server.config.AppInfo) Test(org.junit.Test)

Example 3 with AppInfo

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);
}
Also used : AppInfo(com.nike.riposte.server.config.AppInfo) Test(org.junit.Test)

Example 4 with AppInfo

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);
}
Also used : AppInfo(com.nike.riposte.server.config.AppInfo) Test(org.junit.Test)

Example 5 with AppInfo

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;
    });
}
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

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