Skip to content

Commit

Permalink
Merge pull request #547 from Ginger-Automation/BugFix/6729-Couch_base…
Browse files Browse the repository at this point in the history
…_DB_Configuration

Bug fix/6729 couch base db configuration
  • Loading branch information
JaspreetGIT committed Jan 22, 2019
2 parents 3ffbc69 + f9dd043 commit 1518773
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 6 deletions.
26 changes: 22 additions & 4 deletions Ginger/Ginger/Actions/ValidationDBPage.xaml.cs
Expand Up @@ -260,6 +260,16 @@ private void KeySpaceComboBox_DropDownOpened(object sender, EventArgs e)
NoSqlBase NoSqlDriver = null;
NoSqlDriver = new GingerCassandra(db);

List<string> keyspace = NoSqlDriver.GetKeyspaceList();
foreach (string s in keyspace)
{
KeySpaceComboBox.Items.Add(s);
}
}else if (db.DBType == Database.eDBTypes.Couchbase)
{
NoSqlBase NoSqlDriver = null;
NoSqlDriver = new GingerCouchbase(db);

List<string> keyspace = NoSqlDriver.GetKeyspaceList();
foreach (string s in keyspace)
{
Expand All @@ -276,10 +286,14 @@ private void TablesComboBox_DropDownOpened(object sender, EventArgs e)
if (db == null) return;
string KeySpace = KeySpaceComboBox.Text;
List<string> Tables = db.GetTablesList(KeySpace);
foreach (string s in Tables)
{
TablesComboBox.Items.Add(s);
}
if (Tables == null)
{
return;
}
foreach (string s in Tables)
{
TablesComboBox.Items.Add(s);
}
}

private void ColumnComboBox_DropDownOpened(object sender, EventArgs e)
Expand All @@ -298,6 +312,10 @@ private void ColumnComboBox_DropDownOpened(object sender, EventArgs e)
table = TablesComboBox.Text;
}
List<string> Columns = db.GetTablesColumns(table);
if (Columns == null)
{
return;
}
foreach (string s in Columns)
{
ColumnComboBox.Items.Add(s);
Expand Down
4 changes: 4 additions & 0 deletions Ginger/Ginger/App.config
Expand Up @@ -124,6 +124,10 @@
<assemblyIdentity name="System.Diagnostics.StackTrace" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
</dependentAssembly>
</assemblyBinding>

<AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false" />
Expand Down
2 changes: 1 addition & 1 deletion Ginger/Ginger/Environments/AppDataBasesPage.xaml.cs
Expand Up @@ -85,7 +85,7 @@ private void TestDBConnection(object sender, RoutedEventArgs e)
db.DSList = WorkSpace.Instance.SolutionRepository.GetAllRepositoryItems<DataSourceBase>();
db.ProjEnvironment = App.AutomateTabEnvironment;
db.BusinessFlow = App.BusinessFlow;
if (string.IsNullOrEmpty(db.ConnectionString) && db.TNS.ToLower().Contains("data source=") && db.TNS.ToLower().Contains("password=") && db.TNS.ToLower().Contains("user id="))
if (string.IsNullOrEmpty(db.ConnectionString) && !string.IsNullOrEmpty(db.TNS) && db.TNS.ToLower().Contains("data source=") && db.TNS.ToLower().Contains("password=") && db.TNS.ToLower().Contains("user id="))
{
System.Data.SqlClient.SqlConnectionStringBuilder scSB = new System.Data.SqlClient.SqlConnectionStringBuilder();
scSB.ConnectionString = db.TNS;
Expand Down
7 changes: 6 additions & 1 deletion Ginger/GingerCore/Actions/ActDBValidation.cs
Expand Up @@ -133,7 +133,7 @@ public eDatabaseTye DatabaseType
get
{try
{
if (DB.DBType == Database.eDBTypes.Cassandra)
if (DB.DBType == Database.eDBTypes.Cassandra || DB.DBType == Database.eDBTypes.Couchbase)
return eDatabaseTye.NoSQL;
else return eDatabaseTye.Relational;
}
Expand Down Expand Up @@ -273,6 +273,11 @@ private void HandleNoSQLDBAction()
NoSqlDriver= new GingerCassandra(DBValidationType,DB,this);
NoSqlDriver.PerformDBAction();

break;
case Database.eDBTypes.Couchbase:
NoSqlDriver = new GingerCouchbase(DBValidationType, DB, this);
NoSqlDriver.PerformDBAction();

break;
}
}
Expand Down
21 changes: 21 additions & 0 deletions Ginger/GingerCore/Environments/Database.cs
Expand Up @@ -48,6 +48,7 @@ public enum eDBTypes
Cassandra,
PostgreSQL,
MySQL,
Couchbase,
}

