Search in sources :

Example 1 with ComputeManager

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 workspaceId = azureTestUtils.createWorkspace(workspaceService);
    AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest();
    // Cloud context needs to be created first
    FlightState createAzureContextFlightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateAzureContextFlight.class, azureTestUtils.createAzureContextInputParameters(workspaceId, userRequest), STAIRWAY_FLIGHT_TIMEOUT, null);
    assertEquals(FlightStatus.SUCCESS, createAzureContextFlightState.getFlightStatus());
    assertTrue(workspaceService.getAuthorizedAzureCloudContext(workspaceId, userRequest).isPresent());
    // Create ip
    ControlledAzureIpResource ipResource = createIp(workspaceId, userRequest);
    // Create disk
    ControlledAzureDiskResource diskResource = createDisk(workspaceId, userRequest);
    // Create network
    ControlledAzureNetworkResource networkResource = createNetwork(workspaceId, 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().workspaceId(workspaceId).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()).vmImageUri(creationParameters.getVmImageUri()).region(creationParameters.getRegion()).ipId(ipResource.getResourceId()).diskId(diskResource.getResourceId()).networkId(networkResource.getResourceId()).build();
    // Submit a VM creation flight.
    FlightState flightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateControlledResourceFlight.class, azureTestUtils.createControlledResourceInputParameters(workspaceId, userRequest, resource), STAIRWAY_FLIGHT_TIMEOUT, null);
    assertEquals(FlightStatus.SUCCESS, flightState.getFlightStatus());
    // Verify controlled resource exists in the workspace.
    ControlledResource res = controlledResourceService.getControlledResource(workspaceId, resourceId, userRequest);
    try {
        ControlledAzureVmResource azureVmResource = res.castByEnum(WsmResourceType.CONTROLLED_AZURE_VM);
        assertEquals(resource, azureVmResource);
    } catch (Exception e) {
        fail("Failed to cast resource to ControlledAzureVmResource", e);
    }
    // Exercise resource enumeration for the underlying resources.
    // Verify that the resources we created are in the enumeration.
    List<WsmResource> resourceList = wsmResourceService.enumerateResources(workspaceId, null, null, 0, 100, userRequest);
    checkForResource(resourceList, ipResource);
    checkForResource(resourceList, diskResource);
    checkForResource(resourceList, networkResource);
    checkForResource(resourceList, resource);
    ComputeManager computeManager = azureTestUtils.getComputeManager();
    VirtualMachine vmTemp = null;
    var retries = 20;
    while (vmTemp == null) {
        try {
            retries = retries - 1;
            if (retries >= 0) {
                vmTemp = computeManager.virtualMachines().getByResourceGroup(azureTestUtils.getAzureCloudContext().getAzureResourceGroupId(), creationParameters.getName());
            } else
                throw new RuntimeException(String.format("%s is not created in time in Azure", creationParameters.getName()));
        } catch (com.azure.core.exception.HttpResponseException ex) {
            if (ex.getResponse().getStatusCode() == 404)
                Thread.sleep(10000);
            else
                throw ex;
        }
    }
    final VirtualMachine resolvedVm = vmTemp;
    // Submit a VM deletion flight.
    FlightState deleteFlightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), DeleteControlledResourceFlight.class, azureTestUtils.deleteControlledResourceInputParameters(workspaceId, 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()));
}
Also used : ControlledAzureIpResource(bio.terra.workspace.service.resource.controlled.cloud.azure.ip.ControlledAzureIpResource) WsmResource(bio.terra.workspace.service.resource.model.WsmResource) ControlledAzureVmResource(bio.terra.workspace.service.resource.controlled.cloud.azure.vm.ControlledAzureVmResource) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) ApiAzureVmCreationParameters(bio.terra.workspace.generated.model.ApiAzureVmCreationParameters) ManagementException(com.azure.core.management.exception.ManagementException) ComputeManager(com.azure.resourcemanager.compute.ComputeManager) FlightState(bio.terra.stairway.FlightState) ControlledAzureDiskResource(bio.terra.workspace.service.resource.controlled.cloud.azure.disk.ControlledAzureDiskResource) ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) UUID(java.util.UUID) ControlledAzureNetworkResource(bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine) Test(org.junit.jupiter.api.Test) BaseAzureTest(bio.terra.workspace.common.BaseAzureTest)

Example 2 with ComputeManager

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;
}
Also used : AzureProfile(com.azure.core.management.profile.AzureProfile) TokenCredential(com.azure.core.credential.TokenCredential) ComputeManager(com.azure.resourcemanager.compute.ComputeManager)

Example 3 with ComputeManager

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

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

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();
}
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 5 with ComputeManager

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

the class CreateAzureNetworkStep 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 {
        NetworkSecurityGroup subnetNsg = computeManager.networkManager().networkSecurityGroups().define(resource.getSubnetName()).withRegion(resource.getRegion()).withExistingResourceGroup(azureCloudContext.getAzureResourceGroupId()).withTag("workspaceId", resource.getWorkspaceId().toString()).withTag("resourceId", resource.getResourceId().toString()).defineRule("AllowHttpInComing").allowInbound().fromAddress("INTERNET").fromAnyPort().toAnyAddress().toPort(8080).withProtocol(SecurityRuleProtocol.TCP).attach().create(Defaults.buildContext(CreateNetworkSecurityGroupRequestData.builder().setName(resource.getSubnetName()).setRegion(Region.fromName(resource.getRegion())).setTenantId(azureCloudContext.getAzureTenantId()).setSubscriptionId(azureCloudContext.getAzureSubscriptionId()).setResourceGroupName(azureCloudContext.getAzureResourceGroupId()).setRules(Collections.emptyList()).build()));
        computeManager.networkManager().networks().define(resource.getNetworkName()).withRegion(resource.getRegion()).withExistingResourceGroup(azureCloudContext.getAzureResourceGroupId()).withTag("workspaceId", resource.getWorkspaceId().toString()).withTag("resourceId", resource.getResourceId().toString()).withAddressSpace(resource.getAddressSpaceCidr()).defineSubnet(resource.getSubnetName()).withAddressPrefix(resource.getSubnetAddressCidr()).withExistingNetworkSecurityGroup(subnetNsg).attach().create(Defaults.buildContext(CreateNetworkRequestData.builder().setName(resource.getNetworkName()).setTenantId(azureCloudContext.getAzureTenantId()).setSubscriptionId(azureCloudContext.getAzureSubscriptionId()).setResourceGroupName(azureCloudContext.getAzureResourceGroupId()).setRegion(Region.fromName(resource.getRegion())).setSubnetName(resource.getSubnetName()).setNetworkSecurityGroup(subnetNsg).setAddressPrefix(resource.getSubnetAddressCidr()).setAddressSpaceCidr(resource.getAddressSpaceCidr()).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 Network {} in managed resource group {} already exists", resource.getNetworkName(), azureCloudContext.getAzureResourceGroupId());
            return StepResult.getStepResultSuccess();
        }
        if (StringUtils.equals(e.getValue().getCode(), "SubnetsNotInSameVnet")) {
            logger.info("Azure Network {} and Subnet {} in managed resource group {} must belong to the same virtual network", resource.getNetworkName(), resource.getSubnetName(), azureCloudContext.getAzureResourceGroupId());
            return StepResult.getStepResultSuccess();
        }
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : NetworkSecurityGroup(com.azure.resourcemanager.network.models.NetworkSecurityGroup) 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)

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