Search in sources :

Example 1 with DomainResource

use of org.cloudfoundry.client.v2.domains.DomainResource in project promregator by promregator.

the class CFAccessorSimulator method retrieveAllDomains.

@Override
public Mono<ListOrganizationDomainsResponse> retrieveAllDomains(String orgId) {
    List<DomainResource> domains = new ArrayList<DomainResource>();
    for (int i = 1; i <= 100; i++) {
        DomainResource domain = DomainResource.builder().entity(DomainEntity.builder().name(SHARED_DOMAIN).internal(false).build()).metadata(Metadata.builder().id(SHARED_DOMAIN_UUID + i).createdAt(CREATED_AT_TIMESTAMP).build()).build();
        domains.add(domain);
    }
    DomainResource domain = DomainResource.builder().entity(DomainEntity.builder().name(INTERNAL_DOMAIN).internal(true).build()).metadata(Metadata.builder().id(INTERNAL_DOMAIN_UUID).createdAt(CREATED_AT_TIMESTAMP).build()).build();
    domains.add(domain);
    ListOrganizationDomainsResponse response = ListOrganizationDomainsResponse.builder().addAllResources(domains).build();
    return Mono.just(response);
}
Also used : DomainResource(org.cloudfoundry.client.v2.domains.DomainResource) ArrayList(java.util.ArrayList) ListOrganizationDomainsResponse(org.cloudfoundry.client.v2.organizations.ListOrganizationDomainsResponse)

Example 2 with DomainResource

use of org.cloudfoundry.client.v2.domains.DomainResource in project promregator by promregator.

the class CFAccessorSimulatorTest method testRetrieveAllDomains.

@Test
void testRetrieveAllDomains() {
    CFAccessorSimulator subject = new CFAccessorSimulator(2);
    Mono<ListOrganizationDomainsResponse> mono = subject.retrieveAllDomains(CFAccessorSimulator.ORG_UUID);
    ListOrganizationDomainsResponse result = mono.block();
    Assertions.assertNotNull(result);
    Assertions.assertNotNull(result.getResources());
    Assertions.assertEquals(101, result.getResources().size());
    for (int i = 0; i <= 99; i++) {
        int domainSequenceId = i + 1;
        DomainResource item = result.getResources().get(i);
        Assertions.assertEquals(CFAccessorSimulator.SHARED_DOMAIN, item.getEntity().getName());
        Assertions.assertFalse(item.getEntity().getInternal());
        Assertions.assertTrue(item.getMetadata().getId().contains(CFAccessorSimulator.SHARED_DOMAIN_UUID + domainSequenceId));
    }
    // get the shared domain
    DomainResource sharedDomain = result.getResources().get(100);
    Assertions.assertTrue(sharedDomain.getEntity().getInternal());
    Assertions.assertEquals(CFAccessorSimulator.INTERNAL_DOMAIN, sharedDomain.getEntity().getName());
}
Also used : DomainResource(org.cloudfoundry.client.v2.domains.DomainResource) ListOrganizationDomainsResponse(org.cloudfoundry.client.v2.organizations.ListOrganizationDomainsResponse) Test(org.junit.jupiter.api.Test)

Example 3 with DomainResource

use of org.cloudfoundry.client.v2.domains.DomainResource in project promregator by promregator.

the class CFAccessorMock method retrieveAllDomains.

@Override
public Mono<ListOrganizationDomainsResponse> retrieveAllDomains(String orgId) {
    List<DomainResource> domains = new ArrayList<DomainResource>();
    DomainResource domain = DomainResource.builder().entity(DomainEntity.builder().name(UNITTEST_INTERNAL_DOMAIN).internal(true).build()).metadata(Metadata.builder().id(UNITTEST_INTERNAL_DOMAIN_UUID).createdAt(CREATED_AT_TIMESTAMP).build()).build();
    domains.add(domain);
    domain = DomainResource.builder().entity(DomainEntity.builder().name(UNITTEST_SHARED_DOMAIN).internal(false).build()).metadata(Metadata.builder().id(UNITTEST_SHARED_DOMAIN_UUID).createdAt(CREATED_AT_TIMESTAMP).build()).build();
    domains.add(domain);
    domain = DomainResource.builder().entity(DomainEntity.builder().name(UNITTEST_ADDITIONAL_SHARED_DOMAIN).internal(false).build()).metadata(Metadata.builder().id(UNITTEST_ADDITIONAL_SHARED_DOMAIN_UUID).createdAt(CREATED_AT_TIMESTAMP).build()).build();
    domains.add(domain);
    ListOrganizationDomainsResponse response = ListOrganizationDomainsResponse.builder().addAllResources(domains).build();
    return Mono.just(response);
}
Also used : DomainResource(org.cloudfoundry.client.v2.domains.DomainResource) ArrayList(java.util.ArrayList) ListOrganizationDomainsResponse(org.cloudfoundry.client.v2.organizations.ListOrganizationDomainsResponse)

Example 4 with DomainResource

