Search in sources :

Example 1 with WsmResource

use of bio.terra.workspace.service.resource.model.WsmResource in project terra-workspace-manager by DataBiosphere.

the class CreateAndDeleteAzureControlledResourceFlightTest method createAndDeleteAzureVmControlledResource.

@Test
public void createAndDeleteAzureVmControlledResource() throws InterruptedException {
    // Setup workspace and cloud context
    UUID workspaceId = azureTestUtils.createWorkspace(workspaceService);
    AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest();
    // Cloud context needs to be created first
    FlightState createAzureContextFlightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateAzureContextFlight.class, azureTestUtils.createAzureContextInputParameters(workspaceId, userRequest), STAIRWAY_FLIGHT_TIMEOUT, null);
    assertEquals(FlightStatus.SUCCESS, createAzureContextFlightState.getFlightStatus());
    assertTrue(workspaceService.getAuthorizedAzureCloudContext(workspaceId, userRequest).isPresent());
    // Create ip
    ControlledAzureIpResource ipResource = createIp(workspaceId, userRequest);
    // Create disk
    ControlledAzureDiskResource diskResource = createDisk(workspaceId, userRequest);
    // Create network
    ControlledAzureNetworkResource networkResource = createNetwork(workspaceId, userRequest);
    final ApiAzureVmCreationParameters creationParameters = ControlledResourceFixtures.getAzureVmCreationParameters();
    // TODO: make this application-private resource once the POC supports it
    final UUID resourceId = UUID.randomUUID();
    ControlledAzureVmResource resource = ControlledAzureVmResource.builder().common(ControlledResourceFields.builder().workspaceId(workspaceId).resourceId(resourceId).name(getAzureName("vm")).description(getAzureName("vm-desc")).cloningInstructions(CloningInstructions.COPY_RESOURCE).accessScope(AccessScopeType.fromApi(ApiAccessScope.SHARED_ACCESS)).managedBy(ManagedByType.fromApi(ApiManagedBy.USER)).build()).vmName(creationParameters.getName()).vmSize(creationParameters.getVmSize()).vmImageUri(creationParameters.getVmImageUri()).region(creationParameters.getRegion()).ipId(ipResource.getResourceId()).diskId(diskResource.getResourceId()).networkId(networkResource.getResourceId()).build();
    // Submit a VM creation flight.
    FlightState flightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateControlledResourceFlight.class, azureTestUtils.createControlledResourceInputParameters(workspaceId, userRequest, resource), STAIRWAY_FLIGHT_TIMEOUT, null);
    assertEquals(FlightStatus.SUCCESS, flightState.getFlightStatus());
    // Verify controlled resource exists in the workspace.
    ControlledResource res = controlledResourceService.getControlledResource(workspaceId, resourceId, userRequest);
    try {
        ControlledAzureVmResource azureVmResource = res.castByEnum(WsmResourceType.CONTROLLED_AZURE_VM);
        assertEquals(resource, azureVmResource);
    } catch (Exception e) {
        fail("Failed to cast resource to ControlledAzureVmResource", e);
    }
    // Exercise resource enumeration for the underlying resources.
    // Verify that the resources we created are in the enumeration.
    List<WsmResource> resourceList = wsmResourceService.enumerateResources(workspaceId, null, null, 0, 100, userRequest);
    checkForResource(resourceList, ipResource);
    checkForResource(resourceList, diskResource);
    checkForResource(resourceList, networkResource);
    checkForResource(resourceList, resource);
    ComputeManager computeManager = azureTestUtils.getComputeManager();
    VirtualMachine vmTemp = null;
    var retries = 20;
    while (vmTemp == null) {
        try {
            retries = retries - 1;
            if (retries >= 0) {
                vmTemp = computeManager.virtualMachines().getByResourceGroup(azureTestUtils.getAzureCloudContext().getAzureResourceGroupId(), creationParameters.getName());
            } else
                throw new RuntimeException(String.format("%s is not created in time in Azure", creationParameters.getName()));
        } catch (com.azure.core.exception.HttpResponseException ex) {
            if (ex.getResponse().getStatusCode() == 404)
                Thread.sleep(10000);
            else
                throw ex;
        }
    }
    final VirtualMachine resolvedVm = vmTemp;
    // Submit a VM deletion flight.
    FlightState deleteFlightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), DeleteControlledResourceFlight.class, azureTestUtils.deleteControlledResourceInputParameters(workspaceId, resourceId, userRequest, resource), STAIRWAY_FLIGHT_TIMEOUT, null);
    assertEquals(FlightStatus.SUCCESS, deleteFlightState.getFlightStatus());
    Thread.sleep(10000);
    resolvedVm.networkInterfaceIds().forEach(nic -> assertThrows(com.azure.core.exception.HttpResponseException.class, () -> computeManager.networkManager().networks().getById(nic)));
    assertThrows(com.azure.core.exception.HttpResponseException.class, () -> computeManager.disks().getById(resolvedVm.osDiskId()));
}
Also used : ControlledAzureIpResource(bio.terra.workspace.service.resource.controlled.cloud.azure.ip.ControlledAzureIpResource) WsmResource(bio.terra.workspace.service.resource.model.WsmResource) ControlledAzureVmResource(bio.terra.workspace.service.resource.controlled.cloud.azure.vm.ControlledAzureVmResource) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) ApiAzureVmCreationParameters(bio.terra.workspace.generated.model.ApiAzureVmCreationParameters) ManagementException(com.azure.core.management.exception.ManagementException) ComputeManager(com.azure.resourcemanager.compute.ComputeManager) FlightState(bio.terra.stairway.FlightState) ControlledAzureDiskResource(bio.terra.workspace.service.resource.controlled.cloud.azure.disk.ControlledAzureDiskResource) ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) UUID(java.util.UUID) ControlledAzureNetworkResource(bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine) Test(org.junit.jupiter.api.Test) BaseAzureTest(bio.terra.workspace.common.BaseAzureTest)

