-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add functionality to get wds client set up and working
add tests for wds service and client changes move sam configuration block into its own section in application yml
- Loading branch information
Jose Soto
committed
Nov 28, 2023
1 parent
9f9e8c1
commit 7d26e4d
Showing
23 changed files
with
613 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
.../src/main/java/bio/terra/pipelines/app/configuration/external/WdsServerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package bio.terra.pipelines.app.configuration.external; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@ConfigurationProperties(prefix = "wds") | ||
public record WdsServerConfiguration(String apiV, Boolean debugApiLogging) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ardo/DependencyNotAvailableException.java → ...mmon/DependencyNotAvailableException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
service/src/main/java/bio/terra/pipelines/dependencies/common/HealthCheckWorkspaceApps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package bio.terra.pipelines.dependencies.common; | ||
|
||
public interface HealthCheckWorkspaceApps { | ||
|
||
record Result(boolean isOk, String message) {} | ||
|
||
Result checkHealth(String baseUri, String accessToken); | ||
} |
1 change: 1 addition & 0 deletions
1
service/src/main/java/bio/terra/pipelines/dependencies/leonardo/AppUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
service/src/main/java/bio/terra/pipelines/dependencies/wds/WdsClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package bio.terra.pipelines.dependencies.wds; | ||
|
||
import bio.terra.pipelines.app.configuration.external.WdsServerConfiguration; | ||
import bio.terra.pipelines.dependencies.common.DependencyNotAvailableException; | ||
import okhttp3.OkHttpClient; | ||
import org.databiosphere.workspacedata.api.GeneralWdsInformationApi; | ||
import org.databiosphere.workspacedata.api.RecordsApi; | ||
import org.databiosphere.workspacedata.api.SchemaApi; | ||
import org.databiosphere.workspacedata.client.ApiClient; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class WdsClient { | ||
|
||
private final OkHttpClient singletonHttpClient; | ||
private final WdsServerConfiguration wdsServerConfiguration; | ||
|
||
public WdsClient(WdsServerConfiguration wdsServerConfiguration) { | ||
this.wdsServerConfiguration = wdsServerConfiguration; | ||
singletonHttpClient = new ApiClient().getHttpClient().newBuilder().build(); | ||
} | ||
|
||
protected ApiClient getApiClient(String wdsBaseUri, String accessToken) | ||
throws DependencyNotAvailableException { | ||
|
||
ApiClient apiClient = new ApiClient().setBasePath(wdsBaseUri); | ||
apiClient.setHttpClient(singletonHttpClient); | ||
apiClient.addDefaultHeader("Authorization", "Bearer " + accessToken); | ||
// By closing the connection after each request, we avoid the problem of the open connection | ||
// being force-closed ungracefully by the Azure Relay/Listener infrastructure: | ||
apiClient.addDefaultHeader("Connection", "close"); | ||
apiClient.setDebugging(wdsServerConfiguration.debugApiLogging()); | ||
return apiClient; | ||
} | ||
|
||
RecordsApi recordsApi(String wdsBaseUri, String accessToken) | ||
throws DependencyNotAvailableException { | ||
return new RecordsApi(getApiClient(wdsBaseUri, accessToken)); | ||
} | ||
|
||
GeneralWdsInformationApi generalWdsInformationApi(String wdsBaseUri, String accessToken) | ||
throws DependencyNotAvailableException { | ||
return new GeneralWdsInformationApi(getApiClient(wdsBaseUri, accessToken)); | ||
} | ||
|
||
SchemaApi schemaApi(String wdsBaseUri, String accessToken) | ||
throws DependencyNotAvailableException { | ||
return new SchemaApi(getApiClient(wdsBaseUri, accessToken)); | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
service/src/main/java/bio/terra/pipelines/dependencies/wds/WdsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package bio.terra.pipelines.dependencies.wds; | ||
|
||
import bio.terra.pipelines.app.configuration.external.WdsServerConfiguration; | ||
import bio.terra.pipelines.dependencies.common.DependencyNotAvailableException; | ||
import bio.terra.pipelines.dependencies.common.HealthCheckWorkspaceApps; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import org.databiosphere.workspacedata.client.ApiException; | ||
import org.databiosphere.workspacedata.model.RecordRequest; | ||
import org.databiosphere.workspacedata.model.RecordResponse; | ||
import org.databiosphere.workspacedata.model.RecordTypeSchema; | ||
import org.databiosphere.workspacedata.model.StatusResponse; | ||
import org.springframework.retry.support.RetryTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** class to encapsulate interacting with WDS client */ | ||
@Service | ||
public class WdsService implements HealthCheckWorkspaceApps { | ||
|
||
private final WdsClient wdsClient; | ||
private final WdsServerConfiguration wdsServerConfiguration; | ||
private final RetryTemplate listenerResetRetryTemplate; | ||
|
||
public WdsService( | ||
WdsClient wdsClient, | ||
WdsServerConfiguration wdsServerConfiguration, | ||
RetryTemplate listenerResetRetryTemplate) { | ||
this.wdsClient = wdsClient; | ||
this.wdsServerConfiguration = wdsServerConfiguration; | ||
this.listenerResetRetryTemplate = listenerResetRetryTemplate; | ||
} | ||
|
||
/** | ||
* query for a record from WDS table | ||
* | ||
* @param wdsBaseUri - URI of WDS app, retrieved from Leonardo | ||
* @param bearerToken - SA token, retrieved from google default credentials | ||
* @param recordType - WDS table name | ||
* @param workspaceId - workspace id WDS app is deployed in | ||
* @param recordId - id for the record | ||
* @return - returns Record details | ||
* @throws WdsServiceException ; | ||
*/ | ||
public RecordResponse getRecord( | ||
String wdsBaseUri, String bearerToken, String recordType, String workspaceId, String recordId) | ||
throws WdsServiceException { | ||
return executionWithRetryTemplate( | ||
listenerResetRetryTemplate, | ||
() -> | ||
wdsClient | ||
.recordsApi(wdsBaseUri, bearerToken) | ||
.getRecord(workspaceId, wdsServerConfiguration.apiV(), recordType, recordId)); | ||
} | ||
|
||
/** | ||
* Update a record in a table | ||
* | ||
* @param wdsBaseUri - URI of WDS app, retrieved from Leonardo | ||
* @param bearerToken - SA token, retrieved from google default credentials | ||
* @param request - request for what fields to update | ||
* @param workspaceId - workspace id WDS app is deployed in | ||
* @param type - WDS table name | ||
* @param id - id of record to update | ||
* @return - the updated record | ||
* @throws WdsServiceException ; | ||
*/ | ||
public RecordResponse updateRecord( | ||
String wdsBaseUri, | ||
String bearerToken, | ||
RecordRequest request, | ||
String workspaceId, | ||
String type, | ||
String id) | ||
throws WdsServiceException { | ||
return executionWithRetryTemplate( | ||
listenerResetRetryTemplate, | ||
() -> | ||
wdsClient | ||
.recordsApi(wdsBaseUri, bearerToken) | ||
.updateRecord(request, workspaceId, wdsServerConfiguration.apiV(), type, id)); | ||
} | ||
|
||
/** | ||
* Query for all the tables and their structure for this WDS app | ||
* | ||
* @param wdsBaseUri - URI of WDS app, retrieved from Leonardo | ||
* @param bearerToken - SA token, retrieved from google default credentials | ||
* @param workspaceId - workspace id WDS app is deployed in | ||
* @return - list of all the tables in this WDS app | ||
* @throws WdsServiceException ; | ||
*/ | ||
public List<RecordTypeSchema> querySchema( | ||
String wdsBaseUri, String bearerToken, String workspaceId) throws WdsServiceException { | ||
return executionWithRetryTemplate( | ||
listenerResetRetryTemplate, | ||
() -> | ||
wdsClient | ||
.schemaApi(wdsBaseUri, bearerToken) | ||
.describeAllRecordTypes(workspaceId, wdsServerConfiguration.apiV())); | ||
} | ||
|
||
@Override | ||
public Result checkHealth(String wdsBaseUri, String accessToken) { | ||
try { | ||
StatusResponse result = | ||
wdsClient.generalWdsInformationApi(wdsBaseUri, accessToken).statusGet(); | ||
return new Result(Objects.equals(result.getStatus(), "UP"), result.toString()); | ||
} catch (ApiException e) { | ||
return new Result(false, e.getMessage()); | ||
} | ||
} | ||
|
||
interface WdsAction<T> { | ||
T execute() throws ApiException, DependencyNotAvailableException; | ||
} | ||
|
||
@SuppressWarnings("java:S125") // The comment here isn't "commented code" | ||
static <T> T executionWithRetryTemplate(RetryTemplate retryTemplate, WdsAction<T> action) | ||
throws WdsServiceException { | ||
|
||
// Why all this song and dance to catch exceptions and map them to almost identical exceptions? | ||
// Because the RetryTemplate's execute function only allows us to declare one Throwable type. | ||
// So we have a top-level WdsServiceException that we can catch and handle, and then we have | ||
// subclasses of that exception representing the types of exception that can be thrown. This | ||
// way, we can keep well typed exceptions (no "catch (Exception e)") and still make use of the | ||
// retry framework. | ||
return retryTemplate.execute( | ||
context -> { | ||
try { | ||
return action.execute(); | ||
} catch (ApiException e) { | ||
throw new WdsServiceApiException(e); | ||
} catch (DependencyNotAvailableException e) { | ||
throw new WdsServiceNotAvailableException(e); | ||
} | ||
}); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
service/src/main/java/bio/terra/pipelines/dependencies/wds/WdsServiceApiException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package bio.terra.pipelines.dependencies.wds; | ||
|
||
import org.databiosphere.workspacedata.client.ApiException; | ||
|
||
public class WdsServiceApiException extends WdsServiceException { | ||
private final ApiException exception; | ||
|
||
public WdsServiceApiException(ApiException exception) { | ||
this.exception = exception; | ||
} | ||
|
||
@Override | ||
public synchronized ApiException getCause() { | ||
return exception; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
service/src/main/java/bio/terra/pipelines/dependencies/wds/WdsServiceException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package bio.terra.pipelines.dependencies.wds; | ||
|
||
public abstract class WdsServiceException extends Exception { | ||
@Override | ||
public String getMessage() { | ||
return getCause().getMessage(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...e/src/main/java/bio/terra/pipelines/dependencies/wds/WdsServiceNotAvailableException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package bio.terra.pipelines.dependencies.wds; | ||
|
||
import bio.terra.pipelines.dependencies.common.DependencyNotAvailableException; | ||
|
||
public class WdsServiceNotAvailableException extends WdsServiceException { | ||
private final DependencyNotAvailableException exception; | ||
|
||
public WdsServiceNotAvailableException(DependencyNotAvailableException exception) { | ||
this.exception = exception; | ||
} | ||
|
||
@Override | ||
public synchronized DependencyNotAvailableException getCause() { | ||
return exception; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.