use of org.cloudfoundry.client.v2.domains.DomainResource in project promregator by promregator.

the class ReactiveAppInstanceScanner method determineInstancesFromTargets.

@Override
public List<Instance> determineInstancesFromTargets(List<ResolvedTarget> targets, @Nullable Predicate<? super String> applicationIdFilter, @Nullable Predicate<? super Instance> instanceFilter) {
    Flux<ResolvedTarget> targetsFlux = Flux.fromIterable(targets);
    Flux<OSAVector> initialOSAVectorFlux = targetsFlux.map(target -> {
        OSAVector v = new OSAVector();
        v.setTarget(target);
        v.setApplicationId(target.getApplicationId());
        v.setInternalRoutePort(target.getOriginalTarget().getInternalRoutePort());
        return v;
    });
    Flux<String> orgIdFlux = initialOSAVectorFlux.flatMapSequential(v -> this.getOrgId(v.getTarget().getOrgName()));
    Flux<OSAVector> osaVectorOrgFlux = Flux.zip(initialOSAVectorFlux, orgIdFlux).flatMap(tuple -> {
        OSAVector v = tuple.getT1();
        if (INVALID_ORG_ID.equals(tuple.getT2())) {
            // NB: This drops the current target!
            return Mono.empty();
        }
        v.setOrgId(tuple.getT2());
        return Mono.just(v);
    });
    Flux<String> spaceIdFlux = osaVectorOrgFlux.flatMapSequential(v -> this.getSpaceId(v.getOrgId(), v.getTarget().getSpaceName()));
    Flux<OSAVector> osaVectorSpaceFlux = Flux.zip(osaVectorOrgFlux, spaceIdFlux).flatMap(tuple -> {
        OSAVector v = tuple.getT1();
        if (INVALID_SPACE_ID.equals(tuple.getT2())) {
            // NB: This drops the current target!
            return Mono.empty();
        }
        v.setSpaceId(tuple.getT2());
        return Mono.just(v);
    });
    Flux<Map<String, SpaceApplicationSummary>> spaceSummaryFlux = osaVectorSpaceFlux.flatMapSequential(v -> this.getSpaceSummary(v.getSpaceId()));
    Flux<OSAVector> osaVectorApplicationFlux = Flux.zip(osaVectorSpaceFlux, spaceSummaryFlux).flatMap(tuple -> {
        OSAVector v = tuple.getT1();
        if (INVALID_SUMMARY == tuple.getT2()) {
            // NB: This drops the current target!
            return Mono.empty();
        }
        Map<String, SpaceApplicationSummary> spaceSummaryMap = tuple.getT2();
        SpaceApplicationSummary sas = spaceSummaryMap.get(v.getTarget().getApplicationName().toLowerCase(LOCALE_OF_LOWER_CASE_CONVERSION_FOR_IDENTIFIER_COMPARISON));
        if (sas == null) {
            // NB: This drops the current target!
            return Mono.empty();
        }
        List<String> urls = sas.getUrls();
        if (urls != null && !urls.isEmpty()) {
            // Set the access url to the selected route (without any protocol or path yet)
            v.setAccessURL(this.determineApplicationRoute(urls, v.getTarget().getOriginalTarget().getPreferredRouteRegexPatterns()));
        } else {
            // if there is no url, skip this one
            return Mono.empty();
        }
        // There is little reason why this should not find the correct domain
        try {
            Route route = sas.getRoutes().stream().filter(rt -> v.getAccessURL().startsWith(rt.getHost() + "." + rt.getDomain().getName())).findFirst().get();
            v.setDomainId(route.getDomain().getId());
        } catch (Exception e) {
            log.warn(String.format("unable to find matching domain for the url %s", v.getAccessURL()));
        }
        v.setNumberOfInstances(sas.getInstances());
        return Mono.just(v);
    });
    Flux<List<DomainResource>> domainFlux = osaVectorApplicationFlux.flatMapSequential(v -> {
        return this.cfAccessor.retrieveAllDomains(v.getOrgId()).map(mapper -> mapper.getResources());
    });
    Flux<OSAVector> osaVectorDomainApplicationFlux = Flux.zip(osaVectorApplicationFlux, domainFlux).flatMap(tuple -> {
        OSAVector v = tuple.getT1();
        List<DomainResource> domains = tuple.getT2();
        if (domains.size() == 0 || v.getDomainId() == null) {
            // NB: This drops the current target!
            return Mono.empty();
        }
        // this is to make sure we have compatibility with existing behaviour
        if (!v.getDomainId().isEmpty()) {
            try {
                DomainResource domain = domains.stream().filter(r -> r.getMetadata().getId().equals(v.getDomainId())).findFirst().get();
                v.setInternal(domain.getEntity().getInternal());
            } catch (Exception e) {
                log.warn(String.format("unable to find matching domain for the domain with id %s", v.getDomainId()));
            }
        }
        return Mono.just(v);
    });
    // perform pre-filtering, if available
    if (applicationIdFilter != null) {
        osaVectorDomainApplicationFlux = osaVectorDomainApplicationFlux.filter(v -> applicationIdFilter.test(v.getApplicationId()));
    }
    Flux<Instance> instancesFlux = osaVectorDomainApplicationFlux.flatMapSequential(v -> {
        List<Instance> instances = new ArrayList<>(v.getNumberOfInstances());
        for (int i = 0; i < v.numberOfInstances; i++) {
            Instance inst = new Instance(v.getTarget(), String.format("%s:%d", v.getApplicationId(), i), v.getAccessURL(), v.isInternal());
            if (v.isInternal()) {
                inst.setAccessUrl(this.formatInternalAccessURL(v.getAccessURL(), v.getTarget().getPath(), v.getInternalRoutePort(), i));
            } else {
                inst.setAccessUrl(this.formatAccessURL(v.getTarget().getProtocol(), v.getAccessURL(), v.getTarget().getPath()));
            }
            instances.add(inst);
        }
        return Flux.fromIterable(instances);
    });
    // perform pre-filtering, if available
    if (instanceFilter != null) {
        instancesFlux = instancesFlux.filter(instanceFilter);
    }
    Mono<List<Instance>> listInstancesMono = instancesFlux.collectList();
    List<Instance> result = null;
    try {
        result = listInstancesMono.block();
    } catch (RuntimeException e) {
        log.error("Error during retrieving the instances of a list of targets", e);
        result = null;
    }
    return result;
}
Also used : Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) SpaceApplicationSummary(org.cloudfoundry.client.v2.spaces.SpaceApplicationSummary) DomainResource(org.cloudfoundry.client.v2.domains.DomainResource) CFAccessor(org.cloudfoundry.promregator.cfaccessor.CFAccessor) ArrayList(java.util.ArrayList) SpaceResource(org.cloudfoundry.client.v2.spaces.SpaceResource) ListSpacesResponse(org.cloudfoundry.client.v2.spaces.ListSpacesResponse) Value(org.springframework.beans.factory.annotation.Value) Flux(reactor.core.publisher.Flux) List(java.util.List) Matcher(java.util.regex.Matcher) Locale(java.util.Locale) CollectionUtils(org.springframework.util.CollectionUtils) Map(java.util.Map) OrganizationResource(org.cloudfoundry.client.v2.organizations.OrganizationResource) Pattern(java.util.regex.Pattern) Route(org.cloudfoundry.client.v2.routes.Route) Nullable(javax.annotation.Nullable) DomainResource(org.cloudfoundry.client.v2.domains.DomainResource) ArrayList(java.util.ArrayList) SpaceApplicationSummary(org.cloudfoundry.client.v2.spaces.SpaceApplicationSummary) ArrayList(java.util.ArrayList) List(java.util.List) Route(org.cloudfoundry.client.v2.routes.Route) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with DomainResource

