use of bio.terra.workspace.service.workspace.model.GcpCloudContext in project terra-workspace-manager by DataBiosphere.
the class WorkspaceDaoTest method checkCloudContext.
private void checkCloudContext(Optional<GcpCloudContext> optionalContext) {
assertTrue(optionalContext.isPresent());
GcpCloudContext context = optionalContext.get();
assertEquals(context.getGcpProjectId(), PROJECT_ID);
assertTrue(context.getSamPolicyOwner().isPresent());
assertEquals(context.getSamPolicyOwner().get(), POLICY_OWNER);
assertTrue(context.getSamPolicyWriter().isPresent());
assertEquals(context.getSamPolicyWriter().get(), POLICY_WRITER);
assertTrue(context.getSamPolicyReader().isPresent());
assertEquals(context.getSamPolicyReader().get(), POLICY_READER);
assertTrue(context.getSamPolicyApplication().isPresent());
assertEquals(context.getSamPolicyApplication().get(), POLICY_APPLICATION);
}
use of bio.terra.workspace.service.workspace.model.GcpCloudContext in project terra-workspace-manager by DataBiosphere.
the class GcpCloudContextUnitTest method autoUpgradeTest.
@Test
public void autoUpgradeTest() throws Exception {
// By default, allow all spend link calls as authorized. (All other isAuthorized calls return
// false by Mockito default.
Mockito.when(mockSamService.isAuthorized(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.eq(SamSpendProfileAction.LINK))).thenReturn(true);
// Fake groups
Mockito.when(mockSamService.getWorkspacePolicy(any(), Mockito.eq(WsmIamRole.READER), any())).thenReturn(POLICY_READER);
Mockito.when(mockSamService.getWorkspacePolicy(any(), Mockito.eq(WsmIamRole.WRITER), any())).thenReturn(POLICY_WRITER);
Mockito.when(mockSamService.getWorkspacePolicy(any(), Mockito.eq(WsmIamRole.OWNER), any())).thenReturn(POLICY_OWNER);
Mockito.when(mockSamService.getWorkspacePolicy(any(), Mockito.eq(WsmIamRole.APPLICATION), any())).thenReturn(POLICY_APPLICATION);
// Create a workspace record
UUID workspaceId = UUID.randomUUID();
var workspace = new Workspace(workspaceId, "gcpCloudContextAutoUpgradeTest", "cloud context description", new SpendProfileId("spend-profile"), Collections.emptyMap(), WorkspaceStage.MC_WORKSPACE);
workspaceDao.createWorkspace(workspace);
// Create a cloud context in the database with a V1 format
final String flightId = UUID.randomUUID().toString();
workspaceDao.createCloudContextStart(workspaceId, CloudPlatform.GCP, flightId);
workspaceDao.createCloudContextFinish(workspaceId, CloudPlatform.GCP, V1_JSON, flightId);
// Run the service call that should do the upgrade
GcpCloudContext updatedContext = gcpCloudContextService.getRequiredGcpCloudContext(workspaceId, USER_REQUEST);
assertEquals(updatedContext.getSamPolicyOwner().orElse(null), POLICY_OWNER);
assertEquals(updatedContext.getSamPolicyWriter().orElse(null), POLICY_WRITER);
assertEquals(updatedContext.getSamPolicyReader().orElse(null), POLICY_READER);
assertEquals(updatedContext.getSamPolicyApplication().orElse(null), POLICY_APPLICATION);
}
use of bio.terra.workspace.service.workspace.model.GcpCloudContext in project terra-workspace-manager by DataBiosphere.
the class GcpCloudContextUnitTest method serdesTest.
@Test
void serdesTest() {
final String v2Json = String.format("{\"version\": 2, \"gcpProjectId\": \"%s\", \"samPolicyOwner\": \"%s\", \"samPolicyWriter\": \"%s\", \"samPolicyReader\": \"%s\", \"samPolicyApplication\": \"%s\" }", GCP_PROJECT_ID, POLICY_OWNER, POLICY_WRITER, POLICY_READER, POLICY_APPLICATION);
final String badV1Json = String.format("{\"version\": 3, \"gcpProjectId\": \"%s\"}", GCP_PROJECT_ID);
final String badV2Json = String.format("{\"version\": 3, \"gcpProjectId\": \"%s\", \"samPolicyOwner\": \"%s\", \"samPolicyWriter\": \"%s\", \"samPolicyReader\": \"%s\", \"samPolicyApplication\": \"%s\" }", GCP_PROJECT_ID, POLICY_OWNER, POLICY_WRITER, POLICY_READER, POLICY_APPLICATION);
final String incompleteV2Json = String.format("{\"version\": 2, \"gcpProjectId\": \"%s\"}", GCP_PROJECT_ID);
final String junkJson = "{\"foo\": 15, \"bar\": \"xyzzy\"}";
// Case 1: successful V1 deserialization
GcpCloudContext goodV1 = GcpCloudContext.deserialize(V1_JSON);
assertEquals(goodV1.getGcpProjectId(), GCP_PROJECT_ID);
assertTrue(goodV1.getSamPolicyOwner().isEmpty());
assertTrue(goodV1.getSamPolicyWriter().isEmpty());
assertTrue(goodV1.getSamPolicyReader().isEmpty());
assertTrue(goodV1.getSamPolicyApplication().isEmpty());
// Case 2: successful V2 deserialization
GcpCloudContext goodV2 = GcpCloudContext.deserialize(v2Json);
assertEquals(goodV2.getGcpProjectId(), GCP_PROJECT_ID);
assertEquals(goodV2.getSamPolicyOwner().orElse(null), POLICY_OWNER);
assertEquals(goodV2.getSamPolicyWriter().orElse(null), POLICY_WRITER);
assertEquals(goodV2.getSamPolicyReader().orElse(null), POLICY_READER);
assertEquals(goodV2.getSamPolicyApplication().orElse(null), POLICY_APPLICATION);
// Case 3: bad V1 format
assertThrows(InvalidSerializedVersionException.class, () -> GcpCloudContext.deserialize(badV1Json), "Bad V1 JSON should throw");
// Case 4: bad V2 format
assertThrows(InvalidSerializedVersionException.class, () -> GcpCloudContext.deserialize(badV2Json), "Bad V2 JSON should throw");
// Case 5: incomplete V2
assertThrows(InvalidSerializedVersionException.class, () -> GcpCloudContext.deserialize(incompleteV2Json), "Incomplete V2 JSON should throw");
// Case 6: junk input
assertThrows(InvalidSerializedVersionException.class, () -> GcpCloudContext.deserialize(junkJson), "Junk JSON should throw");
}
use of bio.terra.workspace.service.workspace.model.GcpCloudContext in project terra-workspace-manager by DataBiosphere.
the class DeleteGcpProjectStep method doStep.
@Override
public StepResult doStep(FlightContext flightContext) throws InterruptedException, RetryException {
Optional<GcpCloudContext> cloudContext = getContext(flightContext);
if (cloudContext.isPresent()) {
CloudResourceManagerCow resourceManager = crl.getCloudResourceManagerCow();
try {
String projectId = cloudContext.get().getGcpProjectId();
GcpUtils.deleteProject(projectId, resourceManager);
} catch (IOException e) {
return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
}
}
return StepResult.getStepResultSuccess();
}
use of bio.terra.workspace.service.workspace.model.GcpCloudContext in project terra-workspace-manager by DataBiosphere.
the class SetGcpContextOutputStep method doStep.
@Override
public StepResult doStep(FlightContext flightContext) throws InterruptedException, RetryException {
String projectId = flightContext.getWorkingMap().get(GCP_PROJECT_ID, String.class);
GcpCloudContext cloudContext = new GcpCloudContext(projectId);
FlightUtils.setResponse(flightContext, cloudContext, HttpStatus.OK);
return StepResult.getStepResultSuccess();
}
Aggregations