Esempio n. 1
0
        public void CanGetAllConnectionStrings()
        {
            using (var store = GetDocumentStore())
            {
                var ravenConnectionStrings         = new List <RavenConnectionString>();
                var sqlConnectionStrings           = new List <SqlConnectionString>();
                var elasticSearchConnectionStrings = new List <ElasticSearchConnectionString>();
                for (var i = 0; i < 5; i++)
                {
                    var ravenConnectionStr = new RavenConnectionString()
                    {
                        Name = $"RavenConnectionString{i}",
                        TopologyDiscoveryUrls = new[] { $"http://127.0.0.1:808{i}" },
                        Database = "Northwind",
                    };
                    var sqlConnectionStr = new SqlConnectionString
                    {
                        Name             = $"SqlConnectionString{i}",
                        ConnectionString = MssqlConnectionString.Instance.VerifiedConnectionString.Value + $";Initial Catalog={store.Database}"
                    };
                    var elasticConnectionStr = new ElasticSearchConnectionString
                    {
                        Name  = $"ElasticConnectionString{i}",
                        Nodes = new[] { $"http://127.0.0.1:808{i}" },
                    };

                    ravenConnectionStrings.Add(ravenConnectionStr);
                    sqlConnectionStrings.Add(sqlConnectionStr);
                    elasticSearchConnectionStrings.Add(elasticConnectionStr);

                    var result1 = store.Maintenance.Send(new PutConnectionStringOperation <RavenConnectionString>(ravenConnectionStr));
                    Assert.NotNull(result1.RaftCommandIndex);
                    var result2 = store.Maintenance.Send(new PutConnectionStringOperation <SqlConnectionString>(sqlConnectionStr));
                    Assert.NotNull(result2.RaftCommandIndex);
                    var result3 = store.Maintenance.Send(new PutConnectionStringOperation <ElasticSearchConnectionString>(elasticConnectionStr));
                    Assert.NotNull(result3.RaftCommandIndex);
                }

                var result = store.Maintenance.Send(new GetConnectionStringsOperation());
                Assert.NotNull(result.SqlConnectionStrings);
                Assert.NotNull(result.RavenConnectionStrings);
                Assert.NotNull(result.ElasticSearchConnectionStrings);

                for (var i = 0; i < 5; i++)
                {
                    result.SqlConnectionStrings.TryGetValue($"SqlConnectionString{i}", out var sql);
                    Assert.Equal(sql?.ConnectionString, sqlConnectionStrings[i].ConnectionString);

                    result.ElasticSearchConnectionStrings.TryGetValue($"ElasticConnectionString{i}", out var elastic);
                    Assert.Equal(elastic?.Nodes, elasticSearchConnectionStrings[i].Nodes);

                    result.RavenConnectionStrings.TryGetValue($"RavenConnectionString{i}", out var raven);
                    Assert.Equal(raven?.TopologyDiscoveryUrls, ravenConnectionStrings[i].TopologyDiscoveryUrls);
                    Assert.Equal(raven?.Database, ravenConnectionStrings[i].Database);
                }
            }
        }
