PY-17573 DockerCredentialsHolder extended to hold Compose properties
authorAlexander Koshevoy <Alexander.Koshevoy@jetbrains.com>
Fri, 15 Jan 2016 08:19:07 +0000 (11:19 +0300)
committerAlexander Koshevoy <Alexander.Koshevoy@jetbrains.com>
Fri, 15 Jan 2016 08:19:07 +0000 (11:19 +0300)
platform/platform-impl/src/com/intellij/remote/DockerCredentialsHolder.java
platform/platform-impl/src/com/intellij/remote/RemoteConnectionCredentialsWrapper.java

index 2ea73e504b9d8984e08edcac159e88e73681a693..c0515ac87d24302111ec8efc294baa628ba97f88 100644 (file)
@@ -18,6 +18,7 @@ package com.intellij.remote;
 import com.intellij.openapi.util.text.StringUtil;
 import org.jdom.Element;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 
 /**
  * @author Alexander Koshevoy
@@ -28,12 +29,19 @@ public class DockerCredentialsHolder {
   public static final String DOCKER_CONTAINER_NAME = "DOCKER_CONTAINER_NAME";
   public static final String DOCKER_REMOTE_PROJECT_PATH = "DOCKER_REMOTE_PROJECT_PATH";
 
+  public static final String DOCKER_COMPOSE_FILE_PATH = "DOCKER_COMPOSE_FILE_PATH";
+  public static final String DOCKER_COMPOSE_SERVICE_NAME = "DOCKER_COMPOSE_SERVICE_NAME";
+
   private String myMachineName;
 
   private String myImageName;
 
   private String myContainerName;
 
+  private String myComposeFilePath;
+
+  private String myComposeServiceName;
+
   private String myRemoteProjectPath;
 
   public DockerCredentialsHolder() {
@@ -49,6 +57,20 @@ public class DockerCredentialsHolder {
     myRemoteProjectPath = remoteProjectPath;
   }
 
+  public DockerCredentialsHolder(String machineName,
+                                 String composeFilePath,
+                                 String composeServiceName,
+                                 String imageName,
+                                 String containerName,
+                                 String remoteProjectPath) {
+    myMachineName = machineName;
+    myComposeFilePath = composeFilePath;
+    myComposeServiceName = composeServiceName;
+    myImageName = imageName;
+    myContainerName = containerName;
+    myRemoteProjectPath = remoteProjectPath;
+  }
+
   public String getMachineName() {
     return myMachineName;
   }
@@ -65,21 +87,47 @@ public class DockerCredentialsHolder {
     return myRemoteProjectPath;
   }
 
+  public String getComposeFilePath() {
+    return myComposeFilePath;
+  }
+
+  public String getComposeServiceName() {
+    return myComposeServiceName;
+  }
+
   public void save(@NotNull Element element) {
-    if (StringUtil.isNotEmpty(myMachineName)) {
-      element.setAttribute(DOCKER_MACHINE_NAME, myMachineName);
-    }
-    element.setAttribute(DOCKER_IMAGE_NAME, myImageName);
-    if (StringUtil.isNotEmpty(myContainerName)) {
-      element.setAttribute(DOCKER_CONTAINER_NAME, myContainerName);
-    }
+    setAttributeIfNotEmpty(element, DOCKER_MACHINE_NAME, myMachineName);
+    setAttributeIfNotEmpty(element, DOCKER_IMAGE_NAME, myImageName);
+    setAttributeIfNotEmpty(element, DOCKER_CONTAINER_NAME, myContainerName);
+    setAttributeIfNotEmpty(element, DOCKER_COMPOSE_FILE_PATH, myComposeFilePath);
+    setAttributeIfNotEmpty(element, DOCKER_COMPOSE_SERVICE_NAME, myComposeServiceName);
     element.setAttribute(DOCKER_REMOTE_PROJECT_PATH, myRemoteProjectPath);
   }
 
+  private static void setAttributeIfNotEmpty(@NotNull Element element, @NotNull String attribute, @Nullable String value) {
+    if (StringUtil.isNotEmpty(value)) {
+      element.setAttribute(attribute, value);
+    }
+  }
+
   public void load(@NotNull Element element) {
     myMachineName = element.getAttributeValue(DOCKER_MACHINE_NAME);
     myImageName = element.getAttributeValue(DOCKER_IMAGE_NAME);
     myContainerName = element.getAttributeValue(DOCKER_CONTAINER_NAME);
+    myComposeFilePath = element.getAttributeValue(DOCKER_COMPOSE_FILE_PATH);
+    myComposeServiceName = element.getAttributeValue(DOCKER_COMPOSE_SERVICE_NAME);
     myRemoteProjectPath = element.getAttributeValue(DOCKER_REMOTE_PROJECT_PATH);
   }
+
+  public boolean isDockerComposeCredentials() {
+    return myComposeFilePath != null;
+  }
+
+  @NotNull
+  public static DockerCredentialsHolder newDockerComposeCredentials(@Nullable String machineName,
+                                                                    @Nullable String composeFilePath,
+                                                                    @Nullable String composeServiceName,
+                                                                    @Nullable String remoteProjectPath) {
+    return new DockerCredentialsHolder(machineName, composeFilePath, composeServiceName, null, null, remoteProjectPath);
+  }
 }
index 7cbac57740977e397d271f9fe94da0059a0c8b50..7898c4f885048068993cbc2db3343763b7f8546a 100644 (file)
@@ -185,7 +185,13 @@ public class RemoteConnectionCredentialsWrapper {
       @Override
       public void docker(@NotNull DockerCredentialsHolder cred) {
         // TODO [Docker] review
-        String name = StringUtil.isNotEmpty(cred.getContainerName()) ? cred.getContainerName() : cred.getImageName();
+        String name;
+        if (cred.isDockerComposeCredentials()) {
+          name = cred.getComposeFilePath() + ":" + cred.getComposeServiceName();
+        }
+        else {
+          name = StringUtil.isNotEmpty(cred.getContainerName()) ? cred.getContainerName() : cred.getImageName();
+        }
         result.set(DOCKER_PREFIX + name + "/");
       }
     });
@@ -270,7 +276,12 @@ public class RemoteConnectionCredentialsWrapper {
       public void docker(@NotNull DockerCredentialsHolder credentials) {
         String containerName = StringUtil.isNotEmpty(credentials.getContainerName())
                                ? credentials.getContainerName() + " " : "";
-        result.set("Docker " + containerName + "(" + credentials.getImageName() + ")");
+        if (credentials.isDockerComposeCredentials()) {
+          result.set("Docker Compose " + credentials.getComposeFilePath() + ":" + credentials.getComposeServiceName());
+        }
+        else {
+          result.set("Docker " + containerName + "(" + credentials.getImageName() + ")");
+        }
       }
     });