Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import org.cloudfoundry.client.v2.OrderDirection;
import org.cloudfoundry.client.v2.applications.AbstractApplicationResource;
import org.cloudfoundry.client.v2.applications.ApplicationEntity;
import org.cloudfoundry.client.v2.applications.ApplicationEnvironmentRequest;
import org.cloudfoundry.client.v2.applications.ApplicationEnvironmentResponse;
import org.cloudfoundry.client.v2.applications.ApplicationInstanceInfo;
import org.cloudfoundry.client.v2.applications.ApplicationInstancesRequest;
import org.cloudfoundry.client.v2.applications.ApplicationInstancesResponse;
Expand Down Expand Up @@ -86,8 +84,15 @@
import org.cloudfoundry.client.v3.Lifecycle;
import org.cloudfoundry.client.v3.Relationship;
import org.cloudfoundry.client.v3.ToOneRelationship;
import org.cloudfoundry.client.v3.applications.ApplicationFeature;
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.GetApplicationResponse;
import org.cloudfoundry.client.v3.applications.GetApplicationSshEnabledRequest;
import org.cloudfoundry.client.v3.applications.GetApplicationSshEnabledResponse;
import org.cloudfoundry.client.v3.applications.ListApplicationsRequest;
import org.cloudfoundry.client.v3.applications.UpdateApplicationFeatureRequest;
import org.cloudfoundry.client.v3.applications.ListApplicationProcessesRequest;
import org.cloudfoundry.client.v3.applications.ListApplicationsRequest;
import org.cloudfoundry.client.v3.applications.SetApplicationCurrentDropletRequest;
Expand Down Expand Up @@ -200,6 +205,8 @@ public final class DefaultApplications implements Applications {

private static final String STOPPED_STATE = "STOPPED";

private static final String APP_FEATURE_SSH = "ssh";

private final Mono<CloudFoundryClient> cloudFoundryClient;

private final Mono<DopplerClient> dopplerClient;
Expand Down Expand Up @@ -257,8 +264,14 @@ public Mono<Void> disableSsh(DisableApplicationSshRequest request) {
.zip(this.cloudFoundryClient, this.spaceId)
.flatMap(function((cloudFoundryClient, spaceId) -> Mono.zip(
Mono.just(cloudFoundryClient),
getApplicationIdWhere(cloudFoundryClient, request.getName(), spaceId, sshEnabled(true))
getApplicationIdV3(cloudFoundryClient, request.getName(), spaceId)
Comment on lines 265 to +267
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might consider extracting this into a method. It seems to be common across a few methods. Here, enableSsh and sshEnabled and possibly others. It's minor, but could be a chance to improve the readability of the code, like .flatMap(getApplicationId) instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @dmikusa,
Thank you for reviewing the PR.

I checked your comment. Yes, the code is there multiple times. I believe that even if create a method like Mono<Tuple2<CloudFoundryClient,String>> getCloudFoundryClientAndApplicationIdV3, it will have a similar readability experience.

Please let me know if you still want me to update the method.

)))
.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)))
.then()
.transform(OperationsLogging.log("Disable Application SSH"))
Expand All @@ -271,8 +284,14 @@ public Mono<Void> enableSsh(EnableApplicationSshRequest request) {
.zip(this.cloudFoundryClient, this.spaceId)
.flatMap(function((cloudFoundryClient, spaceId) -> Mono.zip(
Mono.just(cloudFoundryClient),
getApplicationIdWhere(cloudFoundryClient, request.getName(), spaceId, sshEnabled(false))
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)))
.then()
.transform(OperationsLogging.log("Enable Application SSH"))
Expand Down Expand Up @@ -322,7 +341,7 @@ public Mono<ApplicationEnvironments> getEnvironments(GetApplicationEnvironmentsR
.zip(this.cloudFoundryClient, this.spaceId)
.flatMap(function((cloudFoundryClient, spaceId) -> Mono.zip(
Mono.just(cloudFoundryClient),
getApplicationId(cloudFoundryClient, request.getName(), spaceId)
getApplicationIdV3(cloudFoundryClient, request.getName(), spaceId)
)))
.flatMap(function(DefaultApplications::requestApplicationEnvironment))
.map(DefaultApplications::toApplicationEnvironments)
Expand Down Expand Up @@ -612,12 +631,21 @@ public Mono<Void> setHealthCheck(SetApplicationHealthCheckRequest request) {
public Mono<Boolean> sshEnabled(ApplicationSshEnabledRequest request) {
return Mono
.zip(this.cloudFoundryClient, this.spaceId)
.flatMap(function((cloudFoundryClient, spaceId) -> getApplication(cloudFoundryClient, request.getName(), spaceId)))
.map(applicationResource -> ResourceUtils.getEntity(applicationResource).getEnableSsh())
.flatMap(function((cloudFoundryClient, spaceId) -> Mono.zip(
Mono.just(cloudFoundryClient),
getApplicationIdV3(cloudFoundryClient, request.getName(), spaceId))))
.flatMap(function(DefaultApplications::getSshEnabled))
.transform(OperationsLogging.log("Is Application SSH Enabled"))
.checkpoint();
}

private static Mono<Boolean> getSshEnabled(CloudFoundryClient cloudFoundryClient, String applicationId) {
return cloudFoundryClient.applicationsV3()
.getSshEnabled(GetApplicationSshEnabledRequest.builder()
.applicationId(applicationId).build())
.map(GetApplicationSshEnabledResponse::getEnabled);
}

@Override
public Mono<Void> start(StartApplicationRequest request) {
return Mono
Expand Down Expand Up @@ -1343,11 +1371,11 @@ private static Mono<Void> removeServiceBindings(CloudFoundryClient cloudFoundryC
.then();
}

private static Mono<ApplicationEnvironmentResponse> requestApplicationEnvironment(CloudFoundryClient cloudFoundryClient, String applicationId) {
return cloudFoundryClient.applicationsV2()
.environment(ApplicationEnvironmentRequest.builder()
.applicationId(applicationId)
.build());
private static Mono<GetApplicationEnvironmentResponse> requestApplicationEnvironment(CloudFoundryClient cloudFoundryClient, String applicationId) {
return cloudFoundryClient.applicationsV3()
.getEnvironment(GetApplicationEnvironmentRequest.builder()
.applicationId(applicationId)
.build());
}

private static Mono<ApplicationInstancesResponse> requestApplicationInstances(CloudFoundryClient cloudFoundryClient, String applicationId) {
Expand Down Expand Up @@ -1791,8 +1819,16 @@ private static Mono<AbstractApplicationResource> requestUpdateApplicationScale(C
return requestUpdateApplication(cloudFoundryClient, applicationId, builder -> builder.diskQuota(disk).instances(instances).memory(memory));
}

private static Mono<AbstractApplicationResource> requestUpdateApplicationSsh(CloudFoundryClient cloudFoundryClient, String applicationId, Boolean enabled) {
return requestUpdateApplication(cloudFoundryClient, applicationId, builder -> builder.enableSsh(enabled));
private static Mono<ApplicationFeature> requestUpdateApplicationSsh(CloudFoundryClient cloudFoundryClient, String applicationId, boolean enabled) {
return requestUpdateApplicationFeature(cloudFoundryClient, applicationId,builder -> builder.featureName(APP_FEATURE_SSH).enabled(enabled));
}

private static Mono<ApplicationFeature> requestUpdateApplicationFeature(CloudFoundryClient cloudFoundryClient, String applicationId, UnaryOperator<UpdateApplicationFeatureRequest.Builder> modifier) {
return cloudFoundryClient.applicationsV3()
.updateFeature(modifier.apply(org.cloudfoundry.client.v3.applications.UpdateApplicationFeatureRequest.builder()
.applicationId(applicationId))
.build())
.cast(ApplicationFeature.class);
}

private static Mono<AbstractApplicationResource> requestUpdateApplicationState(CloudFoundryClient cloudFoundryClient, String applicationId, String state) {
Expand Down Expand Up @@ -1847,10 +1883,6 @@ private static boolean shouldStartApplication(PushApplicationManifestRequest req
return !Optional.ofNullable(request.getNoStart()).orElse(false);
}

private static Predicate<AbstractApplicationResource> sshEnabled(Boolean enabled) {
return resource -> enabled.equals(ResourceUtils.getEntity(resource).getEnableSsh());
}

private static Mono<Void> startApplicationAndWait(CloudFoundryClient cloudFoundryClient, String application, String applicationId, Duration stagingTimeout, Duration startupTimeout) {
return requestUpdateApplicationState(cloudFoundryClient, applicationId, STARTED_STATE)
.flatMap(response -> waitForStaging(cloudFoundryClient, application, applicationId, stagingTimeout))
Expand Down Expand Up @@ -1893,12 +1925,12 @@ private static ApplicationDetail toApplicationDetail(List<String> buildpacks, Su
.build();
}

private static ApplicationEnvironments toApplicationEnvironments(ApplicationEnvironmentResponse response) {
private static ApplicationEnvironments toApplicationEnvironments(GetApplicationEnvironmentResponse response) {
return ApplicationEnvironments.builder()
.running(response.getRunningEnvironmentJsons())
.staging(response.getStagingEnvironmentJsons())
.systemProvided(response.getSystemEnvironmentJsons())
.userProvided(response.getEnvironmentJsons())
.running(response.getRunningEnvironmentVariables())
.staging(response.getStagingEnvironmentVariables())
.systemProvided(response.getSystemEnvironmentVariables())
.userProvided(response.getEnvironmentVariables())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public abstract class AbstractOperationsTest {

protected static final String TEST_USERNAME = "test-username";

protected static final String APP_FEATURE_SSH = "ssh";

protected final ApplicationsV2 applications = mock(ApplicationsV2.class, RETURNS_SMART_NULLS);

protected final ApplicationsV3 applicationsV3 = mock(ApplicationsV3.class, RETURNS_SMART_NULLS);
Expand Down
Loading