Search in sources :

Example 1 with AzureCloudContext

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

the class AzureWorkspaceTest method createGetDeleteAzureContext.

@Test
void createGetDeleteAzureContext() {
    AuthenticatedUserRequest userRequest = new AuthenticatedUserRequest().token(Optional.of("fake-token")).email("fake@email.com").subjectId("fakeID123");
    Workspace request = Workspace.builder().workspaceId(UUID.randomUUID()).spendProfileId(spendUtils.defaultSpendId()).workspaceStage(WorkspaceStage.MC_WORKSPACE).build();
    workspaceService.createWorkspace(request, userRequest);
    String jobId = UUID.randomUUID().toString();
    AzureCloudContext azureCloudContext = new AzureCloudContext(azureTestConfiguration.getTenantId(), azureTestConfiguration.getSubscriptionId(), azureTestConfiguration.getManagedResourceGroupId());
    workspaceService.createAzureCloudContext(request.getWorkspaceId(), jobId, userRequest, "/fake/value", azureCloudContext);
    jobService.waitForJob(jobId);
    assertNull(jobService.retrieveJobResult(jobId, Object.class, userRequest).getException());
    assertTrue(testUtils.getAuthorizedAzureCloudContext(request.getWorkspaceId(), userRequest).isPresent());
    workspaceService.deleteAzureCloudContext(request.getWorkspaceId(), userRequest);
    assertTrue(testUtils.getAuthorizedAzureCloudContext(request.getWorkspaceId(), userRequest).isEmpty());
}
Also used : AzureCloudContext(bio.terra.workspace.service.workspace.model.AzureCloudContext) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) Workspace(bio.terra.workspace.service.workspace.model.Workspace) Test(org.junit.jupiter.api.Test) BaseAzureTest(bio.terra.workspace.common.BaseAzureTest)

Example 2 with AzureCloudContext

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

the class ValidateMRGStep method doStep.

@Override
public StepResult doStep(FlightContext flightContext) throws InterruptedException {
    AzureCloudContext azureCloudContext = flightContext.getInputParameters().get(JobMapKeys.REQUEST.getKeyName(), AzureCloudContext.class);
    try {
        ResourceManager resourceManager = crlService.getResourceManager(azureCloudContext, azureConfig);
        resourceManager.resourceGroups().getByName(azureCloudContext.getAzureResourceGroupId());
    } catch (Exception azureError) {
        throw new CloudContextRequiredException("Invalid Azure cloud context", azureError);
    }
    return StepResult.getStepResultSuccess();
}
Also used : CloudContextRequiredException(bio.terra.workspace.service.workspace.exceptions.CloudContextRequiredException) AzureCloudContext(bio.terra.workspace.service.workspace.model.AzureCloudContext) ResourceManager(com.azure.resourcemanager.resources.ResourceManager) CloudContextRequiredException(bio.terra.workspace.service.workspace.exceptions.CloudContextRequiredException)

Example 3 with AzureCloudContext

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

the class GetAzureStorageStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
    final AzureCloudContext azureCloudContext = context.getWorkingMap().get(ControlledResourceKeys.AZURE_CLOUD_CONTEXT, AzureCloudContext.class);
    StorageManager storageManager = crlService.getStorageManager(azureCloudContext, azureConfig);
    if (storageManager.storageAccounts().checkNameAvailability(resource.getStorageAccountName()).isAvailable()) {
        return StepResult.getStepResultSuccess();
    }
    return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, new DuplicateResourceException(String.format("An Azure Storage Account with name %s already exists", resource.getStorageAccountName())));
}
Also used : AzureCloudContext(bio.terra.workspace.service.workspace.model.AzureCloudContext) StorageManager(com.azure.resourcemanager.storage.StorageManager) StepResult(bio.terra.stairway.StepResult) DuplicateResourceException(bio.terra.workspace.service.resource.exception.DuplicateResourceException)

Example 4 with AzureCloudContext

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

