use of com.azure.resourcemanager.compute.ComputeManager in project terra-workspace-manager by DataBiosphere.
the class CreateAzureNetworkStep 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.networkManager().networks().deleteByResourceGroup(azureCloudContext.getAzureResourceGroupId(), resource.getNetworkName());
} 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 Network {} in managed resource group {} already deleted", resource.getNetworkName(), azureCloudContext.getAzureResourceGroupId());
return StepResult.getStepResultSuccess();
}
return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
}
return StepResult.getStepResultSuccess();
}
use of com.azure.resourcemanager.compute.ComputeManager in project terra-workspace-manager by DataBiosphere.
the class CreateAndDeleteAzureControlledResourceFlightTest method createAndDeleteAzureVmControlledResourceWithCustomScriptExtension.
@Test
public void createAndDeleteAzureVmControlledResourceWithCustomScriptExtension() throws InterruptedException {
// Setup workspace and cloud context
UUID workspaceUuid = azureTestUtils.createWorkspace(workspaceService);
AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest();
createCloudContext(workspaceUuid, userRequest);
// Create ip
ControlledAzureIpResource ipResource = createIp(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()).ipId(ipResource.getResourceId()).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 - vm deletion flight doesn't remove network and ip resources, so we need
// to remove them;
submitControlledResourceDeletionFlight(workspaceUuid, userRequest, networkResource, azureTestUtils.getAzureCloudContext().getAzureResourceGroupId(), networkResource.getNetworkName(), azureTestUtils.getComputeManager().networkManager().networks()::getByResourceGroup);
submitControlledResourceDeletionFlight(workspaceUuid, userRequest, ipResource, azureTestUtils.getAzureCloudContext().getAzureResourceGroupId(), ipResource.getIpName(), azureTestUtils.getComputeManager().networkManager().publicIpAddresses()::getByResourceGroup);
// we need to create data disk as well
submitControlledResourceDeletionFlight(workspaceUuid, userRequest, diskResource, azureTestUtils.getAzureCloudContext().getAzureResourceGroupId(), diskResource.getDiskName(), azureTestUtils.getComputeManager().disks()::getByResourceGroup);
}
use of com.azure.resourcemanager.compute.ComputeManager in project terra-workspace-manager by DataBiosphere.
the class CreateAndDeleteAzureControlledResourceFlightTest method createAndDeleteAzureVmControlledResource.
@Test
public void createAndDeleteAzureVmControlledResource() throws InterruptedException {
// Setup workspace and cloud context
UUID workspaceUuid = azureTestUtils.createWorkspace(workspaceService);
AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest();
createCloudContext(workspaceUuid, userRequest);
// Create ip
ControlledAzureIpResource ipResource = createIp(workspaceUuid, userRequest);
// Create disk
ControlledAzureDiskResource diskResource = createDisk(workspaceUuid, userRequest);
// Create network
ControlledAzureNetworkResource networkResource = createNetwork(workspaceUuid, userRequest);
final ApiAzureVmCreationParameters creationParameters = ControlledResourceFixtures.getAzureVmCreationParameters();
// 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()).ipId(ipResource.getResourceId()).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 - vm deletion flight doesn't remove network and ip resources, so we need
// to remove them;
submitControlledResourceDeletionFlight(workspaceUuid, userRequest, networkResource, azureTestUtils.getAzureCloudContext().getAzureResourceGroupId(), networkResource.getNetworkName(), azureTestUtils.getComputeManager().networkManager().networks()::getByResourceGroup);
submitControlledResourceDeletionFlight(workspaceUuid, userRequest, ipResource, azureTestUtils.getAzureCloudContext().getAzureResourceGroupId(), ipResource.getIpName(), azureTestUtils.getComputeManager().networkManager().publicIpAddresses()::getByResourceGroup);
// we need to create data disk as well
submitControlledResourceDeletionFlight(workspaceUuid, userRequest, diskResource, azureTestUtils.getAzureCloudContext().getAzureResourceGroupId(), diskResource.getDiskName(), azureTestUtils.getComputeManager().disks()::getByResourceGroup);
}
use of com.azure.resourcemanager.compute.ComputeManager in project terra-workspace-manager by DataBiosphere.
the class CrlService method buildComputeManager.
private ComputeManager buildComputeManager(AzureCloudContext azureCloudContext, AzureConfiguration azureConfig) {
TokenCredential azureCreds = getManagedAppCredentials(azureConfig);
AzureProfile azureProfile = new AzureProfile(azureCloudContext.getAzureTenantId(), azureCloudContext.getAzureSubscriptionId(), AzureEnvironment.AZURE);
// We must use FQDN because there are two `Defaults` symbols imported otherwise.
ComputeManager manager = bio.terra.cloudres.azure.resourcemanager.common.Defaults.crlConfigure(clientConfig, ComputeManager.configure()).authenticate(azureCreds, azureProfile);
return manager;
}
use of com.azure.resourcemanager.compute.ComputeManager in project terra-workspace-manager by DataBiosphere.
the class CreateAzureDiskStep 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().define(resource.getDiskName()).withRegion(resource.getRegion()).withExistingResourceGroup(azureCloudContext.getAzureResourceGroupId()).withData().withSizeInGB(resource.getSize()).withTag("workspaceId", resource.getWorkspaceId().toString()).withTag("resourceId", resource.getResourceId().toString()).create(Defaults.buildContext(CreateDiskRequestData.builder().setName(resource.getDiskName()).setRegion(Region.fromName(resource.getRegion())).setResourceGroupName(azureCloudContext.getAzureResourceGroupId()).setTenantId(azureCloudContext.getAzureTenantId()).setSubscriptionId(azureCloudContext.getAzureSubscriptionId()).setSize(resource.getSize()).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 Disk {} in managed resource group {} already exists", resource.getDiskName(), azureCloudContext.getAzureResourceGroupId());
return StepResult.getStepResultSuccess();
}
return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
}
return StepResult.getStepResultSuccess();
}
Aggregations