Example 2 with WsmResource

use of bio.terra.workspace.service.resource.model.WsmResource in project terra-workspace-manager by DataBiosphere.

the class StairwayTestUtils method enumerateJobsDump.

public static void enumerateJobsDump(Alpha1Service alpha1Service, UUID workspaceId, AuthenticatedUserRequest userRequest) {
    EnumeratedJobs jobs = alpha1Service.enumerateJobs(workspaceId, userRequest, 1000, null, null, null, null, null);
    System.out.printf("Enumerated Jobs: total=%d, pageToken=%s%n", jobs.getTotalResults(), jobs.getPageToken());
    for (EnumeratedJob job : jobs.getResults()) {
        FlightState flightState = job.getFlightState();
        System.out.printf("  Job %s %s%n", flightState.getFlightId(), flightState.getFlightStatus());
        System.out.printf("    description: %s%n", job.getJobDescription());
        System.out.printf("    submitted  : %s%n", flightState.getSubmitted());
        System.out.printf("    completed  : %s%n", flightState.getCompleted().map(Instant::toString).orElse("<incomplete>"));
        if (flightState.getException().isPresent()) {
            System.out.printf("   error       : %s%n", flightState.getException().get().getMessage());
        }
        System.out.printf("    operation : %s%n", job.getOperationType());
        if (job.getResource().isPresent()) {
            WsmResource resource = job.getResource().get();
            System.out.println("    resource:");
            System.out.printf("      name: %s%n", resource.getName());
            System.out.printf("      id  : %s%n", resource.getResourceId());
            System.out.printf("      desc: %s%n", resource.getDescription());
            System.out.printf("      stew: %s%n", resource.getStewardshipType());
            System.out.printf("      type: %s%n", resource.getResourceType());
        }
    }
}
Also used : FlightState(bio.terra.stairway.FlightState) WsmResource(bio.terra.workspace.service.resource.model.WsmResource) Instant(java.time.Instant) EnumeratedJobs(bio.terra.workspace.service.workspace.model.EnumeratedJobs) EnumeratedJob(bio.terra.workspace.service.workspace.model.EnumeratedJob)

Example 3 with WsmResource

use of bio.terra.workspace.service.resource.model.WsmResource in project terra-workspace-manager by DataBiosphere.

the class ControlledResourceMetadataManager method validateControlledResourceAndAction.

/**
 * Convenience function that checks existence of a controlled resource within a workspace,
 * followed by an authorization check against that resource.
 *
 * <p>Throws ResourceNotFound from getResource if the workspace does not exist in the specified
 * workspace, regardless of the user's permission.
 *
 * <p>Throws InvalidControlledResourceException if the given resource is not controlled.
 *
 * <p>Throws ForbiddenException if the user is not permitted to perform the specified action on
 * the resource in question.
 *
 * @param userRequest the user's authenticated request
 * @param workspaceId if of the workspace this resource exists in
 * @param resourceId id of the resource in question
 * @param action the action to authorize against the resource
 * @return validated resource
 */
@Traced
public WsmResource validateControlledResourceAndAction(AuthenticatedUserRequest userRequest, UUID workspaceId, UUID resourceId, String action) {
    WsmResource resource = resourceDao.getResource(workspaceId, resourceId);
    ControlledResource controlledResource = resource.castToControlledResource();
    SamRethrow.onInterrupted(() -> samService.checkAuthz(userRequest, controlledResource.getCategory().getSamResourceName(), resourceId.toString(), action), "checkAuthz");
    return resource;
}
Also used : WsmResource(bio.terra.workspace.service.resource.model.WsmResource) ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) Traced(io.opencensus.contrib.spring.aop.Traced)

Example 4 with WsmResource

use of bio.terra.workspace.service.resource.model.WsmResource in project terra-workspace-manager by DataBiosphere.

