public static IElasticClient GetOrAddClient <TConfig>(
            this IEphemeralCluster <TConfig> cluster,
            Func <ConnectionSettings, ConnectionSettings> createSettings = null,
            Func <ICollection <Uri>, IConnectionPool> createPool         = null)
            where TConfig : EphemeralClusterConfiguration
        {
            createSettings = createSettings ?? (s => s);
            return(cluster.GetOrAddClient(c =>
            {
                var host = (RunningFiddler) ? "ipv4.fiddler" : "localhost";
                createPool = createPool ?? (uris => new StaticConnectionPool(uris));
                var connectionPool = createPool(c.NodesUris(host));
                var connection = TestClient.Configuration.RunIntegrationTests ? TestClient.CreateLiveConnection() : new InMemoryConnection();
                var settings = TestClient.CreateSettings(createSettings, connection, connectionPool);

                var current = (IConnectionConfigurationValues)settings;
                var notAlreadyAuthenticated = current.BasicAuthenticationCredentials == null && current.ClientCertificates == null;
                var noCertValidation = current.ServerCertificateValidationCallback == null;

                if (cluster.ClusterConfiguration.EnableSecurity && notAlreadyAuthenticated)
                {
                    settings = settings.BasicAuthentication(ClusterAuthentication.Admin.Username, ClusterAuthentication.Admin.Password);
                }
                if (cluster.ClusterConfiguration.EnableSsl && noCertValidation)
                {
                    var ca = new X509Certificate2(cluster.ClusterConfiguration.FileSystem.CaCertificate);
                    settings = settings.ServerCertificateValidationCallback(CertificateValidations.AllowAll);
                }
                var client = new ElasticClient(settings);
                return client;
            }));
        }
Beispiel #2
0
        public void SeenByVisitor()
        {
            var visitor = new DslPrettyPrintVisitor(TestClient.CreateSettings());
            var query   = this.QueryFluent(new QueryContainerDescriptor <Project>());

            query.Accept(visitor);
            var pretty = visitor.PrettyPrint;

            pretty.Should().NotBeNullOrWhiteSpace();
        }
Beispiel #3
0
        [I] public void ThrowsExceptionOnConnectionError()
        {
            if (TestClient.RunningFiddler)
            {
                return;                                        //fiddler meddles here
            }
            var    client   = new ElasticClient(TestClient.CreateSettings(port: 9500));
            Action response = () => client.GetMany <Developer>(_ids.Select(i => i * 100));

            response.ShouldThrow <ElasticsearchClientException>();
        }
Beispiel #4
0
        public void ExpressionsAreCachedButSeeDifferentTypes()
        {
            var connectionSettings = TestClient.CreateSettings(forceInMemory: true);
            var client             = new ElasticClient(connectionSettings);

            var fieldNameOnA = client.Infer.Field(Field <A>(p => p.C.Name));
            var fieldNameOnB = client.Infer.Field(Field <B>(p => p.C.Name));

            /**
             * Here we have to similary shaped expressions on coming from A and on from B
             * that will resolve to the same field name, as expected
             */

            fieldNameOnA.Should().Be("c.name");
            fieldNameOnB.Should().Be("c.name");

            /**
             * now we create a new connection settings with a re-map for `C` on class `A` to `"d"`
             * now when we resolve the field path for property `C` on `A`, it will be different than
             * for property `C` on `B`
             */
            var newConnectionSettings = TestClient.CreateSettings(forceInMemory: true, modifySettings: s => s
                                                                  .InferMappingFor <A>(m => m
                                                                                       .Rename(p => p.C, "d")
                                                                                       )
                                                                  );
            var newClient = new ElasticClient(newConnectionSettings);

            fieldNameOnA = newClient.Infer.Field(Field <A>(p => p.C.Name));
            fieldNameOnB = newClient.Infer.Field(Field <B>(p => p.C.Name));

            fieldNameOnA.Should().Be("d.name");
            fieldNameOnB.Should().Be("c.name");

            /** however we didn't break inferrence on the first client instance using its separate connection settings */
            fieldNameOnA = client.Infer.Field(Field <A>(p => p.C.Name));
            fieldNameOnB = client.Infer.Field(Field <B>(p => p.C.Name));

            fieldNameOnA.Should().Be("c.name");
            fieldNameOnB.Should().Be("c.name");
        }
        public void RequestPipeline()
        {
            var settings = TestClient.CreateSettings();

            /** When calling Request(Async) on Transport the whole coordination of the request is deferred to a new instance in a `using` block. */
            var pipeline = new RequestPipeline(settings, DateTimeProvider.Default, new MemoryStreamFactory(), new SearchRequestParameters());

            pipeline.GetType().Should().Implement <IDisposable>();

            /** However the transport does not instantiate RequestPipeline directly, it uses a pluggable `IRequestPipelineFactory`*/
            var requestPipelineFactory = new RequestPipelineFactory();
            var requestPipeline        = requestPipelineFactory.Create(settings, DateTimeProvider.Default, new MemoryStreamFactory(), new SearchRequestParameters());

            requestPipeline.Should().BeOfType <RequestPipeline>();
            requestPipeline.GetType().Should().Implement <IDisposable>();

            /** which can be passed to the transport when instantiating a client */
            var transport = new Transport <ConnectionSettings>(settings, requestPipelineFactory, DateTimeProvider.Default, new MemoryStreamFactory());

            /** this allows you to have requests executed on your own custom request pipeline */
        }
        public void RequestPipeline()
        {
            var settings = TestClient.CreateSettings();

            /** When calling `Request()` or `RequestAsync()` on an `ITransport`,
             * the whole coordination of the request is deferred to a new instance in a `using` block.
             */
            var pipeline = new RequestPipeline(
                settings,
                DateTimeProvider.Default,
                new MemoryStreamFactory(),
                new SearchRequestParameters());

            pipeline.GetType().Should().Implement <IDisposable>();

            /** An `ITransport` does not instantiate a `RequestPipeline` directly; it uses a pluggable `IRequestPipelineFactory`
             * to create it
             */
            var requestPipelineFactory = new RequestPipelineFactory();
            var requestPipeline        = requestPipelineFactory.Create(
                settings,
                DateTimeProvider.Default,                 //<1> An <<date-time-providers,`IDateTimeProvider` implementation>>
                new MemoryStreamFactory(),
                new SearchRequestParameters());

            requestPipeline.Should().BeOfType <RequestPipeline>();
            requestPipeline.GetType().Should().Implement <IDisposable>();

            /** You can pass your own `IRequestPipeline` implementation to the Transport when instantiating a client,
             * allowing you to have requests executed on your own custom request pipeline
             */
            var transport = new Transport <ConnectionSettings>(
                settings,
                requestPipelineFactory,
                DateTimeProvider.Default,
                new MemoryStreamFactory());
        }