Dhandapani Gopal has uploaded a new change for review.

Change subject: engine: Validation added in Import Cluster
......................................................................

engine: Validation added in Import Cluster

 - Validation added to check whether the given server or any server in the peer 
list
   is already part of the existing cluster by checking with the database.

 - Renamed GetGlusterServersQuery to GetGlusterServersForImportQuery,
   since it is related to import cluster operation.

Change-Id: I8323f2dcc4f278dfbc01f99ebe18b0f2bd0296ca
Signed-off-by: Dhandapani <[email protected]>
---
R 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersForImportQuery.java
A 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersForImportQueryTest.java
D 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersQueryTest.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/VdcQueryType.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/VdcBllMessages.java
M backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/dataprovider/AsyncDataProvider.java
7 files changed, 131 insertions(+), 87 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/84/9784/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersQuery.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersForImportQuery.java
similarity index 75%
rename from 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersQuery.java
rename to 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersForImportQuery.java
index a6b57eb..9f9ce7a 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersQuery.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersForImportQuery.java
@@ -13,6 +13,9 @@
 import org.ovirt.engine.core.common.config.Config;
 import org.ovirt.engine.core.common.config.ConfigValues;
 import 
org.ovirt.engine.core.common.queries.gluster.GlusterServersQueryParameters;
+import org.ovirt.engine.core.dal.VdcBllMessages;
+import org.ovirt.engine.core.dal.dbbroker.DbFacade;
+import org.ovirt.engine.core.dao.VdsStaticDAO;
 import org.ovirt.engine.core.engineencryptutils.OpenSSHUtils;
 import org.ovirt.engine.core.utils.XmlUtils;
 import org.ovirt.engine.core.utils.ssh.ConstraintByteArrayOutputStream;
@@ -25,12 +28,14 @@
 /**
  * Query to fetch list of gluster servers via ssh using the given serverName 
and password.
  *
- * This query will be invoked from Import Gluster Cluster dialog. In the 
dialog the user will provide the servername and
- * password of any one of the server in the cluster. Since, the importing 
cluster haven't been bootstarped yet, we are
- * running the gluster peer status command via ssh.
+ * This query will be invoked from Import Gluster Cluster dialog. In the 
dialog the user will provide the servername,
+ * password and fingerprint of any one of the server in the cluster. This 
Query will validate if the given server is
+ * already part of the cluster by checking with the database. If exists the 
query will return the error message.
+ *
+ * Since, the importing cluster haven't been bootstarped yet, we are running 
the gluster peer status command via ssh.
  *
  */
