Skip to content

Commit f62ab0d

Browse files
committed
add Health Status Api support
1 parent 8647a1d commit f62ab0d

File tree

9 files changed

+348
-26
lines changed

9 files changed

+348
-26
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ The API has been broken up into sub APIs classes to make it easier to learn and
139139
&nbsp;&nbsp;[DeployKeysApi](#deploykeysapi)<br/>
140140
&nbsp;&nbsp;[EventsApi](#eventsapi)<br/>
141141
&nbsp;&nbsp;[GroupApi](#groupapi)<br/>
142+
&nbsp;&nbsp;[HealthCheckApi](#healthcheckapi)<br/>
142143
&nbsp;&nbsp;[IssuesApi](#issuesapi)<br/>
143144
&nbsp;&nbsp;[JobApi](#jobapi)<br/>
144145
&nbsp;&nbsp;[LabelsApi](#labelsapi)<br/>
@@ -191,6 +192,13 @@ List<Event> events = gitLabApi.getEventsApi().getAuthenticatedUserEvents(null, n
191192
List<Group> groups = gitLabApi.getGroupApi().getGroups();
192193
```
193194

195+
#### HealthCheckApi
196+
```java
197+
// Get the liveness endpoint health check results.
198+
// Assumes ip_whitelisted
199+
LivenessHealthCheck healthCheck = gitLabApi.getHealthCheckApi().getLiveness();
200+
```
201+
194202
#### IssuesApi
195203
```java
196204
// Get a list of issues for the specified project ID

src/main/java/org/gitlab4j/api/GitLabApi.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public String getApiNamespace() {
4848
private CommitsApi commitsApi;
4949
private DeployKeysApi deployKeysApi;
5050
private GroupApi groupApi;
51+
private HealthCheckApi healthCheckApi;
5152
private IssuesApi issuesApi;
5253
private MergeRequestApi mergeRequestApi;
5354
private MilestonesApi milestonesApi;
@@ -866,6 +867,25 @@ public GroupApi getGroupApi() {
866867
return (groupApi);
867868
}
868869

870+
/**
871+
* Gets the HealthCheckApi instance owned by this GitLabApi instance. The HealthCheckApi is used
872+
* to perform all admin level gitlab health monitoring.
873+
*
874+
* @return the HealthCheckApi instance owned by this GitLabApi instance
875+
*/
876+
public HealthCheckApi getHealthCheckApi() {
877+
878+
if (healthCheckApi == null) {
879+
synchronized (this) {
880+
if (healthCheckApi == null) {
881+
healthCheckApi = new HealthCheckApi(this);
882+
}
883+
}
884+
}
885+
886+
return (healthCheckApi);
887+
}
888+
869889
/**
870890
* Gets the IssuesApi instance owned by this GitLabApi instance. The IssuesApi is used
871891
* to perform all iossue related API calls.

src/main/java/org/gitlab4j/api/GitLabApiClient.java

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class GitLabApiClient {
4646

4747
private ClientConfig clientConfig;
4848
private Client apiClient;
49+
private String baseUrl;
4950
private String hostUrl;
5051
private TokenType tokenType = TokenType.PRIVATE;
5152
private String authToken;
@@ -83,7 +84,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
8384
/**
8485
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
8586
* server URL, private token, and secret token.
86-
*
87+
*
8788
* @param hostUrl the URL to the GitLab API server
8889
* @param privateToken the private token to authenticate with
8990
*/
@@ -94,7 +95,7 @@ public GitLabApiClient(String hostUrl, String privateToken) {
9495
/**
9596
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
9697
* server URL, private token, and secret token.
97-
*
98+
*
9899
* @param hostUrl the URL to the GitLab API server
99100
* @param tokenType the type of auth the token is for, PRIVATE or ACCESS
100101
* @param authToken the token to authenticate with
@@ -133,7 +134,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
133134
/**
134135
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
135136
* server URL, private token, and secret token.
136-
*
137+
*
137138
* @param hostUrl the URL to the GitLab API server
138139
* @param privateToken the private token to authenticate with
139140
* @param secretToken use this token to validate received payloads
@@ -145,7 +146,7 @@ public GitLabApiClient(String hostUrl, String privateToken, String secretToken)
145146
/**
146147
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
147148
* server URL, private token, and secret token.
148-
*
149+
*
149150
* @param hostUrl the URL to the GitLab API server
150151
* @param tokenType the type of auth the token is for, PRIVATE or ACCESS
151152
* @param authToken the token to authenticate with
@@ -158,7 +159,7 @@ public GitLabApiClient(String hostUrl, TokenType tokenType, String authToken, St
158159
/**
159160
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
160161
* server URL and private token.
161-
*
162+
*
162163
* @param hostUrl the URL to the GitLab API server
163164
* @param privateToken the private token to authenticate with
164165
* @param secretToken use this token to validate received payloads
@@ -169,7 +170,7 @@ public GitLabApiClient(String hostUrl, String privateToken, String secretToken,
169170
}
170171

171172
/**
172-
* Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
173+
* Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
173174
* server URL and private token.
174175
*
175176
* @param apiVersion the ApiVersion specifying which version of the API to use
@@ -183,7 +184,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, String privateToke
183184
}
184185

185186
/**
186-
* Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
187+
* Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
187188
* server URL and private token.
188189
*
189190
* @param apiVersion the ApiVersion specifying which version of the API to use
@@ -197,6 +198,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
197198

198199
// Remove the trailing "/" from the hostUrl if present
199200
this.hostUrl = (hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl);
201+
this.baseUrl = this.hostUrl;
200202
if (ApiVersion.OAUTH2_CLIENT != apiVersion) {
201203
this.hostUrl += apiVersion.getApiNamespace();
202204
}
@@ -256,7 +258,6 @@ TokenType getTokenType() {
256258
/**
257259
* Set the ID of the user to sudo as.
258260
*
259-
* @param sudoAsId the ID of the user to sudo as
260261
*/
261262
Integer getSudoAsId() {
262263
return (sudoAsId);
@@ -273,29 +274,44 @@ void setSudoAsId(Integer sudoAsId) {
273274

274275
/**
275276
* Construct a REST URL with the specified path arguments.
276-
*
277+
*
277278
* @param pathArgs variable list of arguments used to build the URI
278279
* @return a REST URL with the specified path arguments
279280
* @throws IOException if an error occurs while constructing the URL
280281
*/
281282
protected URL getApiUrl(Object... pathArgs) throws IOException {
283+
String url = appendPathArgs(this.hostUrl, pathArgs);
284+
return (new URL(url));
285+
}
286+
287+
/**
288+
* Construct a REST URL with the specified path arguments using
289+
* Gitlab base url.
290+
*
291+
* @param pathArgs variable list of arguments used to build the URI
292+
* @return a REST URL with the specified path arguments
293+
* @throws IOException if an error occurs while constructing the URL
294+
*/
295+
protected URL getUrlWithBase(Object... pathArgs) throws IOException {
296+
String url = appendPathArgs(this.baseUrl, pathArgs);
297+
return (new URL(url));
298+
}
282299

283-
StringBuilder url = new StringBuilder();
284-
url.append(hostUrl);
300+
private String appendPathArgs(String url, Object... pathArgs) {
301+
StringBuilder urlBuilder = new StringBuilder(url);
285302
for (Object pathArg : pathArgs) {
286303
if (pathArg != null) {
287-
url.append("/");
288-
url.append(pathArg.toString());
304+
urlBuilder.append("/");
305+
urlBuilder.append(pathArg.toString());
289306
}
290307
}
291-
292-
return (new URL(url.toString()));
308+
return urlBuilder.toString();
293309
}
294310

295311
/**
296312
* Validates the secret token (X-GitLab-Token) header against the expected secret token, returns true if valid,
297313
* otherwise returns false.
298-
*
314+
*
299315
* @param response the Response instance sent from the GitLab server
300316
* @return true if the response's secret token is valid, otherwise returns false
301317
*/
@@ -314,7 +330,7 @@ protected boolean validateSecretToken(Response response) {
314330
/**
315331
* Perform an HTTP GET call with the specified query parameters and path objects, returning
316332
* a ClientResponse instance with the data returned from the endpoint.
317-
*
333+
*
318334
* @param queryParams multivalue map of request parameters
319335
* @param pathArgs variable list of arguments used to build the URI
320336
* @return a ClientResponse instance with the data returned from the endpoint
@@ -328,7 +344,7 @@ protected Response get(MultivaluedMap<String, String> queryParams, Object... pat
328344
/**
329345
* Perform an HTTP GET call with the specified query parameters and URL, returning
330346
* a ClientResponse instance with the data returned from the endpoint.
331-
*
347+
*
332348
* @param queryParams multivalue map of request parameters
333349
* @param url the fully formed path to the GitLab API endpoint
334350
* @return a ClientResponse instance with the data returned from the endpoint
@@ -340,7 +356,7 @@ protected Response get(MultivaluedMap<String, String> queryParams, URL url) {
340356
/**
341357
* Perform an HTTP GET call with the specified query parameters and path objects, returning
342358
* a ClientResponse instance with the data returned from the endpoint.
343-
*
359+
*
344360
* @param queryParams multivalue map of request parameters
345361
* @param accepts if non-empty will set the Accepts header to this value
346362
* @param pathArgs variable list of arguments used to build the URI
@@ -355,7 +371,7 @@ protected Response getWithAccepts(MultivaluedMap<String, String> queryParams, St
355371
/**
356372
* Perform an HTTP GET call with the specified query parameters and URL, returning
357373
* a ClientResponse instance with the data returned from the endpoint.
358-
*
374+
*
359375
* @param queryParams multivalue map of request parameters
360376
* @param url the fully formed path to the GitLab API endpoint
361377
* @param accepts if non-empty will set the Accepts header to this value
@@ -368,7 +384,7 @@ protected Response getWithAccepts(MultivaluedMap<String, String> queryParams, UR
368384
/**
369385
* Perform an HTTP POST call with the specified form data and path objects, returning
370386
* a ClientResponse instance with the data returned from the endpoint.
371-
*
387+
*
372388
* @param formData the Form containing the name/value pairs
373389
* @param pathArgs variable list of arguments used to build the URI
374390
* @return a ClientResponse instance with the data returned from the endpoint
@@ -382,7 +398,7 @@ protected Response post(Form formData, Object... pathArgs) throws IOException {
382398
/**
383399
* Perform an HTTP POST call with the specified form data and path objects, returning
384400
* a ClientResponse instance with the data returned from the endpoint.
385-
*
401+
*
386402
* @param queryParams multivalue map of request parameters
387403
* @param pathArgs variable list of arguments used to build the URI
388404
* @return a Response instance with the data returned from the endpoint
@@ -396,7 +412,7 @@ protected Response post(MultivaluedMap<String, String> queryParams, Object... pa
396412
/**
397413
* Perform an HTTP POST call with the specified form data and URL, returning
398414
* a ClientResponse instance with the data returned from the endpoint.
399-
*
415+
*
400416
* @param formData the Form containing the name/value pairs
401417
* @param url the fully formed path to the GitLab API endpoint
402418
* @return a ClientResponse instance with the data returned from the endpoint
@@ -453,7 +469,7 @@ protected Response post(StreamingOutput stream, String mediaType, Object... path
453469
/**
454470
* Perform an HTTP PUT call with the specified form data and path objects, returning
455471
* a ClientResponse instance with the data returned from the endpoint.
456-
*
472+
*
457473
* @param queryParams multivalue map of request parameters
458474
* @param pathArgs variable list of arguments used to build the URI
459475
* @return a ClientResponse instance with the data returned from the endpoint
@@ -513,7 +529,7 @@ protected Response put(Form formData, URL url) {
513529
/**
514530
* Perform an HTTP DELETE call with the specified form data and path objects, returning
515531
* a Response instance with the data returned from the endpoint.
516-
*
532+
*
517533
* @param queryParams multivalue map of request parameters
518534
* @param pathArgs variable list of arguments used to build the URI
519535
* @return a Response instance with the data returned from the endpoint
@@ -526,7 +542,7 @@ protected Response delete(MultivaluedMap<String, String> queryParams, Object...
526542
/**
527543
* Perform an HTTP DELETE call with the specified form data and URL, returning
528544
* a Response instance with the data returned from the endpoint.
529-
*
545+
*
530546
* @param queryParams multivalue map of request parameters
531547
* @param url the fully formed path to the GitLab API endpoint
532548
* @return a Response instance with the data returned from the endpoint
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.gitlab4j.api;
2+
3+
import org.gitlab4j.api.models.LivenessHealthCheck;
4+
import org.gitlab4j.api.models.ReadinessHealthCheck;
5+
6+
import javax.ws.rs.core.Response;
7+
import java.io.IOException;
8+
import java.net.URL;
9+
10+
public class HealthCheckApi extends AbstractApi {
11+
public HealthCheckApi(GitLabApi gitLabApi) {
12+
super(gitLabApi);
13+
}
14+
15+
/**
16+
* Get Health Checks from the liveness endpoint.
17+
* Requires ip_whitelist
18+
*
19+
* GET /-/liveness
20+
*
21+
* @return LivenessHealthCheck instance
22+
* @throws GitLabApiException if any exception occurs
23+
*/
24+
public LivenessHealthCheck getLiveness() throws GitLabApiException, IOException {
25+
URL livenessUrl = getApiClient().getUrlWithBase("-", "liveness");
26+
Response response = get(Response.Status.OK, null, livenessUrl);
27+
return (response.readEntity(LivenessHealthCheck.class));
28+
}
29+
30+
/**
31+
* Get Health Checks from the liveness endpoint.
32+
*
33+
* GET /-/liveness
34+
*
35+
* @param token Health Status token
36+
* @return LivenessHealthCheck instance
37+
* @throws GitLabApiException if any exception occurs
38+
* @deprecated
39+
*/
40+
public LivenessHealthCheck getLiveness(String token) throws GitLabApiException, IOException {
41+
URL livenessUrl = getApiClient().getUrlWithBase("-", "liveness");
42+
GitLabApiForm formData = new GitLabApiForm()
43+
.withParam("token", token, false);
44+
Response response = get(Response.Status.OK, formData.asMap(), livenessUrl);
45+
return (response.readEntity(LivenessHealthCheck.class));
46+
}
47+
48+
/**
49+
* Get Health Checks from the readiness endpoint.
50+
* Requires ip_whitelist
51+
*
52+
* GET /-/readiness
53+
*
54+
* @return ReadinessHealthCheck instance
55+
* @throws GitLabApiException if any exception occurs
56+
*/
57+
public ReadinessHealthCheck getReadiness() throws GitLabApiException, IOException {
58+
URL readinessUrl = getApiClient().getUrlWithBase("-", "readiness");
59+
Response response = get(Response.Status.OK, null, readinessUrl);
60+
return (response.readEntity(ReadinessHealthCheck.class));
61+
}
62+
63+
/**
64+
* Get Health Checks from the readiness endpoint.
65+
*
66+
* GET /-/readiness
67+
*
68+
* @param token Health Status token
69+
* @return LivenessHealthCheck instance
70+
* @throws GitLabApiException if any exception occurs
71+
* @deprecated
72+
*/
73+
public ReadinessHealthCheck getReadiness(String token) throws GitLabApiException, IOException {
74+
URL readinessUrl = getApiClient().getUrlWithBase("-", "readiness");
75+
GitLabApiForm formData = new GitLabApiForm()
76+
.withParam("token", token, false);
77+
Response response = get(Response.Status.OK, formData.asMap(), readinessUrl);
78+
return (response.readEntity(ReadinessHealthCheck.class));
79+
}
80+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.gitlab4j.api.models;
2+
3+
import javax.xml.bind.annotation.XmlAccessType;
4+
import javax.xml.bind.annotation.XmlAccessorType;
5+
6+
@XmlAccessorType(XmlAccessType.FIELD)
7+
public class HealthCheckStatus {
8+
private String status;
9+
10+
public String getStatus() {
11+
return this.status;
12+
}
13+
14+
public void setStatus(String status) {
15+
this.status = status;
16+
}
17+
}

0 commit comments

Comments
 (0)