Search in sources :

Example 16 with ComputeManager

use of com.azure.resourcemanager.compute.ComputeManager in project terra-workspace-manager by DataBiosphere.

the class CreateAzureDiskStep 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.disks().deleteByResourceGroup(azureCloudContext.getAzureResourceGroupId(), resource.getDiskName());
    } 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 Disk {} in managed resource group {} already deleted", resource.getDiskName(), 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)

Example 17 with ComputeManager

use of com.azure.resourcemanager.compute.ComputeManager in project terra-workspace-manager by DataBiosphere.

the class DeleteAzureDiskStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException {
    final AzureCloudContext azureCloudContext = context.getWorkingMap().get(ControlledResourceKeys.AZURE_CLOUD_CONTEXT, AzureCloudContext.class);
    ComputeManager computeManager = crlService.getComputeManager(azureCloudContext, azureConfig);
    var azureResourceId = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/disks/%s", azureCloudContext.getAzureSubscriptionId(), azureCloudContext.getAzureResourceGroupId(), resource.getDiskName());
    try {
        logger.info("Attempting to delete disk " + azureResourceId);
        computeManager.disks().deleteById(azureResourceId);
        return StepResult.getStepResultSuccess();
    } catch (Exception ex) {
        logger.info("Attempt to delete Azure disk failed on this try: " + azureResourceId, ex);
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, ex);
    }
}
Also used : AzureCloudContext(bio.terra.workspace.service.workspace.model.AzureCloudContext) StepResult(bio.terra.stairway.StepResult) ComputeManager(com.azure.resourcemanager.compute.ComputeManager)

Example 18 with ComputeManager

use of com.azure.resourcemanager.compute.ComputeManager in project terra-workspace-manager by DataBiosphere.

the class CreateAndDeleteAzureControlledResourceFlightTest method createAndDeleteAzureVmControlledResourceWithCustomScriptExtensionWithNoPublicIp.

@Test
public void createAndDeleteAzureVmControlledResourceWithCustomScriptExtensionWithNoPublicIp() throws InterruptedException {
    // Setup workspace and cloud context
    UUID workspaceUuid = azureTestUtils.createWorkspace(workspaceService);
    AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest();
    createCloudContext(workspaceUuid, userRequest);
    // Create disk
    ControlledAzureDiskResource diskResource = createDisk(workspaceUuid, userRequest);
    // Create network
    ControlledAzureNetworkResource networkResource = createNetwork(workspaceUuid, userRequest);
    final ApiAzureVmCreationParameters creationParameters = ControlledResourceFixtures.getAzureVmCreationParametersWithCustomScriptExtension();
    // TODO: make this application-private resource once the POC supports it
    final UUID resourceId = UUID.randomUUID();
    ControlledAzureVmResource resource = ControlledAzureVmResource.builder().common(ControlledResourceFields.builder().workspaceUuid(workspaceUuid).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()).vmImage(AzureVmUtils.getImageData(creationParameters.getVmImage())).region(creationParameters.getRegion()).diskId(diskResource.getResourceId()).networkId(networkResource.getResourceId()).build();
    // Submit a VM creation flight and verify the resource exists in the workspace.
    createVMResource(workspaceUuid, userRequest, resource, creationParameters);
    // Exercise resource enumeration for the underlying resources.
    // Verify that the resources we created are in the enumeration.
    List<WsmResource> resourceList = wsmResourceService.enumerateResources(workspaceUuid, null, null, 0, 100, userRequest);
    // checkForResource(resourceList, ipResource);
    checkForResource(resourceList, diskResource);
    checkForResource(resourceList, networkResource);
    checkForResource(resourceList, resource);
    ComputeManager computeManager = azureTestUtils.getComputeManager();
    final VirtualMachine resolvedVm = getVirtualMachine(creationParameters, computeManager);
    // Submit a VM deletion flight.
    FlightState deleteFlightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), DeleteControlledResourceFlight.class, azureTestUtils.deleteControlledResourceInputParameters(workspaceUuid, 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()));
    // clean up resources - we need to remove only network since current VM has no public ip;
    submitControlledResourceDeletionFlight(workspaceUuid, userRequest, networkResource, azureTestUtils.getAzureCloudContext().getAzureResourceGroupId(), networkResource.getNetworkName(), azureTestUtils.getComputeManager().networkManager().networks()::getByResourceGroup);
    // we need to create data disk as well
    submitControlledResourceDeletionFlight(workspaceUuid, userRequest, diskResource, azureTestUtils.getAzureCloudContext().getAzureResourceGroupId(), diskResource.getDiskName(), azureTestUtils.getComputeManager().disks()::getByResourceGroup);
}
Also used : 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) ComputeManager(com.azure.resourcemanager.compute.ComputeManager) FlightState(bio.terra.stairway.FlightState) ControlledAzureDiskResource(bio.terra.workspace.service.resource.controlled.cloud.azure.disk.ControlledAzureDiskResource) 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 19 with ComputeManager

