Search in sources :

Example 6 with ComputeManager

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

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

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

the class DeleteAzureVmStep 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/virtualMachines/%s", azureCloudContext.getAzureSubscriptionId(), azureCloudContext.getAzureResourceGroupId(), resource.getVmName());
    try {
        logger.info("Attempting to delete vm " + azureResourceId);
        VirtualMachine resolvedVm = computeManager.virtualMachines().getByResourceGroup(azureCloudContext.getAzureResourceGroupId(), resource.getVmName());
        computeManager.virtualMachines().deleteById(azureResourceId);
        resolvedVm.networkInterfaceIds().forEach(nic -> computeManager.networkManager().networkInterfaces().deleteById(nic));
        // Delete the OS disk
        computeManager.disks().deleteById(resolvedVm.osDiskId());
        return StepResult.getStepResultSuccess();
    } catch (Exception ex) {
        logger.info("Attempt to delete Azure vm 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) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine)

Example 8 with ComputeManager

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

the class CreateAzureVmStep 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.virtualMachines().deleteByResourceGroup(azureCloudContext.getAzureResourceGroupId(), resource.getVmName());
    } 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 VM {} in managed resource group {} already deleted", resource.getVmName(), 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 9 with ComputeManager

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

the class CreateAzureVmStep 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);
    final ControlledAzureIpResource ipResource = resourceDao.getResource(resource.getWorkspaceId(), resource.getIpId()).castByEnum(WsmResourceType.CONTROLLED_AZURE_IP);
    final ControlledAzureDiskResource diskResource = resourceDao.getResource(resource.getWorkspaceId(), resource.getDiskId()).castByEnum(WsmResourceType.CONTROLLED_AZURE_DISK);
    final ControlledAzureNetworkResource networkResource = resourceDao.getResource(resource.getWorkspaceId(), resource.getNetworkId()).castByEnum(WsmResourceType.CONTROLLED_AZURE_NETWORK);
    try {
        Disk existingAzureDisk = computeManager.disks().getByResourceGroup(azureCloudContext.getAzureResourceGroupId(), diskResource.getDiskName());
        PublicIpAddress existingAzureIp = computeManager.networkManager().publicIpAddresses().getByResourceGroup(azureCloudContext.getAzureResourceGroupId(), ipResource.getIpName());
        Network existingNetwork = computeManager.networkManager().networks().getByResourceGroup(azureCloudContext.getAzureResourceGroupId(), networkResource.getNetworkName());
        computeManager.virtualMachines().define(resource.getVmName()).withRegion(resource.getRegion()).withExistingResourceGroup(azureCloudContext.getAzureResourceGroupId()).withExistingPrimaryNetwork(existingNetwork).withSubnet(networkResource.getSubnetName()).withPrimaryPrivateIPAddressDynamic().withExistingPrimaryPublicIPAddress(existingAzureIp).withSpecializedLinuxCustomImage(resource.getVmImageUri()).withExistingDataDisk(existingAzureDisk).withTag("workspaceId", resource.getWorkspaceId().toString()).withTag("resourceId", resource.getResourceId().toString()).withSize(VirtualMachineSizeTypes.fromString(resource.getVmSize())).create(Defaults.buildContext(CreateVirtualMachineRequestData.builder().setName(resource.getVmName()).setRegion(Region.fromName(resource.getRegion())).setTenantId(azureCloudContext.getAzureTenantId()).setSubscriptionId(azureCloudContext.getAzureSubscriptionId()).setResourceGroupName(azureCloudContext.getAzureResourceGroupId()).setNetwork(existingNetwork).setSubnetName(networkResource.getSubnetName()).setDisk(existingAzureDisk).setPublicIpAddress(existingAzureIp).setImage(resource.getVmImageUri()).build()));
    } catch (ManagementException e) {
        // https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/common-deployment-errors
        if (StringUtils.equals(e.getValue().getCode(), "Conflict")) {
            logger.info("Azure Vm {} in managed resource group {} already exists", resource.getVmName(), azureCloudContext.getAzureResourceGroupId());
            return StepResult.getStepResultSuccess();
        }
        if (StringUtils.equals(e.getValue().getCode(), "ResourceNotFound")) {
            logger.info("Either the disk, ip, or network passed into this createVm does not exist " + String.format("%nResource Group: %s%n\tIp Name: %s%n\tNetwork Name: %s%n\tDisk Name: %s", azureCloudContext.getAzureResourceGroupId(), ipResource.getIpName(), "TODO", diskResource.getDiskName()));
            return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, e);
        }
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : ControlledAzureIpResource(bio.terra.workspace.service.resource.controlled.cloud.azure.ip.ControlledAzureIpResource) PublicIpAddress(com.azure.resourcemanager.network.models.PublicIpAddress) ManagementException(com.azure.core.management.exception.ManagementException) AzureCloudContext(bio.terra.workspace.service.workspace.model.AzureCloudContext) ControlledAzureDiskResource(bio.terra.workspace.service.resource.controlled.cloud.azure.disk.ControlledAzureDiskResource) Network(com.azure.resourcemanager.network.models.Network) StepResult(bio.terra.stairway.StepResult) Disk(com.azure.resourcemanager.compute.models.Disk) ComputeManager(com.azure.resourcemanager.compute.ComputeManager) ControlledAzureNetworkResource(bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource)

Example 10 with ComputeManager

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

the class GetAzureVmStep 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.virtualMachines().getByResourceGroup(azureCloudContext.getAzureResourceGroupId(), resource.getVmName());
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, new DuplicateResourceException(String.format("An Azure VM with name %s already exists in resource group %s", azureCloudContext.getAzureResourceGroupId(), resource.getVmName())));
    } 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)18 StepResult (bio.terra.stairway.StepResult)16 AzureCloudContext (bio.terra.workspace.service.workspace.model.AzureCloudContext)16 ManagementException (com.azure.core.management.exception.ManagementException)13 DuplicateResourceException (bio.terra.workspace.service.resource.exception.DuplicateResourceException)4 ControlledAzureDiskResource (bio.terra.workspace.service.resource.controlled.cloud.azure.disk.ControlledAzureDiskResource)2 ControlledAzureIpResource (bio.terra.workspace.service.resource.controlled.cloud.azure.ip.ControlledAzureIpResource)2 ControlledAzureNetworkResource (bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource)2 VirtualMachine (com.azure.resourcemanager.compute.models.VirtualMachine)2 FlightState (bio.terra.stairway.FlightState)1 BaseAzureTest (bio.terra.workspace.common.BaseAzureTest)1 ApiAzureVmCreationParameters (bio.terra.workspace.generated.model.ApiAzureVmCreationParameters)1 AuthenticatedUserRequest (bio.terra.workspace.service.iam.AuthenticatedUserRequest)1 ControlledAzureVmResource (bio.terra.workspace.service.resource.controlled.cloud.azure.vm.ControlledAzureVmResource)1 ControlledResource (bio.terra.workspace.service.resource.controlled.model.ControlledResource)1 WsmResource (bio.terra.workspace.service.resource.model.WsmResource)1 TokenCredential (com.azure.core.credential.TokenCredential)1 AzureProfile (com.azure.core.management.profile.AzureProfile)1 Disk (com.azure.resourcemanager.compute.models.Disk)1 Network (com.azure.resourcemanager.network.models.Network)1