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();
}
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;
}
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);
}
Aggregations