Skip to content

Commit

Permalink
AJ-1409: add property to policy-only snapshot references (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidangb authored Oct 31, 2023
1 parent 2b64d5a commit e4906e9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected void linkSnapshots(List<UUID> snapshotIds) {
for (UUID uuid : newSnapshotIds) {
try {
RestClientRetry.VoidRestCall voidRestCall =
(() -> wsmDao.createDataRepoSnapshotReference(new SnapshotModel().id(uuid)));
(() -> wsmDao.linkSnapshotForPolicy(new SnapshotModel().id(uuid)));
restClientRetry.withRetryAndErrorHandling(
voidRestCall, "WSM.createDataRepoSnapshotReference");
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void importSnapshot(UUID instanceId, UUID snapshotId) {

// createDataRepoSnapshotReference is required to setup policy and will throw exception if
// policy conflicts
workspaceManagerDao.createDataRepoSnapshotReference(snapshot);
workspaceManagerDao.linkSnapshotForPolicy(snapshot);
activityLogger.saveEventForCurrentUser(
user -> user.linked().snapshotReference().withUuid(snapshotId));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@

public class WorkspaceManagerDao {
public static final String INSTANCE_NAME = "terra";

/**
* indicates the purpose of a snapshot reference - e.g. is it created for the sole purpose of
* linking policies.
*/
public static final String PROP_PURPOSE = "purpose";

public static final String PURPOSE_POLICY = "policy";

private final WorkspaceManagerClientFactory workspaceManagerClientFactory;
private final String workspaceId;
private static final Logger LOGGER = LoggerFactory.getLogger(WorkspaceManagerDao.class);
Expand All @@ -23,8 +32,19 @@ public WorkspaceManagerDao(
this.workspaceId = workspaceId;
}

/** Creates a snapshot reference in workspaces manager and creates policy linkages. */
public void createDataRepoSnapshotReference(SnapshotModel snapshotModel) {
/** Creates a snapshot reference in workspace manager for the sole purpose of policy linkages. */
public void linkSnapshotForPolicy(SnapshotModel snapshotModel) {
Properties properties = null;
Property policyProperty = new Property();
policyProperty.setKey(PROP_PURPOSE);
policyProperty.setValue(PURPOSE_POLICY);
properties = new Properties();
properties.add(policyProperty);
createDataRepoSnapshotReference(snapshotModel, properties);
}

/* Creates a snapshot reference in workspace manager. */
private void createDataRepoSnapshotReference(SnapshotModel snapshotModel, Properties properties) {
final ReferencedGcpResourceApi resourceApi =
this.workspaceManagerClientFactory.getReferencedGcpResourceApi(null);

Expand All @@ -39,6 +59,7 @@ public void createDataRepoSnapshotReference(SnapshotModel snapshotModel) {
.metadata(
new ReferenceResourceCommonFields()
.cloningInstructions(CloningInstructionsEnum.REFERENCE)
.properties(properties)
.name("%s_%s".formatted(snapshotModel.getName(), timeStamp))),
UUID.fromString(workspaceId));
} catch (ApiException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void linkAllNewSnapshots() throws ApiException {
// capture calls
ArgumentCaptor<SnapshotModel> argumentCaptor = ArgumentCaptor.forClass(SnapshotModel.class);
// should have called WSM's create-snapshot-reference 10 times
verify(wsmDao, times(input.size())).createDataRepoSnapshotReference(argumentCaptor.capture());
verify(wsmDao, times(input.size())).linkSnapshotForPolicy(argumentCaptor.capture());
// those 10 calls should have used our 10 input UUIDs
List<SnapshotModel> actualModels = argumentCaptor.getAllValues();
List<UUID> actualUuids = actualModels.stream().map(SnapshotModel::getId).toList();
Expand All @@ -75,7 +75,7 @@ void linkNothingWhenAllExist() throws ApiException {
new PfbQuartzJob(jobDao, wsmDao, restClientRetry, UUID.randomUUID());
pfbQuartzJob.linkSnapshots(input);
// should not call WSM's create-snapshot-reference at all
verify(wsmDao, times(0)).createDataRepoSnapshotReference(any());
verify(wsmDao, times(0)).linkSnapshotForPolicy(any());
}

@Test
Expand Down Expand Up @@ -104,8 +104,7 @@ void linkSomeWhenSomeExist() throws ApiException {
// should call WSM's create-snapshot-reference only for the references that didn't already exist
int expectedCallCount = input.size() - resourceDescriptions.size();
ArgumentCaptor<SnapshotModel> argumentCaptor = ArgumentCaptor.forClass(SnapshotModel.class);
verify(wsmDao, times(expectedCallCount))
.createDataRepoSnapshotReference(argumentCaptor.capture());
verify(wsmDao, times(expectedCallCount)).linkSnapshotForPolicy(argumentCaptor.capture());
List<UUID> actual = argumentCaptor.getAllValues().stream().map(SnapshotModel::getId).toList();
actual.forEach(
id ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void doNotFailOnMissingSnapshotId() throws JobExecutionException, ApiException {
new PfbQuartzJob(jobDao, wsmDao, restClientRetry, UUID.randomUUID()).execute(mockContext);

// Should not call wsm dao
verify(wsmDao, times(0)).createDataRepoSnapshotReference(any());
verify(wsmDao, times(0)).linkSnapshotForPolicy(any());
// But job should succeed
verify(jobDao).succeeded(jobId);
}
Expand All @@ -88,7 +88,7 @@ void snapshotIdsAreParsed() throws JobExecutionException, ApiException {

// This is the snapshotId given in the test pfb
verify(wsmDao)
.createDataRepoSnapshotReference(
.linkSnapshotForPolicy(
ArgumentMatchers.argThat(
new SnapshotModelMatcher(UUID.fromString("790795c4-49b1-4ac8-a060-207b92ea08c5"))));
// Job should succeed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.databiosphere.workspacedataservice.workspacemanager;

import static org.databiosphere.workspacedataservice.workspacemanager.WorkspaceManagerDao.PROP_PURPOSE;
import static org.databiosphere.workspacedataservice.workspacemanager.WorkspaceManagerDao.PURPOSE_POLICY;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -19,6 +22,7 @@
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -62,7 +66,7 @@ void setUp() {
void testSnapshotReturned() throws ApiException {
final SnapshotModel testSnapshot =
new SnapshotModel().name("test snapshot").id(UUID.randomUUID());
workspaceManagerDao.createDataRepoSnapshotReference(testSnapshot);
workspaceManagerDao.linkSnapshotForPolicy(testSnapshot);
verify(mockReferencedGcpResourceApi)
.createDataRepoSnapshotReference(
argThat(
Expand All @@ -85,7 +89,7 @@ void testErrorThrown() throws ApiException {
var exception =
assertThrows(
WorkspaceManagerException.class,
() -> workspaceManagerDao.createDataRepoSnapshotReference(testSnapshot));
() -> workspaceManagerDao.linkSnapshotForPolicy(testSnapshot));
assertEquals(statusCode, exception.getRawStatusCode());
}

Expand All @@ -105,6 +109,32 @@ void testResourceReturnFailure() {
assertNull(resourceUUID);
}

@Test
void policyOnlyProperty() throws ApiException {
// set up inputs
UUID snapshotId = UUID.randomUUID();
SnapshotModel snapshotModel = new SnapshotModel().id(snapshotId);
// call the create-reference method
workspaceManagerDao.linkSnapshotForPolicy(snapshotModel);

// validate that it sent correct Properties to resourceApi.createDataRepoSnapshotReference
ArgumentCaptor<CreateDataRepoSnapshotReferenceRequestBody> argumentCaptor =
ArgumentCaptor.forClass(CreateDataRepoSnapshotReferenceRequestBody.class);
verify(mockReferencedGcpResourceApi)
.createDataRepoSnapshotReference(argumentCaptor.capture(), any());

CreateDataRepoSnapshotReferenceRequestBody createBody = argumentCaptor.getValue();
assertNotNull(createBody.getSnapshot());
assertEquals(snapshotId.toString(), createBody.getSnapshot().getSnapshot());

assertNotNull(createBody.getMetadata());
assertNotNull(createBody.getMetadata().getProperties());
assertEquals(1, createBody.getMetadata().getProperties().size());
Property actual = createBody.getMetadata().getProperties().get(0);
assertEquals(PROP_PURPOSE, actual.getKey());
assertEquals(PURPOSE_POLICY, actual.getValue());
}

UUID buildResourceListObjectAndCallExtraction(UUID workspaceId, String name, ResourceType type) {
ResourceList resourceList = new ResourceList();
ResourceDescription resourceDescription = new ResourceDescription();
Expand Down

0 comments on commit e4906e9

Please sign in to comment.