diff --git a/README.md b/README.md index 975125f87..0a7bb3c5c 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,7 @@ The API has been broken up into sub APIs classes to make it easier to learn and   [DeployKeysApi](#deploykeysapi)
  [EventsApi](#eventsapi)
  [GroupApi](#groupapi)
+  [HealthCheckApi](#healthcheckapi)
  [IssuesApi](#issuesapi)
  [JobApi](#jobapi)
  [LabelsApi](#labelsapi)
@@ -191,6 +192,13 @@ List events = gitLabApi.getEventsApi().getAuthenticatedUserEvents(null, n List groups = gitLabApi.getGroupApi().getGroups(); ``` +#### HealthCheckApi +```java +// Get the liveness endpoint health check results. +// Assumes ip_whitelisted +LivenessHealthCheck healthCheck = gitLabApi.getHealthCheckApi().getLiveness(); +``` + #### IssuesApi ```java // Get a list of issues for the specified project ID diff --git a/src/main/java/org/gitlab4j/api/GitLabApi.java b/src/main/java/org/gitlab4j/api/GitLabApi.java index 7cbecd28e..ffecae366 100644 --- a/src/main/java/org/gitlab4j/api/GitLabApi.java +++ b/src/main/java/org/gitlab4j/api/GitLabApi.java @@ -51,6 +51,7 @@ public String getApiNamespace() { private CommitsApi commitsApi; private DeployKeysApi deployKeysApi; private GroupApi groupApi; + private HealthCheckApi healthCheckApi; private IssuesApi issuesApi; private MergeRequestApi mergeRequestApi; private MilestonesApi milestonesApi; @@ -878,6 +879,25 @@ public GroupApi getGroupApi() { return (groupApi); } + /** + * Gets the HealthCheckApi instance owned by this GitLabApi instance. The HealthCheckApi is used + * to perform all admin level gitlab health monitoring. + * + * @return the HealthCheckApi instance owned by this GitLabApi instance + */ + public HealthCheckApi getHealthCheckApi() { + + if (healthCheckApi == null) { + synchronized (this) { + if (healthCheckApi == null) { + healthCheckApi = new HealthCheckApi(this); + } + } + } + + return (healthCheckApi); + } + /** * Gets the IssuesApi instance owned by this GitLabApi instance. The IssuesApi is used * to perform all iossue related API calls. diff --git a/src/main/java/org/gitlab4j/api/GitLabApiClient.java b/src/main/java/org/gitlab4j/api/GitLabApiClient.java index a093543d1..c1231eefe 100755 --- a/src/main/java/org/gitlab4j/api/GitLabApiClient.java +++ b/src/main/java/org/gitlab4j/api/GitLabApiClient.java @@ -54,6 +54,7 @@ public class GitLabApiClient { private ClientConfig clientConfig; private Client apiClient; + private String baseUrl; private String hostUrl; private TokenType tokenType = TokenType.PRIVATE; private String authToken; @@ -91,7 +92,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp /** * Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified * server URL, private token, and secret token. - * + * * @param hostUrl the URL to the GitLab API server * @param privateToken the private token to authenticate with */ @@ -102,7 +103,7 @@ public GitLabApiClient(String hostUrl, String privateToken) { /** * Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified * server URL, private token, and secret token. - * + * * @param hostUrl the URL to the GitLab API server * @param tokenType the type of auth the token is for, PRIVATE or ACCESS * @param authToken the token to authenticate with @@ -141,7 +142,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp /** * Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified * server URL, private token, and secret token. - * + * * @param hostUrl the URL to the GitLab API server * @param privateToken the private token to authenticate with * @param secretToken use this token to validate received payloads @@ -153,7 +154,7 @@ public GitLabApiClient(String hostUrl, String privateToken, String secretToken) /** * Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified * server URL, private token, and secret token. - * + * * @param hostUrl the URL to the GitLab API server * @param tokenType the type of auth the token is for, PRIVATE or ACCESS * @param authToken the token to authenticate with @@ -166,7 +167,7 @@ public GitLabApiClient(String hostUrl, TokenType tokenType, String authToken, St /** * Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified * server URL and private token. - * + * * @param hostUrl the URL to the GitLab API server * @param privateToken the private token to authenticate with * @param secretToken use this token to validate received payloads @@ -177,7 +178,7 @@ public GitLabApiClient(String hostUrl, String privateToken, String secretToken, } /** - * Construct an instance to communicate with a GitLab API server using the specified GitLab API version, + * Construct an instance to communicate with a GitLab API server using the specified GitLab API version, * server URL and private token. * * @param apiVersion the ApiVersion specifying which version of the API to use @@ -191,7 +192,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, String privateToke } /** - * Construct an instance to communicate with a GitLab API server using the specified GitLab API version, + * Construct an instance to communicate with a GitLab API server using the specified GitLab API version, * server URL and private token. * * @param apiVersion the ApiVersion specifying which version of the API to use @@ -205,6 +206,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp // Remove the trailing "/" from the hostUrl if present this.hostUrl = (hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl); + this.baseUrl = this.hostUrl; if (ApiVersion.OAUTH2_CLIENT != apiVersion) { this.hostUrl += apiVersion.getApiNamespace(); } @@ -265,7 +267,6 @@ TokenType getTokenType() { /** * Set the ID of the user to sudo as. * - * @param sudoAsId the ID of the user to sudo as */ Integer getSudoAsId() { return (sudoAsId); @@ -282,29 +283,44 @@ void setSudoAsId(Integer sudoAsId) { /** * Construct a REST URL with the specified path arguments. - * + * * @param pathArgs variable list of arguments used to build the URI * @return a REST URL with the specified path arguments * @throws IOException if an error occurs while constructing the URL */ protected URL getApiUrl(Object... pathArgs) throws IOException { + String url = appendPathArgs(this.hostUrl, pathArgs); + return (new URL(url)); + } + + /** + * Construct a REST URL with the specified path arguments using + * Gitlab base url. + * + * @param pathArgs variable list of arguments used to build the URI + * @return a REST URL with the specified path arguments + * @throws IOException if an error occurs while constructing the URL + */ + protected URL getUrlWithBase(Object... pathArgs) throws IOException { + String url = appendPathArgs(this.baseUrl, pathArgs); + return (new URL(url)); + } - StringBuilder url = new StringBuilder(); - url.append(hostUrl); + private String appendPathArgs(String url, Object... pathArgs) { + StringBuilder urlBuilder = new StringBuilder(url); for (Object pathArg : pathArgs) { if (pathArg != null) { - url.append("/"); - url.append(pathArg.toString()); + urlBuilder.append("/"); + urlBuilder.append(pathArg.toString()); } } - - return (new URL(url.toString())); + return urlBuilder.toString(); } /** * Validates the secret token (X-GitLab-Token) header against the expected secret token, returns true if valid, * otherwise returns false. - * + * * @param response the Response instance sent from the GitLab server * @return true if the response's secret token is valid, otherwise returns false */ @@ -323,7 +339,7 @@ protected boolean validateSecretToken(Response response) { /** * Perform an HTTP GET call with the specified query parameters and path objects, returning * a ClientResponse instance with the data returned from the endpoint. - * + * * @param queryParams multivalue map of request parameters * @param pathArgs variable list of arguments used to build the URI * @return a ClientResponse instance with the data returned from the endpoint @@ -337,7 +353,7 @@ protected Response get(MultivaluedMap queryParams, Object... pat /** * Perform an HTTP GET call with the specified query parameters and URL, returning * a ClientResponse instance with the data returned from the endpoint. - * + * * @param queryParams multivalue map of request parameters * @param url the fully formed path to the GitLab API endpoint * @return a ClientResponse instance with the data returned from the endpoint @@ -349,7 +365,7 @@ protected Response get(MultivaluedMap queryParams, URL url) { /** * Perform an HTTP GET call with the specified query parameters and path objects, returning * a ClientResponse instance with the data returned from the endpoint. - * + * * @param queryParams multivalue map of request parameters * @param accepts if non-empty will set the Accepts header to this value * @param pathArgs variable list of arguments used to build the URI @@ -364,7 +380,7 @@ protected Response getWithAccepts(MultivaluedMap queryParams, St /** * Perform an HTTP GET call with the specified query parameters and URL, returning * a ClientResponse instance with the data returned from the endpoint. - * + * * @param queryParams multivalue map of request parameters * @param url the fully formed path to the GitLab API endpoint * @param accepts if non-empty will set the Accepts header to this value @@ -377,7 +393,7 @@ protected Response getWithAccepts(MultivaluedMap queryParams, UR /** * Perform an HTTP POST call with the specified form data and path objects, returning * a ClientResponse instance with the data returned from the endpoint. - * + * * @param formData the Form containing the name/value pairs * @param pathArgs variable list of arguments used to build the URI * @return a ClientResponse instance with the data returned from the endpoint @@ -391,7 +407,7 @@ protected Response post(Form formData, Object... pathArgs) throws IOException { /** * Perform an HTTP POST call with the specified form data and path objects, returning * a ClientResponse instance with the data returned from the endpoint. - * + * * @param queryParams multivalue map of request parameters * @param pathArgs variable list of arguments used to build the URI * @return a Response instance with the data returned from the endpoint @@ -405,7 +421,7 @@ protected Response post(MultivaluedMap queryParams, Object... pa /** * Perform an HTTP POST call with the specified form data and URL, returning * a ClientResponse instance with the data returned from the endpoint. - * + * * @param formData the Form containing the name/value pairs * @param url the fully formed path to the GitLab API endpoint * @return a ClientResponse instance with the data returned from the endpoint @@ -501,7 +517,7 @@ protected Response upload(String name, File fileToUpload, String mediaTypeString /** * Perform an HTTP PUT call with the specified form data and path objects, returning * a ClientResponse instance with the data returned from the endpoint. - * + * * @param queryParams multivalue map of request parameters * @param pathArgs variable list of arguments used to build the URI * @return a ClientResponse instance with the data returned from the endpoint @@ -561,7 +577,7 @@ protected Response put(Form formData, URL url) { /** * Perform an HTTP DELETE call with the specified form data and path objects, returning * a Response instance with the data returned from the endpoint. - * + * * @param queryParams multivalue map of request parameters * @param pathArgs variable list of arguments used to build the URI * @return a Response instance with the data returned from the endpoint @@ -574,7 +590,7 @@ protected Response delete(MultivaluedMap queryParams, Object... /** * Perform an HTTP DELETE call with the specified form data and URL, returning * a Response instance with the data returned from the endpoint. - * + * * @param queryParams multivalue map of request parameters * @param url the fully formed path to the GitLab API endpoint * @return a Response instance with the data returned from the endpoint diff --git a/src/main/java/org/gitlab4j/api/HealthCheckApi.java b/src/main/java/org/gitlab4j/api/HealthCheckApi.java new file mode 100644 index 000000000..251e020b7 --- /dev/null +++ b/src/main/java/org/gitlab4j/api/HealthCheckApi.java @@ -0,0 +1,89 @@ +package org.gitlab4j.api; + +import org.gitlab4j.api.models.HealthCheckInfo; + +import javax.ws.rs.core.Response; +import java.io.IOException; +import java.net.URL; + +public class HealthCheckApi extends AbstractApi { + public HealthCheckApi(GitLabApi gitLabApi) { + super(gitLabApi); + } + + /** + * Get Health Checks from the liveness endpoint. + * + * Requires ip_whitelist + * https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html + * + * GET /-/liveness + * + * @return HealthCheckInfo instance + * @throws GitLabApiException if any exception occurs + */ + public HealthCheckInfo getLiveness() throws GitLabApiException, IOException { + URL livenessUrl = getApiClient().getUrlWithBase("-", "liveness"); + Response response = get(Response.Status.OK, null, livenessUrl); + return (response.readEntity(HealthCheckInfo.class)); + } + + /** + * Get Health Checks from the liveness endpoint. + * + * Requires ip_whitelist + * https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html + * + * GET /-/liveness + * + * @param token Health Status token + * @return HealthCheckInfo instance + * @throws GitLabApiException if any exception occurs + * @deprecated + */ + public HealthCheckInfo getLiveness(String token) throws GitLabApiException, IOException { + URL livenessUrl = getApiClient().getUrlWithBase("-", "liveness"); + GitLabApiForm formData = new GitLabApiForm() + .withParam("token", token, false); + Response response = get(Response.Status.OK, formData.asMap(), livenessUrl); + return (response.readEntity(HealthCheckInfo.class)); + } + + /** + * Get Health Checks from the readiness endpoint. + * + * Requires ip_whitelist + * https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html + * + * GET /-/readiness + * + * @return HealthCheckInfo instance + * @throws GitLabApiException if any exception occurs + */ + public HealthCheckInfo getReadiness() throws GitLabApiException, IOException { + URL readinessUrl = getApiClient().getUrlWithBase("-", "readiness"); + Response response = get(Response.Status.OK, null, readinessUrl); + return (response.readEntity(HealthCheckInfo.class)); + } + + /** + * Get Health Checks from the readiness endpoint. + * + * Requires ip_whitelist + * https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html + * + * GET /-/readiness + * + * @param token Health Status token + * @return HealthCheckInfo instance + * @throws GitLabApiException if any exception occurs + * @deprecated + */ + public HealthCheckInfo getReadiness(String token) throws GitLabApiException, IOException { + URL readinessUrl = getApiClient().getUrlWithBase("-", "readiness"); + GitLabApiForm formData = new GitLabApiForm() + .withParam("token", token, false); + Response response = get(Response.Status.OK, formData.asMap(), readinessUrl); + return (response.readEntity(HealthCheckInfo.class)); + } +} diff --git a/src/main/java/org/gitlab4j/api/models/HealthCheckInfo.java b/src/main/java/org/gitlab4j/api/models/HealthCheckInfo.java new file mode 100644 index 000000000..a83f2bfae --- /dev/null +++ b/src/main/java/org/gitlab4j/api/models/HealthCheckInfo.java @@ -0,0 +1,73 @@ +package org.gitlab4j.api.models; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class HealthCheckInfo { + private HealthCheckItem dbCheck; + private HealthCheckItem redisCheck; + private HealthCheckItem cacheCheck; + private HealthCheckItem queuesCheck; + private HealthCheckItem sharedStateCheck; + private HealthCheckItem fsShardsCheck; + private HealthCheckItem gitalyCheck; + + public HealthCheckItem getDbCheck() { + return this.dbCheck; + } + + public void setDbCheck(HealthCheckItem dbCheck) { + this.dbCheck = dbCheck; + } + + public HealthCheckItem getRedisCheck() { + return this.redisCheck; + } + + public void setRedisCheck(HealthCheckItem redisCheck) { + this.redisCheck = redisCheck; + } + + public HealthCheckItem getCacheCheck() { + return this.cacheCheck; + } + + public void setCacheCheck(HealthCheckItem cacheCheck) { + this.cacheCheck = cacheCheck; + } + + public HealthCheckItem getQueuesCheck() { + return this.queuesCheck; + } + + public void setQueuesCheck(HealthCheckItem queuesCheck) { + this.queuesCheck = queuesCheck; + } + + public HealthCheckItem getSharedStateCheck() { + return this.sharedStateCheck; + } + + public void setSharedStateCheck(HealthCheckItem sharedStateCheck) { + this.sharedStateCheck = sharedStateCheck; + } + + public HealthCheckItem getFsShardsCheck() { + return this.fsShardsCheck; + } + + public void setFsShardsCheck(HealthCheckItem fsShardsCheck) { + this.fsShardsCheck = fsShardsCheck; + } + + public HealthCheckItem getGitalyCheck() { + return this.gitalyCheck; + } + + public void setGitalyCheck(HealthCheckItem gitalyCheck) { + this.gitalyCheck = gitalyCheck; + } +} diff --git a/src/main/java/org/gitlab4j/api/models/HealthCheckItem.java b/src/main/java/org/gitlab4j/api/models/HealthCheckItem.java new file mode 100644 index 000000000..d855948ef --- /dev/null +++ b/src/main/java/org/gitlab4j/api/models/HealthCheckItem.java @@ -0,0 +1,37 @@ +package org.gitlab4j.api.models; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import java.util.Map; + +@XmlAccessorType(XmlAccessType.FIELD) +public class HealthCheckItem { + private HealthCheckStatus status; + private Map labels; + private String message; + + public HealthCheckStatus getStatus() { + return this.status; + } + + public void setStatus(HealthCheckStatus status) { + this.status = status; + } + + public Map getLabels() { + return labels; + } + + public void setLabels(Map labels) { + this.labels = labels; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + +} diff --git a/src/main/java/org/gitlab4j/api/models/HealthCheckStatus.java b/src/main/java/org/gitlab4j/api/models/HealthCheckStatus.java new file mode 100644 index 000000000..5112bcd4b --- /dev/null +++ b/src/main/java/org/gitlab4j/api/models/HealthCheckStatus.java @@ -0,0 +1,26 @@ +package org.gitlab4j.api.models; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; +import org.gitlab4j.api.utils.JacksonJsonEnumHelper; + +public enum HealthCheckStatus { + OK, FAILED; + + private static JacksonJsonEnumHelper enumHelper = new JacksonJsonEnumHelper<>(HealthCheckStatus.class); + + @JsonCreator + public static HealthCheckStatus forValue(String value) { + return enumHelper.forValue(value); + } + + @JsonValue + public String toValue() { + return enumHelper.toString(this); + } + + @Override + public String toString() { + return enumHelper.toString(this); + } +}