Skip to content

Commit 900c1a9

Browse files
lamdavgmessner
authored andcommitted
Health Check Api (#159)
* Added Health Check API support
1 parent 4c8b29a commit 900c1a9

File tree

7 files changed

+295
-26
lines changed

7 files changed

+295
-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
@@ -51,6 +51,7 @@ public String getApiNamespace() {
5151
private CommitsApi commitsApi;
5252
private DeployKeysApi deployKeysApi;
5353
private GroupApi groupApi;
54+
private HealthCheckApi healthCheckApi;
5455
private IssuesApi issuesApi;
5556
private MergeRequestApi mergeRequestApi;
5657
private MilestonesApi milestonesApi;
@@ -878,6 +879,25 @@ public GroupApi getGroupApi() {
878879
return (groupApi);
879880
}
880881

882+
/**
883+
* Gets the HealthCheckApi instance owned by this GitLabApi instance. The HealthCheckApi is used
884+
* to perform all admin level gitlab health monitoring.
885+
*
886+
* @return the HealthCheckApi instance owned by this GitLabApi instance
887+
*/
888+
public HealthCheckApi getHealthCheckApi() {
889+
890+
if (healthCheckApi == null) {
891+
synchronized (this) {
892+
if (healthCheckApi == null) {
893+
healthCheckApi = new HealthCheckApi(this);
894+
}
895+
}
896+
}
897+
898+
return (healthCheckApi);
899+
}
900+
881901
/**
882902
* Gets the IssuesApi instance owned by this GitLabApi instance. The IssuesApi is used
883903
* 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
@@ -54,6 +54,7 @@ public class GitLabApiClient {
5454

5555
private ClientConfig clientConfig;
5656
private Client apiClient;
57+
private String baseUrl;
5758
private String hostUrl;
5859
private TokenType tokenType = TokenType.PRIVATE;
5960
private String authToken;
@@ -91,7 +92,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
9192
/**
9293
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
9394
* server URL, private token, and secret token.
94-
*
95+
*
9596
* @param hostUrl the URL to the GitLab API server
9697
* @param privateToken the private token to authenticate with
9798
*/
@@ -102,7 +103,7 @@ public GitLabApiClient(String hostUrl, String privateToken) {
102103
/**
103104
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
104105
* server URL, private token, and secret token.
105-
*
106+
*
106107
* @param hostUrl the URL to the GitLab API server
107108
* @param tokenType the type of auth the token is for, PRIVATE or ACCESS
108109
* @param authToken the token to authenticate with
@@ -141,7 +142,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
141142
/**
142143
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
143144
* server URL, private token, and secret token.
144-
*
145+
*
145146
* @param hostUrl the URL to the GitLab API server
146147
* @param privateToken the private token to authenticate with
147148
* @param secretToken use this token to validate received payloads
@@ -153,7 +154,7 @@ public GitLabApiClient(String hostUrl, String privateToken, String secretToken)
153154
/**
154155
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
155156
* server URL, private token, and secret token.
156-
*
157+
*
157158
* @param hostUrl the URL to the GitLab API server
158159
* @param tokenType the type of auth the token is for, PRIVATE or ACCESS
159160
* @param authToken the token to authenticate with
@@ -166,7 +167,7 @@ public GitLabApiClient(String hostUrl, TokenType tokenType, String authToken, St
166167
/**
167168
* Construct an instance to communicate with a GitLab API server using GitLab API version 4, and the specified
168169
* server URL and private token.
169-
*
170+
*
170171
* @param hostUrl the URL to the GitLab API server
171172
* @param privateToken the private token to authenticate with
172173
* @param secretToken use this token to validate received payloads
@@ -177,7 +178,7 @@ public GitLabApiClient(String hostUrl, String privateToken, String secretToken,
177178
}
178179

179180
/**
180-
* Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
181+
* Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
181182
* server URL and private token.
182183
*
183184
* @param apiVersion the ApiVersion specifying which version of the API to use
@@ -191,7 +192,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, String privateToke
191192
}
192193

193194
/**
194-
* Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
195+
* Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
195196
* server URL and private token.
196197
*
197198
* @param apiVersion the ApiVersion specifying which version of the API to use
@@ -205,6 +206,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
205206

206207
// Remove the trailing "/" from the hostUrl if present
207208
this.hostUrl = (hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl);
209+
this.baseUrl = this.hostUrl;
208210
if (ApiVersion.OAUTH2_CLIENT != apiVersion) {
209211
this.hostUrl += apiVersion.getApiNamespace();
210212
}
@@ -265,7 +267,6 @@ TokenType getTokenType() {
265267
/**
266268
* Set the ID of the user to sudo as.
267269
*
268-
* @param sudoAsId the ID of the user to sudo as
269270
*/
270271
Integer getSudoAsId() {
271272
return (sudoAsId);
@@ -282,29 +283,44 @@ void setSudoAsId(Integer sudoAsId) {
282283

283284
/**
284285
* Construct a REST URL with the specified path arguments.
285-
*
286+
*
286287
* @param pathArgs variable list of arguments used to build the URI
287288
* @return a REST URL with the specified path arguments
288289
* @throws IOException if an error occurs while constructing the URL
289290
*/
290291
protected URL getApiUrl(Object... pathArgs) throws IOException {
292+
String url = appendPathArgs(this.hostUrl, pathArgs);
293+
return (new URL(url));
294+
}
295+
296+
/**
297+
* Construct a REST URL with the specified path arguments using
298+
* Gitlab base url.
299+
*
300+
* @param pathArgs variable list of arguments used to build the URI
301+
* @return a REST URL with the specified path arguments
302+
* @throws IOException if an error occurs while constructing the URL
303+
*/
304+
protected URL getUrlWithBase(Object... pathArgs) throws IOException {
305+
String url = appendPathArgs(this.baseUrl, pathArgs);
306+
return (new URL(url));
307+
}
291308

292-
StringBuilder url = new StringBuilder();
293-
url.append(hostUrl);
309+
private String appendPathArgs(String url, Object... pathArgs) {
310+
StringBuilder urlBuilder = new StringBuilder(url);
294311
for (Object pathArg : pathArgs) {
295312
if (pathArg != null) {
296-
url.append("/");
297-
url.append(pathArg.toString());
313+
urlBuilder.append("/");
314+
urlBuilder.append(pathArg.toString());
298315
}
299316
}
300-
301-
return (new URL(url.toString()));
317+
return urlBuilder.toString();
302318
}
303319

304320
/**
305321
* Validates the secret token (X-GitLab-Token) header against the expected secret token, returns true if valid,
306322
* otherwise returns false.
307-
*
323+
*
308324
* @param response the Response instance sent from the GitLab server
309325
* @return true if the response's secret token is valid, otherwise returns false
310326
*/
@@ -323,7 +339,7 @@ protected boolean validateSecretToken(Response response) {
323339
/**
324340
* Perform an HTTP GET call with the specified query parameters and path objects, returning
325341
* a ClientResponse instance with the data returned from the endpoint.
326-
*
342+
*
327343
* @param queryParams multivalue map of request parameters
328344
* @param pathArgs variable list of arguments used to build the URI
329345
* @return a ClientResponse instance with the data returned from the endpoint
@@ -337,7 +353,7 @@ protected Response get(MultivaluedMap<String, String> queryParams, Object... pat
337353
/**
338354
* Perform an HTTP GET call with the specified query parameters and URL, returning
339355
* a ClientResponse instance with the data returned from the endpoint.
340-
*
356+
*
341357
* @param queryParams multivalue map of request parameters
342358
* @param url the fully formed path to the GitLab API endpoint
343359
* @return a ClientResponse instance with the data returned from the endpoint
@@ -349,7 +365,7 @@ protected Response get(MultivaluedMap<String, String> queryParams, URL url) {
349365
/**
350366
* Perform an HTTP GET call with the specified query parameters and path objects, returning
351367
* a ClientResponse instance with the data returned from the endpoint.
352-
*
368+
*
353369
* @param queryParams multivalue map of request parameters
354370
* @param accepts if non-empty will set the Accepts header to this value
355371
* @param pathArgs variable list of arguments used to build the URI
@@ -364,7 +380,7 @@ protected Response getWithAccepts(MultivaluedMap<String, String> queryParams, St
364380
/**
365381
* Perform an HTTP GET call with the specified query parameters and URL, returning
366382
* a ClientResponse instance with the data returned from the endpoint.
367-
*
383+
*
368384
* @param queryParams multivalue map of request parameters
369385
* @param url the fully formed path to the GitLab API endpoint
370386
* @param accepts if non-empty will set the Accepts header to this value
@@ -377,7 +393,7 @@ protected Response getWithAccepts(MultivaluedMap<String, String> queryParams, UR
377393
/**
378394
* Perform an HTTP POST call with the specified form data and path objects, returning
379395
* a ClientResponse instance with the data returned from the endpoint.
380-
*
396+
*
381397
* @param formData the Form containing the name/value pairs
382398
* @param pathArgs variable list of arguments used to build the URI
383399
* @return a ClientResponse instance with the data returned from the endpoint
@@ -391,7 +407,7 @@ protected Response post(Form formData, Object... pathArgs) throws IOException {
391407
/**
392408
* Perform an HTTP POST call with the specified form data and path objects, returning
393409
* a ClientResponse instance with the data returned from the endpoint.
394-
*
410+
*
395411
* @param queryParams multivalue map of request parameters
396412
* @param pathArgs variable list of arguments used to build the URI
397413
* @return a Response instance with the data returned from the endpoint
@@ -405,7 +421,7 @@ protected Response post(MultivaluedMap<String, String> queryParams, Object... pa
405421
/**
406422
* Perform an HTTP POST call with the specified form data and URL, returning
407423
* a ClientResponse instance with the data returned from the endpoint.
408-
*
424+
*
409425
* @param formData the Form containing the name/value pairs
410426
* @param url the fully formed path to the GitLab API endpoint
411427
* @return a ClientResponse instance with the data returned from the endpoint
@@ -501,7 +517,7 @@ protected Response upload(String name, File fileToUpload, String mediaTypeString
501517
/**
502518
* Perform an HTTP PUT call with the specified form data and path objects, returning
503519
* a ClientResponse instance with the data returned from the endpoint.
504-
*
520+
*
505521
* @param queryParams multivalue map of request parameters
506522
* @param pathArgs variable list of arguments used to build the URI
507523
* @return a ClientResponse instance with the data returned from the endpoint
@@ -561,7 +577,7 @@ protected Response put(Form formData, URL url) {
561577
/**
562578
* Perform an HTTP DELETE call with the specified form data and path objects, returning
563579
* a Response instance with the data returned from the endpoint.
564-
*
580+
*
565581
* @param queryParams multivalue map of request parameters
566582
* @param pathArgs variable list of arguments used to build the URI
567583
* @return a Response instance with the data returned from the endpoint
@@ -574,7 +590,7 @@ protected Response delete(MultivaluedMap<String, String> queryParams, Object...
574590
/**
575591
* Perform an HTTP DELETE call with the specified form data and URL, returning
576592
* a Response instance with the data returned from the endpoint.
577-
*
593+
*
578594
* @param queryParams multivalue map of request parameters
579595
* @param url the fully formed path to the GitLab API endpoint
580596
* @return a Response instance with the data returned from the endpoint
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.gitlab4j.api;
2+
3+
import org.gitlab4j.api.models.HealthCheckInfo;
4+
5+
import javax.ws.rs.core.Response;
6+
import java.io.IOException;
7+
import java.net.URL;
8+
9+
public class HealthCheckApi extends AbstractApi {
10+
public HealthCheckApi(GitLabApi gitLabApi) {
11+
super(gitLabApi);
12+
}
13+
14+
/**
15+
* Get Health Checks from the liveness endpoint.
16+
*
17+
* Requires ip_whitelist
18+
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
19+
*
20+
* GET /-/liveness
21+
*
22+
* @return HealthCheckInfo instance
23+
* @throws GitLabApiException if any exception occurs
24+
*/
25+
public HealthCheckInfo getLiveness() throws GitLabApiException, IOException {
26+
URL livenessUrl = getApiClient().getUrlWithBase("-", "liveness");
27+
Response response = get(Response.Status.OK, null, livenessUrl);
28+
return (response.readEntity(HealthCheckInfo.class));
29+
}
30+
31+
/**
32+
* Get Health Checks from the liveness endpoint.
33+
*
34+
* Requires ip_whitelist
35+
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
36+
*
37+
* GET /-/liveness
38+
*
39+
* @param token Health Status token
40+
* @return HealthCheckInfo instance
41+
* @throws GitLabApiException if any exception occurs
42+
* @deprecated
43+
*/
44+
public HealthCheckInfo getLiveness(String token) throws GitLabApiException, IOException {
45+
URL livenessUrl = getApiClient().getUrlWithBase("-", "liveness");
46+
GitLabApiForm formData = new GitLabApiForm()
47+
.withParam("token", token, false);
48+
Response response = get(Response.Status.OK, formData.asMap(), livenessUrl);
49+
return (response.readEntity(HealthCheckInfo.class));
50+
}
51+
52+
/**
53+
* Get Health Checks from the readiness endpoint.
54+
*
55+
* Requires ip_whitelist
56+
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
57+
*
58+
* GET /-/readiness
59+
*
60+
* @return HealthCheckInfo instance
61+
* @throws GitLabApiException if any exception occurs
62+
*/
63+
public HealthCheckInfo getReadiness() throws GitLabApiException, IOException {
64+
URL readinessUrl = getApiClient().getUrlWithBase("-", "readiness");
65+
Response response = get(Response.Status.OK, null, readinessUrl);
66+
return (response.readEntity(HealthCheckInfo.class));
67+
}
68+
69+
/**
70+
* Get Health Checks from the readiness endpoint.
71+
*
72+
* Requires ip_whitelist
73+
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
74+
*
75+
* GET /-/readiness
76+
*
77+
* @param token Health Status token
78+
* @return HealthCheckInfo instance
79+
* @throws GitLabApiException if any exception occurs
80+
* @deprecated
81+
*/
82+
public HealthCheckInfo getReadiness(String token) throws GitLabApiException, IOException {
83+
URL readinessUrl = getApiClient().getUrlWithBase("-", "readiness");
84+
GitLabApiForm formData = new GitLabApiForm()
85+
.withParam("token", token, false);
86+
Response response = get(Response.Status.OK, formData.asMap(), readinessUrl);
87+
return (response.readEntity(HealthCheckInfo.class));
88+
}
89+
}

0 commit comments

Comments
 (0)