-public class GetGlusterServersQuery<P extends GlusterServersQueryParameters> 
extends GlusterQueriesCommandBase<P> {
+public class GetGlusterServersForImportQuery<P extends 
GlusterServersQueryParameters> extends GlusterQueriesCommandBase<P> {
 
     private static final String PEER = "peer";
     private static final String HOST_NAME = "hostname";
@@ -39,12 +44,19 @@
     private static final int PORT = 22;
     private static final String ROOT = "root";
 
-    public GetGlusterServersQuery(P parameters) {
+    public GetGlusterServersForImportQuery(P parameters) {
         super(parameters);
     }
 
     @Override
     protected void executeQueryCommand() {
+        // Check whether the given server is already part of the cluster
+        if (getVdsStaticDao().get(getParameters().getServerName()) != null) {
+            getQueryReturnValue().setSucceeded(false);
+            
getQueryReturnValue().setExceptionString(VdcBllMessages.SERVER_ALREADY_EXIST_IN_ANOTHER_CLUSTER.name());
+            return;
+        }
+
         SSHClient client = null;
 
         try {
@@ -52,7 +64,14 @@
             validateFingerprint(client, getParameters().getFingerprint());
             authenticate(client, ROOT, getParameters().getPassword());
             String serversXml = executeCommand(client);
-            getQueryReturnValue().setReturnValue(extractServers(serversXml));
+
+            Map<String, String> serverFingerPrint = extractServers(serversXml);
+            if (serverFingerPrint == null) {
+                getQueryReturnValue().setSucceeded(false);
+                
getQueryReturnValue().setExceptionString(VdcBllMessages.SERVER_ALREADY_EXIST_IN_ANOTHER_CLUSTER.name());
+                return;
+            }
+            getQueryReturnValue().setReturnValue(serverFingerPrint);
         } catch (Exception e) {
             throw new RuntimeException(e);
         } finally {
@@ -83,6 +102,10 @@
                 // Add the server only if the state is 3
                 if (state == PEER_IN_CLUSTER) {
                     String hostName = XmlUtils.getTextValue(firstHostElement, 
HOST_NAME);
+                    // Check if any of the server in the peer list is already 
part of some other cluster.
+                    if (getVdsStaticDao().get(hostName) != null) {
+                        return null;
+                    }
                     fingerprints.put(hostName, getFingerprint(hostName));
                 }
             }
@@ -155,4 +178,8 @@
 
         return OpenSSHUtils.getKeyFingerprintString(hostKey);
     }
+
+    protected VdsStaticDAO getVdsStaticDao() {
+        return DbFacade.getInstance().getVdsStaticDao();
+    }
 }
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersForImportQueryTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersForImportQueryTest.java
new file mode 100644
index 0000000..fe639dd
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersForImportQueryTest.java
@@ -0,0 +1,94 @@
+package org.ovirt.engine.core.bll.gluster;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.ovirt.engine.core.utils.MockConfigRule.mockConfig;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.ovirt.engine.core.bll.AbstractQueryTest;
+import org.ovirt.engine.core.common.businessentities.VdsStatic;
+import org.ovirt.engine.core.common.config.ConfigValues;
+import 
org.ovirt.engine.core.common.queries.gluster.GlusterServersQueryParameters;
+import org.ovirt.engine.core.compat.Guid;
+import org.ovirt.engine.core.dao.VdsStaticDAO;
+import org.ovirt.engine.core.utils.MockConfigRule;
+import org.ovirt.engine.core.utils.ssh.SSHClient;
+
+public class GetGlusterServersForImportQueryTest extends 
AbstractQueryTest<GlusterServersQueryParameters, 
GetGlusterServersForImportQuery<GlusterServersQueryParameters>> {
+
+    private static final String SERVER_NAME1 = "testserver1";
+    private static final String SERVER_NAME2 = "testserver2";
+    private static final String NEW_SERVER = "testserver3";
+    private static final String PASSWORD = "password";
+    private Map<String, String> EXPECTED_MAP = new HashMap<String, String>();
+    private static final String FINGER_PRINT1 = 
"31:e2:1b:7e:89:86:99:c3:f7:1e:57:35:fe:9b:5c:31";
+    private static final int CONNECT_TO_SERVER_TIMEOUT = 20;
+    private static final String GLUSTER_PEER_STATUS_CMD = "gluster peer status 
--xml";
+    private static final String OUTPUT_XML =
+            
"<cliOutput><peerStatus><peer><uuid>85c42b0d-c2b7-424a-ae72-5174c25da40b</uuid><hostname>testserver1</hostname><connected>1</connected><state>3</state></peer>"
+                    +
+                    
"<peer><uuid>85c42b0d-c2b7-424a-ae72-5174c25da40b</uuid><hostname>testserver2</hostname><connected>1</connected><state>3</state></peer></peerStatus></cliOutput>";
+    private SSHClient clientMock;
+    private VdsStaticDAO vdsStaticDaoMock;
+
+    @ClassRule
+    public static MockConfigRule mcr = new MockConfigRule(
+            mockConfig(ConfigValues.ConnectToServerTimeoutInSeconds, 
CONNECT_TO_SERVER_TIMEOUT),
+            mockConfig(ConfigValues.GlusterPeerStatusCommand, 
GLUSTER_PEER_STATUS_CMD));
+
+    @Before
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        setupMock();
+        setupExpectedFingerPrint();
+    }
+
+    private void setupMock() {
+        vdsStaticDaoMock = mock(VdsStaticDAO.class);
+        doReturn(vdsStaticDaoMock).when(getQuery()).getVdsStaticDao();
+        doReturn(getVdsStatic()).when(vdsStaticDaoMock).get(NEW_SERVER);
+
+        clientMock = mock(SSHClient.class);
+        doReturn(clientMock).when(getQuery()).createSSHClient();
+    }
+
+    private VdsStatic getVdsStatic() {
+        VdsStatic vds = new VdsStatic();
+        vds.setId(new Guid());
+        vds.sethost_name(NEW_SERVER);
+        return vds;
+    }
+
+    private void setupExpectedFingerPrint() throws Exception {
+        doReturn(SERVER_NAME1).when(getQueryParameters()).getServerName();
+        doReturn(PASSWORD).when(getQueryParameters()).getPassword();
+        doReturn(FINGER_PRINT1).when(getQueryParameters()).getFingerprint();
+        doReturn(clientMock).when(getQuery()).connect(SERVER_NAME1);
+        doReturn(OUTPUT_XML).when(getQuery()).executeCommand(clientMock);
+        doNothing().when(getQuery()).authenticate(clientMock, "root", 
PASSWORD);
+        doNothing().when(getQuery()).validateFingerprint(clientMock, 
FINGER_PRINT1);
+
+        EXPECTED_MAP.put(SERVER_NAME1, FINGER_PRINT1);
+        EXPECTED_MAP.put(SERVER_NAME2, FINGER_PRINT1);
+        doReturn(EXPECTED_MAP).when(getQuery()).extractServers(OUTPUT_XML);
+    }
+
+    @Test
+    public void testExecuteQueryCommand() {
+        getQuery().executeQueryCommand();
+        Map<String, String> serverFingerprintMap =
+                (Map<String, String>) 
getQuery().getQueryReturnValue().getReturnValue();
+
+        assertNotNull(serverFingerprintMap);
+        assertEquals(EXPECTED_MAP, serverFingerprintMap);
+    }
+}
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersQueryTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersQueryTest.java
deleted file mode 100644
index b0f1c56..0000000
--- 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GetGlusterServersQueryTest.java
+++ /dev/null
@@ -1,79 +0,0 @@
-package org.ovirt.engine.core.bll.gluster;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-import static org.ovirt.engine.core.utils.MockConfigRule.mockConfig;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Test;
-import org.ovirt.engine.core.bll.AbstractQueryTest;
-import org.ovirt.engine.core.common.config.ConfigValues;
-import 
org.ovirt.engine.core.common.queries.gluster.GlusterServersQueryParameters;
-import org.ovirt.engine.core.utils.MockConfigRule;
-import org.ovirt.engine.core.utils.ssh.SSHClient;
-
-public class GetGlusterServersQueryTest extends 
AbstractQueryTest<GlusterServersQueryParameters, 
GetGlusterServersQuery<GlusterServersQueryParameters>> {
-
-    String serverName1 = "testserver1";
-    String password = "password";
-    Map<String, String> expectedMap = new HashMap<String, String>();
-    SSHClient clientMock;
-    String serverName2 = "testserver2";
-    String fingerprint1 = "31:e2:1b:7e:89:86:99:c3:f7:1e:57:35:fe:9b:5c:31";
-    String fingerprint2 = "31:e2:1b:7e:89:86:99:c3:f7:1e:57:35:fe:9b:5c:32";
-    private static final int CONNECT_TO_SERVER_TIMEOUT = 20;
-    private static final String GLUSTER_PEER_STATUS_CMD = "gluster peer status 
--xml";
-    private static final String outputXml =
-            
"<cliOutput><peerStatus><peer><uuid>85c42b0d-c2b7-424a-ae72-5174c25da40b</uuid><hostname>testserver1</hostname><connected>1</connected><state>3</state></peer>"
-                    +
-                    
"<peer><uuid>85c42b0d-c2b7-424a-ae72-5174c25da40b</uuid><hostname>testserver2</hostname><connected>1</connected><state>3</state></peer></peerStatus></cliOutput>";
-
-    @ClassRule
-    public static MockConfigRule mcr = new MockConfigRule(
-            mockConfig(ConfigValues.ConnectToServerTimeoutInSeconds, 
CONNECT_TO_SERVER_TIMEOUT),
-            mockConfig(ConfigValues.GlusterPeerStatusCommand, 
GLUSTER_PEER_STATUS_CMD));
-
-    @Before
-    @Override
-    public void setUp() throws Exception {
-        super.setUp();
-        setupMock();
-        setupExpectedFingerPrint();
-    }
-
-    private void setupMock() {
-        clientMock = mock(SSHClient.class);
-        doReturn(clientMock).when(getQuery()).createSSHClient();
-    }
-
-    private void setupExpectedFingerPrint() throws Exception {
-        doReturn(serverName1).when(getQueryParameters()).getServerName();
-        doReturn(password).when(getQueryParameters()).getPassword();
-        doReturn(fingerprint1).when(getQueryParameters()).getFingerprint();
-        doReturn(clientMock).when(getQuery()).connect(serverName1);
-        doReturn(outputXml).when(getQuery()).executeCommand(clientMock);
-        doNothing().when(getQuery()).authenticate(clientMock, "root", 
password);
-        doNothing().when(getQuery()).validateFingerprint(clientMock, 
fingerprint1);
-
-        expectedMap.put(serverName1, fingerprint1);
-        expectedMap.put(serverName2, fingerprint1);
-        doReturn(expectedMap).when(getQuery()).extractServers(outputXml);
-    }
-
-    @Test
-    public void testExecuteQueryCommand() {
-        getQuery().executeQueryCommand();
-        Map<String, String> serverFingerprintMap =
-                (Map<String, String>) 
getQuery().getQueryReturnValue().getReturnValue();
-
-        assertNotNull(serverFingerprintMap);
-        assertEquals(expectedMap, serverFingerprintMap);
-    }
-}
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/VdcQueryType.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/VdcQueryType.java
index 609bc75..aa90ddf 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/VdcQueryType.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/VdcQueryType.java
@@ -283,7 +283,7 @@
     GetGlusterVolumeBricks,
     GetGlusterBrickById,
     GetServerSSHKeyFingerprint,
-    GetGlusterServers,
+    GetGlusterServersForImport,
     GetAddedGlusterServers,
     GetGlusterVolumeAdvancedDetails,
     GetGlusterVolumeProfileInfo,
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/VdcBllMessages.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/VdcBllMessages.java
index 5128be3..4ff6dca 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/VdcBllMessages.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/VdcBllMessages.java
@@ -667,6 +667,7 @@
     ACTION_TYPE_FAILED_NO_GLUSTER_HOST_TO_PEER_PROBE,
     MIGRATE_PAUSED_VM_IS_UNSUPPORTED,
     ACTION_TYPE_FAILED_SERVER_NAME_REQUIRED,
+    SERVER_ALREADY_EXIST_IN_ANOTHER_CLUSTER,
 
     VM_INTERFACE_NOT_EXIST,
     ACTION_TYPE_FAILED_CANNOT_REMOVE_ACTIVE_DEVICE,
diff --git 
a/backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties 
b/backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties
index 379e566..878c620 100644
--- 
a/backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties
+++ 
b/backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties
@@ -808,3 +808,4 @@
 ENGINE_IS_RUNNING_IN_MAINTENANCE_MODE=Engine is running in Maintenance mode 
and is not accepting commands.
 ENGINE_IS_RUNNING_IN_PREPARE_MODE=This action is not allowed when Engine is 
preparing for maintenance.
 ACTION_TYPE_FAILED_SERVER_NAME_REQUIRED=Cannot ${action} ${type}. Server Name 
required.
+HOST_NAME_ALREADY_EXIST_IN_ANOTHER_CLUSTER= Server is already part of another 
cluster.
diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/dataprovider/AsyncDataProvider.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/dataprovider/AsyncDataProvider.java
index 2b21fab..a08721b 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/dataprovider/AsyncDataProvider.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/dataprovider/AsyncDataProvider.java
@@ -1082,7 +1082,7 @@
         };
         GlusterServersQueryParameters parameters = new 
GlusterServersQueryParameters(hostAddress, rootPassword);
         parameters.setFingerprint(fingerprint);
-        Frontend.RunQuery(VdcQueryType.GetGlusterServers,
+        Frontend.RunQuery(VdcQueryType.GetGlusterServersForImport,
                 parameters,
                 aQuery);
     }


--
To view, visit http://gerrit.ovirt.org/9784
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8323f2dcc4f278dfbc01f99ebe18b0f2bd0296ca
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Dhandapani Gopal <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to