Search in sources :

Example 1 with RelayManager

use of com.azure.resourcemanager.relay.RelayManager in project terra-workspace-manager by DataBiosphere.

the class CrlService method buildRelayManager.

private RelayManager buildRelayManager(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.
    RelayManager manager = bio.terra.cloudres.azure.resourcemanager.relay.Defaults.crlConfigure(clientConfig, RelayManager.configure()).authenticate(azureCreds, azureProfile);
    return manager;
}
Also used : RelayManager(com.azure.resourcemanager.relay.RelayManager) AzureProfile(com.azure.core.management.profile.AzureProfile) TokenCredential(com.azure.core.credential.TokenCredential)

Example 2 with RelayManager

use of com.azure.resourcemanager.relay.RelayManager in project terra-workspace-manager by DataBiosphere.

the class DeleteAzureRelayNamespaceStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException {
    final AzureCloudContext azureCloudContext = context.getWorkingMap().get(ControlledResourceKeys.AZURE_CLOUD_CONTEXT, AzureCloudContext.class);
    RelayManager manager = crlService.getRelayManager(azureCloudContext, azureConfig);
    var azureResourceId = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Relay/namespaces/%s", azureCloudContext.getAzureSubscriptionId(), azureCloudContext.getAzureResourceGroupId(), resource.getNamespaceName());
    try {
        logger.info("Attempting to delete Relay Namespace " + azureResourceId);
        manager.namespaces().deleteById(azureResourceId);
        return StepResult.getStepResultSuccess();
    } catch (Exception ex) {
        logger.info("Attempt to delete Azure Relay Namespace failed on this try: " + azureResourceId, ex);
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, ex);
    }
}
Also used : AzureCloudContext(bio.terra.workspace.service.workspace.model.AzureCloudContext) RelayManager(com.azure.resourcemanager.relay.RelayManager) StepResult(bio.terra.stairway.StepResult)

Example 3 with RelayManager

use of com.azure.resourcemanager.relay.RelayManager in project terra-workspace-manager by DataBiosphere.

the class CreateAzureRelayNamespaceStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
    final AzureCloudContext azureCloudContext = context.getWorkingMap().get(ControlledResourceKeys.AZURE_CLOUD_CONTEXT, AzureCloudContext.class);
    RelayManager manager = crlService.getRelayManager(azureCloudContext, azureConfig);
    try {
        manager.namespaces().define(resource.getNamespaceName()).withRegion(resource.getRegion()).withExistingResourceGroup(azureCloudContext.getAzureResourceGroupId()).create(Defaults.buildContext(CreateRelayRequestData.builder().setName(resource.getNamespaceName()).setRegion(Region.fromName(resource.getRegion())).setResourceGroupName(azureCloudContext.getAzureResourceGroupId()).setTenantId(azureCloudContext.getAzureTenantId()).setSubscriptionId(azureCloudContext.getAzureSubscriptionId()).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 Relay Namepace {} in managed resource group {} already exists", resource.getNamespaceName(), 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) RelayManager(com.azure.resourcemanager.relay.RelayManager) StepResult(bio.terra.stairway.StepResult)

Example 4 with RelayManager

use of com.azure.resourcemanager.relay.RelayManager in project terra-workspace-manager by DataBiosphere.

the class CreateAzureRelayNamespaceStep method undoStep.

@Override
public StepResult undoStep(FlightContext context) throws InterruptedException {
    final AzureCloudContext azureCloudContext = context.getWorkingMap().get(ControlledResourceKeys.AZURE_CLOUD_CONTEXT, AzureCloudContext.class);
    RelayManager manager = crlService.getRelayManager(azureCloudContext, azureConfig);
    try {
        manager.namespaces().deleteByResourceGroup(azureCloudContext.getAzureResourceGroupId(), resource.getNamespaceName());
    } 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 Relay Namespace {} in managed resource group {} already deleted", resource.getNamespaceName(), 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) RelayManager(com.azure.resourcemanager.relay.RelayManager) StepResult(bio.terra.stairway.StepResult)

Example 5 with RelayManager

use of com.azure.resourcemanager.relay.RelayManager in project terra-workspace-manager by DataBiosphere.

the class GetAzureRelayNamespaceStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
    final AzureCloudContext azureCloudContext = context.getWorkingMap().get(ControlledResourceKeys.AZURE_CLOUD_CONTEXT, AzureCloudContext.class);
    RelayManager manager = crlService.getRelayManager(azureCloudContext, azureConfig);
    try {
        manager.namespaces().getByResourceGroup(azureCloudContext.getAzureResourceGroupId(), resource.getNamespaceName());
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, new DuplicateResourceException(String.format("An Azure Relay Namespace with name %s already exists in resource group %s", azureCloudContext.getAzureResourceGroupId(), resource.getName())));
    } catch (ManagementException e) {
        // https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/common-deployment-errors
        if (StringUtils.contains(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) RelayManager(com.azure.resourcemanager.relay.RelayManager) StepResult(bio.terra.stairway.StepResult) DuplicateResourceException(bio.terra.workspace.service.resource.exception.DuplicateResourceException)

Aggregations

RelayManager (com.azure.resourcemanager.relay.RelayManager)6 StepResult (bio.terra.stairway.StepResult)4 AzureCloudContext (bio.terra.workspace.service.workspace.model.AzureCloudContext)4 ManagementException (com.azure.core.management.exception.ManagementException)4 FlightState (bio.terra.stairway.FlightState)1 BaseAzureTest (bio.terra.workspace.common.BaseAzureTest)1 ApiAzureRelayNamespaceCreationParameters (bio.terra.workspace.generated.model.ApiAzureRelayNamespaceCreationParameters)1 AuthenticatedUserRequest (bio.terra.workspace.service.iam.AuthenticatedUserRequest)1 ControlledAzureRelayNamespaceResource (bio.terra.workspace.service.resource.controlled.cloud.azure.relayNamespace.ControlledAzureRelayNamespaceResource)1 ControlledResource (bio.terra.workspace.service.resource.controlled.model.ControlledResource)1 DuplicateResourceException (bio.terra.workspace.service.resource.exception.DuplicateResourceException)1 TokenCredential (com.azure.core.credential.TokenCredential)1 AzureProfile (com.azure.core.management.profile.AzureProfile)1 UUID (java.util.UUID)1 Test (org.junit.jupiter.api.Test)1