Esempio n. 2
0
        public static ElasticClient CreateClient(ElasticSearchConnectionString connectionString, TimeSpan?requestTimeout = null, TimeSpan?pingTimeout = null)
        {
            Uri[] nodesUrls = connectionString.Nodes.Select(x => new Uri(x)).ToArray();

            StaticConnectionPool pool     = new StaticConnectionPool(nodesUrls);
            ConnectionSettings   settings = new ConnectionSettings(pool);

            if (requestTimeout != null)
            {
                settings.RequestTimeout(requestTimeout.Value);
            }

            if (pingTimeout != null)
            {
                settings.PingTimeout(pingTimeout.Value);
            }

            if (connectionString.Authentication != null)
            {
                if (connectionString.Authentication.Basic != null)
                {
                    settings.BasicAuthentication(connectionString.Authentication.Basic.Username, connectionString.Authentication.Basic.Password);
                }
                else if (connectionString.Authentication.ApiKey != null)
                {
                    settings.ApiKeyAuthentication(connectionString.Authentication.ApiKey.ApiKeyId, connectionString.Authentication.ApiKey.ApiKey);
                }
                else if (connectionString.Authentication.Certificate != null)
                {
                    if (connectionString.Authentication.Certificate.CertificatesBase64.Length == 1)
                    {
                        var cert = new X509Certificate2(Convert.FromBase64String(connectionString.Authentication.Certificate.CertificatesBase64.First()));
                        settings.ClientCertificate(cert);
                    }
                    else
                    {
                        var certificates = new X509CertificateCollection();

                        foreach (var certificateBase64 in connectionString.Authentication.Certificate.CertificatesBase64)
                        {
                            certificates.Add(new X509Certificate2(Convert.FromBase64String(certificateBase64)));
                        }

                        settings.ClientCertificates(certificates);
                    }
                }
            }

            ElasticClient client = new(settings);

            return(client);
        }
        public static ElasticSearchConnectionString Parse(string connectionString)
        {
            // "Server=127.0.0.1;Port=9200;User=admin;Password=123456;"

            var str = new ElasticSearchConnectionString();

            str.Server    = "127.0.0.1";
            str.Port      = 9200;
            str.FetchSize = 1000;

            var spl = connectionString.Split(';');

            foreach (var item in spl)
            {
                var pos = item.IndexOf("=");
                if (pos == -1)
                {
                    continue;
                }

                var id    = item.Substring(0, pos).Trim().ToLower();
                var value = item.Substring(pos + 1).Trim();

                switch (id)
                {
                case "server": str.Server = value; break;

                case "port": str.Port = int.Parse(value); break;

                case "user": str.User = value; break;

                case "password": str.Password = value; break;

                case "fetchsize": str.FetchSize = int.Parse(value); break;

                default: break;
                }
            }

            return(str);
        }
Esempio n. 4
0
        public void CanGetConnectionStringByName()
        {
            using (var store = GetDocumentStore())
            {
                var ravenConnectionStr = new RavenConnectionString()
                {
                    Name = "RavenConnectionString",
                    TopologyDiscoveryUrls = new[] { "http://127.0.0.1:8080" },
                    Database = "Northwind",
                };
                var sqlConnectionStr = new SqlConnectionString
                {
                    Name             = "SqlConnectionString",
                    ConnectionString = MssqlConnectionString.Instance.VerifiedConnectionString.Value + $";Initial Catalog={store.Database}"
                };
                var elasticConnectionStr = new ElasticSearchConnectionString
                {
                    Name  = "ElasticConnectionString",
                    Nodes = new[] { "http://127.0.0.1:8080" },
                };

                var result1 = store.Maintenance.Send(new PutConnectionStringOperation <RavenConnectionString>(ravenConnectionStr));
                Assert.NotNull(result1.RaftCommandIndex);
                var result2 = store.Maintenance.Send(new PutConnectionStringOperation <SqlConnectionString>(sqlConnectionStr));
                Assert.NotNull(result2.RaftCommandIndex);
                var result3 = store.Maintenance.Send(new PutConnectionStringOperation <ElasticSearchConnectionString>(elasticConnectionStr));
                Assert.NotNull(result3.RaftCommandIndex);

                var resultSql = store.Maintenance.Send(new GetConnectionStringsOperation(connectionStringName: sqlConnectionStr.Name, type: sqlConnectionStr.Type));
                Assert.True(resultSql.SqlConnectionStrings.Count > 0);
                Assert.True(resultSql.RavenConnectionStrings.Count == 0);
                Assert.True(resultSql.ElasticSearchConnectionStrings.Count == 0);

                var resultElastic = store.Maintenance.Send(new GetConnectionStringsOperation(connectionStringName: elasticConnectionStr.Name, type: elasticConnectionStr.Type));
                Assert.True(resultElastic.SqlConnectionStrings.Count == 0);
                Assert.True(resultElastic.RavenConnectionStrings.Count == 0);
                Assert.True(resultElastic.ElasticSearchConnectionStrings.Count > 0);
            }
        }
