org.cloudfoundry.client.v2.organizations.OrganizationResource Java Examples

The following examples show how to use org.cloudfoundry.client.v2.organizations.OrganizationResource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CFAccessorSimulator.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Override
public Mono<ListOrganizationsResponse> retrieveOrgId(String orgName) {
	
	if ("simorg".equals(orgName)) {
		
		OrganizationResource or = OrganizationResource.builder().entity(
				OrganizationEntity.builder().name(orgName).build()
			).metadata(
				Metadata.builder().createdAt(CREATED_AT_TIMESTAMP).id(ORG_UUID).build()
				// Note that UpdatedAt is not set here, as this can also happen in real life!
			).build();
		
		List<OrganizationResource> list = new LinkedList<>();
		list.add(or);
		
		ListOrganizationsResponse resp = ListOrganizationsResponse.builder().addAllResources(list).build();
		
		return Mono.just(resp).delayElement(this.getSleepRandomDuration());
	}
	log.error("Invalid OrgId request");
	return null;
}
 
Example #2
Source File: ReactiveCFAccessorImpl.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Override
public Mono<ListOrganizationsResponse> retrieveAllOrgIds() {
	PaginatedRequestGeneratorFunction<ListOrganizationsRequest> requestGenerator = (orderDirection, resultsPerPage, pageNumber) ->
		ListOrganizationsRequest.builder()
			.orderDirection(orderDirection)
			.resultsPerPage(resultsPerPage)
			.page(pageNumber)
			.build();
	
	PaginatedResponseGeneratorFunction<OrganizationResource, ListOrganizationsResponse> responseGenerator = (list, numberOfPages) ->
			ListOrganizationsResponse.builder()
			.addAllResources(list)
			.totalPages(numberOfPages)
			.totalResults(list.size())
			.build();
	
	return this.paginatedRequestFetcher.performGenericPagedRetrieval(RequestType.ALL_ORGS, "(empty)", requestGenerator, 
			r -> this.cloudFoundryClient.organizations().list(r),  this.requestTimeoutOrg, responseGenerator);
}
 
Example #3
Source File: ReactiveAppInstanceScanner.java    From promregator with Apache License 2.0 6 votes vote down vote up
private Mono<String> getOrgId(String orgNameString) {
	return this.cfAccessor.retrieveOrgId(orgNameString).flatMap(response -> {
		List<OrganizationResource> resources = response.getResources();
		if (resources == null) {
			return Mono.just(INVALID_ORG_ID);
		}
		
		if (resources.isEmpty()) {
			log.warn(String.format("Received empty result on requesting org %s", orgNameString));
			return Mono.just(INVALID_ORG_ID);
		}
		
		OrganizationResource organizationResource = resources.get(0);
		return Mono.just(organizationResource.getMetadata().getId());
	}).onErrorResume(e -> {
		log.error(String.format("retrieving Org Id for org Name '%s' resulted in an exception", orgNameString), e);
		return Mono.just(INVALID_ORG_ID);
	}).cache();
}
 
Example #4
Source File: ReactiveCFPaginatedRequestFetcherTest.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithContentOnePage() {
	ReactiveCFPaginatedRequestFetcher subject = new ReactiveCFPaginatedRequestFetcher(this.internalMetricsMocked, Double.MAX_VALUE, Duration.ofMillis(100));
	
	Mono<ListOrganizationsResponse> subjectResponseMono = subject.performGenericPagedRetrieval(RequestType.OTHER, "nokey", requestGenerator, request -> {
		LinkedList<OrganizationResource> list = new LinkedList<>();
		list.add(OrganizationResource.builder().build());
		list.add(OrganizationResource.builder().build());
		
		ListOrganizationsResponse response = ListOrganizationsResponse.builder()
				.resources(list)
				.totalPages(1)
				.totalResults(2)
				.build();
		
		return Mono.just(response);
	}, 100, responseGenerator);
	
	ListOrganizationsResponse subjectResponse = subjectResponseMono.block();
	Assert.assertEquals(1, subjectResponse.getTotalPages().intValue());
	Assert.assertEquals(2, subjectResponse.getTotalResults().intValue());
	Assert.assertNotNull (subjectResponse.getResources());
	Assert.assertEquals(2, subjectResponse.getResources().size());
}
 
