Skip to content

Commit

Permalink
Add status checks for RBS and TPS
Browse files Browse the repository at this point in the history
  • Loading branch information
aherbst-broad committed Aug 5, 2024
1 parent 4467a90 commit 10867e3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package bio.terra.workspace.service.buffer;

import bio.terra.buffer.api.BufferApi;
import bio.terra.buffer.api.UnauthenticatedApi;
import bio.terra.buffer.client.ApiClient;
import bio.terra.buffer.client.ApiException;
import bio.terra.buffer.model.HandoutRequestBody;
import bio.terra.buffer.model.ResourceInfo;
import bio.terra.common.exception.InternalServerErrorException;
import bio.terra.common.logging.RequestIdFilter;
import bio.terra.common.tracing.JakartaTracingFilter;
import bio.terra.workspace.app.configuration.external.BufferServiceConfiguration;
import bio.terra.workspace.service.buffer.exception.BufferServiceAPIException;
import bio.terra.workspace.service.buffer.exception.BufferServiceAuthorizationException;
import io.opentelemetry.api.OpenTelemetry;
import jakarta.ws.rs.client.Client;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/** A service for integrating with the Resource Buffer Service. */
@Component
@Service
public class BufferService {
private final Logger logger = LoggerFactory.getLogger(BufferService.class);

Expand All @@ -36,10 +37,35 @@ public BufferService(
new ApiClient().getHttpClient().register(new JakartaTracingFilter(openTelemetry));
}

/**
* Verify that the Buffer Service configuration is valid.
*
* @throws InternalServerErrorException if the application is misconfigured
*/
public void verifyConfiguration() {
this.bufferServiceConfiguration.getAccessToken();
}

/**
* Check the status of the Buffer Service.
*
* @return true if the service is up and running
*/
public boolean status() {
try {
var unauthenticatedApi =
new UnauthenticatedApi(
new ApiClient()
.setHttpClient(commonHttpClient)
.setBasePath(bufferServiceConfiguration.getInstanceUrl()));
var result = unauthenticatedApi.serviceStatus();
return result.isOk();
} catch (ApiException e) {
logger.error("Error querying Buffer API status", e);
return false;
}
}

private ApiClient getApiClient(String accessToken) {
ApiClient client =
new ApiClient()
Expand All @@ -50,7 +76,7 @@ private ApiClient getApiClient(String accessToken) {
return client;
}

private BufferApi bufferApi(String instanceUrl) throws IOException {
private BufferApi bufferApi(String instanceUrl) {
return new BufferApi(
getApiClient(bufferServiceConfiguration.getAccessToken()).setBasePath(instanceUrl));
}
Expand All @@ -72,12 +98,6 @@ public ResourceInfo handoutResource(HandoutRequestBody requestBody) {
bufferServiceConfiguration.getPoolId(),
bufferServiceConfiguration.getInstanceUrl());
return info;
} catch (IOException e) {
throw new BufferServiceAuthorizationException(
String.format(
"Error reading or parsing credentials file at %s",
bufferServiceConfiguration.getClientCredentialFilePath()),
e.getCause());
} catch (ApiException e) {
if (e.getCode() == HttpStatus.UNAUTHORIZED.value()) {
throw new BufferServiceAuthorizationException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import bio.terra.common.logging.RequestIdFilter;
import bio.terra.common.tracing.JakartaTracingFilter;
import bio.terra.policy.api.PublicApi;
import bio.terra.policy.api.TpsApi;
import bio.terra.policy.client.ApiClient;
import bio.terra.policy.client.ApiException;
Expand Down Expand Up @@ -44,9 +45,10 @@
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Component
/** A service for integrating with the Terra Policy Service (TPS). */
@Service
public class TpsApiDispatch {
private static final Logger logger = LoggerFactory.getLogger(TpsApiDispatch.class);
private final FeatureConfiguration features;
Expand Down Expand Up @@ -92,11 +94,36 @@ public void createPao(
}
}

/** Verify that we can read the needed TPS client credentials and create a TPS API client. */
/**
* Verify that we can read the needed TPS client credentials and create a TPS API client.
*
* @throws bio.terra.common.exception.InternalServerErrorException exception if the application is
* misconfigured
*/
public void verifyConfiguration() {
this.policyServiceConfiguration.getAccessToken();
}

/**
* Check the status of TPS.
*
* @return true if the service is up and running
*/
public boolean status() {
try {
var publicApi =
new PublicApi(
getApiClient(policyServiceConfiguration.getAccessToken())
.setBasePath(policyServiceConfiguration.getBasePath()));

publicApi.getStatus();
return true;
} catch (ApiException e) {
logger.error("Error querying TPS API status", e);
return false;
}
}

/**
* Get a PAO. Create an empty PAO if it does not exist.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
import bio.terra.common.migrate.LiquibaseMigrator;
import bio.terra.landingzone.job.LandingZoneJobService;
import bio.terra.landingzone.library.configuration.LandingZoneDatabaseConfiguration;
import bio.terra.workspace.app.configuration.external.BufferServiceConfiguration;
import bio.terra.workspace.app.configuration.external.FeatureConfiguration;
import bio.terra.workspace.app.configuration.external.WorkspaceDatabaseConfiguration;
import bio.terra.workspace.common.annotations.Unit;
import bio.terra.workspace.service.buffer.BufferService;
import bio.terra.workspace.service.iam.SamService;
import bio.terra.workspace.service.job.StairwayInitializerService;
import bio.terra.workspace.service.policy.TpsApiDispatch;
import bio.terra.workspace.service.workspace.WsmApplicationService;
import javax.sql.DataSource;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -38,6 +42,10 @@ class StartupInitializerTest {
@Mock private LandingZoneJobService landingZoneJobService;
@Mock private DataSource wsmDataSource;
@Mock private DataSource lzsDataSource;
@Mock private SamService samService;
@Mock private TpsApiDispatch tpsApiDispatch;
@Mock private BufferService bufferService;
@Mock private BufferServiceConfiguration bufferServiceConfiguration;

private enum DatabaseInitializationInstruction {
INITIALIZE,
Expand All @@ -47,7 +55,7 @@ private enum DatabaseInitializationInstruction {

@ParameterizedTest
@EnumSource(value = DatabaseInitializationInstruction.class)
void initialize(DatabaseInitializationInstruction dbInstruction) {
void initialize(DatabaseInitializationInstruction dbInstruction) throws InterruptedException {
when(context.getBean(DataSourceManager.class)).thenReturn(dataSourceManager);
when(context.getBean(WorkspaceDatabaseConfiguration.class))
.thenReturn(workspaceDatabaseConfiguration);
Expand All @@ -59,9 +67,16 @@ void initialize(DatabaseInitializationInstruction dbInstruction) {
.thenReturn(landingZoneDatabaseConfiguration);
when(context.getBean("landingZoneJobService", LandingZoneJobService.class))
.thenReturn(landingZoneJobService);
when(context.getBean(SamService.class)).thenReturn(samService);
when(context.getBean(BufferServiceConfiguration.class)).thenReturn(bufferServiceConfiguration);
when(context.getBean(BufferService.class)).thenReturn(bufferService);
when(context.getBean(TpsApiDispatch.class)).thenReturn(tpsApiDispatch);

when(featureConfiguration.isTpsEnabled()).thenReturn(true);
when(bufferServiceConfiguration.getEnabled()).thenReturn(true);
when(dataSourceManager.initializeDataSource(workspaceDatabaseConfiguration))
.thenReturn(wsmDataSource);

switch (dbInstruction) {
case INITIALIZE -> {
when(workspaceDatabaseConfiguration.isInitializeOnStart()).thenReturn(true);
Expand Down Expand Up @@ -95,5 +110,6 @@ void initialize(DatabaseInitializationInstruction dbInstruction) {
verify(stairwayInitializerService).initialize();
verify(wsmApplicationService).configure();
verify(landingZoneJobService).initialize();
verify(samService).initializeWsmServiceAccount();
}
}

0 comments on commit 10867e3

Please sign in to comment.