the class GetAzureIpStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
    final AzureCloudContext azureCloudContext = context.getWorkingMap().get(ControlledResourceKeys.AZURE_CLOUD_CONTEXT, AzureCloudContext.class);
    ComputeManager computeManager = crlService.getComputeManager(azureCloudContext, azureConfig);
    try {
        computeManager.networkManager().publicIpAddresses().getByResourceGroup(azureCloudContext.getAzureResourceGroupId(), resource.getIpName());
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, new DuplicateResourceException(String.format("An Azure IP with name %s already exists in resource group %s", azureCloudContext.getAzureResourceGroupId(), resource.getIpName())));
    } catch (ManagementException e) {
        // https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/common-deployment-errors
        if (StringUtils.equals(e.getValue().getCode(), "ResourceNotFound")) {
            return StepResult.getStepResultSuccess();
        }
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
}
Also used : ManagementException(com.azure.core.management.exception.ManagementException) AzureCloudContext(bio.terra.workspace.service.workspace.model.AzureCloudContext) StepResult(bio.terra.stairway.StepResult) ComputeManager(com.azure.resourcemanager.compute.ComputeManager) DuplicateResourceException(bio.terra.workspace.service.resource.exception.DuplicateResourceException)

Example 5 with AzureCloudContext

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

the class CreateAzureNetworkStep method undoStep.

@Override
public StepResult undoStep(FlightContext context) throws InterruptedException {
    final AzureCloudContext azureCloudContext = context.getWorkingMap().get(ControlledResourceKeys.AZURE_CLOUD_CONTEXT, AzureCloudContext.class);
    ComputeManager computeManager = crlService.getComputeManager(azureCloudContext, azureConfig);
    try {
        computeManager.networkManager().networks().deleteByResourceGroup(azureCloudContext.getAzureResourceGroupId(), resource.getNetworkName());
    } catch (ManagementException e) {
        // Stairway steps may run multiple times, so we may already have deleted this resource.
        if (StringUtils.equals(e.getValue().getCode(), "ResourceNotFound")) {
            logger.info("Azure Network {} in managed resource group {} already deleted", resource.getNetworkName(), azureCloudContext.getAzureResourceGroupId());
            return StepResult.getStepResultSuccess();
        }
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : ManagementException(com.azure.core.management.exception.ManagementException) AzureCloudContext(bio.terra.workspace.service.workspace.model.AzureCloudContext) StepResult(bio.terra.stairway.StepResult) ComputeManager(com.azure.resourcemanager.compute.ComputeManager)

Aggregations

AzureCloudContext (bio.terra.workspace.service.workspace.model.AzureCloudContext)29 StepResult (bio.terra.stairway.StepResult)23 ManagementException (com.azure.core.management.exception.ManagementException)17 ComputeManager (com.azure.resourcemanager.compute.ComputeManager)16 DuplicateResourceException (bio.terra.workspace.service.resource.exception.DuplicateResourceException)6 RelayManager (com.azure.resourcemanager.relay.RelayManager)4 StorageManager (com.azure.resourcemanager.storage.StorageManager)3 BaseAzureTest (bio.terra.workspace.common.BaseAzureTest)2 AuthenticatedUserRequest (bio.terra.workspace.service.iam.AuthenticatedUserRequest)2 Test (org.junit.jupiter.api.Test)2 FlightMap (bio.terra.stairway.FlightMap)1 FlightState (bio.terra.stairway.FlightState)1 InternalLogicException (bio.terra.workspace.common.exception.InternalLogicException)1 ControlledAzureDiskResource (bio.terra.workspace.service.resource.controlled.cloud.azure.disk.ControlledAzureDiskResource)1 ControlledAzureIpResource (bio.terra.workspace.service.resource.controlled.cloud.azure.ip.ControlledAzureIpResource)1 ControlledAzureNetworkResource (bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource)1 CloudContextRequiredException (bio.terra.workspace.service.workspace.exceptions.CloudContextRequiredException)1 CloudContextHolder (bio.terra.workspace.service.workspace.model.CloudContextHolder)1 GcpCloudContext (bio.terra.workspace.service.workspace.model.GcpCloudContext)1 Workspace (bio.terra.workspace.service.workspace.model.Workspace)1