Search in sources :

Example 1 with StorageManager

use of com.azure.resourcemanager.storage.StorageManager in project terra-workspace-manager by DataBiosphere.

the class GetAzureStorageStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
    final AzureCloudContext azureCloudContext = context.getWorkingMap().get(ControlledResourceKeys.AZURE_CLOUD_CONTEXT, AzureCloudContext.class);
    StorageManager storageManager = crlService.getStorageManager(azureCloudContext, azureConfig);
    if (storageManager.storageAccounts().checkNameAvailability(resource.getStorageAccountName()).isAvailable()) {
        return StepResult.getStepResultSuccess();
    }
    return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, new DuplicateResourceException(String.format("An Azure Storage Account with name %s already exists", resource.getStorageAccountName())));
}
Also used : AzureCloudContext(bio.terra.workspace.service.workspace.model.AzureCloudContext) StorageManager(com.azure.resourcemanager.storage.StorageManager) StepResult(bio.terra.stairway.StepResult) DuplicateResourceException(bio.terra.workspace.service.resource.exception.DuplicateResourceException)

Example 2 with StorageManager

use of com.azure.resourcemanager.storage.StorageManager in project terra-workspace-manager by DataBiosphere.

the class CreateAzureStorageStep method undoStep.

/**
 * Deletes the storage account if the account is available. If the storage account is available
 * and deletes fails, the failure is considered fatal and must looked into it.
 *
 * @param context
 * @return Step result.
 * @throws InterruptedException
 */
@Override
public StepResult undoStep(FlightContext context) throws InterruptedException {
    final AzureCloudContext azureCloudContext = context.getWorkingMap().get(ControlledResourceKeys.AZURE_CLOUD_CONTEXT, AzureCloudContext.class);
    StorageManager storageManager = crlService.getStorageManager(azureCloudContext, azureConfig);
    // then return success.
    if (storageManager.storageAccounts().checkNameAvailability(resource.getStorageAccountName()).isAvailable()) {
        logger.warn("Deletion of the storage account is not required. Storage account does not exist. {}", resource.getStorageAccountName());
        return StepResult.getStepResultSuccess();
    }
    try {
        logger.warn("Attempting to delete storage account: {}", resource.getStorageAccountName());
        storageManager.storageAccounts().deleteByResourceGroup(azureCloudContext.getAzureResourceGroupId(), resource.getStorageAccountName());
        logger.warn("Successfully deleted storage account: {}", resource.getStorageAccountName());
    } catch (ManagementException e) {
        logger.error("Failed to delete storage account: {}", resource.getStorageAccountName());
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : ManagementException(com.azure.core.management.exception.ManagementException) AzureCloudContext(bio.terra.workspace.service.workspace.model.AzureCloudContext) StorageManager(com.azure.resourcemanager.storage.StorageManager) StepResult(bio.terra.stairway.StepResult)

Example 3 with StorageManager

use of com.azure.resourcemanager.storage.StorageManager in project terra-workspace-manager by DataBiosphere.

the class CreateAzureStorageStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
    final AzureCloudContext azureCloudContext = context.getWorkingMap().get(ControlledResourceKeys.AZURE_CLOUD_CONTEXT, AzureCloudContext.class);
    StorageManager storageManager = crlService.getStorageManager(azureCloudContext, azureConfig);
    try {
        storageManager.storageAccounts().define(resource.getStorageAccountName()).withRegion(resource.getRegion()).withExistingResourceGroup(azureCloudContext.getAzureResourceGroupId()).withHnsEnabled(true).withTag("workspaceId", resource.getWorkspaceId().toString()).withTag("resourceId", resource.getResourceId().toString()).create(Defaults.buildContext(CreateStorageAccountRequestData.builder().setName(resource.getStorageAccountName()).setRegion(Region.fromName(resource.getRegion())).setResourceGroupName(azureCloudContext.getAzureResourceGroupId()).build()));
    } catch (ManagementException e) {
        logger.error("Failed to create the Azure Storage account with the name: {} Error Code: {}", resource.getStorageAccountName(), e.getValue().getCode(), e);
        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) StorageManager(com.azure.resourcemanager.storage.StorageManager) StepResult(bio.terra.stairway.StepResult)

Example 4 with StorageManager

use of com.azure.resourcemanager.storage.StorageManager in project terra-workspace-manager by DataBiosphere.

the class CrlService method buildStorageManager.

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

Example 5 with StorageManager

use of com.azure.resourcemanager.storage.StorageManager in project azure-maven-plugins by microsoft.

the class AzureStorageAccount method checkNameAvailability.

public CheckNameAvailabilityResultEntity checkNameAvailability(String subscriptionId, String name) {
    StorageManager manager = StorageManagerFactory.create(subscriptionId);
    CheckNameAvailabilityResult result = manager.storageAccounts().checkNameAvailability(name);
    return new CheckNameAvailabilityResultEntity(result.isAvailable(), Optional.ofNullable(result.reason()).map(Reason::toString).orElse(null), result.message());
}
Also used : StorageManager(com.azure.resourcemanager.storage.StorageManager) CheckNameAvailabilityResult(com.azure.resourcemanager.storage.models.CheckNameAvailabilityResult) CheckNameAvailabilityResultEntity(com.microsoft.azure.toolkit.lib.common.entity.CheckNameAvailabilityResultEntity) Reason(com.azure.resourcemanager.storage.models.Reason)

Aggregations

StorageManager (com.azure.resourcemanager.storage.StorageManager)5 StepResult (bio.terra.stairway.StepResult)3 AzureCloudContext (bio.terra.workspace.service.workspace.model.AzureCloudContext)3 ManagementException (com.azure.core.management.exception.ManagementException)2 DuplicateResourceException (bio.terra.workspace.service.resource.exception.DuplicateResourceException)1 TokenCredential (com.azure.core.credential.TokenCredential)1 AzureProfile (com.azure.core.management.profile.AzureProfile)1 CheckNameAvailabilityResult (com.azure.resourcemanager.storage.models.CheckNameAvailabilityResult)1 Reason (com.azure.resourcemanager.storage.models.Reason)1 CheckNameAvailabilityResultEntity (com.microsoft.azure.toolkit.lib.common.entity.CheckNameAvailabilityResultEntity)1