use of com.azure.resourcemanager.compute.ComputeManager in project terra-workspace-manager by DataBiosphere.

the class DeleteAzureNetworkStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException {
    final AzureCloudContext azureCloudContext = context.getWorkingMap().get(ControlledResourceKeys.AZURE_CLOUD_CONTEXT, AzureCloudContext.class);
    ComputeManager computeManager = crlService.getComputeManager(azureCloudContext, azureConfig);
    var azureNetworkResourceId = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/network/%s", azureCloudContext.getAzureSubscriptionId(), azureCloudContext.getAzureResourceGroupId(), resource.getNetworkName());
    var azureSubnetResourceId = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s/subnets/%s", azureCloudContext.getAzureSubscriptionId(), azureCloudContext.getAzureResourceGroupId(), resource.getNetworkName(), resource.getSubnetName());
    try {
        logger.info("Attempting to delete network " + azureNetworkResourceId);
        // Delete network first to remove association between subnet and network
        computeManager.networkManager().networks().deleteById(azureNetworkResourceId);
        computeManager.networkManager().networkSecurityGroups().deleteById(azureSubnetResourceId);
        return StepResult.getStepResultSuccess();
    } catch (Exception ex) {
        logger.info("Attempt to delete Azure network failed on this try: " + azureNetworkResourceId, ex);
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, ex);
    }
}
Also used : AzureCloudContext(bio.terra.workspace.service.workspace.model.AzureCloudContext) StepResult(bio.terra.stairway.StepResult) ComputeManager(com.azure.resourcemanager.compute.ComputeManager)

Example 20 with ComputeManager

use of com.azure.resourcemanager.compute.ComputeManager in project terra-workspace-manager by DataBiosphere.

the class GetAzureNetworkStep 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().networks().getByResourceGroup(azureCloudContext.getAzureResourceGroupId(), resource.getNetworkName());
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, new DuplicateResourceException(String.format("An Azure Network with name %s already exists in resource group %s", azureCloudContext.getAzureResourceGroupId(), resource.getNetworkName())));
    } 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)

Aggregations

ComputeManager (com.azure.resourcemanager.compute.ComputeManager)20 AzureCloudContext (bio.terra.workspace.service.workspace.model.AzureCloudContext)16 StepResult (bio.terra.stairway.StepResult)14 ManagementException (com.azure.core.management.exception.ManagementException)11 ControlledAzureDiskResource (bio.terra.workspace.service.resource.controlled.cloud.azure.disk.ControlledAzureDiskResource)4 ControlledAzureNetworkResource (bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource)4 DuplicateResourceException (bio.terra.workspace.service.resource.exception.DuplicateResourceException)4 FlightState (bio.terra.stairway.FlightState)3 BaseAzureTest (bio.terra.workspace.common.BaseAzureTest)3 AuthenticatedUserRequest (bio.terra.workspace.service.iam.AuthenticatedUserRequest)3 ControlledAzureIpResource (bio.terra.workspace.service.resource.controlled.cloud.azure.ip.ControlledAzureIpResource)3 ControlledAzureVmResource (bio.terra.workspace.service.resource.controlled.cloud.azure.vm.ControlledAzureVmResource)3 WsmResource (bio.terra.workspace.service.resource.model.WsmResource)3 VirtualMachine (com.azure.resourcemanager.compute.models.VirtualMachine)3 UUID (java.util.UUID)3 Test (org.junit.jupiter.api.Test)3 FlightMap (bio.terra.stairway.FlightMap)1 TokenCredential (com.azure.core.credential.TokenCredential)1 AzureProfile (com.azure.core.management.profile.AzureProfile)1 Disk (com.azure.resourcemanager.compute.models.Disk)1