Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds codes #59

Merged
merged 7 commits into from
Oct 16, 2023
Merged
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
42 changes: 35 additions & 7 deletions src/main/java/io/github/jopenlibs/vault/api/Debug.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Debug withNameSpace(final String nameSpace) {
* </blockquote>
*/
public HealthResponse health() throws VaultException {
return health(null, null, null, null, null);
return health(null, null, null, null, null, null, null, null);
}

/**
Expand All @@ -75,23 +75,32 @@ public HealthResponse health() throws VaultException {
*
* @param standbyOk (optional) Indicates that being a standby should still return the active
* status code instead of the standby code
* @param perfstandbyOk (optional) Indicates that being a performance standby should still
* return the active status code instead of the performance standby code.
* @param activeCode (optional) Indicates the status code that should be returned for an active
* node instead of the default of 200
* @param standbyCode (optional) Indicates the status code that should be returned for a standby
* node instead of the default of 429
* @param drsecondaryCode (optional) Indicates the status code that should be returned for a DR
* secondary node instead of the default of 472
* @param performanceStandbyCode (optional) Indicates the status code that should be returned
* for a performance standby node instead of the default of 473
* @param sealedCode (optional) Indicates the status code that should be returned for a sealed
* node instead of the default of 500
* @param performanceStandbyCode (optional) Indicates the status code that should be
* returned for a performanceStandbyCode node instead of the default of 473
* @param uninitCode (optional) Indicates the status code that should be returned for a
* uninitialized node instead of the default of 500
* @return The response information returned from Vault
* @throws VaultException If an error occurs or unexpected response received from Vault
*/
public HealthResponse health(
final Boolean standbyOk,
final Boolean perfstandbyOk,
final Integer activeCode,
final Integer standbyCode,
final Integer drsecondaryCode,
final Integer performanceStandbyCode,
final Integer sealedCode,
final Integer performanceStandbyCode
final Integer uninitCode
) throws VaultException {
final String path = "sys/health";

Expand All @@ -110,38 +119,57 @@ public HealthResponse health(
if (standbyOk != null) {
rest.parameter("standbyok", standbyOk.toString());
}
if (perfstandbyOk != null) {
rest.parameter("perfstandbyok", perfstandbyOk.toString());
}
if (activeCode != null) {
rest.parameter("activecode", activeCode.toString());
}
if (standbyCode != null) {
rest.parameter("standbycode", standbyCode.toString());
}
if (drsecondaryCode != null) {
rest.parameter("drsecondarycode", drsecondaryCode.toString());
}
if (performanceStandbyCode != null) {
rest.parameter("performancestandbycode", performanceStandbyCode.toString());
}
if (sealedCode != null) {
rest.parameter("sealedcode", sealedCode.toString());
}
if (performanceStandbyCode != null) rest.parameter("performancestandbycode",
performanceStandbyCode.toString());
if (uninitCode != null) {
rest.parameter("uninitcode", uninitCode.toString());
}
// Execute request
final RestResponse restResponse = rest.get();

// Validate response
final Set<Integer> validCodes = new HashSet<>();//NOPMD
validCodes.add(200);
validCodes.add(429);
validCodes.add(500);
validCodes.add(472);
validCodes.add(473);
validCodes.add(500);
validCodes.add(501);
validCodes.add(503);
if (activeCode != null) {
validCodes.add(activeCode);
}
if (standbyCode != null) {
validCodes.add(standbyCode);
}
if (drsecondaryCode != null) {
validCodes.add(drsecondaryCode);
}
if (sealedCode != null) {
validCodes.add(sealedCode);
}
if (performanceStandbyCode != null) {
validCodes.add(performanceStandbyCode);
}
if (uninitCode != null) {
validCodes.add(uninitCode);
}

if (!validCodes.contains(restResponse.getStatus())) {
throw new VaultException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class HealthResponse implements Serializable {
private Boolean initialized;
private Boolean sealed;
private Boolean standby;
private Boolean drsecondary;
private Boolean perfstandby;
private Boolean uninit;
private Long serverTimeUTC;

/**
Expand All @@ -31,8 +34,7 @@ public class HealthResponse implements Serializable {
* <code>standby</code>, and <code>serverTimeUTC</code> set to <code>null</code>. This
* typically happens when you use optional parameters in the health call, to designate
* non-standard HTTP status codes. See docs for
* {@link Debug#health(Boolean, Integer, Integer, Integer, Integer)}.</p>
*
* {@link Debug#health(Boolean, Boolean, Integer, Integer, Integer,Integer, Integer, Integer)}.</p>
* @param restResponse The raw HTTP response from Vault
* @param retries The number of retry attempts that occurred during the API call (can be zero)
* @throws VaultException If any error occurs or unexpected response is received from Vault
Expand Down Expand Up @@ -65,6 +67,12 @@ public HealthResponse(final RestResponse restResponse, final int retries)
: jsonObject.get("sealed").asBoolean();
this.standby = jsonObject.get("standby") == null ? null
: jsonObject.get("standby").asBoolean();
this.drsecondary = jsonObject.get("drsecondary") == null ? null
: jsonObject.get("drsecondary").asBoolean();
this.perfstandby = jsonObject.get("perfstandby") == null ? null
: jsonObject.get("perfstandby").asBoolean();
this.uninit = jsonObject.get("uninit") == null ? null
: jsonObject.get("uninit").asBoolean();
this.serverTimeUTC = jsonObject.get("server_time_utc") == null ? null
: jsonObject.get("server_time_utc").asLong();
} catch (final Exception e) {
Expand Down Expand Up @@ -94,6 +102,18 @@ public Boolean getStandby() {
return standby;
}

public Boolean getDrsecondary() {
return drsecondary;
}

public Boolean getPerfstandby() {
return perfstandby;
}

public Boolean getuninit() {
return uninit;
}

/**
* @return A value representing the number of milliseconds since the epoch. With all of the
* changes in date API's between Java 8 and pre-Java 8, it seemed best for the library not to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,23 @@ public void testHealth_Plain() throws VaultException {
assertFalse(response.getSealed());
assertFalse(response.getStandby());
assertNotNull(response.getServerTimeUTC());
assertNull(response.getDrsecondary());
assertNull(response.getPerfstandby());
assertNull(response.getuninit());
TestCase.assertEquals(200, response.getRestResponse().getStatus());
}

@Test
public void testHealth_WithParams() throws VaultException {
final HealthResponse response = vault.debug().health(null, 212, null, null, null);
final HealthResponse response = vault.debug().health(null, null, 212, null, null, null, null, null);

assertTrue(response.getInitialized());
assertFalse(response.getSealed());
assertFalse(response.getStandby());
assertNotNull(response.getServerTimeUTC());
assertNull(response.getDrsecondary());
assertNull(response.getPerfstandby());
assertNull(response.getuninit());
TestCase.assertEquals(212, response.getRestResponse().getStatus());
}

Expand All @@ -66,12 +73,15 @@ public void testHealth_WithParams() throws VaultException {
*/
@Test
public void testHealth_WonkyActiveCode() throws VaultException {
final HealthResponse response = vault.debug().health(null, 204, null,
null, null);
final HealthResponse response = vault.debug().health(null, null, 204,
null, null, null, null, null);
assertNull(response.getInitialized());
assertNull(response.getSealed());
assertNull(response.getStandby());
assertNull(response.getServerTimeUTC());
assertNull(response.getDrsecondary());
assertNull(response.getPerfstandby());
assertNull(response.getuninit());
TestCase.assertEquals(204, response.getRestResponse().getStatus());
}
}
Loading