Esempio n. 5
0
        public void CanUpdateConnectionStrings()
        {
            using (var store = GetDocumentStore())
            {
                var ravenConnectionString = new RavenConnectionString()
                {
                    Name = "RavenConnectionString",
                    TopologyDiscoveryUrls = new[] { "http://127.0.0.1:8080" },
                    Database = "Northwind",
                };
                var result1 = store.Maintenance.Send(new PutConnectionStringOperation <RavenConnectionString>(ravenConnectionString));
                Assert.NotNull(result1.RaftCommandIndex);

                var sqlConnectionString = new SqlConnectionString
                {
                    Name             = "SqlConnectionString",
                    ConnectionString = MssqlConnectionString.Instance.VerifiedConnectionString.Value + $";Initial Catalog={store.Database}",
                };

                var result2 = store.Maintenance.Send(new PutConnectionStringOperation <SqlConnectionString>(sqlConnectionString));
                Assert.NotNull(result2.RaftCommandIndex);

                var elasticSearchConnectionString = new ElasticSearchConnectionString
                {
                    Name  = "ElasticConnectionString",
                    Nodes = new[] { "http://127.0.0.1:8080" },
                };

                var result3 = store.Maintenance.Send(new PutConnectionStringOperation <ElasticSearchConnectionString>(elasticSearchConnectionString));
                Assert.NotNull(result3.RaftCommandIndex);

                //update url
                ravenConnectionString.TopologyDiscoveryUrls = new[] { "http://127.0.0.1:8081" };
                var result4 = store.Maintenance.Send(new PutConnectionStringOperation <RavenConnectionString>(ravenConnectionString));
                Assert.NotNull(result4.RaftCommandIndex);

                //update name : need to remove the old entry
                var result5 = store.Maintenance.Send(new RemoveConnectionStringOperation <SqlConnectionString>(sqlConnectionString));
                Assert.NotNull(result5.RaftCommandIndex);
                sqlConnectionString.Name = "New-Name";
                var result6 = store.Maintenance.Send(new PutConnectionStringOperation <SqlConnectionString>(sqlConnectionString));
                Assert.NotNull(result6.RaftCommandIndex);

                //update url
                elasticSearchConnectionString.Nodes = new[] { "http://127.0.0.1:8081" };
                var result7 = store.Maintenance.Send(new PutConnectionStringOperation <ElasticSearchConnectionString>(elasticSearchConnectionString));
                Assert.NotNull(result7.RaftCommandIndex);

                DatabaseRecord record;
                using (Server.ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
                    using (context.OpenReadTransaction())
                    {
                        record = Server.ServerStore.Cluster.ReadDatabase(context, store.Database);
                    }

                Assert.True(record.RavenConnectionStrings.ContainsKey("RavenConnectionString"));
                Assert.Equal("http://127.0.0.1:8081", record.RavenConnectionStrings["RavenConnectionString"].TopologyDiscoveryUrls.First());

                Assert.True(record.ElasticSearchConnectionStrings.ContainsKey("ElasticConnectionString"));
                Assert.Equal("http://127.0.0.1:8081", record.ElasticSearchConnectionStrings["ElasticConnectionString"].Nodes.First());

                Assert.False(record.SqlConnectionStrings.ContainsKey("SqlConnectionString"));
                Assert.True(record.SqlConnectionStrings.ContainsKey("New-Name"));
                Assert.Equal(sqlConnectionString.ConnectionString, record.SqlConnectionStrings["New-Name"].ConnectionString);
            }
        }