use of org.cloudfoundry.client.v2.domains.DomainResource in project promregator by promregator.

the class CFAccessorMassMock method retrieveAllDomains.

@Override
public Mono<ListOrganizationDomainsResponse> retrieveAllDomains(String orgId) {
    List<DomainResource> domains = new ArrayList<DomainResource>();
    for (int i = 0; i < 100; i++) {
        DomainResource domain = DomainResource.builder().entity(DomainEntity.builder().name(UNITTEST_SHARED_DOMAIN).internal(false).build()).metadata(Metadata.builder().id(UNITTEST_SHARED_DOMAIN_UUID + i).createdAt(CREATED_AT_TIMESTAMP).build()).build();
        domains.add(domain);
    }
    ListOrganizationDomainsResponse response = ListOrganizationDomainsResponse.builder().addAllResources(domains).build();
    return Mono.just(response);
}
Also used : DomainResource(org.cloudfoundry.client.v2.domains.DomainResource) ArrayList(java.util.ArrayList) ListOrganizationDomainsResponse(org.cloudfoundry.client.v2.organizations.ListOrganizationDomainsResponse)

Aggregations

DomainResource (org.cloudfoundry.client.v2.domains.DomainResource)5 ArrayList (java.util.ArrayList)4 ListOrganizationDomainsResponse (org.cloudfoundry.client.v2.organizations.ListOrganizationDomainsResponse)4 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Predicate (java.util.function.Predicate)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Nullable (javax.annotation.Nullable)1 OrganizationResource (org.cloudfoundry.client.v2.organizations.OrganizationResource)1 Route (org.cloudfoundry.client.v2.routes.Route)1 ListSpacesResponse (org.cloudfoundry.client.v2.spaces.ListSpacesResponse)1 SpaceApplicationSummary (org.cloudfoundry.client.v2.spaces.SpaceApplicationSummary)1 SpaceResource (org.cloudfoundry.client.v2.spaces.SpaceResource)1 CFAccessor (org.cloudfoundry.promregator.cfaccessor.CFAccessor)1 Test (org.junit.jupiter.api.Test)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1