Search in sources :

Example 26 with AzureToolkitRuntimeException

use of com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException in project azure-tools-for-java by Microsoft.

the class EclipseAzureTaskManager method doRunInUnBackgroundableModal.

protected void doRunInUnBackgroundableModal(final Runnable runnable, final AzureTask<?> task) {
    final String title = String.format("Azure: %s...", Objects.requireNonNull(task.getTitle()));
    try {
        new ProgressMonitorDialog(new Shell()).run(true, task.isCancellable(), monitor -> {
            monitor.beginTask(title, IProgressMonitor.UNKNOWN);
            try {
                task.setBackgrounded(false);
                task.setMonitor(new EclipseTaskMonitor(monitor));
                runnable.run();
            } finally {
                monitor.done();
            }
        });
    } catch (InvocationTargetException | InterruptedException e) {
        String msg = String.format("failed to execute task (%s)", task.getTitle());
        throw new AzureToolkitRuntimeException(msg, e);
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 27 with AzureToolkitRuntimeException

use of com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException in project azure-tools-for-java by Microsoft.

the class IDEHelperImpl method openAppServiceFile.

@AzureOperation(name = "appservice|file.open", params = { "target.getName()" }, type = AzureOperation.Type.SERVICE)
@SneakyThrows
public void openAppServiceFile(AppServiceFile target, Object context) {
    final IAppService appService = target.getApp();
    final FileEditorManager fileEditorManager = FileEditorManager.getInstance((Project) context);
    final VirtualFile virtualFile = getOrCreateVirtualFile(target, fileEditorManager);
    final OutputStream output = virtualFile.getOutputStream(null);
    final AzureString title = AzureOperationBundle.title("appservice|file.open", virtualFile.getName());
    final AzureTask<Void> task = new AzureTask<>(null, title, false, () -> {
        final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
        indicator.setIndeterminate(true);
        indicator.setText2("Checking file existence");
        final AppServiceFile file = appService.getFileByPath(target.getPath());
        if (file == null) {
            final String failureFileDeleted = String.format("Target file (%s) has been deleted", target.getName());
            UIUtil.invokeLaterIfNeeded(() -> Messages.showWarningDialog(failureFileDeleted, "Open File"));
            return;
        }
        indicator.setText2("Loading file content");
        final String failure = String.format("Can not open file (%s). Try downloading it first and open it manually.", virtualFile.getName());
        appService.getFileContent(file.getPath()).doOnComplete(() -> AzureTaskManager.getInstance().runLater(() -> {
            final Consumer<String> contentSaver = content -> saveFileToAzure(target, content, fileEditorManager.getProject());
            if (!openFileInEditor(contentSaver, virtualFile, fileEditorManager)) {
                Messages.showWarningDialog(failure, "Open File");
            }
        }, AzureTask.Modality.NONE)).doAfterTerminate(() -> IOUtils.closeQuietly(output, null)).subscribe(bytes -> {
            try {
                if (bytes != null) {
                    output.write(bytes.array(), 0, bytes.limit());
                }
            } catch (final IOException e) {
                final String error = "failed to load data into editor";
                final String action = "try later or downloading it first";
                throw new AzureToolkitRuntimeException(error, e, action);
            }
        }, IDEHelperImpl::onRxException);
    });
    AzureTaskManager.getInstance().runInModal(task);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) IAppService(com.microsoft.azure.toolkit.lib.appservice.service.IAppService) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) Consumer(com.intellij.util.Consumer) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) AzureTask(com.microsoft.azure.toolkit.lib.common.task.AzureTask) AppServiceFile(com.microsoft.azure.toolkit.lib.appservice.model.AppServiceFile) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation) SneakyThrows(lombok.SneakyThrows)

Example 28 with AzureToolkitRuntimeException

use of com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException in project azure-tools-for-java by Microsoft.

the class AzureSignInAction method call.

private static <T> T call(Callable<T> loginCallable, String authMethod) {
    final Operation operation = TelemetryManager.createOperation(ACCOUNT, SIGNIN);
    final Map<String, String> properties = new HashMap<>();
    properties.put(SIGNIN_METHOD, authMethod);
    Optional.ofNullable(ProgressManager.getInstance().getProgressIndicator()).ifPresent(indicator -> indicator.setText2("Signing in..."));
    try {
        operation.start();
        operation.trackProperties(properties);
        operation.trackProperty(AZURE_ENVIRONMENT, Azure.az(AzureCloud.class).getName());
        return loginCallable.call();
    } catch (Exception e) {
        if (shouldNoticeErrorToUser(e)) {
            EventUtil.logError(operation, ErrorType.userError, e, properties, null);
        }
        throw new AzureToolkitRuntimeException(e.getMessage(), e);
    } finally {
        operation.complete();
    }
}
Also used : HashMap(java.util.HashMap) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) Operation(com.microsoft.azuretools.telemetrywrapper.Operation) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) MsalClientException(com.microsoft.aad.msal4j.MsalClientException) CancellationException(java.util.concurrent.CancellationException) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)