Example #5
Source File: ReactiveCFPaginatedRequestFetcherTest.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithContentTwoPages() {
	ReactiveCFPaginatedRequestFetcher subject = new ReactiveCFPaginatedRequestFetcher(this.internalMetricsMocked, 0, Duration.ofMillis(100));
	
	Mono<ListOrganizationsResponse> subjectResponseMono = subject.performGenericPagedRetrieval(RequestType.OTHER, "nokey", requestGenerator, request -> {
		LinkedList<OrganizationResource> list = new LinkedList<>();
		
		for (int i = 0;i<request.getResultsPerPage(); i++) {
			list.add(OrganizationResource.builder().build());
		}
		
		ListOrganizationsResponse response = ListOrganizationsResponse.builder()
				.resources(list)
				.totalPages(2)
				.totalResults(2 * request.getResultsPerPage())
				.build();
		
		return Mono.just(response);
	}, 100, responseGenerator);
	
	ListOrganizationsResponse subjectResponse = subjectResponseMono.block();
	Assert.assertEquals(2, subjectResponse.getTotalPages().intValue());
	Assert.assertEquals(2*100, subjectResponse.getTotalResults().intValue());
	Assert.assertNotNull(subjectResponse.getResources());
	Assert.assertEquals(2*100, subjectResponse.getResources().size());
}
 
Example #6
Source File: CFAccessorMassMock.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Override
public Mono<ListOrganizationsResponse> retrieveOrgId(String orgName) {
	
	if ("unittestorg".equals(orgName)) {
		
		OrganizationResource or = OrganizationResource.builder().entity(
				OrganizationEntity.builder().name(orgName).build()
			).metadata(
				Metadata.builder().createdAt(CREATED_AT_TIMESTAMP).id(UNITTEST_ORG_UUID).build()
				// Note that UpdatedAt is not set here, as this can also happen in real life!
			).build();
		
		List<org.cloudfoundry.client.v2.organizations.OrganizationResource> list = new LinkedList<>();
		list.add(or);
		
		ListOrganizationsResponse resp = org.cloudfoundry.client.v2.organizations.ListOrganizationsResponse.builder().addAllResources(list).build();
		
		return Mono.just(resp).delayElement(this.getSleepRandomDuration());
	}
	Assert.fail("Invalid OrgId request");
	return null;
}
 
Example #7
Source File: ReactiveCFPaginatedRequestFetcherTest.java    From promregator with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithContentTwoPagesSecondWithoutItems() {
	ReactiveCFPaginatedRequestFetcher subject = new ReactiveCFPaginatedRequestFetcher(this.internalMetricsMocked, Double.MAX_VALUE, Duration.ofMillis(100));
	
	Mono<ListOrganizationsResponse> subjectResponseMono = subject.performGenericPagedRetrieval(RequestType.OTHER, "nokey", requestGenerator, request -> {
		LinkedList<OrganizationResource> list = new LinkedList<>();
		
		if (request.getPage() == 1) {
			for (int i = 0;i<request.getResultsPerPage(); i++) {
				list.add(OrganizationResource.builder().build());
			}
		}
		
		ListOrganizationsResponse response = ListOrganizationsResponse.builder()
				.resources(list)
				.totalPages(2)
				.totalResults( (2 - request.getPage()) * request.getResultsPerPage()) // that's tricky&ugly, but correct: It would be the answer, if suddenly the items on the 2nd page vanished
				.build();
		
		return Mono.just(response);
	}, 100, responseGenerator);
	
	ListOrganizationsResponse subjectResponse = subjectResponseMono.block();
	Assert.assertEquals(2, subjectResponse.getTotalPages().intValue());
	Assert.assertEquals(1*100, subjectResponse.getTotalResults().intValue());
	Assert.assertNotNull (subjectResponse.getResources());
	Assert.assertEquals(1*100, subjectResponse.getResources().size());
}
 
