Search in sources :

Example 1 with PublicIpAddress

use of com.azure.resourcemanager.network.models.PublicIpAddress 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 2 with PublicIpAddress

use of com.azure.resourcemanager.network.models.PublicIpAddress in project terra-cloud-resource-lib by DataBiosphere.

the class ComputeRequestDataTest method mockPublicIpAddress.

private static PublicIpAddress mockPublicIpAddress() {
    PublicIpAddress mock = mock(PublicIpAddress.class);
    when(mock.name()).thenReturn("my-ip");
    return mock;
}
Also used : PublicIpAddress(com.azure.resourcemanager.network.models.PublicIpAddress)

Aggregations

PublicIpAddress (com.azure.resourcemanager.network.models.PublicIpAddress)2 StepResult (bio.terra.stairway.StepResult)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 AzureCloudContext (bio.terra.workspace.service.workspace.model.AzureCloudContext)1 ManagementException (com.azure.core.management.exception.ManagementException)1 ComputeManager (com.azure.resourcemanager.compute.ComputeManager)1 Disk (com.azure.resourcemanager.compute.models.Disk)1 Network (com.azure.resourcemanager.network.models.Network)1