Search in sources :

Example 1 with RouteMappingResource

use of org.cloudfoundry.client.v2.routemappings.RouteMappingResource in project promregator by promregator.

the class ReactiveAppInstanceScanner method getApplicationUrl.

private Mono<String> getApplicationUrl(Mono<String> applicationIdMono, String protocol) {
    String key = String.format("%d", applicationIdMono.hashCode());
    synchronized (key.intern()) {
        Mono<String> cached = this.hostnameMap.get(key);
        if (cached != null) {
            this.internalMetrics.countHit("appinstancescanner.route");
            return cached;
        }
        this.internalMetrics.countMiss("appinstancescanner.route");
        ReactiveTimer reactiveTimer = new ReactiveTimer(this.internalMetrics, "route");
        Mono<RouteEntity> routeMono = applicationIdMono.zipWith(Mono.just(reactiveTimer)).map(tuple -> {
            tuple.getT2().start();
            return tuple.getT1();
        }).flatMap(appId -> {
            return this.cfAccessor.retrieveRouteMapping(appId);
        }).flatMap(mappingResponse -> {
            List<RouteMappingResource> resourceList = mappingResponse.getResources();
            if (resourceList.isEmpty())
                return Mono.empty();
            String routeId = resourceList.get(0).getEntity().getRouteId();
            if (routeId == null)
                return Mono.empty();
            return this.cfAccessor.retrieveRoute(routeId);
        }).flatMap(GetRouteResponse -> {
            RouteEntity route = GetRouteResponse.getEntity();
            if (route == null)
                return Mono.empty();
            // and not the URL which points to the endpoint of the cell!
            return Mono.just(route);
        });
        Mono<String> domainMono = routeMono.map(route -> route.getDomainId()).flatMap(domainId -> {
            return this.getDomain(domainId);
        });
        Mono<String> applicationUrlMono = Mono.zip(domainMono, routeMono).map(tuple -> {
            String domain = tuple.getT1();
            RouteEntity route = tuple.getT2();
            String url = String.format("%s://%s.%s", protocol, route.getHost(), domain);
            if (route.getPath() != null) {
                url += "/" + route.getPath();
            }
            return url;
        }).zipWith(Mono.just(reactiveTimer)).map(tuple -> {
            tuple.getT2().stop();
            return tuple.getT1();
        }).cache();
        this.hostnameMap.put(key, applicationUrlMono);
        return applicationUrlMono;
    }
}
Also used : RouteEntity(org.cloudfoundry.client.v2.routes.RouteEntity) Target(org.cloudfoundry.promregator.config.Target) Autowired(org.springframework.beans.factory.annotation.Autowired) SharedDomainEntity(org.cloudfoundry.client.v2.shareddomains.SharedDomainEntity) ListProcessesResponse(org.cloudfoundry.client.v3.processes.ListProcessesResponse) Mono(reactor.core.publisher.Mono) CFAccessor(org.cloudfoundry.promregator.cfaccessor.CFAccessor) SpaceResource(org.cloudfoundry.client.v2.spaces.SpaceResource) TimeUnit(java.util.concurrent.TimeUnit) Value(org.springframework.beans.factory.annotation.Value) PassiveExpiringMap(org.apache.commons.collections4.map.PassiveExpiringMap) Logger(org.apache.log4j.Logger) Flux(reactor.core.publisher.Flux) List(java.util.List) Component(org.springframework.stereotype.Component) ApplicationResource(org.cloudfoundry.client.v2.applications.ApplicationResource) Timer(io.prometheus.client.Histogram.Timer) ProcessResource(org.cloudfoundry.client.v3.processes.ProcessResource) PostConstruct(javax.annotation.PostConstruct) OrganizationResource(org.cloudfoundry.client.v2.organizations.OrganizationResource) LinkedList(java.util.LinkedList) InternalMetrics(org.cloudfoundry.promregator.internalmetrics.InternalMetrics) RouteMappingResource(org.cloudfoundry.client.v2.routemappings.RouteMappingResource) List(java.util.List) LinkedList(java.util.LinkedList) RouteEntity(org.cloudfoundry.client.v2.routes.RouteEntity)

Example 2 with RouteMappingResource

use of org.cloudfoundry.client.v2.routemappings.RouteMappingResource in project promregator by promregator.

the class CFAccessorMock method retrieveRouteMapping.

@Override
public Mono<ListRouteMappingsResponse> retrieveRouteMapping(String appId) {
    RouteMappingEntity entity = null;
    if (appId.equals(UNITTEST_APP1_UUID)) {
        entity = RouteMappingEntity.builder().applicationId(appId).routeId(UNITTEST_APP1_ROUTE_UUID).build();
    } else if (appId.equals(UNITTEST_APP2_UUID)) {
        entity = RouteMappingEntity.builder().applicationId(appId).routeId(UNITTEST_APP2_ROUTE_UUID).build();
    }
    if (entity == null) {
        Assert.fail("Invalid route mapping request");
        return null;
    }
    RouteMappingResource rmr = null;
    rmr = RouteMappingResource.builder().entity(entity).build();
    List<RouteMappingResource> list = new LinkedList<>();
    list.add(rmr);
    ListRouteMappingsResponse resp = ListRouteMappingsResponse.builder().addAllResources(list).build();
    return Mono.just(resp);
}
Also used : RouteMappingEntity(org.cloudfoundry.client.v2.routemappings.RouteMappingEntity) RouteMappingResource(org.cloudfoundry.client.v2.routemappings.RouteMappingResource) LinkedList(java.util.LinkedList) ListRouteMappingsResponse(org.cloudfoundry.client.v2.routemappings.ListRouteMappingsResponse)

Aggregations

LinkedList (java.util.LinkedList)2 RouteMappingResource (org.cloudfoundry.client.v2.routemappings.RouteMappingResource)2 Timer (io.prometheus.client.Histogram.Timer)1 List (java.util.List)1 TimeUnit (java.util.concurrent.TimeUnit)1 PostConstruct (javax.annotation.PostConstruct)1 PassiveExpiringMap (org.apache.commons.collections4.map.PassiveExpiringMap)1 Logger (org.apache.log4j.Logger)1 ApplicationResource (org.cloudfoundry.client.v2.applications.ApplicationResource)1 OrganizationResource (org.cloudfoundry.client.v2.organizations.OrganizationResource)1 ListRouteMappingsResponse (org.cloudfoundry.client.v2.routemappings.ListRouteMappingsResponse)1 RouteMappingEntity (org.cloudfoundry.client.v2.routemappings.RouteMappingEntity)1 RouteEntity (org.cloudfoundry.client.v2.routes.RouteEntity)1 SharedDomainEntity (org.cloudfoundry.client.v2.shareddomains.SharedDomainEntity)1 SpaceResource (org.cloudfoundry.client.v2.spaces.SpaceResource)1 ListProcessesResponse (org.cloudfoundry.client.v3.processes.ListProcessesResponse)1 ProcessResource (org.cloudfoundry.client.v3.processes.ProcessResource)1 CFAccessor (org.cloudfoundry.promregator.cfaccessor.CFAccessor)1 Target (org.cloudfoundry.promregator.config.Target)1 InternalMetrics (org.cloudfoundry.promregator.internalmetrics.InternalMetrics)1