the class DeleteSamResourceStep method doStep.

@Override
public StepResult doStep(FlightContext flightContext) throws InterruptedException {
    WsmResource wsmResource = resourceDao.getResource(workspaceId, resourceId);
    ControlledResource resource = wsmResource.castToControlledResource();
    // deleteControlledResource already handles duplicate deletion, so we do not need to explicitly
    // handle it inside this step.
    samService.deleteControlledResource(resource, userRequest);
    return StepResult.getStepResultSuccess();
}
Also used : WsmResource(bio.terra.workspace.service.resource.model.WsmResource) ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource)

Example 5 with WsmResource

use of bio.terra.workspace.service.resource.model.WsmResource in project terra-workspace-manager by DataBiosphere.

the class CloneAllResourcesFlight method addFlightLaunchStepsForResource.

private void addFlightLaunchStepsForResource(ResourceWithFlightId resourceWithFlightId, FlightBeanBag flightBeanBag) {
    final WsmResource resource = resourceWithFlightId.getResource();
    switch(resource.getStewardshipType()) {
        case REFERENCED:
            addStep(new LaunchCreateReferenceResourceFlightStep(flightBeanBag.getReferencedResourceService(), resourceWithFlightId.getResource().castToReferencedResource(), resourceWithFlightId.getFlightId()));
            addStep(new AwaitCreateReferenceResourceFlightStep(resourceWithFlightId.getResource().castToReferencedResource(), resourceWithFlightId.getFlightId(), flightBeanBag.getResourceDao()), RetryRules.cloudLongRunning());
            break;
        case CONTROLLED:
            switch(resourceWithFlightId.getResource().getResourceType()) {
                case CONTROLLED_GCP_GCS_BUCKET:
                    addStep(new LaunchCloneGcsBucketResourceFlightStep(resource.castByEnum(WsmResourceType.CONTROLLED_GCP_GCS_BUCKET), resourceWithFlightId.getFlightId()));
                    addStep(new AwaitCloneGcsBucketResourceFlightStep(resource.castByEnum(WsmResourceType.CONTROLLED_GCP_GCS_BUCKET), resourceWithFlightId.getFlightId()), RetryRules.cloudLongRunning());
                    break;
                case CONTROLLED_GCP_BIG_QUERY_DATASET:
                    addStep(new LaunchCloneControlledGcpBigQueryDatasetResourceFlightStep(resource.castByEnum(WsmResourceType.CONTROLLED_GCP_BIG_QUERY_DATASET), resourceWithFlightId.getFlightId()));
                    addStep(new AwaitCloneControlledGcpBigQueryDatasetResourceFlightStep(resource.castByEnum(WsmResourceType.CONTROLLED_GCP_BIG_QUERY_DATASET), resourceWithFlightId.getFlightId()), RetryRules.cloudLongRunning());
                    break;
                case CONTROLLED_GCP_AI_NOTEBOOK_INSTANCE:
                default:
                    // Can't throw in a flight constructor
                    logger.error("Unsupported controlled resource type {}", resourceWithFlightId.getResource().getResourceType());
                    break;
            }
            break;
        default:
            logger.error("Unsupported stewardship type {}", resourceWithFlightId.getResource().getStewardshipType());
            break;
    }
}
Also used : WsmResource(bio.terra.workspace.service.resource.model.WsmResource)

Aggregations

WsmResource (bio.terra.workspace.service.resource.model.WsmResource)18 UUID (java.util.UUID)6 Test (org.junit.jupiter.api.Test)4 FlightState (bio.terra.stairway.FlightState)3 StepResult (bio.terra.stairway.StepResult)3 BaseUnitTest (bio.terra.workspace.common.BaseUnitTest)3 AuthenticatedUserRequest (bio.terra.workspace.service.iam.AuthenticatedUserRequest)3 ControlledResource (bio.terra.workspace.service.resource.controlled.model.ControlledResource)3 EnumeratedJob (bio.terra.workspace.service.workspace.model.EnumeratedJob)3 EnumeratedJobs (bio.terra.workspace.service.workspace.model.EnumeratedJobs)3 FlightMap (bio.terra.stairway.FlightMap)2 ControllerValidationUtils (bio.terra.workspace.common.utils.ControllerValidationUtils)2 ApiResourceType (bio.terra.workspace.generated.model.ApiResourceType)2 ApiStewardshipType (bio.terra.workspace.generated.model.ApiStewardshipType)2 AuthenticatedUserRequestFactory (bio.terra.workspace.service.iam.AuthenticatedUserRequestFactory)2 StewardshipType (bio.terra.workspace.service.resource.model.StewardshipType)2 WsmResourceFamily (bio.terra.workspace.service.resource.model.WsmResourceFamily)2 WorkspaceService (bio.terra.workspace.service.workspace.WorkspaceService)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2