Skip to content

Commit

Permalink
Initial impl
Browse files Browse the repository at this point in the history
  • Loading branch information
I562548 committed Nov 29, 2024
1 parent fcf01f5 commit 5f8cad3
Show file tree
Hide file tree
Showing 7 changed files with 374 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ private Messages() {
public static final String CALLING_CF_ROOT_0_TO_ACCESS_LOG_CACHE_URL = "Calling CF root: {0} to access log-cache URL";
public static final String CF_ROOT_REQUEST_FINISHED = "CF root request finished";
public static final String TRYING_TO_GET_APP_LOGS = "Trying to get app logs";
public static final String TRYING_TO_UPDATE_APPLICATION_PROCESS_WITH_ID = "Trying to update application process with id: {0}";
public static final String APP_LOGS_WERE_FETCHED_SUCCESSFULLY = "App logs were fetched successfully";

// WARN messages
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.sap.cloudfoundry.client.facade.adapters;

import com.sap.cloudfoundry.client.facade.CloudException;
import com.sap.cloudfoundry.client.facade.CloudOperationException;
import com.sap.cloudfoundry.client.facade.Messages;
import com.sap.cloudfoundry.client.facade.domain.ReadinessHealthCheck;
import com.sap.cloudfoundry.client.facade.oauth2.OAuthClient;
import com.sap.cloudfoundry.client.facade.util.CloudUtil;
import com.sap.cloudfoundry.client.facade.util.JsonUtil;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.time.Duration;
import java.util.Map;

public class ReadinessHealthCheckClient {

private static final Logger LOGGER = LoggerFactory.getLogger(ReadinessHealthCheckClient.class);
private final OAuthClient oAuthClient;
private final URI controllerApi;

public ReadinessHealthCheckClient(OAuthClient oAuthClient, URL api) {
this.oAuthClient = oAuthClient;
this.controllerApi = getControllerUriFromUrl(api);
}

public void updateApplicationReadinessHealthCheck(String processId, Map<String, ReadinessHealthCheck> readinessHealthCheck) {
CloudUtil.executeWithRetry(() -> executeRequest(processId, readinessHealthCheck));
}

private String executeRequest(String processId, Map<String, ReadinessHealthCheck> readinessHealthCheck) {
try {
LOGGER.info(MessageFormat.format(Messages.TRYING_TO_UPDATE_APPLICATION_PROCESS_WITH_ID, processId));
HttpRequest request = buildGetLogsRequest(processId, readinessHealthCheck);
var response = CloudFoundryClientFactory.HTTP_CLIENT.send(request, BodyHandlers.ofInputStream());
if (response.statusCode() / 100 != 2) {
var status = HttpStatus.valueOf(response.statusCode());
throw new CloudOperationException(status, status.getReasonPhrase(), parseBodyToString(response.body()));
}
return parseBodyToString(response.body());
} catch (IOException | InterruptedException e) {
throw new CloudException(e.getMessage(), e);
}
}

private URI getControllerUriFromUrl(URL controllerUrl) {
try {
return controllerUrl.toURI();
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}

private HttpRequest buildGetLogsRequest(String processId, Map<String, ReadinessHealthCheck> readinessHealthCheck) {
var requestBuilder = HttpRequest.newBuilder()
.method("PATCH", HttpRequest.BodyPublishers.ofString(JsonUtil.convertToJson(readinessHealthCheck)))
.uri(buildUpdateApplicationReadinessHealthCheck(processId))
.timeout(Duration.ofMinutes(5))
.header(HttpHeaders.AUTHORIZATION, oAuthClient.getAuthorizationHeaderValue())
.header("Content-type", "application/json");
return requestBuilder.build();
}

private String parseBodyToString(InputStream is) {
try (InputStream wrapped = is) {
return IOUtils.toString(wrapped, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new CloudException(e);
}
}

private URI buildUpdateApplicationReadinessHealthCheck(String processId) {
return UriComponentsBuilder.fromUri(controllerApi)
.pathSegment("v3", "processes", processId)
.build()
.toUri();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.sap.cloudfoundry.client.facade.domain;

public record ReadinessHealthCheck(String type, java.util.Map<String, Object> data) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ public interface Staging {
@Nullable
String getHealthCheckHttpEndpoint();

/**
* @return readiness health check interval
*/
@Nullable
Integer getReadinessHealthCheckInterval();

/**
* @return readiness health check timeout
*/
@Nullable
Integer getReadinessHealthCheckTimeout();

/**
* @return readiness health check type
*/
@Nullable
String getReadinessHealthCheckType();

/**
* @return readiness health check http endpoint
*/
@Nullable
String getReadinessHealthCheckHttpEndpoint();

/**
* @return boolean value to see if ssh is enabled
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.sap.cloudfoundry.client.facade.CloudCredentials;
import com.sap.cloudfoundry.client.facade.adapters.CloudFoundryClientFactory;
import com.sap.cloudfoundry.client.facade.adapters.ImmutableCloudFoundryClientFactory;
import com.sap.cloudfoundry.client.facade.adapters.ReadinessHealthCheckClient;
import com.sap.cloudfoundry.client.facade.domain.CloudSpace;
import com.sap.cloudfoundry.client.facade.oauth2.OAuthClient;
import com.sap.cloudfoundry.client.facade.util.RestUtil;
Expand Down Expand Up @@ -64,7 +65,7 @@ public CloudControllerRestClient createClient(URL controllerUrl, CloudCredential
OAuthClient oAuthClient, Map<String, String> requestTags) {
oAuthClient.init(credentials);
CloudFoundryClient delegate = getCloudFoundryClientFactory().createClient(controllerUrl, oAuthClient, requestTags);
return new CloudControllerRestClientImpl(delegate, target);
return new CloudControllerRestClientImpl(delegate, target, new ReadinessHealthCheckClient(oAuthClient, controllerUrl));
}

private OAuthClient createOAuthClient(URL controllerUrl, String origin) {
Expand Down
Loading

0 comments on commit 5f8cad3

Please sign in to comment.