Merge remote-tracking branch 'origin/master' into pycharm/docker
[idea/community.git] / platform / platform-impl / src / com / intellij / remote / RemoteConnectionCredentialsWrapper.java
1 /*
2  * Copyright 2000-2014 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.remote;
17
18 import com.intellij.openapi.util.Key;
19 import com.intellij.openapi.util.Ref;
20 import com.intellij.openapi.util.UserDataHolderBase;
21 import com.intellij.openapi.util.io.FileUtil;
22 import com.intellij.openapi.util.text.StringUtil;
23 import org.jdom.Element;
24 import org.jetbrains.annotations.NotNull;
25
26 /**
27  * @author traff
28  */
29 public class RemoteConnectionCredentialsWrapper {
30   public static final String VAGRANT_PREFIX = "vagrant://";
31   public static final String SFTP_DEPLOYMENT_PREFIX = "sftp://";
32   public static final String DOCKER_PREFIX = "docker://";
33
34   /**
35    * Connection types
36    */
37   public final Key<VagrantBasedCredentialsHolder> VAGRANT_BASED_CREDENTIALS = Key.create("VAGRANT_BASED_CREDENTIALS");
38   public final Key<WebDeploymentCredentialsHolder> WEB_DEPLOYMENT_BASED_CREDENTIALS = Key.create("WEB_DEPLOYMENT_BASED_CREDENTIALS");
39   public final Key<RemoteCredentialsHolder> PLAIN_SSH_CREDENTIALS = Key.create("PLAIN_SSH_CREDENTIALS");
40   public final Key<DockerCredentialsHolder> DOCKER_CREDENTIALS = Key.create("DOCKER_CREDENTIALS");
41
42   private UserDataHolderBase myCredentialsTypeHolder = new UserDataHolderBase();
43
44   public void setVagrantConnectionType(VagrantBasedCredentialsHolder vagrantBasedCredentials) {
45     myCredentialsTypeHolder = new UserDataHolderBase();
46     myCredentialsTypeHolder.putUserData(VAGRANT_BASED_CREDENTIALS, vagrantBasedCredentials);
47   }
48
49
50   private VagrantBasedCredentialsHolder getVagrantCredentials() {
51     return myCredentialsTypeHolder.getUserData(VAGRANT_BASED_CREDENTIALS);
52   }
53
54   public void setPlainSshCredentials(RemoteCredentialsHolder credentials) {
55     myCredentialsTypeHolder = new UserDataHolderBase();
56     myCredentialsTypeHolder.putUserData(PLAIN_SSH_CREDENTIALS, credentials);
57   }
58
59   private RemoteCredentialsHolder getPlainSshCredentials() {
60     return myCredentialsTypeHolder.getUserData(PLAIN_SSH_CREDENTIALS);
61   }
62
63
64   public void setWebDeploymentCredentials(WebDeploymentCredentialsHolder webDeploymentCredentials) {
65     myCredentialsTypeHolder = new UserDataHolderBase();
66     myCredentialsTypeHolder.putUserData(WEB_DEPLOYMENT_BASED_CREDENTIALS, webDeploymentCredentials);
67   }
68
69   private WebDeploymentCredentialsHolder getWebDeploymentCredentials() {
70     return myCredentialsTypeHolder.getUserData(WEB_DEPLOYMENT_BASED_CREDENTIALS);
71   }
72
73   private DockerCredentialsHolder getDockerCredentials() {
74     return myCredentialsTypeHolder.getUserData(DOCKER_CREDENTIALS);
75   }
76
77   private boolean isVagrantConnection() {
78     return getVagrantCredentials() != null;
79   }
80
81   private boolean isPlainSshConnection() {
82     return getPlainSshCredentials() != null;
83   }
84
85   private boolean isWebDeploymentConnection() {
86     return getWebDeploymentCredentials() != null;
87   }
88
89   private boolean isDockerConnection() {
90     return getDockerCredentials() != null;
91   }
92
93   public Object getConnectionKey() {
94     if (isVagrantConnection()) {
95       return getVagrantCredentials();
96     }
97     else if (isPlainSshConnection()) {
98       return getPlainSshCredentials();
99     }
100     else if (isWebDeploymentConnection()) {
101       return getWebDeploymentCredentials();
102     }
103     else {
104       throw unknownConnectionType();
105     }
106   }
107
108   public void save(final Element rootElement) {
109     switchType(new RemoteSdkConnectionAcceptor() {
110       @Override
111       public void ssh(@NotNull RemoteCredentialsHolder cred) {
112         cred.save(rootElement);
113       }
114
115       @Override
116       public void vagrant(@NotNull VagrantBasedCredentialsHolder cred) {
117         cred.save(rootElement);
118       }
119
120       @Override
121       public void deployment(@NotNull WebDeploymentCredentialsHolder cred) {
122         cred.save(rootElement);
123       }
124
125       @Override
126       public void docker(@NotNull DockerCredentialsHolder cred) {
127         cred.save(rootElement);
128       }
129     });
130   }
131
132   public static IllegalStateException unknownConnectionType() {
133     return new IllegalStateException("Unknown connection type"); //TODO
134   }
135
136   public void copyTo(final RemoteConnectionCredentialsWrapper copy) {
137     copy.myCredentialsTypeHolder = new UserDataHolderBase();
138     switchType(new RemoteSdkConnectionAcceptor() {
139       @Override
140       public void ssh(@NotNull RemoteCredentialsHolder cred) {
141         copy.setPlainSshCredentials(getPlainSshCredentials());
142       }
143
144       @Override
145       public void vagrant(@NotNull VagrantBasedCredentialsHolder cred) {
146         copy.setVagrantConnectionType(getVagrantCredentials());
147       }
148
149       @Override
150       public void deployment(@NotNull WebDeploymentCredentialsHolder cred) {
151         copy.setWebDeploymentCredentials(getWebDeploymentCredentials());
152       }
153
154       @Override
155       public void docker(@NotNull DockerCredentialsHolder credentials) {
156         copy.setDockerDeploymentCredentials(getDockerCredentials());
157       }
158     });
159   }
160
161   @NotNull
162   public String getId() {
163     final Ref<String> result = Ref.create();
164     switchType(new RemoteSdkConnectionAcceptor() {
165       @Override
166       public void ssh(@NotNull RemoteCredentialsHolder cred) {
167         result.set(constructSshCredentialsFullPath(cred));
168       }
169
170       @Override
171       public void vagrant(@NotNull VagrantBasedCredentialsHolder cred) {
172         result.set(VAGRANT_PREFIX + cred.getVagrantFolder()
173                    + (StringUtil.isNotEmpty(cred.getMachineName()) ?
174                       "@" + cred.getMachineName() : ""));
175       }
176
177       @Override
178       public void deployment(@NotNull WebDeploymentCredentialsHolder cred) {
179         result.set(constructSftpCredentialsFullPath(cred.getSshCredentials()));
180       }
181
182       @Override
183       public void docker(@NotNull DockerCredentialsHolder cred) {
184         // TODO [Docker] review
185         String name = StringUtil.isNotEmpty(cred.getContainerName()) ? cred.getContainerName() : cred.getImageName();
186         result.set(DOCKER_PREFIX + name + "/");
187       }
188     });
189
190     return result.get();
191   }
192
193   @NotNull
194   private static String constructSftpCredentialsFullPath(RemoteCredentials cred) {
195     return SFTP_DEPLOYMENT_PREFIX + cred.getUserName() + "@" + cred.getHost() + ":" + cred.getPort();
196   }
197
198
199   @NotNull
200   public static String constructSshCredentialsFullPath(RemoteCredentials cred) {
201     return RemoteCredentialsHolder.SSH_PREFIX + cred.getUserName() + "@" + cred.getHost() + ":" + cred.getPort();
202   }
203
204   public void switchType(@NotNull final RemoteSdkConnectionAcceptor acceptor) {
205     if (isVagrantConnection()) {
206       acceptor.vagrant(getVagrantCredentials());
207     }
208     else if (isPlainSshConnection()) {
209       acceptor.ssh(getPlainSshCredentials());
210     }
211     else if (isWebDeploymentConnection()) {
212       acceptor.deployment(getWebDeploymentCredentials());
213     }
214     else if (isDockerConnection()) {
215       acceptor.docker(getDockerCredentials());
216     }
217     else {
218       throw unknownConnectionType();
219     }
220   }
221
222   @Override
223   public boolean equals(Object obj) {
224     if (obj instanceof RemoteConnectionCredentialsWrapper) {
225       RemoteConnectionCredentialsWrapper w = (RemoteConnectionCredentialsWrapper)obj;
226       if (isVagrantConnection()) {
227         return w.isVagrantConnection() && getVagrantCredentials().equals(w.getVagrantCredentials());
228       }
229       else if (isWebDeploymentConnection()) {
230         return w.isWebDeploymentConnection() && getWebDeploymentCredentials().equals(w.getWebDeploymentCredentials());
231       }
232       else if (isPlainSshConnection()) {
233         return w.isPlainSshConnection() && getPlainSshCredentials().equals(w.getPlainSshCredentials());
234       }
235     }
236     return false;
237   }
238
239   public String getPresentableDetails(final String interpreterPath) {
240     final Ref<String> result = Ref.create();
241     switchType(new RemoteSdkConnectionAcceptor() {
242       @Override
243       public void ssh(@NotNull RemoteCredentialsHolder cred) {
244         result.set("(" + constructSshCredentialsFullPath(cred) + interpreterPath + ")");
245       }
246
247       @Override
248       public void vagrant(@NotNull VagrantBasedCredentialsHolder cred) {
249         String pathRelativeToHome = FileUtil.getLocationRelativeToUserHome(cred.getVagrantFolder());
250
251         result.set("Vagrant VM " +
252                    (StringUtil.isNotEmpty(cred.getMachineName()) ? "'" + cred.getMachineName() + "' " : "") +
253                    "at " + (pathRelativeToHome.length() < cred.getVagrantFolder().length() ? pathRelativeToHome : cred.getVagrantFolder())
254                    + " (" + interpreterPath + ")");
255       }
256
257       @Override
258       public void deployment(@NotNull WebDeploymentCredentialsHolder cred) {
259         result.set("(" + constructSftpCredentialsFullPath(cred.getSshCredentials()) + interpreterPath + ")");
260       }
261
262       @Override
263       public void docker(@NotNull DockerCredentialsHolder credentials) {
264         String containerName = StringUtil.isNotEmpty(credentials.getContainerName())
265                                ? credentials.getContainerName() + " " : "";
266         result.set("Docker " + containerName + "(" + credentials.getImageName() + ")");
267       }
268     });
269
270     return result.get();
271   }
272
273
274   public void setDockerDeploymentCredentials(DockerCredentialsHolder credentials) {
275     myCredentialsTypeHolder = new UserDataHolderBase();
276     myCredentialsTypeHolder.putUserData(DOCKER_CREDENTIALS, credentials);
277   }
278 }