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);
}
}
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);
}
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();
}
}
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())));
}
}
Aggregations