diff --git a/platform/remote-servers/impl/src/com/intellij/remoteServer/util/CloudRuntimeTask.java b/platform/remote-servers/impl/src/com/intellij/remoteServer/util/CloudRuntimeTask.java index 6ead15621cf8..e377cade4e47 100644 --- a/platform/remote-servers/impl/src/com/intellij/remoteServer/util/CloudRuntimeTask.java +++ b/platform/remote-servers/impl/src/com/intellij/remoteServer/util/CloudRuntimeTask.java @@ -20,10 +20,12 @@ import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.Progressive; import com.intellij.openapi.progress.Task; import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.Messages; import com.intellij.remoteServer.configuration.deployment.DeploymentConfiguration; import com.intellij.util.concurrency.Semaphore; import org.jetbrains.annotations.NotNull; +import javax.swing.*; import java.util.concurrent.atomic.AtomicReference; public abstract class CloudRuntimeTask< @@ -36,6 +38,9 @@ public abstract class CloudRuntimeTask< private final Project myProject; private final String myTitle; + private final AtomicReference mySuccess = new AtomicReference(); + private final AtomicReference myErrorMessage = new AtomicReference(); + public CloudRuntimeTask(Project project, String title) { myProject = project; myTitle = title; @@ -87,6 +92,9 @@ public abstract class CloudRuntimeTask< }; } + mySuccess.set(false); + myErrorMessage.set(null); + AtomicReference result = new AtomicReference(); run(semaphore, result); @@ -110,6 +118,7 @@ public abstract class CloudRuntimeTask< public void run() { try { result.set(CloudRuntimeTask.this.run(serverRuntime)); + mySuccess.set(true); } catch (ServerRuntimeException e) { runtimeErrorOccurred(e.getMessage()); @@ -122,9 +131,21 @@ public abstract class CloudRuntimeTask< } protected void runtimeErrorOccurred(@NotNull String errorMessage) { + myErrorMessage.set(errorMessage); LOG.info(errorMessage); } + public void showMessageDialog(JComponent component, String successMessage, String title) { + if (mySuccess.get()) { + Messages.showInfoMessage(component, successMessage, title); + return; + } + String errorMessage = myErrorMessage.get(); + if (errorMessage != null) { + Messages.showErrorDialog(component, errorMessage, title); + } + } + protected abstract SR getServerRuntime(); protected abstract T run(SR serverRuntime) throws ServerRuntimeException; diff --git a/platform/remote-servers/impl/src/com/intellij/remoteServer/util/ssh/SshKeyChecker.java b/platform/remote-servers/impl/src/com/intellij/remoteServer/util/ssh/SshKeyChecker.java index 9eb3ea3e7fd8..1aca1ecdc4a9 100644 --- a/platform/remote-servers/impl/src/com/intellij/remoteServer/util/ssh/SshKeyChecker.java +++ b/platform/remote-servers/impl/src/com/intellij/remoteServer/util/ssh/SshKeyChecker.java @@ -18,8 +18,15 @@ package com.intellij.remoteServer.util.ssh; import com.intellij.execution.filters.HyperlinkInfo; import com.intellij.notification.Notification; import com.intellij.notification.NotificationListener; +import com.intellij.openapi.options.ConfigurationException; +import com.intellij.openapi.options.UnnamedConfigurable; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.MessageType; +import com.intellij.openapi.ui.Messages; +import com.intellij.remoteServer.ServerType; +import com.intellij.remoteServer.configuration.RemoteServer; +import com.intellij.remoteServer.configuration.ServerConfiguration; +import com.intellij.remoteServer.impl.configuration.RemoteServerImpl; import com.intellij.remoteServer.runtime.ServerConnection; import com.intellij.remoteServer.runtime.ServerConnectionManager; import com.intellij.remoteServer.runtime.deployment.DeploymentLogManager; @@ -27,10 +34,12 @@ import com.intellij.remoteServer.runtime.deployment.DeploymentTask; import com.intellij.remoteServer.runtime.log.LoggingHandler; import com.intellij.remoteServer.runtime.ui.RemoteServersView; import com.intellij.remoteServer.util.*; +import com.intellij.ui.HyperlinkLabel; import com.intellij.util.ParameterizedRunnable; import org.jetbrains.annotations.NotNull; import javax.swing.event.HyperlinkEvent; +import javax.swing.event.HyperlinkListener; import java.io.File; /** @@ -63,6 +72,13 @@ public class SshKeyChecker { } } + public void setupUploadLabel(HyperlinkLabel label, + UnnamedConfigurable serverConfigurable, + C serverConfiguration, + ServerType serverType) { + new ConfigurableHandler(label, serverConfigurable, serverConfiguration, serverType); + } + private class ServerHandler extends HandlerBase { private final CloudNotifier myNotifier; @@ -210,6 +226,62 @@ public class SshKeyChecker { } } + private class ConfigurableHandler extends HandlerBase implements HyperlinkListener { + + private final UnnamedConfigurable myServerConfigurable; + private final C myServerConfiguration; + private final ServerType myServerType; + + private final HyperlinkLabel myLabel; + + public ConfigurableHandler(HyperlinkLabel label, + final UnnamedConfigurable serverConfigurable, + C serverConfiguration, + ServerType serverType) { + myServerConfigurable = serverConfigurable; + myServerConfiguration = serverConfiguration; + myServerType = serverType; + + label.setHyperlinkText("Upload Public SSH Key"); + label.addHyperlinkListener(this); + myLabel = label; + } + + @Override + public void hyperlinkUpdate(HyperlinkEvent e) { + chooseKey(); + } + + @Override + protected void uploadKey(final File sskKey) { + try { + myServerConfigurable.apply(); + } + catch (ConfigurationException e) { + Messages.showErrorDialog("Cannot upload SSH key: " + e.getMessage(), e.getTitle()); + return; + } + + RemoteServer server = new RemoteServerImpl("", myServerType, myServerConfiguration); + + CloudConnectionTask task = new CloudConnectionTask(null, "Uploading SSH key", server) { + + @Override + protected Object run(CloudServerRuntimeInstance serverRuntime) throws ServerRuntimeException { + ((SshKeyAwareServerRuntime)serverRuntime).addSshKey(sskKey); + return null; + } + }; + task.performSync(); + task.showMessageDialog(myLabel, "SSH key was uploaded", "Public SSH Key"); + } + + @Override + protected Project getProject() { + return null; + } + } + private static abstract class HandlerBase { protected void chooseKey() {