Example 29 with AzureToolkitRuntimeException

use of com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException in project azure-tools-for-java by Microsoft.

the class IdentityAzureManager method restoreSignIn.

public Mono<AuthMethodDetails> restoreSignIn(AuthMethodDetails authMethodDetails) {
    if (authMethodDetails == null || authMethodDetails.getAuthMethod() == null || authMethodDetails.getAuthType() == null) {
        return Mono.just(new AuthMethodDetails());
    }
    if (StringUtils.isNotBlank(authMethodDetails.getAzureEnv())) {
        Azure.az(AzureCloud.class).setByName(authMethodDetails.getAzureEnv());
    }
    AuthType authType = authMethodDetails.getAuthType();
    try {
        if (authType == AuthType.SERVICE_PRINCIPAL) {
            AuthConfiguration auth = new AuthConfiguration();
            auth.setType(AuthType.SERVICE_PRINCIPAL);
            auth.setClient(authMethodDetails.getClientId());
            auth.setTenant(authMethodDetails.getTenantId());
            auth.setEnvironment(Azure.az(AzureCloud.class).get());
            if (StringUtils.isNotBlank(authMethodDetails.getCertificate())) {
                auth.setCertificate(authMethodDetails.getCertificate());
            } else {
                secureStore.migratePassword("account|" + auth.getClient(), null, SERVICE_PRINCIPAL_STORE_SERVICE, auth.getClient(), null);
                String key = secureStore == null ? null : secureStore.loadPassword(SERVICE_PRINCIPAL_STORE_SERVICE, authMethodDetails.getClientId(), null);
                if (StringUtils.isBlank(key)) {
                    throw new AzureToolkitRuntimeException(String.format("Cannot find SP security key for '%s' in intellij key pools.", authMethodDetails.getClientId()));
                }
                auth.setKey(key);
            }
            return signInServicePrincipal(auth).map(ac -> authMethodDetails);
        } else {
            if (StringUtils.isNotBlank(authMethodDetails.getClientId())) {
                AccountEntity entity = new AccountEntity();
                entity.setType(authType);
                entity.setEnvironment(Azure.az(AzureCloud.class).get());
                entity.setEmail(authMethodDetails.getAccountEmail());
                entity.setClientId(authMethodDetails.getClientId());
                entity.setTenantIds(StringUtils.isNotBlank(authMethodDetails.getTenantId()) ? Collections.singletonList(authMethodDetails.getTenantId()) : null);
                Account account = Azure.az(AzureAccount.class).account(entity);
                return Mono.just(fromAccountEntity(account.getEntity()));
            } else {
                throw new AzureToolkitRuntimeException("Cannot restore credentials due to version change.");
            }
        }
    } catch (Throwable e) {
        if (StringUtils.isNotBlank(authMethodDetails.getClientId()) && authMethodDetails.getAuthType() == AuthType.SERVICE_PRINCIPAL && secureStore != null) {
            secureStore.forgetPassword(SERVICE_PRINCIPAL_STORE_SERVICE, authMethodDetails.getClientId(), null);
        }
        return Mono.error(new AzureToolkitRuntimeException(String.format("Cannot restore credentials due to error: %s", e.getMessage())));
    }
}
Also used : Account(com.microsoft.azure.toolkit.lib.auth.Account) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) AzureCloud(com.microsoft.azure.toolkit.lib.auth.AzureCloud) AuthConfiguration(com.microsoft.azure.toolkit.lib.auth.model.AuthConfiguration) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) AuthType(com.microsoft.azure.toolkit.lib.auth.model.AuthType) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) AccountEntity(com.microsoft.azure.toolkit.lib.auth.model.AccountEntity)

Aggregations

AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)29 AzureOperation (com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)13 IOException (java.io.IOException)13 AzureString (com.microsoft.azure.toolkit.lib.common.bundle.AzureString)9 File (java.io.File)5 AzureExecutionException (com.microsoft.azure.toolkit.lib.common.exception.AzureExecutionException)4 AzureTask (com.microsoft.azure.toolkit.lib.common.task.AzureTask)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 HashMap (java.util.HashMap)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 Operation (com.microsoft.azuretools.telemetrywrapper.Operation)3 Path (java.nio.file.Path)3 PsiMethod (com.intellij.psi.PsiMethod)2 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)2 MsalClientException (com.microsoft.aad.msal4j.MsalClientException)2 AuthorizationLevel (com.microsoft.azure.functions.annotation.AuthorizationLevel)2 AppServiceFile (com.microsoft.azure.toolkit.lib.appservice.model.AppServiceFile)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1