Search in sources :

Example 11 with GcpCloudContext

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);
}
Also used : GcpCloudContext(bio.terra.workspace.service.workspace.model.GcpCloudContext)

Example 12 with GcpCloudContext

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);
}
Also used : UUID(java.util.UUID) SpendProfileId(bio.terra.workspace.service.spendprofile.SpendProfileId) GcpCloudContext(bio.terra.workspace.service.workspace.model.GcpCloudContext) Workspace(bio.terra.workspace.service.workspace.model.Workspace) Test(org.junit.jupiter.api.Test) BaseUnitTest(bio.terra.workspace.common.BaseUnitTest)

Example 13 with GcpCloudContext

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");
}
Also used : GcpCloudContext(bio.terra.workspace.service.workspace.model.GcpCloudContext) Test(org.junit.jupiter.api.Test) BaseUnitTest(bio.terra.workspace.common.BaseUnitTest)

Example 14 with GcpCloudContext

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();
}
Also used : CloudResourceManagerCow(bio.terra.cloudres.google.cloudresourcemanager.CloudResourceManagerCow) IOException(java.io.IOException) StepResult(bio.terra.stairway.StepResult) GcpCloudContext(bio.terra.workspace.service.workspace.model.GcpCloudContext)

Example 15 with GcpCloudContext

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();
}
Also used : GcpCloudContext(bio.terra.workspace.service.workspace.model.GcpCloudContext)

Aggregations

GcpCloudContext (bio.terra.workspace.service.workspace.model.GcpCloudContext)21 StepResult (bio.terra.stairway.StepResult)9 IOException (java.io.IOException)7 FlightMap (bio.terra.stairway.FlightMap)5 Test (org.junit.jupiter.api.Test)5 AIPlatformNotebooksCow (bio.terra.cloudres.google.notebooks.AIPlatformNotebooksCow)4 InstanceName (bio.terra.cloudres.google.notebooks.InstanceName)4 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)4 UUID (java.util.UUID)4 BaseUnitTest (bio.terra.workspace.common.BaseUnitTest)3 Workspace (bio.terra.workspace.service.workspace.model.Workspace)3 Operation (com.google.api.services.notebooks.v1.model.Operation)3 BigQueryCow (bio.terra.cloudres.google.bigquery.BigQueryCow)2 StorageCow (bio.terra.cloudres.google.storage.StorageCow)2 FlightDebugInfo (bio.terra.stairway.FlightDebugInfo)2 FlightState (bio.terra.stairway.FlightState)2 StepStatus (bio.terra.stairway.StepStatus)2 BaseConnectedTest (bio.terra.workspace.common.BaseConnectedTest)2 CloudContextHolder (bio.terra.workspace.service.workspace.model.CloudContextHolder)2 DisabledIfEnvironmentVariable (org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)2