Search in sources :

Example 1 with ControlledAzureNetworkResource

use of bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource in project terra-workspace-manager by DataBiosphere.

the class CreateAndDeleteAzureControlledResourceFlightTest method createAzureNetworkControlledResource.

@Test
public void createAzureNetworkControlledResource() throws InterruptedException {
    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());
    final ApiAzureNetworkCreationParameters creationParams = ControlledResourceFixtures.getAzureNetworkCreationParameters();
    // TODO: make this application-private resource once the POC supports it
    final UUID resourceId = UUID.randomUUID();
    ControlledAzureNetworkResource resource = ControlledAzureNetworkResource.builder().common(ControlledResourceFields.builder().workspaceId(workspaceId).resourceId(resourceId).name("testNetwork").description("testDesc").cloningInstructions(CloningInstructions.COPY_RESOURCE).accessScope(AccessScopeType.fromApi(ApiAccessScope.SHARED_ACCESS)).managedBy(ManagedByType.fromApi(ApiManagedBy.USER)).build()).networkName(creationParams.getName()).region(creationParams.getRegion()).subnetName(creationParams.getSubnetName()).addressSpaceCidr(creationParams.getAddressSpaceCidr()).subnetAddressCidr(creationParams.getSubnetAddressCidr()).build();
    // Submit a Network 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 {
        ControlledAzureNetworkResource azureNetworkResource = res.castByEnum(WsmResourceType.CONTROLLED_AZURE_NETWORK);
        assertEquals(resource, azureNetworkResource);
    } catch (Exception e) {
        fail("Failed to cast resource to ControlledAzureNetworkResource", e);
    }
}
Also used : FlightState(bio.terra.stairway.FlightState) ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) UUID(java.util.UUID) ApiAzureNetworkCreationParameters(bio.terra.workspace.generated.model.ApiAzureNetworkCreationParameters) ControlledAzureNetworkResource(bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource) ManagementException(com.azure.core.management.exception.ManagementException) Test(org.junit.jupiter.api.Test) BaseAzureTest(bio.terra.workspace.common.BaseAzureTest)

Example 2 with ControlledAzureNetworkResource

use of bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource 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 3 with ControlledAzureNetworkResource

use of bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource in project terra-workspace-manager by DataBiosphere.

the class CreateAndDeleteAzureControlledResourceFlightTest method createNetwork.

private ControlledAzureNetworkResource createNetwork(UUID workspaceId, AuthenticatedUserRequest userRequest) throws InterruptedException {
    final ApiAzureNetworkCreationParameters creationParameters = ControlledResourceFixtures.getAzureNetworkCreationParameters();
    // TODO: make this application-private resource once the POC supports it
    final UUID resourceId = UUID.randomUUID();
    ControlledAzureNetworkResource resource = ControlledAzureNetworkResource.builder().common(ControlledResourceFields.builder().workspaceId(workspaceId).resourceId(resourceId).name(getAzureName("network")).description(getAzureName("network-desc")).cloningInstructions(CloningInstructions.COPY_RESOURCE).accessScope(AccessScopeType.fromApi(ApiAccessScope.SHARED_ACCESS)).managedBy(ManagedByType.fromApi(ApiManagedBy.USER)).build()).networkName(creationParameters.getName()).region(creationParameters.getRegion()).subnetName(creationParameters.getSubnetName()).addressSpaceCidr(creationParameters.getAddressSpaceCidr()).subnetAddressCidr(creationParameters.getSubnetAddressCidr()).build();
    // Submit a Disk creation flight.
    FlightState flightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateControlledResourceFlight.class, azureTestUtils.createControlledResourceInputParameters(workspaceId, userRequest, resource), STAIRWAY_FLIGHT_TIMEOUT, null);
    assertEquals(FlightStatus.SUCCESS, flightState.getFlightStatus());
    return resource;
}
Also used : FlightState(bio.terra.stairway.FlightState) ApiAzureNetworkCreationParameters(bio.terra.workspace.generated.model.ApiAzureNetworkCreationParameters) UUID(java.util.UUID) ControlledAzureNetworkResource(bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource)

Example 4 with ControlledAzureNetworkResource

use of bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource 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 5 with ControlledAzureNetworkResource

use of bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource in project terra-workspace-manager by DataBiosphere.

the class ControlledAzureResourceApiController method getAzureNetwork.

@Override
public ResponseEntity<ApiAzureNetworkResource> getAzureNetwork(UUID workspaceId, UUID resourceId) {
    final AuthenticatedUserRequest userRequest = getAuthenticatedInfo();
    features.azureEnabledCheck();
    final ControlledAzureNetworkResource resource = controlledResourceService.getControlledResource(workspaceId, resourceId, userRequest).castByEnum(WsmResourceType.CONTROLLED_AZURE_NETWORK);
    return new ResponseEntity<>(resource.toApiResource(), HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) ControlledAzureNetworkResource(bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource)

Aggregations

ControlledAzureNetworkResource (bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource)7 AuthenticatedUserRequest (bio.terra.workspace.service.iam.AuthenticatedUserRequest)5 UUID (java.util.UUID)4 FlightState (bio.terra.stairway.FlightState)3 ApiAzureNetworkCreationParameters (bio.terra.workspace.generated.model.ApiAzureNetworkCreationParameters)3 ControlledAzureDiskResource (bio.terra.workspace.service.resource.controlled.cloud.azure.disk.ControlledAzureDiskResource)3 ControlledAzureIpResource (bio.terra.workspace.service.resource.controlled.cloud.azure.ip.ControlledAzureIpResource)3 ManagementException (com.azure.core.management.exception.ManagementException)3 Test (org.junit.jupiter.api.Test)3 BaseAzureTest (bio.terra.workspace.common.BaseAzureTest)2 ApiAzureVmCreationParameters (bio.terra.workspace.generated.model.ApiAzureVmCreationParameters)2 ControlledAzureVmResource (bio.terra.workspace.service.resource.controlled.cloud.azure.vm.ControlledAzureVmResource)2 ControlledResource (bio.terra.workspace.service.resource.controlled.model.ControlledResource)2 ControlledResourceFields (bio.terra.workspace.service.resource.controlled.model.ControlledResourceFields)2 ComputeManager (com.azure.resourcemanager.compute.ComputeManager)2 ResponseEntity (org.springframework.http.ResponseEntity)2 StepResult (bio.terra.stairway.StepResult)1 BaseConnectedTest (bio.terra.workspace.common.BaseConnectedTest)1 ApiAzureDiskCreationParameters (bio.terra.workspace.generated.model.ApiAzureDiskCreationParameters)1 ApiAzureIpCreationParameters (bio.terra.workspace.generated.model.ApiAzureIpCreationParameters)1