Search in sources :

Example 1 with Network

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

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

the class ComputeRequestDataTest method mockNetwork.

private static Network mockNetwork() {
    Network mock = mock(Network.class);
    when(mock.name()).thenReturn("my-network");
    return mock;
}
Also used : Network(com.azure.resourcemanager.network.models.Network) AzureNetwork(bio.terra.janitor.model.AzureNetwork)

Example 3 with Network

use of com.azure.resourcemanager.network.models.Network in project terra-workspace-manager by DataBiosphere.

the class CreateAzureVmStepTest method setup.

@BeforeEach
public void setup() {
    when(mockCrlService.getComputeManager(any(AzureCloudContext.class), any(AzureConfiguration.class))).thenReturn(mockComputeManager);
    when(mockAzureCloudContext.getAzureResourceGroupId()).thenReturn(STUB_STRING_RETURN);
    when(mockAzureCloudContext.getAzureTenantId()).thenReturn(STUB_STRING_RETURN);
    when(mockAzureCloudContext.getAzureSubscriptionId()).thenReturn(STUB_STRING_RETURN);
    when(mockComputeManager.virtualMachines()).thenReturn(mockVms);
    // get disk mocks
    when(mockComputeManager.disks()).thenReturn(mockDisks);
    when(mockDisks.getByResourceGroup(anyString(), anyString())).thenReturn(mockDisk);
    // get ip mocks
    when(mockComputeManager.networkManager()).thenReturn(mockNetworkManager);
    when(mockNetworkManager.publicIpAddresses()).thenReturn(mockPublicIpAddresses);
    when(mockPublicIpAddresses.getByResourceGroup(anyString(), anyString())).thenReturn(mockPublicIpAddress);
    // get network mocks
    when(mockNetworkManager.networks()).thenReturn(mockNetworks);
    when(mockNetworks.getByResourceGroup(anyString(), anyString())).thenReturn(mockNetwork);
    // create network security group mocks
    when(mockNetworkManager.networkSecurityGroups()).thenReturn(mockNsgs);
    when(mockNsgs.define(anyString())).thenReturn(mockNetworkStage1);
    when(mockNetworkStage1.withRegion(anyString())).thenReturn(mockNetworkStage1a);
    when(mockNetworkStage1a.withExistingResourceGroup(anyString())).thenReturn(mockNetworkStage2);
    when(mockNetworkStage2.withTag(anyString(), anyString())).thenReturn(mockNetworkStage2);
    when(mockNetworkStage2.defineRule(anyString())).thenReturn(mockNetworkStage3);
    when(mockNetworkStage3.allowInbound()).thenReturn(mockNetworkStage4);
    when(mockNetworkStage3.denyOutbound()).thenReturn(mockNetworkStage4);
    when(mockNetworkStage4.fromAddress(anyString())).thenReturn(mockNetworkStage5);
    when(mockNetworkStage4.fromAnyAddress()).thenReturn(mockNetworkStage5);
    when(mockNetworkStage5.fromAnyPort()).thenReturn(mockNetworkStage6);
    when(mockNetworkStage6.toAnyAddress()).thenReturn(mockNetworkStage7);
    when(mockNetworkStage6.toAddress(anyString())).thenReturn(mockNetworkStage7);
    when(mockNetworkStage7.toPort(anyInt())).thenReturn(mockNetworkStage8);
    when(mockNetworkStage7.toAnyPort()).thenReturn(mockNetworkStage8);
    when(mockNetworkStage8.withProtocol(any(SecurityRuleProtocol.class))).thenReturn(mockNetworkStage9);
    when(mockNetworkStage8.withAnyProtocol()).thenReturn(mockNetworkStage9);
    when(mockNetworkStage9.attach()).thenReturn(mockNetworkStage2);
    when(mockNetworkStage2.create(any(Context.class))).thenReturn(mockNsg);
    // create network mocks
    when(mockNetworks.define(anyString())).thenReturn(mockNetworkStage10);
    when(mockNetworkStage10.withRegion(anyString())).thenReturn(mockNetworkStage11);
    when(mockNetworkStage11.withExistingResourceGroup(anyString())).thenReturn(mockNetworkStage12);
    when(mockNetworkStage12.withTag(anyString(), anyString())).thenReturn(mockNetworkStage12);
    when(mockNetworkStage12.withAddressSpace(anyString())).thenReturn(mockNetworkStage13);
    when(mockNetworkStage13.defineSubnet(anyString())).thenReturn(mockNetworkStage14);
    when(mockNetworkStage14.withAddressPrefix(anyString())).thenReturn(mockNetworkStage15);
    when(mockNetworkStage15.withExistingNetworkSecurityGroup(any(NetworkSecurityGroup.class))).thenReturn(mockNetworkStage15);
    when(mockNetworkStage15.attach()).thenReturn(mockNetworkStage13);
    when(mockNetworkStage13.create(any(Context.class))).thenReturn(mockNetwork);
    // Creation vm stages mocks
    when(mockVms.define(anyString())).thenReturn(mockVmStage1);
    when(mockVmStage1.withRegion(anyString())).thenReturn(mockVmStage2);
    when(mockVmStage2.withExistingResourceGroup(anyString())).thenReturn(mockVmStage3);
    when(mockVmStage3.withExistingPrimaryNetwork(any(Network.class))).thenReturn(mockVmStage4);
    when(mockVmStage4.withSubnet(anyString())).thenReturn(mockVmStage5);
    when(mockVmStage5.withPrimaryPrivateIPAddressDynamic()).thenReturn(mockVmStage6);
    when(mockVmStage6.withExistingPrimaryPublicIPAddress(any(PublicIpAddress.class))).thenReturn(mockVmStage7);
    when(mockVmStage7.withSpecializedLinuxCustomImage(anyString())).thenReturn(mockVmStage10);
    when(mockVmStage10.withExistingDataDisk(any(Disk.class))).thenReturn(mockVmStage11);
    when(mockVmStage11.withTag(anyString(), anyString())).thenReturn(mockVmStage11a);
    when(mockVmStage11a.withTag(anyString(), anyString())).thenReturn(mockVmStage12);
    when(mockVmStage12.withSize(any(VirtualMachineSizeTypes.class))).thenReturn(mockVmStage12);
    when(mockVmStage12.create(any(Context.class))).thenReturn(mockVm);
    // Resource dao mocks
    when(mockResourceDao.getResource(any(UUID.class), any(UUID.class))).thenReturn(mockWsmResource);
    when(mockWsmResource.castByEnum(WsmResourceType.CONTROLLED_AZURE_DISK)).thenReturn(mockAzureDiskResource);
    when(mockWsmResource.castByEnum(WsmResourceType.CONTROLLED_AZURE_IP)).thenReturn(mockAzureIpResource);
    when(mockWsmResource.castByEnum(WsmResourceType.CONTROLLED_AZURE_NETWORK)).thenReturn(mockAzureNetworkResource);
    // Resource mocks
    when(mockAzureDiskResource.getDiskName()).thenReturn(STUB_DISK_NAME);
    when(mockAzureIpResource.getIpName()).thenReturn(STUB_IP_NAME);
    when(mockAzureNetworkResource.getNetworkName()).thenReturn(STUB_NETWORK_NAME);
    when(mockAzureNetworkResource.getSubnetName()).thenReturn(STUB_SUBNET_NAME);
    // Exception mock
    when(mockException.getValue()).thenReturn(new ManagementError("Conflict", "Resource already exists."));
    when(mockFlightContext.getWorkingMap()).thenReturn(mockWorkingMap);
    when(mockWorkingMap.get(ControlledResourceKeys.AZURE_CLOUD_CONTEXT, AzureCloudContext.class)).thenReturn(mockAzureCloudContext);
}
Also used : AzureCloudContext(bio.terra.workspace.service.workspace.model.AzureCloudContext) FlightContext(bio.terra.stairway.FlightContext) Context(com.azure.core.util.Context) PublicIpAddress(com.azure.resourcemanager.network.models.PublicIpAddress) NetworkSecurityGroup(com.azure.resourcemanager.network.models.NetworkSecurityGroup) AzureCloudContext(bio.terra.workspace.service.workspace.model.AzureCloudContext) AzureConfiguration(bio.terra.workspace.app.configuration.external.AzureConfiguration) Network(com.azure.resourcemanager.network.models.Network) ManagementError(com.azure.core.management.exception.ManagementError) VirtualMachineSizeTypes(com.azure.resourcemanager.compute.models.VirtualMachineSizeTypes) UUID(java.util.UUID) Disk(com.azure.resourcemanager.compute.models.Disk) SecurityRuleProtocol(com.azure.resourcemanager.network.models.SecurityRuleProtocol) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

Network (com.azure.resourcemanager.network.models.Network)3 AzureCloudContext (bio.terra.workspace.service.workspace.model.AzureCloudContext)2 Disk (com.azure.resourcemanager.compute.models.Disk)2 PublicIpAddress (com.azure.resourcemanager.network.models.PublicIpAddress)2 AzureNetwork (bio.terra.janitor.model.AzureNetwork)1 FlightContext (bio.terra.stairway.FlightContext)1 StepResult (bio.terra.stairway.StepResult)1 AzureConfiguration (bio.terra.workspace.app.configuration.external.AzureConfiguration)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 ManagementError (com.azure.core.management.exception.ManagementError)1 ManagementException (com.azure.core.management.exception.ManagementException)1 Context (com.azure.core.util.Context)1 ComputeManager (com.azure.resourcemanager.compute.ComputeManager)1 VirtualMachineSizeTypes (com.azure.resourcemanager.compute.models.VirtualMachineSizeTypes)1 NetworkSecurityGroup (com.azure.resourcemanager.network.models.NetworkSecurityGroup)1 SecurityRuleProtocol (com.azure.resourcemanager.network.models.SecurityRuleProtocol)1 UUID (java.util.UUID)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1