Example #8
Source File: ReactiveCFPaginatedRequestFetcherTest.java    From promregator with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithContentTimeoutOnSecondPage() {
	ReactiveCFPaginatedRequestFetcher subject = new ReactiveCFPaginatedRequestFetcher(this.internalMetricsMocked, Double.MAX_VALUE, Duration.ofMillis(100));
	
	Mono<ListOrganizationsResponse> subjectResponseMono = subject.performGenericPagedRetrieval(RequestType.OTHER, "nokey", requestGenerator, request -> {
		if (request.getPage() == 2) {
			return Mono.error(new TimeoutException());
		}
		
		LinkedList<OrganizationResource> list = new LinkedList<>();
		
		if (request.getPage() == 1) {
			for (int i = 0;i<request.getResultsPerPage(); i++) {
				list.add(OrganizationResource.builder().build());
			}
		}
		
		ListOrganizationsResponse response = ListOrganizationsResponse.builder()
				.resources(list)
				.totalPages(2)
				.totalResults(2 * request.getResultsPerPage()) 
				.build();
		
		return Mono.just(response);
	}, 100, responseGenerator);
	
	ListOrganizationsResponse fallback = ListOrganizationsResponse.builder().build();
	
	ListOrganizationsResponse subjectResponse = subjectResponseMono.doOnError(e ->
		Assert.assertTrue(Exceptions.unwrap(e) instanceof TimeoutException)
	).onErrorReturn(fallback).block();
	Assert.assertEquals(fallback, subjectResponse);
}
 
Example #9
Source File: CFAccessorMock.java    From promregator with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<ListOrganizationsResponse> retrieveOrgId(String orgName) {
	
	if ("unittestorg".equalsIgnoreCase(orgName)) {
		
		OrganizationResource or = OrganizationResource.builder().entity(
				OrganizationEntity.builder().name("unittestorg").build()
			).metadata(
				Metadata.builder().createdAt(CREATED_AT_TIMESTAMP).id(UNITTEST_ORG_UUID).build()
				// Note that UpdatedAt is not set here, as this can also happen in real life!
			).build();
		
		List<org.cloudfoundry.client.v2.organizations.OrganizationResource> list = new LinkedList<>();
		list.add(or);
		
		ListOrganizationsResponse resp = org.cloudfoundry.client.v2.organizations.ListOrganizationsResponse.builder().addAllResources(list).build();
		
		return Mono.just(resp);
	} else if ("doesnotexist".equals(orgName)) {
		return Mono.just(org.cloudfoundry.client.v2.organizations.ListOrganizationsResponse.builder().resources(new ArrayList<>()).build());
	} else if ("exception".equals(orgName)) {
		return Mono.just(org.cloudfoundry.client.v2.organizations.ListOrganizationsResponse.builder().build())
				.map(x -> {throw new Error("exception org name provided");});
	}
	Assert.fail("Invalid OrgId request");
	return null;
}
 
Example #10
Source File: CloudFoundryTaskLauncherCachingTests.java    From spring-cloud-deployer-cloudfoundry with Apache License 2.0 5 votes vote down vote up
private Mono<ListOrganizationsResponse> listOrganizationsResponse(AtomicBoolean error) {
	// defer so that we can conditionally throw within mono
	return Mono.defer(() -> {
		if (error.get()) {
			throw new RuntimeException();
		}
		ListOrganizationsResponse response = ListOrganizationsResponse.builder()
			.addAllResources(Collections.<OrganizationResource>singletonList(
				OrganizationResource.builder()
						.metadata(Metadata.builder().id("123").build()).build())
			)
			.build();
		return Mono.just(response);
	});
}
 
Example #11
Source File: CloudFoundryTaskLauncherTests.java    From spring-cloud-deployer-cloudfoundry with Apache License 2.0 5 votes vote down vote up
private Mono<ListOrganizationsResponse> listOrganizationsResponse() {
	ListOrganizationsResponse response = ListOrganizationsResponse.builder()
	.addAllResources(Collections.<OrganizationResource>singletonList(
			OrganizationResource.builder()
					.metadata(Metadata.builder().id("123").build()).build())
	).build();
	return Mono.just(response);
}
 