public enum eConfigType
Expand Down Expand Up @@ -406,6 +407,16 @@ public Boolean Connect(bool displayErrorPopup = false)
LastConnectionUsedTime = DateTime.Now;
return true;
break;
case eDBTypes.Couchbase:
GingerCouchbase CouchbaseDriver = new GingerCouchbase(this);
bool isConnectionCB;
isConnectionCB = CouchbaseDriver.Connect();
if (isConnectionCB == true)
{
LastConnectionUsedTime = DateTime.Now;
}
return true;
break;

case eDBTypes.MySQL:
oConn = new MySqlConnection();
Expand Down Expand Up @@ -477,6 +488,11 @@ public List<string> GetTablesList(string Keyspace = null)
NoSqlBase.NoSqlBase NoSqlDriver = null;
NoSqlDriver = new GingerCassandra(this);
rc = NoSqlDriver.GetTableList(Keyspace);
} else if (DBType == Database.eDBTypes.Couchbase)
{
NoSqlBase.NoSqlBase NoSqlDriver = null;
NoSqlDriver = new GingerCouchbase(this);
rc = NoSqlDriver.GetTableList(Keyspace);
}
else
{
Expand Down Expand Up @@ -530,6 +546,11 @@ public List<string> GetTablesColumns(string table)
NoSqlBase.NoSqlBase NoSqlDriver = null;
NoSqlDriver = new GingerCassandra(this);
rc = NoSqlDriver.GetColumnList(table);
}else if (DBType == Database.eDBTypes.Couchbase)
{
NoSqlBase.NoSqlBase NoSqlDriver = null;
NoSqlDriver = new GingerCouchbase(this);
rc = NoSqlDriver.GetColumnList(table);
}
else
{
Expand Down
15 changes: 15 additions & 0 deletions Ginger/GingerCore/GingerCore.csproj
Expand Up @@ -96,6 +96,15 @@
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\packages\Castle.Core.4.3.1\lib\net45\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="Common.Logging, Version=3.4.1.0, Culture=neutral, PublicKeyToken=af08829b84f0328e, processorArchitecture=MSIL">
<HintPath>..\packages\Common.Logging.3.4.1\lib\net40\Common.Logging.dll</HintPath>
</Reference>
<Reference Include="Common.Logging.Core, Version=3.4.1.0, Culture=neutral, PublicKeyToken=af08829b84f0328e, processorArchitecture=MSIL">
<HintPath>..\packages\Common.Logging.Core.3.4.1\lib\net40\Common.Logging.Core.dll</HintPath>
</Reference>
<Reference Include="Couchbase.NetClient, Version=2.7.3.0, Culture=neutral, PublicKeyToken=05e9c6b5a9ec94c2, processorArchitecture=MSIL">
<HintPath>..\packages\CouchbaseNetClient.2.7.3\lib\net452\Couchbase.NetClient.dll</HintPath>
</Reference>
<Reference Include="DocumentFormat.OpenXml, Version=2.8.1.0, Culture=neutral, PublicKeyToken=8fb06cb64d019a17, processorArchitecture=MSIL">
<HintPath>..\packages\DocumentFormat.OpenXml.2.8.1\lib\net46\DocumentFormat.OpenXml.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -214,6 +223,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Ginger\DLLs\Open3270.dll</HintPath>
</Reference>
<Reference Include="OpenTracing, Version=0.12.0.0, Culture=neutral, PublicKeyToken=61503406977abdaf, processorArchitecture=MSIL">
<HintPath>..\packages\OpenTracing.0.12.0\lib\net45\OpenTracing.dll</HintPath>
</Reference>
<Reference Include="Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=MSIL">
<HintPath>..\packages\Oracle.ManagedDataAccess.12.1.24160719\lib\net40\Oracle.ManagedDataAccess.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -308,6 +320,7 @@
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
Expand All @@ -318,6 +331,7 @@
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Remoting" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security.Cryptography.Algorithms, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Algorithms.4.3.1\lib\net461\System.Security.Cryptography.Algorithms.dll</HintPath>
Expand Down Expand Up @@ -712,6 +726,7 @@
<DependentUpon>ComboBoxWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Helpers\HTMLHelper.cs" />
<Compile Include="NoSqlBase\GingerCouchbase.cs" />
<Compile Include="NoSqlBase\GingerUDTMap.cs" />
<Compile Include="NoSqlBase\GingerCassandra.cs" />
<Compile Include="NoSqlBase\MyClassBuilder.cs" />
Expand Down
179 changes: 179 additions & 0 deletions Ginger/GingerCore/NoSqlBase/GingerCouchbase.cs
@@ -0,0 +1,179 @@
#region License
/*
Copyright © 2014-2018 European Support Limited
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using GingerCore.Actions;
using Amdocs.Ginger.Common;
using Couchbase;
using Couchbase.Configuration.Client;
using Couchbase.Authentication;
using Couchbase.N1QL;
using Couchbase.Configuration.Server.Serialization;


namespace GingerCore.NoSqlBase
{
public class GingerCouchbase : NoSqlBase
{
Cluster clusterCB = null;
ActDBValidation Act = null;

public override List<eNoSqlOperations> GetSupportedActions()
{
List<eNoSqlOperations> SupportedActions = new List<eNoSqlOperations>();

SupportedActions.Add(eNoSqlOperations.freesql);
return SupportedActions;
}

public bool Connect()
{
try
{
clusterCB = new Couchbase.Cluster(new ClientConfiguration
{
ViewRequestTimeout = 45000,
Servers = new List<Uri> { new Uri(Db.TNSCalculated.ToString()) },//http://incespr021:8091
});
bool res = false;
String deCryptValue = EncryptionHandler.DecryptString(Db.PassCalculated.ToString(), ref res, false);
if (res == true)
{
Db.Pass = deCryptValue;
}

clusterCB.Authenticate(Db.UserCalculated.ToString(), Db.PassCalculated.ToString());
return true;
}
catch (Exception e)
{
Reporter.ToLog(eLogLevel.ERROR, "Failed to connect to Couchbase DB", e);
throw (e);
}
}

//TODO: need this while checking Test Connection , need to find a better way
public GingerCouchbase(Environments.Database mDB)
{
this.Db = mDB;
}

public GingerCouchbase(ActDBValidation.eDBValidationType DBValidationtype, Environments.Database mDB, ActDBValidation mact)
{
Action = DBValidationtype;
this.Db = mDB;
Act = mact;
}

public override List<string> GetKeyspaceList()
{
return null;
}

public override List<string> GetTableList(string keyspace)
{
return null;
}

public override List<string> GetColumnList(string tablename)
{
return null;
}

private void Disconnect()
{
clusterCB.Dispose();
}

public void ConnecttoBucket(string bucketName)
{
var clusterMan = clusterCB.CreateManager(Db.UserCalculated.ToString(),Db.PassCalculated.ToString());
IList<BucketConfig> buckets = clusterMan.ListBuckets().Value;
string bucketpassword = (from buckConfig in buckets where buckConfig.Name == bucketName select buckConfig.SaslPassword).FirstOrDefault();
if(bucketpassword == null)
{
throw new Exception("No bucket found with name " + bucketName);
}
ClassicAuthenticator classicAuthenticator = new ClassicAuthenticator("Administrator", "Administrator");
classicAuthenticator.AddBucketCredential(bucketName, bucketpassword);
clusterCB.Authenticate(classicAuthenticator);
}

public override void PerformDBAction()
{
Connect();
string SQL = Act.SQL;
string keyspace = Act.Keyspace;
ValueExpression VE = new ValueExpression(Db.ProjEnvironment, Db.BusinessFlow, Db.DSList);
VE.Value = SQL;
string SQLCalculated = VE.ValueCalculated;
IQueryResult<dynamic> result =null;
string bucketName = "";
try
{
switch (Action)
{
case Actions.ActDBValidation.eDBValidationType.FreeSQL:

if(!String.IsNullOrEmpty(SQLCalculated))
{
bucketName = SQLCalculated.Substring(SQLCalculated.IndexOf(" from ") + 6);
bucketName = bucketName.Substring(0, bucketName.IndexOf(" ")).Replace("`","");
}
ConnecttoBucket(bucketName);
result = clusterCB.Query<dynamic>(SQLCalculated);
for (int i=0; i< result.Rows.Count;i++)
{
Act.ParseJSONToOutputValues(result.Rows[i].ToString(), i+1);
}
break;
case Actions.ActDBValidation.eDBValidationType.RecordCount:
string SQLRecord = SQLCalculated;
ConnecttoBucket(SQLCalculated);
result = clusterCB.Query<dynamic>("Select Count(*) as RECORDCOUNT from `" + SQLCalculated + "`");
Act.ParseJSONToOutputValues(result.Rows[0].ToString(), 1);
break;
case Actions.ActDBValidation.eDBValidationType.UpdateDB:
if (!String.IsNullOrEmpty(SQLCalculated))
{
bucketName = SQLCalculated.Substring(SQLCalculated.IndexOf(" from ") + 6);
bucketName = bucketName.Substring(0, bucketName.IndexOf(" ")).Replace("`", "");
}
ConnecttoBucket(bucketName);
var RS1 = clusterCB.Query<dynamic>(SQLCalculated);
break;

default:
throw new Exception("Action Not SUpported");
}
}
catch (Exception e)
{
Act.Error = "Failed to execute. Error :" + e.Message;
Reporter.ToLog(eLogLevel.ERROR, $"Method - {MethodBase.GetCurrentMethod().Name}, Error - {e.Message}", e);
}
if (!Db.KeepConnectionOpen)
{
Disconnect();
}
}
}
}
4 changes: 4 additions & 0 deletions Ginger/GingerCore/app.config
Expand Up @@ -58,6 +58,10 @@
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.4.1" newVersion="4.0.4.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.data>
Expand Down

0 comments on commit 1518773

Please sign in to comment.