diff --git a/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/_DefaultCloudFoundryOperations.java b/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/_DefaultCloudFoundryOperations.java index 1d21f29b2b..2b0a9996c2 100644 --- a/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/_DefaultCloudFoundryOperations.java +++ b/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/_DefaultCloudFoundryOperations.java @@ -79,7 +79,7 @@ public Advanced advanced() { @Override @Value.Derived public Applications applications() { - return new DefaultApplications(getCloudFoundryClientPublisher(), getDopplerClientPublisher(), getSpaceId()); + return new DefaultApplications(getCloudFoundryClient(), getDopplerClient(), getSpaceId().block()); } @Override @@ -318,12 +318,14 @@ private static boolean hasText(CharSequence str) { private static Flux requestOrganizations(Mono cloudFoundryClientPublisher, String organization) { return cloudFoundryClientPublisher - .flatMapMany(cloudFoundryClient -> PaginationUtils - .requestClientV3Resources(page -> cloudFoundryClient.organizationsV3() - .list(ListOrganizationsRequest.builder() - .name(organization) - .page(page) - .build()))); + .flatMapMany(cloudFoundryClient -> PaginationUtils + .requestClientV3Resources(page -> cloudFoundryClient.organizationsV3() + .list(ListOrganizationsRequest.builder() + .name(organization) + .page(page) + .build()) + ) + ); } private static Flux requestSpaces(Mono cloudFoundryClientPublisher, String organizationId, String space) { diff --git a/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java b/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java index e51ddbb472..9d915cd1e1 100644 --- a/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java +++ b/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java @@ -19,7 +19,6 @@ import static org.cloudfoundry.client.v3.LifecycleType.BUILDPACK; import static org.cloudfoundry.util.DelayUtils.exponentialBackOff; import static org.cloudfoundry.util.tuple.TupleUtils.function; -import static org.cloudfoundry.util.tuple.TupleUtils.predicate; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -57,7 +56,6 @@ import org.cloudfoundry.client.v2.applications.CreateApplicationResponse; import org.cloudfoundry.client.v2.applications.DockerCredentials; import org.cloudfoundry.client.v2.applications.InstanceStatistics; -import org.cloudfoundry.client.v2.applications.ListApplicationRoutesRequest; import org.cloudfoundry.client.v2.applications.ListApplicationServiceBindingsRequest; import org.cloudfoundry.client.v2.applications.RemoveApplicationRouteRequest; import org.cloudfoundry.client.v2.applications.RemoveApplicationServiceBindingRequest; @@ -113,10 +111,12 @@ import org.cloudfoundry.client.v3.applications.ApplicationResource; import org.cloudfoundry.client.v3.applications.GetApplicationEnvironmentRequest; import org.cloudfoundry.client.v3.applications.GetApplicationEnvironmentResponse; +import org.cloudfoundry.client.v3.applications.GetApplicationProcessStatisticsRequest; +import org.cloudfoundry.client.v3.applications.GetApplicationProcessStatisticsResponse; import org.cloudfoundry.client.v3.applications.GetApplicationResponse; import org.cloudfoundry.client.v3.applications.GetApplicationSshEnabledRequest; import org.cloudfoundry.client.v3.applications.GetApplicationSshEnabledResponse; -import org.cloudfoundry.client.v3.applications.ListApplicationProcessesRequest; +import org.cloudfoundry.client.v3.applications.ListApplicationRoutesRequest; import org.cloudfoundry.client.v3.applications.ListApplicationsRequest; import org.cloudfoundry.client.v3.applications.SetApplicationCurrentDropletRequest; import org.cloudfoundry.client.v3.applications.UpdateApplicationFeatureRequest; @@ -125,8 +125,6 @@ import org.cloudfoundry.client.v3.builds.CreateBuildResponse; import org.cloudfoundry.client.v3.builds.GetBuildRequest; import org.cloudfoundry.client.v3.builds.GetBuildResponse; -import org.cloudfoundry.client.v3.domains.DomainResource; -import org.cloudfoundry.client.v3.domains.ListDomainsRequest; import org.cloudfoundry.client.v3.packages.BitsData; import org.cloudfoundry.client.v3.packages.CreatePackageRequest; import org.cloudfoundry.client.v3.packages.CreatePackageResponse; @@ -137,8 +135,6 @@ import org.cloudfoundry.client.v3.packages.PackageState; import org.cloudfoundry.client.v3.packages.PackageType; import org.cloudfoundry.client.v3.packages.UploadPackageRequest; -import org.cloudfoundry.client.v3.processes.GetProcessStatisticsRequest; -import org.cloudfoundry.client.v3.processes.GetProcessStatisticsResponse; import org.cloudfoundry.client.v3.processes.ProcessState; import org.cloudfoundry.client.v3.processes.ProcessStatisticsResource; import org.cloudfoundry.client.v3.resourcematch.MatchedResource; @@ -210,26 +206,35 @@ public final class DefaultApplications implements Applications { private static final String APP_FEATURE_SSH = "ssh"; - private final Mono cloudFoundryClient; + private final CloudFoundryClient cloudFoundryClient; - private final Mono dopplerClient; + private final DopplerClient dopplerClient; private final RandomWords randomWords; - private final Mono spaceId; + private final String spaceId; + public DefaultApplications( + CloudFoundryClient cloudFoundryClient, DopplerClient dopplerClient, String spaceId) { + this(cloudFoundryClient, dopplerClient, new WordListRandomWords(), spaceId); + } + + /** + * @deprecated Please use {@link DefaultApplications(CloudFoundryClient, DopplerClient, String)} instead. + */ + @Deprecated public DefaultApplications( Mono cloudFoundryClient, Mono dopplerClient, Mono spaceId) { - this(cloudFoundryClient, dopplerClient, new WordListRandomWords(), spaceId); + this(cloudFoundryClient.block(), dopplerClient.block(), spaceId.block()); } DefaultApplications( - Mono cloudFoundryClient, - Mono dopplerClient, + CloudFoundryClient cloudFoundryClient, + DopplerClient dopplerClient, RandomWords randomWords, - Mono spaceId) { + String spaceId) { this.cloudFoundryClient = cloudFoundryClient; this.dopplerClient = dopplerClient; this.randomWords = randomWords; @@ -238,116 +243,58 @@ public DefaultApplications( @Override public Mono copySource(CopySourceApplicationRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationId( - cloudFoundryClient, - request.getName(), - spaceId), - getApplicationIdFromOrgSpace( - cloudFoundryClient, - request.getTargetName(), - spaceId, - request.getTargetOrganization(), - request.getTargetSpace())))) + return Mono.zip( + getApplicationId(request.getName()), + getApplicationIdFromOrgSpace( + request.getTargetName(), + this.spaceId, + request.getTargetOrganization(), + request.getTargetSpace())) .flatMap( function( - (cloudFoundryClient, sourceApplicationId, targetApplicationId) -> + (sourceApplicationId, targetApplicationId) -> copyBits( - cloudFoundryClient, request.getStagingTimeout(), sourceApplicationId, targetApplicationId) - .thenReturn( - Tuples.of( - cloudFoundryClient, - targetApplicationId)))) + .thenReturn(targetApplicationId))) .filter( - predicate( - (cloudFoundryClient, targetApplicationId) -> - Optional.ofNullable(request.getRestart()).orElse(false))) + targetApplicationId -> + Optional.ofNullable(request.getRestart()).orElse(false)) .flatMap( - function( - (cloudFoundryClient, targetApplicationId) -> - restartApplication( - cloudFoundryClient, - request.getTargetName(), - targetApplicationId, - request.getStagingTimeout(), - request.getStartupTimeout()))) + targetApplicationId -> + restartApplication( + request.getTargetName(), + targetApplicationId, + request.getStagingTimeout(), + request.getStartupTimeout())) .transform(OperationsLogging.log("Copy Application Source")) .checkpoint(); } @Override public Mono delete(DeleteApplicationRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) + return getRoutesAndApplicationId( + request, + spaceId, + Optional.ofNullable(request.getDeleteRoutes()).orElse(false)) .flatMap( function( - (cloudFoundryClient, spaceId) -> - getRoutesAndApplicationId( - cloudFoundryClient, - request, - spaceId, - Optional.ofNullable( - request.getDeleteRoutes()) - .orElse(false)) - .map( - function( - (routes, applicationId) -> - Tuples.of( - cloudFoundryClient, - routes, - applicationId))))) - .flatMap( - function( - (cloudFoundryClient, routes, applicationId) -> - deleteRoutes( - cloudFoundryClient, - request.getCompletionTimeout(), - routes) - .thenReturn( - Tuples.of( - cloudFoundryClient, - applicationId)))) - .delayUntil(function(DefaultApplications::removeServiceBindings)) - .flatMap(function(DefaultApplications::requestDeleteApplication)) + (routes, applicationId) -> + deleteRoutes(request.getCompletionTimeout(), routes) + .thenReturn(applicationId))) + .delayUntil(this::removeServiceBindings) + .flatMap(this::requestDeleteApplication) .transform(OperationsLogging.log("Delete Application")) .checkpoint(); } @Override public Mono disableSsh(DisableApplicationSshRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationIdV3( - cloudFoundryClient, - request.getName(), - spaceId)))) - .flatMap( - function( - (cloudFoundryClient, applicationId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - Mono.just(applicationId), - getSshEnabled(cloudFoundryClient, applicationId)))) - .filter( - predicate( - (cloudFoundryClient, applicationId, sshEnabled) -> - sshEnabled.equals(true))) - .flatMap( - function( - (cloudFoundryClient, applicationId) -> - requestUpdateApplicationSsh( - cloudFoundryClient, applicationId, false))) + return getApplicationIdV3(request.getName()) + // TODO dgarnier: is this correct? + .filterWhen(applicationId -> getSshEnabled(applicationId)) + .flatMap(applicationId -> requestUpdateApplicationSsh(applicationId, false)) .then() .transform(OperationsLogging.log("Disable Application SSH")) .checkpoint(); @@ -355,32 +302,9 @@ public Mono disableSsh(DisableApplicationSshRequest request) { @Override public Mono enableSsh(EnableApplicationSshRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationIdV3( - cloudFoundryClient, - request.getName(), - spaceId)))) - .flatMap( - function( - (cloudFoundryClient, applicationId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - Mono.just(applicationId), - getSshEnabled(cloudFoundryClient, applicationId)))) - .filter( - predicate( - (cloudFoundryClient, applicationId, sshEnabled) -> - sshEnabled.equals(false))) - .flatMap( - function( - (cloudFoundryClient, applicationId) -> - requestUpdateApplicationSsh( - cloudFoundryClient, applicationId, true))) + return getApplicationIdV3(request.getName()) + .filterWhen(applicationId -> getSshEnabled(applicationId).map(enabled -> !enabled)) + .flatMap(applicationId -> requestUpdateApplicationSsh(applicationId, true)) .then() .transform(OperationsLogging.log("Enable Application SSH")) .checkpoint(); @@ -388,53 +312,30 @@ public Mono enableSsh(EnableApplicationSshRequest request) { @Override public Mono get(GetApplicationRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplication( - cloudFoundryClient, - request.getName(), - spaceId)))) - .flatMap(function(DefaultApplications::getAuxiliaryContent)) + return getApplication(request.getName()) + .flatMap(app -> getAuxiliaryContent(app)) .map(function(DefaultApplications::toApplicationDetail)) .transform(OperationsLogging.log("Get Application")) .checkpoint(); } + // TODO dgarnier: manifest v3? @Override public Mono getApplicationManifest(GetApplicationManifestRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationId( - cloudFoundryClient, - request.getName(), - spaceId)))) + return getApplicationId(request.getName()) .flatMap( - function( - (cloudFoundryClient, applicationId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - Mono.just(applicationId), - requestApplicationSummary( - cloudFoundryClient, applicationId)))) + applicationId -> + Mono.zip( + Mono.just(applicationId), + requestApplicationSummary(applicationId))) .flatMap( function( - (cloudFoundryClient, applicationId, response) -> + (applicationId, summary) -> Mono.zip( - getApplicationBuildpacks( - cloudFoundryClient, applicationId), - Mono.just(response), - getStackName( - cloudFoundryClient, - response.getStackId())))) - .flatMap(function(DefaultApplications::toApplicationManifest)) + getApplicationBuildpacks(applicationId), + Mono.just(summary), + getStackName(summary.getStackId())))) + .flatMap(function(this::toApplicationManifest)) .transform(OperationsLogging.log("Get Application Manifest")) .checkpoint(); } @@ -442,17 +343,8 @@ public Mono getApplicationManifest(GetApplicationManifestRe @Override public Mono getEnvironments( GetApplicationEnvironmentsRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationIdV3( - cloudFoundryClient, - request.getName(), - spaceId)))) - .flatMap(function(DefaultApplications::requestApplicationEnvironment)) + return getApplicationIdV3(request.getName()) + .flatMap(applicationId -> requestApplicationEnvironment(applicationId)) .map(DefaultApplications::toApplicationEnvironments) .transform(OperationsLogging.log("Get Application Environments")) .checkpoint(); @@ -460,26 +352,13 @@ public Mono getEnvironments( @Override public Flux getEvents(GetApplicationEventsRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationId( - cloudFoundryClient, - request.getName(), - spaceId)))) + return getApplicationId(request.getName()) .flatMapMany( - function( - (cloudFoundryClient, applicationId) -> - requestEvents(applicationId, cloudFoundryClient) - .take( - Optional.ofNullable( - request - .getMaxNumberOfEvents()) - .orElse( - MAX_NUMBER_OF_RECENT_EVENTS)))) + applicationId -> + requestEvents(applicationId) + .take( + Optional.ofNullable(request.getMaxNumberOfEvents()) + .orElse(MAX_NUMBER_OF_RECENT_EVENTS))) .map(DefaultApplications::convertToApplicationEvent) .transform(OperationsLogging.log("Get Application Events")) .checkpoint(); @@ -487,12 +366,7 @@ public Flux getEvents(GetApplicationEventsRequest request) { @Override public Mono getHealthCheck(GetApplicationHealthCheckRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - getApplication( - cloudFoundryClient, request.getName(), spaceId))) + return getApplication(request.getName()) .map(DefaultApplications::toHealthCheck) .transform(OperationsLogging.log("Get Application Health Check")) .checkpoint(); @@ -500,8 +374,7 @@ public Mono getHealthCheck(GetApplicationHealthCheckRequ @Override public Flux list() { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap(function(DefaultApplications::requestSpaceSummary)) + return requestSpaceSummary() .flatMapMany(DefaultApplications::extractApplications) .map(DefaultApplications::toApplicationSummary) .transform(OperationsLogging.log("List Applications")) @@ -510,20 +383,8 @@ public Flux list() { @Override public Flux listTasks(ListApplicationTasksRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationIdV3( - cloudFoundryClient, - request.getName(), - spaceId)))) - .flatMapMany( - function( - (cloudFoundryClient, applicationId) -> - requestListTasks(cloudFoundryClient, applicationId))) + return getApplicationIdV3(request.getName()) + .flatMapMany(applicationId -> requestListTasks(applicationId)) .map(DefaultApplications::toTask) .transform(OperationsLogging.log("List Application Tasks")) .checkpoint(); @@ -531,15 +392,8 @@ public Flux listTasks(ListApplicationTasksRequest request) { @Override public Flux logs(LogsRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - getApplicationId( - cloudFoundryClient, request.getName(), spaceId))) - .flatMapMany( - applicationId -> - getLogs(this.dopplerClient, applicationId, request.getRecent())) + return getApplicationId(request.getName()) + .flatMapMany(applicationId -> getLogs(applicationId, request.getRecent())) .transform(OperationsLogging.log("Get Application Logs")) .checkpoint(); } @@ -610,55 +464,37 @@ public Mono push(PushApplicationRequest request) { .checkpoint(); } + // TODO dgarnier: pass orgId to constructor? @Override public Mono pushManifest(PushApplicationManifestRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getSpaceOrganizationId(cloudFoundryClient, spaceId), - Mono.just(spaceId)))) - .flatMap( - function( - (cloudFoundryClient, organizationId, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - listAvailableDomains( - cloudFoundryClient, organizationId), - Mono.just(spaceId)))) + return getSpaceOrganizationId(this.spaceId) + .flatMap(organizationId -> listAvailableDomains(organizationId)) .flatMapMany( - function( - (cloudFoundryClient, availableDomains, spaceId) -> - Flux.fromIterable(request.getManifests()) - .flatMap( - manifest -> { - if (manifest.getPath() != null) { - return pushApplication( - cloudFoundryClient, - availableDomains, - manifest, - this.randomWords, - request, - spaceId); - } else if (!manifest.getDocker() - .getImage() - .isEmpty()) { - return pushDocker( - cloudFoundryClient, - availableDomains, - manifest, - this.randomWords, - request, - spaceId); - } else { - throw new IllegalStateException( - "One of application or" - + " dockerImage must be" - + " supplied"); - } - }))) + availableDomains -> + Flux.fromIterable(request.getManifests()) + .flatMap( + manifest -> { + if (manifest.getPath() != null) { + return pushApplication( + availableDomains, + manifest, + this.randomWords, + request); + } else if (!manifest.getDocker() + .getImage() + .isEmpty()) { + return pushDocker( + availableDomains, + manifest, + this.randomWords, + request); + } else { + throw new IllegalStateException( + "One of application or" + + " dockerImage must be" + + " supplied"); + } + })) .then() .transform(OperationsLogging.log("Push Manifest")) .checkpoint(); @@ -674,74 +510,32 @@ public Mono pushManifestV3(PushManifestV3Request request) { throw new RuntimeException("Could not serialize manifest", e); } - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - applyManifestAndWaitForCompletion( - cloudFoundryClient, - spaceId, - manifestSerialized) - .then( - Mono.just( - Tuples.of( - cloudFoundryClient, - spaceId))))) - .flatMapMany( - function( - (cloudFoundryClient, spaceId) -> - Flux.fromIterable(request.getManifest().getApplications()) - .map( - manifestApp -> - Tuples.of( - cloudFoundryClient, - spaceId, - manifestApp)))) + return applyManifestAndWaitForCompletion(manifestSerialized) + .flatMapMany(ignored -> Flux.fromIterable(request.getManifest().getApplications())) .flatMap( - function( - (cloudFoundryClient, spaceId, manifestApp) -> - getApplicationIdV3( - cloudFoundryClient, - manifestApp.getName(), - spaceId) - .flatMap( - appId -> - Mono.zip( - Mono.just(appId), - createPackage( - cloudFoundryClient, - appId, - manifestApp))) - .flatMap( - function( - (appId, packageId) -> - buildAndStageAndWaitForRunning( - cloudFoundryClient, - manifestApp, - packageId, - appId))))) + manifestApp -> + getApplicationIdV3(manifestApp.getName()) + .flatMap( + appId -> + Mono.zip( + Mono.just(appId), + createPackage(appId, manifestApp))) + .flatMap( + function( + (appId, packageId) -> + buildAndStageAndWaitForRunning( + manifestApp, + packageId, + appId)))) .then(); } @Override public Mono rename(RenameApplicationRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) + return getApplicationId(request.getName()) .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationId( - cloudFoundryClient, - request.getName(), - spaceId)))) - .flatMap( - function( - (cloudFoundryClient, applicationId) -> - requestUpdateApplicationName( - cloudFoundryClient, - applicationId, - request.getNewName()))) + applicationId -> + requestUpdateApplicationName(applicationId, request.getNewName())) .then() .transform(OperationsLogging.log("Rename Application")) .checkpoint(); @@ -749,101 +543,48 @@ public Mono rename(RenameApplicationRequest request) { @Override public Mono restage(RestageApplicationRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationId( - cloudFoundryClient, - request.getName(), - spaceId)))) + return getApplicationId(request.getName()) .flatMap( - function( - (cloudFoundryClient, applicationId) -> - restageApplication( - cloudFoundryClient, - request.getName(), - applicationId, - request.getStagingTimeout(), - request.getStartupTimeout()))) + applicationId -> + restageApplication( + request.getName(), + applicationId, + request.getStagingTimeout(), + request.getStartupTimeout())) .transform(OperationsLogging.log("Restage Application")) .checkpoint(); } @Override public Mono restart(RestartApplicationRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplication( - cloudFoundryClient, - request.getName(), - spaceId)))) - .flatMap( - function( - (cloudFoundryClient, resource) -> - Mono.zip( - Mono.just(cloudFoundryClient), - stopApplicationIfNotStopped( - cloudFoundryClient, resource)))) + return getApplication(request.getName()) + .flatMap(resource -> stopApplicationIfNotStopped(resource)) .flatMap( - function( - (cloudFoundryClient, stoppedApplication) -> - startApplicationAndWait( - cloudFoundryClient, - request.getName(), - ResourceUtils.getId(stoppedApplication), - request.getStagingTimeout(), - request.getStartupTimeout()))) + stoppedApplication -> + startApplicationAndWait( + request.getName(), + ResourceUtils.getId(stoppedApplication), + request.getStagingTimeout(), + request.getStartupTimeout())) .transform(OperationsLogging.log("Restart Application")) .checkpoint(); } @Override public Mono restartInstance(RestartApplicationInstanceRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationId( - cloudFoundryClient, - request.getName(), - spaceId)))) + return getApplicationId(request.getName()) .flatMap( - function( - (cloudFoundryClient, applicationId) -> - requestTerminateApplicationInstance( - cloudFoundryClient, - applicationId, - String.valueOf(request.getInstanceIndex())))) + applicationId -> + requestTerminateApplicationInstance( + applicationId, String.valueOf(request.getInstanceIndex()))) .transform(OperationsLogging.log("Restart Application Instance")) .checkpoint(); } @Override public Mono runTask(RunApplicationTaskRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationIdV3( - cloudFoundryClient, - request.getApplicationName(), - spaceId)))) - .flatMap( - function( - (cloudFoundryClient, applicationId) -> - requestCreateTask( - cloudFoundryClient, applicationId, request))) + return getApplicationIdV3(request.getApplicationName()) + .flatMap(applicationId -> requestCreateTask(applicationId, request)) .map(DefaultApplications::toTask) .transform(OperationsLogging.log("Run Application Task Instance")) .checkpoint(); @@ -851,67 +592,40 @@ public Mono runTask(RunApplicationTaskRequest request) { @Override public Mono scale(ScaleApplicationRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .filter(predicate((cloudFoundryClient, spaceId) -> areModifiersPresent(request))) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationId( - cloudFoundryClient, - request.getName(), - spaceId)))) + if (!areModifiersPresent(request)) { + return Mono.empty(); + } + return getApplicationId(request.getName()) .flatMap( - function( - (cloudFoundryClient, applicationId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - requestUpdateApplicationScale( - cloudFoundryClient, - applicationId, - request.getDiskLimit(), - request.getInstances(), - request.getMemoryLimit())))) - .filter( - predicate( - (cloudFoundryClient, resource) -> - isRestartRequired(request, resource))) + applicationId -> + requestUpdateApplicationScale( + applicationId, + request.getDiskLimit(), + request.getInstances(), + request.getMemoryLimit())) + .filter(resource -> isRestartRequired(request, resource)) .flatMap( - function( - (cloudFoundryClient, resource) -> - restartApplication( - cloudFoundryClient, - request.getName(), - ResourceUtils.getId(resource), - request.getStagingTimeout(), - request.getStartupTimeout()))) + resource -> + restartApplication( + request.getName(), + ResourceUtils.getId(resource), + request.getStagingTimeout(), + request.getStartupTimeout())) .transform(OperationsLogging.log("Scale Application")) .checkpoint(); } @Override public Mono setEnvironmentVariable(SetEnvironmentVariableApplicationRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) + return getApplication(request.getName()) .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplication( - cloudFoundryClient, - request.getName(), - spaceId)))) - .flatMap( - function( - (cloudFoundryClient, resource) -> - requestUpdateApplicationEnvironment( - cloudFoundryClient, - ResourceUtils.getId(resource), - addToEnvironment( - getEnvironment(resource), - request.getVariableName(), - request.getVariableValue())))) + resource -> + requestUpdateApplicationEnvironment( + ResourceUtils.getId(resource), + addToEnvironment( + getEnvironment(resource), + request.getVariableName(), + request.getVariableValue()))) .then() .transform(OperationsLogging.log("Set Application Environment Variable")) .checkpoint(); @@ -919,23 +633,11 @@ public Mono setEnvironmentVariable(SetEnvironmentVariableApplicationReques @Override public Mono setHealthCheck(SetApplicationHealthCheckRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationId( - cloudFoundryClient, - request.getName(), - spaceId)))) + return getApplicationId(request.getName()) .flatMap( - function( - (cloudFoundryClient, applicationId) -> - requestUpdateApplicationHealthCheckType( - cloudFoundryClient, - applicationId, - request.getType()))) + applicationId -> + requestUpdateApplicationHealthCheckType( + applicationId, request.getType())) .then() .transform(OperationsLogging.log("Set Application Health Check")) .checkpoint(); @@ -943,72 +645,30 @@ public Mono setHealthCheck(SetApplicationHealthCheckRequest request) { @Override public Mono sshEnabled(ApplicationSshEnabledRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationIdV3( - cloudFoundryClient, - request.getName(), - spaceId)))) - .flatMap(function(DefaultApplications::getSshEnabled)) + return getApplicationIdV3(request.getName()) + .flatMap(applicationId -> getSshEnabled(applicationId)) .transform(OperationsLogging.log("Is Application SSH Enabled")) .checkpoint(); } - private static Mono getSshEnabled( - CloudFoundryClient cloudFoundryClient, String applicationId) { - return cloudFoundryClient - .applicationsV3() - .getSshEnabled( - GetApplicationSshEnabledRequest.builder() - .applicationId(applicationId) - .build()) - .map(GetApplicationSshEnabledResponse::getEnabled); - } - @Override public Mono start(StartApplicationRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) + return getApplicationIdWhere(request.getName(), isNotIn(STARTED_STATE)) .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationIdWhere( - cloudFoundryClient, - request.getName(), - spaceId, - isNotIn(STARTED_STATE))))) - .flatMap( - function( - (cloudFoundryClient, applicationId) -> - startApplicationAndWait( - cloudFoundryClient, - request.getName(), - applicationId, - request.getStagingTimeout(), - request.getStartupTimeout()))) + applicationId -> + startApplicationAndWait( + request.getName(), + applicationId, + request.getStagingTimeout(), + request.getStartupTimeout())) .transform(OperationsLogging.log("Start Application")) .checkpoint(); } @Override public Mono stop(StopApplicationRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationIdWhere( - cloudFoundryClient, - request.getName(), - spaceId, - isNotIn(STOPPED_STATE))))) - .flatMap(function(DefaultApplications::stopApplication)) + return getApplicationIdWhere(request.getName(), isNotIn(STOPPED_STATE)) + .flatMap(applicationId -> stopApplication(applicationId)) .then() .transform(OperationsLogging.log("Stop Application")) .checkpoint(); @@ -1016,26 +676,9 @@ public Mono stop(StopApplicationRequest request) { @Override public Mono terminateTask(TerminateApplicationTaskRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplicationIdV3( - cloudFoundryClient, - request.getApplicationName(), - spaceId)))) - .flatMap( - function( - (cloudFoundryClient, applicationId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getTaskId( - cloudFoundryClient, - applicationId, - request.getSequenceId())))) - .flatMap(function(DefaultApplications::requestTerminateTask)) + return getApplicationIdV3(request.getApplicationName()) + .flatMap(applicationId -> getTaskId(applicationId, request.getSequenceId())) + .flatMap(taskId -> requestTerminateTask(taskId)) .then() .transform(OperationsLogging.log("Terminate Application Task Instance")) .checkpoint(); @@ -1043,30 +686,29 @@ public Mono terminateTask(TerminateApplicationTaskRequest request) { @Override public Mono unsetEnvironmentVariable(UnsetEnvironmentVariableApplicationRequest request) { - return Mono.zip(this.cloudFoundryClient, this.spaceId) - .flatMap( - function( - (cloudFoundryClient, spaceId) -> - Mono.zip( - Mono.just(cloudFoundryClient), - getApplication( - cloudFoundryClient, - request.getName(), - spaceId)))) + return getApplication(request.getName()) .flatMap( - function( - (cloudFoundryClient, resource) -> - requestUpdateApplicationEnvironment( - cloudFoundryClient, - ResourceUtils.getId(resource), - removeFromEnvironment( - getEnvironment(resource), - request.getVariableName())))) + resource -> + requestUpdateApplicationEnvironment( + ResourceUtils.getId(resource), + removeFromEnvironment( + getEnvironment(resource), + request.getVariableName()))) .then() .transform(OperationsLogging.log("Unset Application Environment Variable")) .checkpoint(); } + private Mono getSshEnabled(String applicationId) { + return this.cloudFoundryClient + .applicationsV3() + .getSshEnabled( + GetApplicationSshEnabledRequest.builder() + .applicationId(applicationId) + .build()) + .map(GetApplicationSshEnabledResponse::getEnabled); + } + private static Map addToEnvironment( Map environment, String variableName, Object variableValue) { return FluentMap.builder() @@ -1075,9 +717,9 @@ private static Map addToEnvironment( .build(); } - private static Mono applyDropletAndWaitForRunning( - CloudFoundryClient cloudFoundryClient, String appname, String appId, String dropletId) { - return cloudFoundryClient + private Mono applyDropletAndWaitForRunning( + String appname, String appId, String dropletId) { + return this.cloudFoundryClient .applicationsV3() .setCurrentDroplet( SetApplicationCurrentDropletRequest.builder() @@ -1085,24 +727,23 @@ private static Mono applyDropletAndWaitForRunning( .data(Relationship.builder().id(dropletId).build()) .build()) .then( - cloudFoundryClient + this.cloudFoundryClient .applicationsV3() .restart( org.cloudfoundry.client.v3.applications .RestartApplicationRequest.builder() .applicationId(appId) .build())) - .then(waitForRunningV3(cloudFoundryClient, appname, appId, null)); + .then(waitForRunningV3(appname, appId, null)); } - private static Mono applyManifestAndWaitForCompletion( - CloudFoundryClient cloudFoundryClient, String spaceId, byte[] manifestSerialized) { - return cloudFoundryClient + private Mono applyManifestAndWaitForCompletion(byte[] manifestSerialized) { + return this.cloudFoundryClient .spacesV3() .applyManifest( ApplyManifestRequest.builder() .manifest(manifestSerialized) - .spaceId(spaceId) + .spaceId(this.spaceId) .build()) .map( response -> @@ -1115,7 +756,7 @@ private static Mono applyManifestAndWaitForCompletion( .flatMap( jobId -> JobUtils.waitForCompletion( - cloudFoundryClient, Duration.ofMinutes(5), jobId)); + this.cloudFoundryClient, Duration.ofMinutes(5), jobId)); } private static boolean areModifiersPresent(ScaleApplicationRequest request) { @@ -1124,84 +765,54 @@ private static boolean areModifiersPresent(ScaleApplicationRequest request) { || request.getInstances() != null; } - private static Flux associateDefaultDomain( - CloudFoundryClient cloudFoundryClient, + private Flux associateDefaultDomain( String applicationId, List availableDomains, ApplicationManifest manifest, - RandomWords randomWords, - String spaceId) { - return getDefaultDomainId(cloudFoundryClient) + RandomWords randomWords) { + return getDefaultDomainId() .flatMapMany( domainId -> getPushRouteIdFromDomain( - cloudFoundryClient, - availableDomains, - domainId, - manifest, - randomWords, - spaceId)) - .flatMap( - routeId -> - requestAssociateRoute(cloudFoundryClient, applicationId, routeId)) + availableDomains, domainId, manifest, randomWords)) + .flatMap(routeId -> requestAssociateRoute(applicationId, routeId)) .map(ResourceUtils::getId); } - private static Mono bindServices( - CloudFoundryClient cloudFoundryClient, - String applicationId, - ApplicationManifest manifest, - String spaceId) { + private Mono bindServices(String applicationId, ApplicationManifest manifest) { if (manifest.getServices() == null || manifest.getServices().size() == 0) { return Mono.empty(); } return Flux.fromIterable(manifest.getServices()) - .flatMap( - serviceInstanceName -> - getServiceId(cloudFoundryClient, serviceInstanceName, spaceId)) + .flatMap(serviceInstanceName -> getServiceId(serviceInstanceName)) .flatMap( serviceInstanceId -> - requestCreateServiceBinding( - cloudFoundryClient, - applicationId, - serviceInstanceId) + requestCreateServiceBinding(applicationId, serviceInstanceId) .onErrorResume( ExceptionUtils.statusCode(CF_SERVICE_ALREADY_BOUND), t -> Mono.empty())) .then(); } - private static Mono buildAndStageAndWaitForRunning( - CloudFoundryClient cloudFoundryClient, - ManifestV3Application manifestApp, - String packageId, - String appId) { - return buildAndStage(cloudFoundryClient, manifestApp, packageId) + private Mono buildAndStageAndWaitForRunning( + ManifestV3Application manifestApp, String packageId, String appId) { + return buildAndStage(manifestApp, packageId) .flatMap( dropletId -> applyDropletAndWaitForRunning( - cloudFoundryClient, - manifestApp.getName(), - appId, - dropletId)); + manifestApp.getName(), appId, dropletId)); } - private static Mono buildAndStage( - CloudFoundryClient cloudFoundryClient, - ManifestV3Application manifestApp, - String packageId) { - return cloudFoundryClient + private Mono buildAndStage(ManifestV3Application manifestApp, String packageId) { + return this.cloudFoundryClient .builds() .create( CreateBuildRequest.builder() .getPackage(Relationship.builder().id(packageId).build()) .build()) .map(CreateBuildResponse::getId) - .flatMap( - buildId -> - waitForBuildStaging( - cloudFoundryClient, buildId, manifestApp.getName(), null)) + .flatMap(buildId -> waitForBuildStaging(buildId, manifestApp.getName(), null)) .map(build -> build.getDroplet().getId()); } @@ -1209,14 +820,15 @@ private static String cleanName(ApplicationManifest manifest) { return manifest.getName().replaceAll("\\.", ""); } - private static BiFunction collectStates() { + public static BiFunction collectStates() { return (totalState, instanceState) -> { - if ("RUNNING".equals(instanceState) || "RUNNING".equals(totalState)) { - return "RUNNING"; + if (ProcessState.RUNNING.equals(instanceState) + || ProcessState.RUNNING.equals(totalState)) { + return ProcessState.RUNNING; } - if ("FLAPPING".equals(instanceState) || "CRASHED".equals(instanceState)) { - return "FAILED"; + if (ProcessState.CRASHED.equals(instanceState)) { + return ProcessState.CRASHED; } return totalState; @@ -1242,24 +854,18 @@ private static ApplicationEvent convertToApplicationEvent(EventResource resource .build(); } - private static Mono copyBits( - CloudFoundryClient cloudFoundryClient, - Duration completionTimeout, - String sourceApplicationId, - String targetApplicationId) { - return requestCopyBits(cloudFoundryClient, sourceApplicationId, targetApplicationId) + private Mono copyBits( + Duration completionTimeout, String sourceApplicationId, String targetApplicationId) { + return requestCopyBits(sourceApplicationId, targetApplicationId) .flatMap( job -> JobUtils.waitForCompletion( - cloudFoundryClient, completionTimeout, job)); + this.cloudFoundryClient, completionTimeout, job)); } - private static Mono createPackage( - CloudFoundryClient cloudFoundryClient, - String appId, - ManifestV3Application manifestApp) { + private Mono createPackage(String appId, ManifestV3Application manifestApp) { if (manifestApp.getDocker() != null) { - return cloudFoundryClient + return this.cloudFoundryClient .packages() .create( CreatePackageRequest.builder() @@ -1283,7 +889,7 @@ private static Mono createPackage( .build()) .map(CreatePackageResponse::getId); } else { - return cloudFoundryClient + return this.cloudFoundryClient .packages() .create( CreatePackageRequest.builder() @@ -1310,11 +916,10 @@ private static Mono createPackage( .flatMap( packageId -> ResourceMatchingUtilsV3.getMatchedResources( - cloudFoundryClient, manifestApp.getPath()) + this.cloudFoundryClient, manifestApp.getPath()) .flatMap( matchedResources -> uploadPackageBitsAndWait( - cloudFoundryClient, packageId, manifestApp.getPath(), matchedResources, @@ -1323,23 +928,21 @@ private static Mono createPackage( } } - private static Mono deleteRoute( - CloudFoundryClient cloudFoundryClient, String routeId, Duration completionTimeout) { - return requestDeleteRoute(cloudFoundryClient, routeId) + private Mono deleteRoute(String routeId, Duration completionTimeout) { + return requestDeleteRoute(routeId) .flatMap( job -> JobUtils.waitForCompletion( - cloudFoundryClient, completionTimeout, job)); + this.cloudFoundryClient, completionTimeout, job)); } - private static Mono deleteRoutes( - CloudFoundryClient cloudFoundryClient, + private Mono deleteRoutes( Duration completionTimeout, Optional> routes) { return routes.map(Flux::fromIterable) .orElse(Flux.empty()) .map(org.cloudfoundry.client.v2.routes.Route::getId) - .flatMap(routeId -> deleteRoute(cloudFoundryClient, routeId, completionTimeout)) + .flatMap(routeId -> deleteRoute(routeId, completionTimeout)) .then(); } @@ -1394,9 +997,12 @@ private static Flux extractApplications( return Flux.fromIterable(getSpaceSummaryResponse.getApplications()); } - private static Mono getApplication( - CloudFoundryClient cloudFoundryClient, String application, String spaceId) { - return requestApplications(cloudFoundryClient, application, spaceId) + private Mono getApplication(String application) { + return getApplication(application, spaceId); + } + + private Mono getApplication(String application, String spaceId) { + return requestApplications(application, spaceId) .single() .onErrorResume( NoSuchElementException.class, @@ -1405,9 +1011,8 @@ private static Mono getApplication( "Application %s does not exist", application)); } - private static Mono> getApplicationBuildpacks( - CloudFoundryClient cloudFoundryClient, String applicationId) { - return cloudFoundryClient + private Mono> getApplicationBuildpacks(String applicationId) { + return this.cloudFoundryClient .applicationsV3() .get( org.cloudfoundry.client.v3.applications.GetApplicationRequest.builder() @@ -1421,17 +1026,16 @@ private static Mono> getApplicationBuildpacks( .defaultIfEmpty(Collections.emptyList()); } - private static Mono getApplicationId( - CloudFoundryClient cloudFoundryClient, String application, String spaceId) { - return getApplication(cloudFoundryClient, application, spaceId).map(ResourceUtils::getId); + private Mono getApplicationId(String application) { + return getApplicationId(application, spaceId); } - private static Mono getApplicationId( - CloudFoundryClient cloudFoundryClient, - ApplicationManifest manifest, - String spaceId, - String stackId) { - return requestApplications(cloudFoundryClient, manifest.getName(), spaceId) + private Mono getApplicationId(String application, String spaceId) { + return getApplication(application, spaceId).map(ResourceUtils::getId); + } + + private Mono getApplicationId(ApplicationManifest manifest, String stackId) { + return requestApplications(manifest.getName()) .singleOrEmpty() .flatMap( application -> { @@ -1444,7 +1048,6 @@ private static Mono getApplicationId( .ifPresent(merge::putAll); return requestUpdateApplication( - cloudFoundryClient, ResourceUtils.getId(application), merge, manifest, @@ -1452,49 +1055,36 @@ private static Mono getApplicationId( .map(ResourceUtils::getId); }) .switchIfEmpty( - requestCreateApplication(cloudFoundryClient, manifest, spaceId, stackId) - .map(ResourceUtils::getId)); + requestCreateApplication(manifest, stackId).map(ResourceUtils::getId)); } - private static Mono getApplicationIdFromOrgSpace( - CloudFoundryClient cloudFoundryClient, - String application, - String spaceId, - String organization, - String space) { - return getSpaceOrganizationId(cloudFoundryClient, spaceId) + private Mono getApplicationIdFromOrgSpace( + String application, String spaceId, String organization, String space) { + return getSpaceOrganizationId(spaceId) .flatMap( organizationId -> organization != null - ? getOrganizationId(cloudFoundryClient, organization) + ? getOrganizationId(organization) : Mono.just(organizationId)) .flatMap( organizationId -> space != null - ? getSpaceId(cloudFoundryClient, organizationId, space) + ? getSpaceId(organizationId, space) : Mono.just(spaceId)) - .flatMap(spaceId1 -> getApplicationId(cloudFoundryClient, application, spaceId1)); + .flatMap(spaceId1 -> getApplicationId(application, spaceId1)); } - private static Mono getApplicationIdV3( - CloudFoundryClient cloudFoundryClient, String applicationName, String spaceId) { - return getApplicationV3(cloudFoundryClient, applicationName, spaceId) - .map(ApplicationResource::getId); + private Mono getApplicationIdV3(String applicationName) { + return getApplicationV3(applicationName).map(ApplicationResource::getId); } - private static Mono getApplicationIdWhere( - CloudFoundryClient cloudFoundryClient, - String application, - String spaceId, - Predicate predicate) { - return getApplication(cloudFoundryClient, application, spaceId) - .filter(predicate) - .map(ResourceUtils::getId); + private Mono getApplicationIdWhere( + String application, Predicate predicate) { + return getApplication(application).filter(predicate).map(ResourceUtils::getId); } - private static Mono getApplicationInstances( - CloudFoundryClient cloudFoundryClient, String applicationId) { - return requestApplicationInstances(cloudFoundryClient, applicationId) + private Mono getApplicationInstances(String applicationId) { + return requestApplicationInstances(applicationId) .onErrorResume( ExceptionUtils.statusCode( CF_BUILDPACK_COMPILED_FAILED, @@ -1506,22 +1096,20 @@ private static Mono getApplicationInstances( t -> Mono.just(ApplicationInstancesResponse.builder().build())); } - private static Mono> getApplicationRoutes( - CloudFoundryClient cloudFoundryClient, String applicationId) { - return requestApplicationRoutes(cloudFoundryClient, applicationId).collectList(); + private Mono> getApplicationRoutes( + String applicationId) { + return requestApplicationRoutes(applicationId).collectList(); } - private static Mono getApplicationStatistics( - CloudFoundryClient cloudFoundryClient, String applicationId) { - return requestApplicationStatistics(cloudFoundryClient, applicationId) + private Mono getApplicationStatistics(String applicationId) { + return requestApplicationStatistics(applicationId) .onErrorResume( ExceptionUtils.statusCode(CF_APP_STOPPED_STATS_ERROR), t -> Mono.just(ApplicationStatisticsResponse.builder().build())); } - private static Mono getApplicationV3( - CloudFoundryClient cloudFoundryClient, String application, String spaceId) { - return requestApplicationsV3(cloudFoundryClient, application, spaceId) + private Mono getApplicationV3(String application) { + return requestApplicationsV3(application) .single() .onErrorResume( NoSuchElementException.class, @@ -1530,41 +1118,38 @@ private static Mono getApplicationV3( "Application %s does not exist", application)); } - private static Mono< + private Mono< Tuple5< List, SummaryApplicationResponse, GetStackResponse, List, List>> - getAuxiliaryContent( - CloudFoundryClient cloudFoundryClient, - AbstractApplicationResource applicationResource) { + getAuxiliaryContent(AbstractApplicationResource applicationResource) { String applicationId = ResourceUtils.getId(applicationResource); String stackId = ResourceUtils.getEntity(applicationResource).getStackId(); return Mono.zip( - getApplicationStatistics(cloudFoundryClient, applicationId), - requestApplicationSummary(cloudFoundryClient, applicationId), - getApplicationInstances(cloudFoundryClient, applicationId)) + getApplicationStatistics(applicationId), + requestApplicationSummary(applicationId), + getApplicationInstances(applicationId)) .flatMap( function( (applicationStatisticsResponse, summaryApplicationResponse, applicationInstancesResponse) -> Mono.zip( - getApplicationBuildpacks( - cloudFoundryClient, applicationId), + getApplicationBuildpacks(applicationId), Mono.just(summaryApplicationResponse), - requestStack(cloudFoundryClient, stackId), + requestStack(stackId), toInstanceDetailList( applicationInstancesResponse, applicationStatisticsResponse), toUrls(summaryApplicationResponse.getRoutes())))); } - private static Mono getDefaultDomainId(CloudFoundryClient cloudFoundryClient) { - return requestSharedDomains(cloudFoundryClient) + private Mono getDefaultDomainId() { + return requestSharedDomains() .filter( resource -> !Optional.ofNullable( @@ -1600,16 +1185,15 @@ private static int getInstances(AbstractApplicationResource resource) { .orElse(0); } - private static Flux getLogs( - Mono dopplerClient, String applicationId, Boolean recent) { + private Flux getLogs(String applicationId, Boolean recent) { if (Optional.ofNullable(recent).orElse(false)) { - return requestLogsRecent(dopplerClient, applicationId) + return requestLogsRecent(applicationId) .filter(e -> EventType.LOG_MESSAGE == e.getEventType()) .map(Envelope::getLogMessage) .collectSortedList(LOG_MESSAGE_COMPARATOR) .flatMapIterable(d -> d); } else { - return requestLogsStream(dopplerClient, applicationId) + return requestLogsStream(applicationId) .filter(e -> EventType.LOG_MESSAGE == e.getEventType()) .map(Envelope::getLogMessage) .transformDeferred( @@ -1634,25 +1218,23 @@ private static Map getMetadataRequest(EventEntity entity) { } } - private static Mono>> getOptionalRoutes( - CloudFoundryClient cloudFoundryClient, boolean deleteRoutes, String applicationId) { + private Mono>> getOptionalRoutes( + boolean deleteRoutes, String applicationId) { if (deleteRoutes) { - return getRoutes(cloudFoundryClient, applicationId).map(Optional::of); + return getRoutes(applicationId).map(Optional::of); } else { return Mono.just(Optional.empty()); } } - private static Mono> getOptionalStackId( - CloudFoundryClient cloudFoundryClient, String stack) { + private Mono> getOptionalStackId(String stack) { return Optional.ofNullable(stack) - .map(stack1 -> getStackId(cloudFoundryClient, stack1).map(Optional::of)) + .map(stack1 -> getStackId(stack1).map(Optional::of)) .orElse(Mono.just(Optional.empty())); } - private static Mono getOrganization( - CloudFoundryClient cloudFoundryClient, String organization) { - return requestOrganizations(cloudFoundryClient, organization) + private Mono getOrganization(String organization) { + return requestOrganizations(organization) .single() .onErrorResume( NoSuchElementException.class, @@ -1661,14 +1243,12 @@ private static Mono getOrganization( "Organization %s not found", organization)); } - private static Mono getOrganizationId( - CloudFoundryClient cloudFoundryClient, String organization) { - return getOrganization(cloudFoundryClient, organization).map(ResourceUtils::getId); + private Mono getOrganizationId(String organization) { + return getOrganization(organization).map(ResourceUtils::getId); } - private static Mono getOrganizationSpaceByName( - CloudFoundryClient cloudFoundryClient, String organizationId, String space) { - return requestOrganizationSpacesByName(cloudFoundryClient, organizationId, space) + private Mono getOrganizationSpaceByName(String organizationId, String space) { + return requestOrganizationSpacesByName(organizationId, space) .single() .onErrorResume( NoSuchElementException.class, @@ -1681,17 +1261,13 @@ private static String getPassword(DockerCredentials dockerCredentials) { .orElse(null); } - private static Flux getPushRouteIdFromDomain( - CloudFoundryClient cloudFoundryClient, + private Flux getPushRouteIdFromDomain( List availableDomains, String domainId, ApplicationManifest manifest, - RandomWords randomWords, - String spaceId) { + RandomWords randomWords) { if (isTcpDomain(availableDomains, domainId)) { - return requestCreateTcpRoute(cloudFoundryClient, domainId, spaceId) - .map(ResourceUtils::getId) - .flux(); + return requestCreateTcpRoute(domainId).map(ResourceUtils::getId).flux(); } List hosts; @@ -1715,27 +1291,19 @@ private static Flux getPushRouteIdFromDomain( return Flux.fromIterable(hosts) .flatMap( host -> - getRouteId( - cloudFoundryClient, - domainId, - host, - manifest.getRoutePath()) + getRouteId(domainId, host, manifest.getRoutePath()) .switchIfEmpty( requestCreateRoute( - cloudFoundryClient, domainId, host, - manifest.getRoutePath(), - spaceId) + manifest.getRoutePath()) .map(ResourceUtils::getId))); } - private static Flux getPushRouteIdFromRoute( - CloudFoundryClient cloudFoundryClient, + private Flux getPushRouteIdFromRoute( List availableDomains, ApplicationManifest manifest, - RandomWords randomWords, - String spaceId) { + RandomWords randomWords) { return Flux.fromIterable(manifest.getRoutes()) .flatMap( route -> @@ -1748,27 +1316,16 @@ private static Flux getPushRouteIdFromRoute( String domainId = getDomainId(availableDomains, decomposedRoute.getDomain()); if (isTcpDomain(availableDomains, domainId)) { - return getRouteIdForTcpRoute( - cloudFoundryClient, - decomposedRoute, - domainId, - manifest, - spaceId); + return getRouteIdForTcpRoute(decomposedRoute, domainId, manifest); } else { return getRouteIdForHttpRoute( - cloudFoundryClient, - decomposedRoute, - domainId, - manifest, - randomWords, - spaceId); + decomposedRoute, domainId, manifest, randomWords); } }); } - private static Mono getRouteId( - CloudFoundryClient cloudFoundryClient, String domainId, String host, String routePath) { - return requestRoutes(cloudFoundryClient, domainId, host, null, routePath) + private Mono getRouteId(String domainId, String host, String routePath) { + return requestRoutes(domainId, host, null, routePath) .filter(resource -> isIdentical(host, ResourceUtils.getEntity(resource).getHost())) .filter( resource -> @@ -1779,68 +1336,46 @@ private static Mono getRouteId( .map(ResourceUtils::getId); } - private static Mono getRouteIdForHttpRoute( - CloudFoundryClient cloudFoundryClient, + private Mono getRouteIdForHttpRoute( DecomposedRoute decomposedRoute, String domainId, ApplicationManifest manifest, - RandomWords randomWords, - String spaceId) { + RandomWords randomWords) { String derivedHost = deriveHostname(decomposedRoute.getHost(), manifest, randomWords); - return getRouteId(cloudFoundryClient, domainId, derivedHost, decomposedRoute.getPath()) + return getRouteId(domainId, derivedHost, decomposedRoute.getPath()) .switchIfEmpty( - requestCreateRoute( - cloudFoundryClient, - domainId, - derivedHost, - decomposedRoute.getPath(), - spaceId) + requestCreateRoute(domainId, derivedHost, decomposedRoute.getPath()) .map(ResourceUtils::getId)); } - private static Mono getRouteIdForTcpRoute( - CloudFoundryClient cloudFoundryClient, - DecomposedRoute decomposedRoute, - String domainId, - ApplicationManifest manifest, - String spaceId) { + private Mono getRouteIdForTcpRoute( + DecomposedRoute decomposedRoute, String domainId, ApplicationManifest manifest) { if (Optional.ofNullable(manifest.getRandomRoute()).orElse(false)) { - return requestCreateTcpRoute(cloudFoundryClient, domainId, spaceId) - .map(ResourceUtils::getId); + return requestCreateTcpRoute(domainId).map(ResourceUtils::getId); } - return getTcpRouteId(cloudFoundryClient, domainId, decomposedRoute.getPort()) + return getTcpRouteId(domainId, decomposedRoute.getPort()) .switchIfEmpty( - requestCreateTcpRoute( - cloudFoundryClient, - domainId, - decomposedRoute.getPort(), - spaceId) + requestCreateTcpRoute(domainId, decomposedRoute.getPort()) .map(ResourceUtils::getId)); } - private static Mono> getRoutes( - CloudFoundryClient cloudFoundryClient, String applicationId) { - return requestApplicationSummary(cloudFoundryClient, applicationId) - .map(SummaryApplicationResponse::getRoutes); + private Mono> getRoutes(String applicationId) { + return requestApplicationSummary(applicationId).map(SummaryApplicationResponse::getRoutes); } - private static Mono>, String>> + private Mono>, String>> getRoutesAndApplicationId( - CloudFoundryClient cloudFoundryClient, - DeleteApplicationRequest request, - String spaceId, - boolean deleteRoutes) { - return getApplicationId(cloudFoundryClient, request.getName(), spaceId) + DeleteApplicationRequest request, String spaceId, boolean deleteRoutes) { + return getApplicationId(request.getName()) .flatMap( applicationId -> - getOptionalRoutes(cloudFoundryClient, deleteRoutes, applicationId) + getOptionalRoutes(deleteRoutes, applicationId) .zipWith(Mono.just(applicationId))); } - private static Mono getServiceId( - CloudFoundryClient cloudFoundryClient, String serviceInstanceName, String spaceId) { - return requestListServiceInstances(cloudFoundryClient, serviceInstanceName, spaceId) + private Mono getServiceId(String serviceInstanceName) { + return requestListServiceInstances(serviceInstanceName) .map(ResourceUtils::getId) .single() .onErrorResume( @@ -1851,20 +1386,17 @@ private static Mono getServiceId( serviceInstanceName)); } - private static Mono getSpaceId( - CloudFoundryClient cloudFoundryClient, String organizationId, String space) { - return getOrganizationSpaceByName(cloudFoundryClient, organizationId, space) - .map(ResourceUtils::getId); + private Mono getSpaceId(String organizationId, String space) { + return getOrganizationSpaceByName(organizationId, space).map(ResourceUtils::getId); } - private static Mono getSpaceOrganizationId( - CloudFoundryClient cloudFoundryClient, String spaceId) { - return requestSpace(cloudFoundryClient, spaceId) + private Mono getSpaceOrganizationId(String spaceId) { + return requestSpace(spaceId) .map(response -> ResourceUtils.getEntity(response).getOrganizationId()); } - private static Mono getStackId(CloudFoundryClient cloudFoundryClient, String stack) { - return requestStacks(cloudFoundryClient, stack) + private Mono getStackId(String stack) { + return requestStacks(stack) .map(ResourceUtils::getId) .single() .onErrorResume( @@ -1872,15 +1404,13 @@ private static Mono getStackId(CloudFoundryClient cloudFoundryClient, St t -> ExceptionUtils.illegalArgument("Stack %s does not exist", stack)); } - private static Mono getStackName( - CloudFoundryClient cloudFoundryClient, String stackId) { - return requestStack(cloudFoundryClient, stackId) + private Mono getStackName(String stackId) { + return requestStack(stackId) .map(getStackResponse -> getStackResponse.getEntity().getName()); } - private static Mono getTaskId( - CloudFoundryClient cloudFoundryClient, String applicationId, Integer sequenceId) { - return listTasks(cloudFoundryClient, applicationId, sequenceId) + private Mono getTaskId(String applicationId, Integer sequenceId) { + return listTasks(applicationId, sequenceId) .single() .map(Resource::getId) .onErrorResume( @@ -1890,11 +1420,8 @@ private static Mono getTaskId( "Task with sequence id of %s does not exist", sequenceId)); } - private static Mono getTcpRouteId( - CloudFoundryClient cloudFoundryClient, String domainId, Integer port) { - return requestRoutes(cloudFoundryClient, domainId, null, port, null) - .singleOrEmpty() - .map(ResourceUtils::getId); + private Mono getTcpRouteId(String domainId, Integer port) { + return requestRoutes(domainId, null, port, null).singleOrEmpty().map(ResourceUtils::getId); } private static String getUsername(DockerCredentials dockerCredentials) { @@ -1907,8 +1434,8 @@ private static boolean isIdentical(String s, String t) { return Objects.equals(s, t); } - private static Predicate isInstanceComplete() { - return state -> "RUNNING".equals(state) || "FAILED".equals(state); + private static Predicate isInstanceComplete() { + return state -> ProcessState.RUNNING.equals(state) || ProcessState.CRASHED.equals(state); } private static Predicate isNotIn(String expectedState) { @@ -1925,8 +1452,8 @@ private static boolean isRestartRequired( && STARTED_STATE.equals(ResourceUtils.getEntity(applicationResource).getState()); } - private static Predicate isRunning() { - return "RUNNING"::equals; + private static Predicate isRunning() { + return ProcessState.RUNNING::equals; } private static Predicate isStaged() { @@ -1947,37 +1474,29 @@ private static boolean isTcpDomain(List availableDomains, String return tcpDomainIds.contains(domainId); } - private static Mono> listAvailableDomains( - CloudFoundryClient cloudFoundryClient, String organizationId) { - return requestListPrivateDomains(cloudFoundryClient, organizationId) + private Mono> listAvailableDomains(String organizationId) { + return requestListPrivateDomains(organizationId) .map(DefaultApplications::toDomain) - .mergeWith( - requestListSharedDomains(cloudFoundryClient) - .map(DefaultApplications::toDomain)) + .mergeWith(requestListSharedDomains().map(DefaultApplications::toDomain)) .collectList(); } - private static Flux listTasks( - CloudFoundryClient cloudFoundryClient, String applicationId, Integer sequenceId) { - return requestListTasks(cloudFoundryClient, applicationId, sequenceId) + private Flux listTasks( + String applicationId, Integer sequenceId) { + return requestListTasks(applicationId, sequenceId) .cast(org.cloudfoundry.client.v3.tasks.Task.class); } - private static Mono prepareDomainsAndRoutes( - CloudFoundryClient cloudFoundryClient, + private Mono prepareDomainsAndRoutes( String applicationId, List availableDomains, ApplicationManifest manifest, - List existingRoutes, - RandomWords randomWords, - String spaceId) { + List existingRoutes, + RandomWords randomWords) { if (Optional.ofNullable(manifest.getNoRoute()).orElse(false)) { return Flux.fromIterable(existingRoutes) - .map(ResourceUtils::getId) - .flatMap( - routeId -> - requestRemoveRouteFromApplication( - cloudFoundryClient, applicationId, routeId)) + .map(org.cloudfoundry.client.v3.routes.RouteResource::getId) + .flatMap(routeId -> requestRemoveRouteFromApplication(applicationId, routeId)) .then(); } @@ -1985,12 +1504,7 @@ private static Mono prepareDomainsAndRoutes( if (manifest.getDomains() == null) { if (existingRoutes.isEmpty()) { return associateDefaultDomain( - cloudFoundryClient, - applicationId, - availableDomains, - manifest, - randomWords, - spaceId) + applicationId, availableDomains, manifest, randomWords) .then(); } return Mono.empty(); // A route already exists for the application, do nothing @@ -1999,68 +1513,52 @@ private static Mono prepareDomainsAndRoutes( .flatMap( domain -> getPushRouteIdFromDomain( - cloudFoundryClient, availableDomains, getDomainId(availableDomains, domain), manifest, - randomWords, - spaceId) + randomWords) .flatMap( routeId -> requestAssociateRoute( - cloudFoundryClient, - applicationId, - routeId))) + applicationId, routeId))) .then(); } List existingRouteIds = - existingRoutes.stream().map(ResourceUtils::getId).collect(Collectors.toList()); + existingRoutes.stream() + .map(org.cloudfoundry.client.v3.routes.RouteResource::getId) + .collect(Collectors.toList()); - return getPushRouteIdFromRoute( - cloudFoundryClient, availableDomains, manifest, randomWords, spaceId) + return getPushRouteIdFromRoute(availableDomains, manifest, randomWords) .filter(routeId -> !existingRouteIds.contains(routeId)) - .flatMapSequential( - routeId -> - requestAssociateRoute(cloudFoundryClient, applicationId, routeId), - 1) + .flatMapSequential(routeId -> requestAssociateRoute(applicationId, routeId), 1) .then(); } - private static Flux pushApplication( - CloudFoundryClient cloudFoundryClient, + private Flux pushApplication( List availableDomains, ApplicationManifest manifest, RandomWords randomWords, - PushApplicationManifestRequest request, - String spaceId) { + PushApplicationManifestRequest request) { - return getOptionalStackId(cloudFoundryClient, manifest.getStack()) - .flatMapMany( - stackId -> - getApplicationId( - cloudFoundryClient, - manifest, - spaceId, - stackId.orElse(null))) + return getOptionalStackId(manifest.getStack()) + .flatMapMany(stackId -> getApplicationId(manifest, stackId.orElse(null))) .flatMap( applicationId -> Mono.zip( Mono.just(applicationId), - getApplicationRoutes(cloudFoundryClient, applicationId), + getApplicationRoutes(applicationId), ResourceMatchingUtils.getMatchedResources( - cloudFoundryClient, manifest.getPath()))) + this.cloudFoundryClient, manifest.getPath()))) .flatMap( function( (applicationId, existingRoutes, matchedResources) -> prepareDomainsAndRoutes( - cloudFoundryClient, applicationId, availableDomains, manifest, existingRoutes, - randomWords, - spaceId) + randomWords) .thenReturn( Tuples.of( applicationId, matchedResources)))) @@ -2068,17 +1566,9 @@ private static Flux pushApplication( function( (applicationId, matchedResources) -> Mono.when( - bindServices( - cloudFoundryClient, - applicationId, - manifest, - spaceId), - updateBuildpacks( - cloudFoundryClient, - applicationId, - manifest), + bindServices(applicationId, manifest), + updateBuildpacks(applicationId, manifest), uploadApplicationAndWait( - cloudFoundryClient, applicationId, manifest.getPath(), matchedResources, @@ -2087,55 +1577,37 @@ private static Flux pushApplication( .flatMap( applicationId -> stopAndStartApplication( - cloudFoundryClient, - applicationId, - manifest.getName(), - request)); + applicationId, manifest.getName(), request)); } - private static Flux pushDocker( - CloudFoundryClient cloudFoundryClient, + private Flux pushDocker( List availableDomains, ApplicationManifest manifest, RandomWords randomWords, - PushApplicationManifestRequest request, - String spaceId) { + PushApplicationManifestRequest request) { - return getOptionalStackId(cloudFoundryClient, manifest.getStack()) - .flatMapMany( - stackId -> - getApplicationId( - cloudFoundryClient, - manifest, - spaceId, - stackId.orElse(null))) + return getOptionalStackId(manifest.getStack()) + .flatMapMany(stackId -> getApplicationId(manifest, stackId.orElse(null))) .flatMap( applicationId -> Mono.zip( Mono.just(applicationId), - getApplicationRoutes(cloudFoundryClient, applicationId))) + getApplicationRoutes(applicationId))) .flatMap( function( (applicationId, existingRoutes) -> prepareDomainsAndRoutes( - cloudFoundryClient, applicationId, availableDomains, manifest, existingRoutes, - randomWords, - spaceId) + randomWords) .thenReturn(applicationId))) - .delayUntil( - applicationId -> - bindServices(cloudFoundryClient, applicationId, manifest, spaceId)) + .delayUntil(applicationId -> bindServices(applicationId, manifest)) .flatMap( applicationId -> stopAndStartApplication( - cloudFoundryClient, - applicationId, - manifest.getName(), - request)); + applicationId, manifest.getName(), request)); } private static Map removeFromEnvironment( @@ -2145,20 +1617,18 @@ private static Map removeFromEnvironment( return modified; } - private static Mono removeServiceBindings( - CloudFoundryClient cloudFoundryClient, String applicationId) { - return requestListServiceBindings(cloudFoundryClient, applicationId) + private Mono removeServiceBindings(String applicationId) { + return requestListServiceBindings(applicationId) .map(ResourceUtils::getId) .flatMap( serviceBindingId -> - requestRemoveServiceBinding( - cloudFoundryClient, applicationId, serviceBindingId)) + requestRemoveServiceBinding(applicationId, serviceBindingId)) .then(); } - private static Mono requestApplicationEnvironment( - CloudFoundryClient cloudFoundryClient, String applicationId) { - return cloudFoundryClient + private Mono requestApplicationEnvironment( + String applicationId) { + return this.cloudFoundryClient .applicationsV3() .getEnvironment( GetApplicationEnvironmentRequest.builder() @@ -2166,20 +1636,30 @@ private static Mono requestApplicationEnviron .build()); } - private static Mono requestApplicationInstances( - CloudFoundryClient cloudFoundryClient, String applicationId) { - return cloudFoundryClient + private Mono requestApplicationInstances(String applicationId) { + return this.cloudFoundryClient .applicationsV2() .instances( ApplicationInstancesRequest.builder().applicationId(applicationId).build()); } - private static Flux requestApplicationRoutes( - CloudFoundryClient cloudFoundryClient, String applicationId) { - return PaginationUtils.requestClientV2Resources( + private Mono requestApplicationStatisticsV3( + String applicationId) { + return this.cloudFoundryClient + .applicationsV3() + .getProcessStatistics( + GetApplicationProcessStatisticsRequest.builder() + .applicationId(applicationId) + .type("web") + .build()); + } + + private Flux requestApplicationRoutes( + String applicationId) { + return PaginationUtils.requestClientV3Resources( page -> - cloudFoundryClient - .applicationsV2() + this.cloudFoundryClient + .applicationsV3() .listRoutes( ListApplicationRoutesRequest.builder() .applicationId(applicationId) @@ -2187,9 +1667,8 @@ private static Flux requestApplicationRoutes( .build())); } - private static Mono requestApplicationStatistics( - CloudFoundryClient cloudFoundryClient, String applicationId) { - return cloudFoundryClient + private Mono requestApplicationStatistics(String applicationId) { + return this.cloudFoundryClient .applicationsV2() .statistics( ApplicationStatisticsRequest.builder() @@ -2197,18 +1676,21 @@ private static Mono requestApplicationStatistics( .build()); } - private static Mono requestApplicationSummary( - CloudFoundryClient cloudFoundryClient, String applicationId) { - return cloudFoundryClient + private Mono requestApplicationSummary(String applicationId) { + return this.cloudFoundryClient .applicationsV2() .summary(SummaryApplicationRequest.builder().applicationId(applicationId).build()); } - private static Flux requestApplications( - CloudFoundryClient cloudFoundryClient, String application, String spaceId) { + private Flux requestApplications(String application) { + return requestApplications(application, spaceId); + } + + private Flux requestApplications( + String application, String spaceId) { return PaginationUtils.requestClientV2Resources( page -> - cloudFoundryClient + this.cloudFoundryClient .spaces() .listApplications( ListSpaceApplicationsRequest.builder() @@ -2219,23 +1701,22 @@ private static Flux requestApplications( .cast(AbstractApplicationResource.class); } - private static Flux requestApplicationsV3( - CloudFoundryClient cloudFoundryClient, String application, String spaceId) { + private Flux requestApplicationsV3(String application) { return PaginationUtils.requestClientV3Resources( page -> - cloudFoundryClient + this.cloudFoundryClient .applicationsV3() .list( ListApplicationsRequest.builder() .name(application) - .spaceId(spaceId) + .spaceId(this.spaceId) .page(page) .build())); } - private static Mono requestAssociateRoute( - CloudFoundryClient cloudFoundryClient, String applicationId, String routeId) { - return cloudFoundryClient + private Mono requestAssociateRoute( + String applicationId, String routeId) { + return this.cloudFoundryClient .applicationsV2() .associateRoute( AssociateApplicationRouteRequest.builder() @@ -2244,11 +1725,9 @@ private static Mono requestAssociateRoute( .build()); } - private static Mono requestCopyBits( - CloudFoundryClient cloudFoundryClient, - String sourceApplicationId, - String targetApplicationId) { - return cloudFoundryClient + private Mono requestCopyBits( + String sourceApplicationId, String targetApplicationId) { + return this.cloudFoundryClient .applicationsV2() .copy( CopyApplicationRequest.builder() @@ -2257,11 +1736,8 @@ private static Mono requestCopyBits( .build()); } - private static Mono requestCreateApplication( - CloudFoundryClient cloudFoundryClient, - ApplicationManifest manifest, - String spaceId, - String stackId) { + private Mono requestCreateApplication( + ApplicationManifest manifest, String stackId) { CreateApplicationRequest.Builder builder = CreateApplicationRequest.builder() .command(manifest.getCommand()) @@ -2276,7 +1752,7 @@ private static Mono requestCreateApplication( .instances(manifest.getInstances()) .memory(manifest.getMemory()) .name(manifest.getName()) - .spaceId(spaceId) + .spaceId(this.spaceId) .stackId(stackId); if (manifest.getBuildpacks() != null && manifest.getBuildpacks().size() == 1) { @@ -2299,29 +1775,25 @@ private static Mono requestCreateApplication( }); } - return cloudFoundryClient.applicationsV2().create(builder.build()); + return this.cloudFoundryClient.applicationsV2().create(builder.build()); } - private static Mono requestCreateRoute( - CloudFoundryClient cloudFoundryClient, - String domainId, - String host, - String routePath, - String spaceId) { - return cloudFoundryClient + private Mono requestCreateRoute( + String domainId, String host, String routePath) { + return this.cloudFoundryClient .routes() .create( org.cloudfoundry.client.v2.routes.CreateRouteRequest.builder() .domainId(domainId) .host(host) .path(routePath) - .spaceId(spaceId) + .spaceId(this.spaceId) .build()); } - private static Mono requestCreateServiceBinding( - CloudFoundryClient cloudFoundryClient, String applicationId, String serviceInstanceId) { - return cloudFoundryClient + private Mono requestCreateServiceBinding( + String applicationId, String serviceInstanceId) { + return this.cloudFoundryClient .serviceBindingsV2() .create( CreateServiceBindingRequest.builder() @@ -2330,11 +1802,9 @@ private static Mono requestCreateServiceBinding( .build()); } - private static Mono requestCreateTask( - CloudFoundryClient cloudFoundryClient, - String applicationId, - RunApplicationTaskRequest request) { - return cloudFoundryClient + private Mono requestCreateTask( + String applicationId, RunApplicationTaskRequest request) { + return this.cloudFoundryClient .tasks() .create( CreateTaskRequest.builder() @@ -2346,33 +1816,30 @@ private static Mono requestCreateTask( .build()); } - private static Mono requestCreateTcpRoute( - CloudFoundryClient cloudFoundryClient, String domainId, String spaceId) { - return cloudFoundryClient + private Mono requestCreateTcpRoute(String domainId) { + return this.cloudFoundryClient .routes() .create( org.cloudfoundry.client.v2.routes.CreateRouteRequest.builder() .domainId(domainId) .generatePort(true) - .spaceId(spaceId) + .spaceId(this.spaceId) .build()); } - private static Mono requestCreateTcpRoute( - CloudFoundryClient cloudFoundryClient, String domainId, Integer port, String spaceId) { - return cloudFoundryClient + private Mono requestCreateTcpRoute(String domainId, Integer port) { + return this.cloudFoundryClient .routes() .create( org.cloudfoundry.client.v2.routes.CreateRouteRequest.builder() .domainId(domainId) .port(port) - .spaceId(spaceId) + .spaceId(this.spaceId) .build()); } - private static Mono requestDeleteApplication( - CloudFoundryClient cloudFoundryClient, String applicationId) { - return cloudFoundryClient + private Mono requestDeleteApplication(String applicationId) { + return this.cloudFoundryClient .applicationsV2() .delete( org.cloudfoundry.client.v2.applications.DeleteApplicationRequest.builder() @@ -2380,18 +1847,16 @@ private static Mono requestDeleteApplication( .build()); } - private static Mono requestDeleteRoute( - CloudFoundryClient cloudFoundryClient, String routeId) { - return cloudFoundryClient + private Mono requestDeleteRoute(String routeId) { + return this.cloudFoundryClient .routes() .delete(DeleteRouteRequest.builder().async(true).routeId(routeId).build()); } - private static Flux requestEvents( - String applicationId, CloudFoundryClient cloudFoundryClient) { + private Flux requestEvents(String applicationId) { return PaginationUtils.requestClientV2Resources( page -> - cloudFoundryClient + this.cloudFoundryClient .events() .list( ListEventsRequest.builder() @@ -2402,9 +1867,8 @@ private static Flux requestEvents( .build())); } - private static Mono requestGetApplication( - CloudFoundryClient cloudFoundryClient, String applicationId) { - return cloudFoundryClient + private Mono requestGetApplication(String applicationId) { + return this.cloudFoundryClient .applicationsV2() .get( org.cloudfoundry.client.v2.applications.GetApplicationRequest.builder() @@ -2413,20 +1877,10 @@ private static Mono requestGetApplication( .cast(AbstractApplicationResource.class); } - private static Flux requestListDomains( - CloudFoundryClient cloudFoundryClient, String organizationId) { - return PaginationUtils.requestClientV3Resources( - page -> - cloudFoundryClient - .domainsV3() - .list(ListDomainsRequest.builder().page(page).build())); - } - - private static Flux requestListPrivateDomains( - CloudFoundryClient cloudFoundryClient, String organizationId) { + private Flux requestListPrivateDomains(String organizationId) { return PaginationUtils.requestClientV2Resources( page -> - cloudFoundryClient + this.cloudFoundryClient .organizations() .listPrivateDomains( ListOrganizationPrivateDomainsRequest.builder() @@ -2435,11 +1889,10 @@ private static Flux requestListPrivateDomains( .build())); } - private static Flux requestListServiceBindings( - CloudFoundryClient cloudFoundryClient, String applicationId) { + private Flux requestListServiceBindings(String applicationId) { return PaginationUtils.requestClientV2Resources( page -> - cloudFoundryClient + this.cloudFoundryClient .applicationsV2() .listServiceBindings( ListApplicationServiceBindingsRequest.builder() @@ -2448,35 +1901,33 @@ private static Flux requestListServiceBindings( .build())); } - private static Flux requestListServiceInstances( - CloudFoundryClient cloudFoundryClient, String serviceInstanceName, String spaceId) { + private Flux requestListServiceInstances( + String serviceInstanceName) { return PaginationUtils.requestClientV2Resources( page -> - cloudFoundryClient + this.cloudFoundryClient .spaces() .listServiceInstances( ListSpaceServiceInstancesRequest.builder() .page(page) .returnUserProvidedServiceInstances(true) .name(serviceInstanceName) - .spaceId(spaceId) + .spaceId(this.spaceId) .build())); } - private static Flux requestListSharedDomains( - CloudFoundryClient cloudFoundryClient) { + private Flux requestListSharedDomains() { return PaginationUtils.requestClientV2Resources( page -> - cloudFoundryClient + this.cloudFoundryClient .sharedDomains() .list(ListSharedDomainsRequest.builder().page(page).build())); } - private static Flux requestListTasks( - CloudFoundryClient cloudFoundryClient, String applicationId) { + private Flux requestListTasks(String applicationId) { return PaginationUtils.requestClientV3Resources( page -> - cloudFoundryClient + this.cloudFoundryClient .applicationsV3() .listTasks( org.cloudfoundry.client.v3.applications @@ -2486,11 +1937,10 @@ private static Flux requestListTasks( .build())); } - private static Flux requestListTasks( - CloudFoundryClient cloudFoundryClient, String applicationId, Integer sequenceId) { + private Flux requestListTasks(String applicationId, Integer sequenceId) { return PaginationUtils.requestClientV3Resources( page -> - cloudFoundryClient + this.cloudFoundryClient .applicationsV3() .listTasks( org.cloudfoundry.client.v3.applications @@ -2501,27 +1951,20 @@ private static Flux requestListTasks( .build())); } - private static Flux requestLogsRecent( - Mono dopplerClient, String applicationId) { - return dopplerClient.flatMapMany( - client -> - client.recentLogs( - RecentLogsRequest.builder().applicationId(applicationId).build())); + private Flux requestLogsRecent(String applicationId) { + return dopplerClient.recentLogs( + RecentLogsRequest.builder().applicationId(applicationId).build()); } - private static Flux requestLogsStream( - Mono dopplerClient, String applicationId) { - return dopplerClient.flatMapMany( - client -> - client.stream( - StreamRequest.builder().applicationId(applicationId).build())); + private Flux requestLogsStream(String applicationId) { + return dopplerClient.stream(StreamRequest.builder().applicationId(applicationId).build()); } - private static Flux requestOrganizationSpacesByName( - CloudFoundryClient cloudFoundryClient, String organizationId, String space) { + private Flux requestOrganizationSpacesByName( + String organizationId, String space) { return PaginationUtils.requestClientV2Resources( page -> - cloudFoundryClient + this.cloudFoundryClient .organizations() .listSpaces( ListOrganizationSpacesRequest.builder() @@ -2531,11 +1974,10 @@ private static Flux requestOrganizationSpacesByName( .build())); } - private static Flux requestOrganizations( - CloudFoundryClient cloudFoundryClient, String organization) { + private Flux requestOrganizations(String organization) { return PaginationUtils.requestClientV2Resources( page -> - cloudFoundryClient + this.cloudFoundryClient .organizations() .list( ListOrganizationsRequest.builder() @@ -2544,9 +1986,8 @@ private static Flux requestOrganizations( .build())); } - private static Mono requestRemoveRouteFromApplication( - CloudFoundryClient cloudFoundryClient, String applicationId, String routeId) { - return cloudFoundryClient + private Mono requestRemoveRouteFromApplication(String applicationId, String routeId) { + return this.cloudFoundryClient .applicationsV2() .removeRoute( RemoveApplicationRouteRequest.builder() @@ -2555,9 +1996,8 @@ private static Mono requestRemoveRouteFromApplication( .build()); } - private static Mono requestRemoveServiceBinding( - CloudFoundryClient cloudFoundryClient, String applicationId, String serviceBindingId) { - return cloudFoundryClient + private Mono requestRemoveServiceBinding(String applicationId, String serviceBindingId) { + return this.cloudFoundryClient .applicationsV2() .removeServiceBinding( RemoveApplicationServiceBindingRequest.builder() @@ -2566,9 +2006,8 @@ private static Mono requestRemoveServiceBinding( .build()); } - private static Mono requestRestageApplication( - CloudFoundryClient cloudFoundryClient, String applicationId) { - return cloudFoundryClient + private Mono requestRestageApplication(String applicationId) { + return this.cloudFoundryClient .applicationsV2() .restage( org.cloudfoundry.client.v2.applications.RestageApplicationRequest.builder() @@ -2576,59 +2015,54 @@ private static Mono requestRestageApplication( .build()); } - private static Flux requestRoutes( - CloudFoundryClient cloudFoundryClient, - String domainId, - String host, - Integer port, - String routePath) { + private Flux requestRoutes( + String domainId, String host, Integer port, String routePath) { ListRoutesRequest.Builder requestBuilder = ListRoutesRequest.builder().domainId(domainId); Optional.ofNullable(host).ifPresent(requestBuilder::host); Optional.ofNullable(routePath).ifPresent(requestBuilder::path); Optional.ofNullable(port).ifPresent(requestBuilder::port); return PaginationUtils.requestClientV2Resources( - page -> cloudFoundryClient.routes().list(requestBuilder.page(page).build())); + page -> this.cloudFoundryClient.routes().list(requestBuilder.page(page).build())); } - private static Flux requestSharedDomains( - CloudFoundryClient cloudFoundryClient) { + private Flux requestSharedDomains() { return PaginationUtils.requestClientV2Resources( page -> - cloudFoundryClient + this.cloudFoundryClient .sharedDomains() .list(ListSharedDomainsRequest.builder().page(page).build())); } - private static Mono requestSpace( - CloudFoundryClient cloudFoundryClient, String spaceId) { - return cloudFoundryClient.spaces().get(GetSpaceRequest.builder().spaceId(spaceId).build()); + private Mono requestSpace(String spaceId) { + return this.cloudFoundryClient + .spaces() + .get(GetSpaceRequest.builder().spaceId(spaceId).build()); } - private static Mono requestSpaceSummary( - CloudFoundryClient cloudFoundryClient, String spaceId) { - return cloudFoundryClient + private Mono requestSpaceSummary() { + return this.cloudFoundryClient .spaces() - .getSummary(GetSpaceSummaryRequest.builder().spaceId(spaceId).build()); + .getSummary(GetSpaceSummaryRequest.builder().spaceId(this.spaceId).build()); } - private static Mono requestStack( - CloudFoundryClient cloudFoundryClient, String stackId) { - return cloudFoundryClient.stacks().get(GetStackRequest.builder().stackId(stackId).build()); + private Mono requestStack(String stackId) { + return this.cloudFoundryClient + .stacks() + .get(GetStackRequest.builder().stackId(stackId).build()); } - private static Flux requestStacks( - CloudFoundryClient cloudFoundryClient, String stack) { + private Flux requestStacks(String stack) { return PaginationUtils.requestClientV2Resources( page -> - cloudFoundryClient + this.cloudFoundryClient .stacks() .list(ListStacksRequest.builder().page(page).name(stack).build())); } - private static Mono requestTerminateApplicationInstance( - CloudFoundryClient cloudFoundryClient, String applicationId, String instanceIndex) { - return cloudFoundryClient + private Mono requestTerminateApplicationInstance( + String applicationId, String instanceIndex) { + return this.cloudFoundryClient .applicationsV2() .terminateInstance( TerminateApplicationInstanceRequest.builder() @@ -2637,21 +2071,18 @@ private static Mono requestTerminateApplicationInstance( .build()); } - private static Mono requestTerminateTask( - CloudFoundryClient cloudFoundryClient, String taskId) { - return cloudFoundryClient + private Mono requestTerminateTask(String taskId) { + return this.cloudFoundryClient .tasks() .cancel(CancelTaskRequest.builder().taskId(taskId).build()); } - private static Mono requestUpdateApplication( - CloudFoundryClient cloudFoundryClient, + private Mono requestUpdateApplication( String applicationId, Map environmentJsons, ApplicationManifest manifest, String stackId) { return requestUpdateApplication( - cloudFoundryClient, applicationId, builder -> { builder.command(manifest.getCommand()) @@ -2692,11 +2123,9 @@ private static Mono requestUpdateApplication( }); } - private static Mono requestUpdateApplication( - CloudFoundryClient cloudFoundryClient, - String applicationId, - UnaryOperator modifier) { - return cloudFoundryClient + private Mono requestUpdateApplication( + String applicationId, UnaryOperator modifier) { + return this.cloudFoundryClient .applicationsV2() .update( modifier.apply( @@ -2706,45 +2135,32 @@ private static Mono requestUpdateApplication( .cast(AbstractApplicationResource.class); } - private static Mono requestUpdateApplicationEnvironment( - CloudFoundryClient cloudFoundryClient, - String applicationId, - Map environment) { + private Mono requestUpdateApplicationEnvironment( + String applicationId, Map environment) { return requestUpdateApplication( - cloudFoundryClient, - applicationId, - builder -> builder.environmentJsons(environment)); + applicationId, builder -> builder.environmentJsons(environment)); } - private static Mono requestUpdateApplicationHealthCheckType( - CloudFoundryClient cloudFoundryClient, - String applicationId, - ApplicationHealthCheck type) { + private Mono requestUpdateApplicationHealthCheckType( + String applicationId, ApplicationHealthCheck type) { return requestUpdateApplication( - cloudFoundryClient, - applicationId, - builder -> builder.healthCheckType(type.getValue())); + applicationId, builder -> builder.healthCheckType(type.getValue())); } - private static Mono requestUpdateApplicationName( - CloudFoundryClient cloudFoundryClient, String applicationId, String name) { - return requestUpdateApplication( - cloudFoundryClient, applicationId, builder -> builder.name(name)); + private Mono requestUpdateApplicationName( + String applicationId, String name) { + return requestUpdateApplication(applicationId, builder -> builder.name(name)); } - private static Mono requestUpdateApplicationSsh( - CloudFoundryClient cloudFoundryClient, String applicationId, boolean enabled) { + private Mono requestUpdateApplicationSsh( + String applicationId, boolean enabled) { return requestUpdateApplicationFeature( - cloudFoundryClient, - applicationId, - builder -> builder.featureName(APP_FEATURE_SSH).enabled(enabled)); + applicationId, builder -> builder.featureName(APP_FEATURE_SSH).enabled(enabled)); } - private static Mono requestUpdateApplicationFeature( - CloudFoundryClient cloudFoundryClient, - String applicationId, - UnaryOperator modifier) { - return cloudFoundryClient + private Mono requestUpdateApplicationFeature( + String applicationId, UnaryOperator modifier) { + return this.cloudFoundryClient .applicationsV3() .updateFeature( modifier.apply( @@ -2755,32 +2171,24 @@ private static Mono requestUpdateApplicationFeature( .cast(ApplicationFeature.class); } - private static Mono requestUpdateApplicationScale( - CloudFoundryClient cloudFoundryClient, - String applicationId, - Integer disk, - Integer instances, - Integer memory) { + private Mono requestUpdateApplicationScale( + String applicationId, Integer disk, Integer instances, Integer memory) { return requestUpdateApplication( - cloudFoundryClient, applicationId, builder -> builder.diskQuota(disk).instances(instances).memory(memory)); } - private static Mono requestUpdateApplicationSsh( - CloudFoundryClient cloudFoundryClient, String applicationId, Boolean enabled) { - return requestUpdateApplication( - cloudFoundryClient, applicationId, builder -> builder.enableSsh(enabled)); + private Mono requestUpdateApplicationSsh( + String applicationId, Boolean enabled) { + return requestUpdateApplication(applicationId, builder -> builder.enableSsh(enabled)); } - private static Mono requestUpdateApplicationState( - CloudFoundryClient cloudFoundryClient, String applicationId, String state) { - return requestUpdateApplication( - cloudFoundryClient, applicationId, builder -> builder.state(state)); + private Mono requestUpdateApplicationState( + String applicationId, String state) { + return requestUpdateApplication(applicationId, builder -> builder.state(state)); } - private static Mono requestUploadApplication( - CloudFoundryClient cloudFoundryClient, + private Mono requestUploadApplication( String applicationId, Path application, List matchedResources) { @@ -2803,15 +2211,12 @@ private static Mono requestUploadApplication( (a, b) -> a) .build(); - return cloudFoundryClient.applicationsV2().upload(request); + return this.cloudFoundryClient.applicationsV2().upload(request); } - private static Mono requestUploadPackage( - CloudFoundryClient cloudFoundryClient, - String packageId, - Path bits, - List matchedResources) { - return cloudFoundryClient + private Mono requestUploadPackage( + String packageId, Path bits, List matchedResources) { + return this.cloudFoundryClient .packages() .upload( UploadPackageRequest.builder() @@ -2822,39 +2227,25 @@ private static Mono requestUploadPackage( .then(); } - private static Mono restageApplication( - CloudFoundryClient cloudFoundryClient, + private Mono restageApplication( String application, String applicationId, Duration stagingTimeout, Duration startupTimeout) { - return requestRestageApplication(cloudFoundryClient, applicationId) - .flatMap( - response -> - waitForStaging( - cloudFoundryClient, - application, - applicationId, - stagingTimeout)) - .then( - waitForRunning( - cloudFoundryClient, application, applicationId, startupTimeout)); + return requestRestageApplication(applicationId) + .flatMap(response -> waitForStaging(application, applicationId, stagingTimeout)) + .then(waitForRunning(application, applicationId, startupTimeout)); } - private static Mono restartApplication( - CloudFoundryClient cloudFoundryClient, + private Mono restartApplication( String application, String applicationId, Duration stagingTimeout, Duration startupTimeout) { - return stopApplication(cloudFoundryClient, applicationId) + return stopApplication(applicationId) .then( startApplicationAndWait( - cloudFoundryClient, - application, - applicationId, - stagingTimeout, - startupTimeout)); + application, applicationId, stagingTimeout, startupTimeout)); } private static boolean shouldStartApplication( @@ -2866,51 +2257,37 @@ private static boolean shouldStartApplication(PushApplicationManifestRequest req return !Optional.ofNullable(request.getNoStart()).orElse(false); } - private static Mono startApplicationAndWait( - CloudFoundryClient cloudFoundryClient, + private Mono startApplicationAndWait( String application, String applicationId, Duration stagingTimeout, Duration startupTimeout) { - return requestUpdateApplicationState(cloudFoundryClient, applicationId, STARTED_STATE) - .flatMap( - response -> - waitForStaging( - cloudFoundryClient, - application, - applicationId, - stagingTimeout)) - .then( - waitForRunning( - cloudFoundryClient, application, applicationId, startupTimeout)); + return requestUpdateApplicationState(applicationId, STARTED_STATE) + .flatMap(response -> waitForStaging(application, applicationId, stagingTimeout)) + .then(waitForRunning(application, applicationId, startupTimeout)); } - private static Mono stopAndStartApplication( - CloudFoundryClient cloudFoundryClient, - String applicationId, - String name, - PushApplicationManifestRequest request) { - return stopApplication(cloudFoundryClient, applicationId) + private Mono stopAndStartApplication( + String applicationId, String name, PushApplicationManifestRequest request) { + return stopApplication(applicationId) .filter(resource -> shouldStartApplication(request, resource)) .flatMap( resource -> startApplicationAndWait( - cloudFoundryClient, name, applicationId, request.getStagingTimeout(), request.getStartupTimeout())); } - private static Mono stopApplication( - CloudFoundryClient cloudFoundryClient, String applicationId) { - return requestUpdateApplicationState(cloudFoundryClient, applicationId, STOPPED_STATE); + private Mono stopApplication(String applicationId) { + return requestUpdateApplicationState(applicationId, STOPPED_STATE); } - private static Mono stopApplicationIfNotStopped( - CloudFoundryClient cloudFoundryClient, AbstractApplicationResource resource) { + private Mono stopApplicationIfNotStopped( + AbstractApplicationResource resource) { return isNotIn(resource, STOPPED_STATE) - ? stopApplication(cloudFoundryClient, ResourceUtils.getId(resource)) + ? stopApplication(ResourceUtils.getId(resource)) : Mono.just(resource); } @@ -2951,7 +2328,7 @@ private static ApplicationEnvironments toApplicationEnvironments( .build(); } - private static Mono toApplicationManifest( + private Mono toApplicationManifest( List buildpacks, SummaryApplicationResponse response, String stackName) { ApplicationManifest.Builder builder = ApplicationManifest.builder() @@ -3079,7 +2456,7 @@ private static InstanceDetail toInstanceDetail( .build(); } - private static Mono> toInstanceDetailList( + private Mono> toInstanceDetailList( ApplicationInstancesResponse instancesResponse, ApplicationStatisticsResponse statisticsResponse) { return Flux.fromIterable(instancesResponse.getInstances().entrySet()) @@ -3113,19 +2490,16 @@ private static String toUrl(org.cloudfoundry.client.v2.routes.Route route) { return sb.toString(); } - private static Mono> toUrls(List routes) { + private Mono> toUrls(List routes) { return Flux.fromIterable(routes).map(DefaultApplications::toUrl).collectList(); } - private static Mono updateBuildpacks( - CloudFoundryClient cloudFoundryClient, - String applicationId, - ApplicationManifest manifest) { + private Mono updateBuildpacks(String applicationId, ApplicationManifest manifest) { if (manifest.getBuildpacks() == null || manifest.getBuildpacks().size() < 2) { return Mono.empty(); } - return cloudFoundryClient + return this.cloudFoundryClient .applicationsV3() .update( org.cloudfoundry.client.v3.applications.UpdateApplicationRequest.builder() @@ -3143,8 +2517,7 @@ private static Mono updateBuildpacks( .then(); } - private static Mono uploadApplicationAndWait( - CloudFoundryClient cloudFoundryClient, + private Mono uploadApplicationAndWait( String applicationId, Path application, List matchedResources, @@ -3153,10 +2526,7 @@ private static Mono uploadApplicationAndWait( () -> { if (matchedResources.isEmpty()) { return requestUploadApplication( - cloudFoundryClient, - applicationId, - application, - matchedResources); + applicationId, application, matchedResources); } else { List paths = matchedResources.stream() @@ -3169,7 +2539,6 @@ private static Mono uploadApplicationAndWait( .flatMap( filteredApplication -> requestUploadApplication( - cloudFoundryClient, applicationId, filteredApplication, matchedResources) @@ -3188,11 +2557,12 @@ private static Mono uploadApplicationAndWait( } }) .flatMap( - job -> JobUtils.waitForCompletion(cloudFoundryClient, stagingTimeout, job)); + job -> + JobUtils.waitForCompletion( + this.cloudFoundryClient, stagingTimeout, job)); } - private static Mono uploadPackageBitsAndWait( - CloudFoundryClient cloudFoundryClient, + private Mono uploadPackageBitsAndWait( String packageId, Path application, List matchedResources, @@ -3201,10 +2571,7 @@ private static Mono uploadPackageBitsAndWait( () -> { if (matchedResources.isEmpty()) { return requestUploadPackage( - cloudFoundryClient, - packageId, - application, - matchedResources); + packageId, application, matchedResources); } else { List paths = matchedResources.stream() @@ -3215,7 +2582,6 @@ private static Mono uploadPackageBitsAndWait( .flatMap( filteredApplication -> requestUploadPackage( - cloudFoundryClient, packageId, filteredApplication, matchedResources) @@ -3233,18 +2599,13 @@ private static Mono uploadPackageBitsAndWait( })); } }) - .then( - waitForUploadProcessingCompleted( - cloudFoundryClient, packageId, processingTimeout)); + .then(waitForUploadProcessingCompleted(packageId, processingTimeout)); } - private static Mono waitForBuildStaging( - CloudFoundryClient cloudFoundryClient, - String buildId, - String applicationName, - Duration stagingTimeout) { + private Mono waitForBuildStaging( + String buildId, String applicationName, Duration stagingTimeout) { Duration timeout = Optional.ofNullable(stagingTimeout).orElse(Duration.ofMinutes(15)); - return cloudFoundryClient + return this.cloudFoundryClient .builds() .get(GetBuildRequest.builder().buildId(buildId).build()) .filter( @@ -3266,17 +2627,14 @@ private static Mono waitForBuildStaging( buildId, applicationName)); } - private static Mono waitForRunning( - CloudFoundryClient cloudFoundryClient, - String application, - String applicationId, - Duration startupTimeout) { + private Mono waitForRunning( + String application, String applicationId, Duration startupTimeout) { Duration timeout = Optional.ofNullable(startupTimeout).orElse(Duration.ofMinutes(5)); - return requestApplicationInstances(cloudFoundryClient, applicationId) - .flatMapMany(response -> Flux.fromIterable(response.getInstances().values())) - .map(ApplicationInstanceInfo::getState) - .reduce("UNKNOWN", collectStates()) + return requestApplicationStatisticsV3(applicationId) + .flatMapIterable(GetApplicationProcessStatisticsResponse::getResources) + .map(ProcessStatisticsResource::getState) + .reduce(ProcessState.STARTING, collectStates()) .filter(isInstanceComplete()) .repeatWhenEmpty( exponentialBackOff(Duration.ofSeconds(1), Duration.ofSeconds(15), timeout)) @@ -3292,76 +2650,16 @@ private static Mono waitForRunning( .then(); } - private static Mono waitForRunningV3( - CloudFoundryClient cloudFoundryClient, - String applicationName, - String applicationId, - Duration startupTimeout) { - Duration timeout = Optional.ofNullable(startupTimeout).orElse(Duration.ofMinutes(5)); - - return PaginationUtils.requestClientV3Resources( - page -> - cloudFoundryClient - .applicationsV3() - .listProcesses( - ListApplicationProcessesRequest.builder() - .applicationId(applicationId) - .page(page) - .build())) - .filter(p -> p.getInstances() != 0) - .flatMap( - process -> - cloudFoundryClient - .processes() - .getStatistics( - GetProcessStatisticsRequest.builder() - .processId(process.getId()) - .build()) - .flatMapIterable(GetProcessStatisticsResponse::getResources) - .map(ProcessStatisticsResource::getState) - .filter( - state -> - EnumSet.of( - ProcessState.RUNNING, - ProcessState.CRASHED) - .contains(state)) - .reduce( - (totalState, instanceState) -> - totalState.ordinal() - < instanceState.ordinal() - ? totalState - : instanceState) // CRASHED takes - // precedence over - // RUNNING - .repeatWhenEmpty( - exponentialBackOff( - Duration.ofSeconds(1), - Duration.ofSeconds(15), - timeout)) - .filter(state -> state == ProcessState.RUNNING) - .switchIfEmpty( - ExceptionUtils.illegalState( - "Process %s of Application %s failed during" - + " start", - process.getId(), applicationName)) - .onErrorResume( - DelayTimeoutException.class, - t -> - ExceptionUtils.illegalState( - "Process %s of Application %s timed" - + " out during start", - process.getId(), applicationName))) - .then(); + private Mono waitForRunningV3( + String applicationName, String applicationId, Duration startupTimeout) { + return waitForRunning(applicationName, applicationId, startupTimeout); } - private static Mono waitForStaging( - CloudFoundryClient cloudFoundryClient, - String application, - String applicationId, - Duration stagingTimeout) { + private Mono waitForStaging( + String application, String applicationId, Duration stagingTimeout) { Duration timeout = Optional.ofNullable(stagingTimeout).orElse(Duration.ofMinutes(15)); - return requestGetApplication(cloudFoundryClient, applicationId) + return requestGetApplication(applicationId) .map(response -> ResourceUtils.getEntity(response).getPackageState()) .filter(isStagingComplete()) .repeatWhenEmpty( @@ -3378,9 +2676,9 @@ private static Mono waitForStaging( .then(); } - private static Mono waitForUploadProcessingCompleted( - CloudFoundryClient cloudFoundryClient, String packageId, Duration processingTimeout) { - return cloudFoundryClient + private Mono waitForUploadProcessingCompleted( + String packageId, Duration processingTimeout) { + return this.cloudFoundryClient .packages() .get(GetPackageRequest.builder().packageId(packageId).build()) .filter( diff --git a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/AbstractOperationsTest.java b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/AbstractOperationsTest.java index ab1250658a..168e0756c8 100644 --- a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/AbstractOperationsTest.java +++ b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/AbstractOperationsTest.java @@ -48,6 +48,7 @@ import org.cloudfoundry.client.v3.domains.DomainsV3; import org.cloudfoundry.client.v3.jobs.JobsV3; import org.cloudfoundry.client.v3.organizations.OrganizationsV3; +import org.cloudfoundry.client.v3.processes.Processes; import org.cloudfoundry.client.v3.routes.RoutesV3; import org.cloudfoundry.client.v3.spaces.SpacesV3; import org.cloudfoundry.client.v3.stacks.StacksV3; @@ -117,6 +118,7 @@ public abstract class AbstractOperationsTest { protected final Organizations organizations = mock(Organizations.class, RETURNS_SMART_NULLS); protected final OrganizationsV3 organizationsV3 = mock(OrganizationsV3.class, RETURNS_SMART_NULLS); + protected final Processes processes = mock(Processes.class, RETURNS_SMART_NULLS); protected final PrivateDomains privateDomains = mock(PrivateDomains.class, RETURNS_SMART_NULLS); @@ -188,6 +190,7 @@ public final void mockClient() { when(this.cloudFoundryClient.organizationQuotaDefinitions()) .thenReturn(this.organizationQuotaDefinitions); when(this.cloudFoundryClient.privateDomains()).thenReturn(this.privateDomains); + when(this.cloudFoundryClient.processes()).thenReturn(this.processes); when(this.cloudFoundryClient.resourceMatch()).thenReturn(this.resourceMatch); when(this.cloudFoundryClient.routes()).thenReturn(this.routes); when(this.cloudFoundryClient.routesV3()).thenReturn(this.routesV3); diff --git a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/DefaultCloudFoundryOperationsTest.java b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/DefaultCloudFoundryOperationsTest.java index 02e671a3e6..2dfa183183 100644 --- a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/DefaultCloudFoundryOperationsTest.java +++ b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/DefaultCloudFoundryOperationsTest.java @@ -17,20 +17,45 @@ package org.cloudfoundry.operations; import static org.assertj.core.api.Assertions.assertThat; - +import static org.cloudfoundry.operations.TestObjects.fill; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import org.cloudfoundry.client.v3.organizations.ListOrganizationsResponse; +import org.cloudfoundry.client.v3.organizations.OrganizationResource; +import org.cloudfoundry.client.v3.spaces.ListSpacesResponse; +import org.cloudfoundry.client.v3.spaces.SpaceResource; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import reactor.core.publisher.Mono; final class DefaultCloudFoundryOperationsTest extends AbstractOperationsTest { - private final DefaultCloudFoundryOperations operations = - DefaultCloudFoundryOperations.builder() - .cloudFoundryClient(this.cloudFoundryClient) - .dopplerClient(this.dopplerClient) - .routingClient(this.routingClient) - .organization(TEST_ORGANIZATION_NAME) - .space(TEST_SPACE_NAME) - .uaaClient(this.uaaClient) - .build(); + private DefaultCloudFoundryOperations operations; + + @BeforeEach + void setUp() { + ListOrganizationsResponse orgsResponse = + fill(ListOrganizationsResponse.builder()) + .resource(fill(OrganizationResource.builder()).build()) + .build(); + when(this.organizationsV3.list(any())).thenReturn(Mono.just(orgsResponse)); + ListSpacesResponse spacesResponse = + fill(ListSpacesResponse.builder()) + .resource(fill(SpaceResource.builder()).build()) + .build(); + when(this.spacesV3.list(any())).thenReturn(Mono.just(spacesResponse)); + + operations = + DefaultCloudFoundryOperations.builder() + .cloudFoundryClient(this.cloudFoundryClient) + .dopplerClient(this.dopplerClient) + .routingClient(this.routingClient) + .organization(TEST_ORGANIZATION_NAME) + .space(TEST_SPACE_NAME) + .uaaClient(this.uaaClient) + .build(); + } @Test void advanced() { diff --git a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java index cdc9619d2d..cf74e13f80 100644 --- a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java +++ b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java @@ -20,6 +20,7 @@ import static org.cloudfoundry.client.v3.LifecycleType.BUILDPACK; import static org.cloudfoundry.client.v3.LifecycleType.DOCKER; import static org.cloudfoundry.operations.TestObjects.fill; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.RETURNS_SMART_NULLS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -55,8 +56,6 @@ import org.cloudfoundry.client.v2.applications.DockerCredentials; import org.cloudfoundry.client.v2.applications.GetApplicationResponse; import org.cloudfoundry.client.v2.applications.InstanceStatistics; -import org.cloudfoundry.client.v2.applications.ListApplicationRoutesRequest; -import org.cloudfoundry.client.v2.applications.ListApplicationRoutesResponse; import org.cloudfoundry.client.v2.applications.ListApplicationServiceBindingsRequest; import org.cloudfoundry.client.v2.applications.ListApplicationServiceBindingsResponse; import org.cloudfoundry.client.v2.applications.RemoveApplicationRouteRequest; @@ -124,15 +123,21 @@ import org.cloudfoundry.client.v3.BuildpackData; import org.cloudfoundry.client.v3.DockerData; import org.cloudfoundry.client.v3.Lifecycle; +import org.cloudfoundry.client.v3.Pagination; import org.cloudfoundry.client.v3.applications.ApplicationState; import org.cloudfoundry.client.v3.applications.GetApplicationEnvironmentRequest; import org.cloudfoundry.client.v3.applications.GetApplicationEnvironmentResponse; +import org.cloudfoundry.client.v3.applications.GetApplicationProcessStatisticsResponse; import org.cloudfoundry.client.v3.applications.GetApplicationSshEnabledRequest; import org.cloudfoundry.client.v3.applications.GetApplicationSshEnabledResponse; +import org.cloudfoundry.client.v3.applications.ListApplicationRoutesRequest; +import org.cloudfoundry.client.v3.applications.ListApplicationRoutesResponse; import org.cloudfoundry.client.v3.applications.ListApplicationsRequest; import org.cloudfoundry.client.v3.applications.ListApplicationsResponse; import org.cloudfoundry.client.v3.applications.UpdateApplicationFeatureRequest; import org.cloudfoundry.client.v3.applications.UpdateApplicationFeatureResponse; +import org.cloudfoundry.client.v3.processes.ProcessState; +import org.cloudfoundry.client.v3.processes.ProcessStatisticsResource; import org.cloudfoundry.client.v3.tasks.CancelTaskRequest; import org.cloudfoundry.client.v3.tasks.CancelTaskResponse; import org.cloudfoundry.client.v3.tasks.CreateTaskRequest; @@ -161,10 +166,7 @@ final class DefaultApplicationsTest extends AbstractOperationsTest { private final DefaultApplications applications = new DefaultApplications( - Mono.just(this.cloudFoundryClient), - Mono.just(this.dopplerClient), - this.randomWords, - Mono.just(TEST_SPACE_ID)); + this.cloudFoundryClient, this.dopplerClient, this.randomWords, TEST_SPACE_ID); @Test void copySourceNoRestartOrgSpace() { @@ -4111,6 +4113,26 @@ private static void requestApplicationInstancesFailingPartial( .state("FLAPPING") .build()) .build())); + when(cloudFoundryClient.applicationsV3().getProcessStatistics(any())) + .thenReturn( + Mono.just( + GetApplicationProcessStatisticsResponse.builder() + .resources( + fill(ProcessStatisticsResource.builder()) + .state(ProcessState.RUNNING) + .type("web") + .uptime(1L) + .fileDescriptorQuota(1L) + .host("test-host") + .build(), + fill(ProcessStatisticsResource.builder()) + .state(ProcessState.CRASHED) + .type("web") + .uptime(1L) + .fileDescriptorQuota(1L) + .host("test-host") + .build()) + .build())); } private static void requestApplicationInstancesFailingTotal( @@ -4134,6 +4156,19 @@ private static void requestApplicationInstancesFailingTotal( .state("FLAPPING") .build()) .build())); + when(cloudFoundryClient.applicationsV3().getProcessStatistics(any())) + .thenReturn( + Mono.just( + GetApplicationProcessStatisticsResponse.builder() + .resource( + fill(ProcessStatisticsResource.builder()) + .state(ProcessState.CRASHED) + .type("web") + .uptime(1L) + .fileDescriptorQuota(1L) + .host("test-host") + .build()) + .build())); } private static void requestApplicationInstancesRunning( @@ -4157,6 +4192,20 @@ private static void requestApplicationInstancesRunning( .state("RUNNING") .build()) .build())); + + when(cloudFoundryClient.applicationsV3().getProcessStatistics(any())) + .thenReturn( + Mono.just( + GetApplicationProcessStatisticsResponse.builder() + .resource( + fill(ProcessStatisticsResource.builder()) + .state(ProcessState.RUNNING) + .type("web") + .uptime(1L) + .fileDescriptorQuota(1L) + .host("test-host") + .build()) + .build())); } private static void requestApplicationInstancesTimeout( @@ -4180,6 +4229,19 @@ private static void requestApplicationInstancesTimeout( .state("STARTING") .build()) .build())); + when(cloudFoundryClient.applicationsV3().getProcessStatistics(any())) + .thenReturn( + Mono.just( + GetApplicationProcessStatisticsResponse.builder() + .resource( + fill(ProcessStatisticsResource.builder()) + .state(ProcessState.STARTING) + .type("web") + .uptime(1L) + .fileDescriptorQuota(1L) + .host("test-host") + .build()) + .build())); } private static void requestApplicationNoInstances( @@ -4244,30 +4306,30 @@ private static void requestApplicationNullUsage( private static void requestApplicationRoutes( CloudFoundryClient cloudFoundryClient, String applicationId, String routeId) { when(cloudFoundryClient - .applicationsV2() + .applicationsV3() .listRoutes( - ListApplicationRoutesRequest.builder() + org.cloudfoundry.client.v3.applications.ListApplicationRoutesRequest + .builder() .applicationId(applicationId) .page(1) .build())) .thenReturn( Mono.just( - fill(ListApplicationRoutesResponse.builder()) + fill(org.cloudfoundry.client.v3.applications + .ListApplicationRoutesResponse.builder()) .resource( - fill(RouteResource.builder()) - .metadata( - fill(Metadata.builder()) - .id(routeId) - .build()) + fill(org.cloudfoundry.client.v3.routes.RouteResource + .builder()) + .id(routeId) .build()) - .totalPages(1) + .pagination(Pagination.builder().totalPages(1).build()) .build())); } private static void requestApplicationRoutesEmpty( CloudFoundryClient cloudFoundryClient, String applicationId) { when(cloudFoundryClient - .applicationsV2() + .applicationsV3() .listRoutes( ListApplicationRoutesRequest.builder() .applicationId(applicationId) @@ -4276,7 +4338,7 @@ private static void requestApplicationRoutesEmpty( .thenReturn( Mono.just( fill(ListApplicationRoutesResponse.builder()) - .totalPages(1) + .pagination(Pagination.builder().totalPages(1).build()) .build())); } @@ -5050,6 +5112,20 @@ private static void requestInstancesApplicationFailing( .state("FAILED") .build()) .build())); + + when(cloudFoundryClient.applicationsV3().getProcessStatistics(any())) + .thenReturn( + Mono.just( + GetApplicationProcessStatisticsResponse.builder() + .resources( + fill(ProcessStatisticsResource.builder()) + .state(ProcessState.CRASHED) + .type("web") + .uptime(1L) + .fileDescriptorQuota(1L) + .host("test-host") + .build()) + .build())); } private static void requestGetApplicationTimeout( diff --git a/integration-test/src/test/java/org/cloudfoundry/IntegrationTestConfiguration.java b/integration-test/src/test/java/org/cloudfoundry/IntegrationTestConfiguration.java index c0b3f44b30..6622fbef8b 100644 --- a/integration-test/src/test/java/org/cloudfoundry/IntegrationTestConfiguration.java +++ b/integration-test/src/test/java/org/cloudfoundry/IntegrationTestConfiguration.java @@ -263,6 +263,7 @@ ReactorCloudFoundryClient cloudFoundryClient( } @Bean + @DependsOn({"organizationId", "spaceId"}) DefaultCloudFoundryOperations cloudFoundryOperations( CloudFoundryClient cloudFoundryClient, DopplerClient dopplerClient,