Example #12
Source File: MultiplePlatformTypeTests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
private Mono<ListOrganizationsResponse> listOrganizationsResponse() {
	ListOrganizationsResponse response = ListOrganizationsResponse.builder()
			.addAllResources(Collections.<OrganizationResource>singletonList(
					OrganizationResource.builder()
							.metadata(Metadata.builder().id("123").build()).build())
			).build();
	return Mono.just(response);
}
 
Example #13
Source File: CloudFoundrySchedulerTests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
private Mono<ListOrganizationsResponse> listOrganizationsResponse() {
	ListOrganizationsResponse response = ListOrganizationsResponse.builder()
			.addAllResources(Collections.<OrganizationResource>singletonList(
					OrganizationResource.builder()
							.metadata(Metadata.builder().id("123").build()).build())
			).build();
	return Mono.just(response);
}
 
Example #14
Source File: CloudFoundryTaskPlatformFactoryTests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
private Mono<ListOrganizationsResponse> listOrganizationsResponse() {
	ListOrganizationsResponse response = ListOrganizationsResponse.builder()
			.addAllResources(Collections.<OrganizationResource>singletonList(
					OrganizationResource.builder()
							.metadata(Metadata.builder().id("123").build()).build())
			).build();
	return Mono.just(response);
}
 
Example #15
Source File: ReactiveTargetResolver.java    From promregator with Apache License 2.0 4 votes vote down vote up
private Flux<IntermediateTarget> resolveOrg(IntermediateTarget it) {
	/* NB: Now we have to consider three cases:
	 * Case 1: both orgName and orgRegex is empty => select all orgs
	 * Case 2: orgName is null, but orgRegex is filled => filter all orgs with the regex
	 * Case 3: orgName is filled, but orgRegex is null => select a single org
	 * In cases 1 and 2, we need the list of all orgs on the platform.
	 */
	
	if (it.getConfigTarget().getOrgRegex() == null && it.getConfigTarget().getOrgName() != null) {
		// Case 3: we have the orgName, but we also need its id
		Mono<IntermediateTarget> itMono = this.cfAccessor.retrieveOrgId(it.getConfigTarget().getOrgName())
				.map(ListOrganizationsResponse::getResources)
				.flatMap(resList -> {
					if (resList == null || resList.isEmpty()) {
						return Mono.empty();
					}
					
					return Mono.just(resList.get(0));
				})
				.map(res -> {
					it.setResolvedOrgName(res.getEntity().getName());
					it.setResolvedOrgId(res.getMetadata().getId());
					return it;
				})
				.doOnError(e -> log.warn(String.format("Error on retrieving org id for org '%s'", it.getConfigTarget().getOrgName()), e))
				.onErrorResume(__ -> Mono.empty());
		
		return itMono.flux();
	}
	
	// Case 1 & 2: Get all orgs from the platform
	Mono<ListOrganizationsResponse> responseMono = this.cfAccessor.retrieveAllOrgIds();

	Flux<OrganizationResource> orgResFlux = responseMono.map(ListOrganizationsResponse::getResources)
		.flatMapMany(Flux::fromIterable);
	
	if (it.getConfigTarget().getOrgRegex() != null) {
		// Case 2
		final Pattern filterPattern = Pattern.compile(it.getConfigTarget().getOrgRegex(), Pattern.CASE_INSENSITIVE);
		
		orgResFlux = orgResFlux.filter(orgRes -> {
			Matcher m = filterPattern.matcher(orgRes.getEntity().getName());
			return m.matches();
		});
	}
	
	return orgResFlux.map(orgRes -> {
		IntermediateTarget itnew = new IntermediateTarget(it);
		itnew.setResolvedOrgId(orgRes.getMetadata().getId());
		itnew.setResolvedOrgName(orgRes.getEntity().getName());
		
		return itnew;
	});
}
 
Example #16
Source File: RawCloudOrganizationTest.java    From cf-java-client-sap with Apache License 2.0 4 votes vote down vote up
private static Resource<OrganizationEntity> buildTestResource() {
    return OrganizationResource.builder()
                               .metadata(RawCloudEntityTest.METADATA)
                               .entity(buildTestEntity())
                               .build();
}