public void Test_GetConfig_Non_Default_Bucket()
        {
            var saslMechanism = new PlainTextMechanism(_ioService, "authenticated", "secret", new DefaultTranscoder());
            _ioService = new PooledIOService(_connectionPool, saslMechanism);

            var response = _ioService.Execute(new Config(new DefaultTranscoder(), OperationLifespan, _endPoint));

            Assert.IsTrue(response.Success);
            Assert.IsNotNull(response.Value);
            Assert.AreEqual("authenticated", response.Value.Name);
            Console.WriteLine(response.Value.ToString());
        }
        public override IConfigInfo GetConfig(string bucketName, string password)
        {
            Log.Debug(m => m("Getting config for bucket {0}", bucketName));
            var bucketConfiguration = GetOrCreateConfiguration(bucketName);

            //if the client is using a password make sure the client configuration references it
            password = string.IsNullOrEmpty(password) ? bucketConfiguration.Password : password;
            if (string.IsNullOrEmpty(bucketConfiguration.Password))
            {
                bucketConfiguration.Password = password;
            }

            var exceptions = new List <Exception>();
            CouchbaseConfigContext configInfo = null;

            foreach (var server in bucketConfiguration.Servers.Shuffle())
            {
                var port     = bucketConfiguration.UseSsl ? BucketConfiguration.SslPort : bucketConfiguration.Port;
                var endPoint = server.GetIPEndPoint(port);
                Log.Debug(m => m("Bootstrapping with {0}", endPoint));

                IIOService ioService = null;
                try
                {
                    var poolConfig     = bucketConfiguration.PoolConfiguration.Clone(server);
                    var connectionPool = ConnectionPoolFactory(poolConfig, endPoint);

                    ioService = IOServiceFactory(connectionPool);
                    var saslMechanism = SaslFactory(bucketName, password, ioService, Transcoder);
                    ioService.SaslMechanism = saslMechanism;

                    var operationResult = ioService.Execute(
                        new Config(Transcoder, ClientConfig.DefaultOperationLifespan, endPoint));

                    if (operationResult.Success)
                    {
                        var bucketConfig = operationResult.Value;
                        bucketConfig.SurrogateHost = connectionPool.EndPoint.Address.ToString();
                        bucketConfig.Password      = password;
                        configInfo = new CouchbaseConfigContext(bucketConfig,
                                                                ClientConfig,
                                                                IOServiceFactory,
                                                                ConnectionPoolFactory,
                                                                SaslFactory,
                                                                Transcoder);

                        Log.Info(m => m("Bootstrap config: {0}", JsonConvert.SerializeObject(bucketConfig)));

                        configInfo.LoadConfig(ioService);
                        Configs[bucketName] = configInfo;
                        break;
                    }

                    var exception = operationResult.Exception;
                    if (exception != null)
                    {
                        exceptions.Add(exception);
                    }

                    //CCCP only supported for Couchbase Buckets
                    if (operationResult.Status == ResponseStatus.UnknownCommand)
                    {
                        throw new ConfigException("{0} is this a Memcached bucket?", operationResult.Value);
                    }
                    Log.Warn(m => m("Could not retrieve configuration for {0}. Reason: {1}",
                                    bucketName,
                                    operationResult.Message));
                }
                catch (ConfigException)
                {
                    ioService.Dispose();
                    Log.Debug(m => m("Bootstrapping with {0} failed.", endPoint));
                    throw;
                }
                catch (AuthenticationException e)
                {
                    const string msg =
                        "A failure to authenticate may mean that the server has not joined the cluster" +
                        " yet or that the Bucket does not exist. Please check that {0} has joined that" +
                        " cluster and that the Bucket '{1}' exists.";

                    Log.Warn(m => m(msg, endPoint, bucketName));
                    Log.Warn(e);
                    exceptions.Add(e);
                }
                catch (Exception e)
                {
                    Log.Debug(m => m("Bootstrapping with {0} failed.", endPoint));
                    Log.Warn(e);
                    exceptions.Add(e);
                }
                finally
                {
                    if (ioService != null && configInfo == null)
                    {
                        ioService.Dispose();
                    }
                }
            }

            //Client cannot bootstrap with this provider
            if (configInfo == null)
            {
                throw new AggregateException(exceptions);
            }

            return(configInfo);
        }
        public override IConfigInfo GetConfig(string bucketName, string username, string password)
        {
            Log.Debug("Getting config for bucket {0}", bucketName);
            var bucketConfiguration = GetOrCreateConfiguration(bucketName);

            //if the client is using a password make sure the client configuration references it
            password = string.IsNullOrEmpty(password) ? bucketConfiguration.Password : password;
            if (string.IsNullOrEmpty(bucketConfiguration.Password))
            {
                bucketConfiguration.Password = password;
            }

            var exceptions = new List <Exception>();
            CouchbaseConfigContext configInfo = null;

            foreach (var server in bucketConfiguration.Servers.Shuffle())
            {
                var port     = bucketConfiguration.UseSsl ? BucketConfiguration.SslPort : bucketConfiguration.Port;
                var endPoint = server.GetIPEndPoint(port);
                Log.Debug("Bootstrapping with {0}", endPoint);

                IIOService ioService = null;
                try
                {
                    var poolConfig = bucketConfiguration.ClonePoolConfiguration(server);

                    var connectionPool = ConnectionPoolFactory(poolConfig, endPoint);
                    var saslMechanism  = SaslFactory(username, password, connectionPool, Transcoder);
                    connectionPool.SaslMechanism = saslMechanism;

                    // setup IO service, this does SASL negotiation & hello
                    ioService = IOServiceFactory(connectionPool);

                    // finish initialising connection pool ready to be used
                    connectionPool.Initialize();

                    var operation = new Config(Transcoder, ClientConfig.DefaultOperationLifespan, endPoint);

                    IOperationResult <BucketConfig> operationResult;
                    using (poolConfig.ClientConfiguration.Tracer.StartParentScope(operation, addIgnoreTag: true, ignoreActiveSpan: true))
                    {
                        operationResult = ioService.Execute(operation);
                    }

                    if (operationResult.Success)
                    {
                        var bucketConfig = operationResult.Value;
                        if (string.IsNullOrWhiteSpace(bucketConfig.BucketType) && bucketConfig.BucketCapabilities != null)
                        {
                            bucketConfig.BucketType = (bucketConfig.BucketCapabilities.Contains("couchapi", StringComparer.OrdinalIgnoreCase)
                                ? BucketTypeEnum.Couchbase
                                : BucketTypeEnum.Ephemeral).ToString().ToLowerInvariant();
                        }
                        bucketConfig.SurrogateHost = connectionPool.EndPoint.Address.ToString();
                        bucketConfig.Password      = password;
                        if (ClientConfig.UseSsl)
                        {
                            foreach (var ipEndPoint in bucketConfig.VBucketServerMap.IPEndPoints)
                            {
                                ipEndPoint.Port = ClientConfig.SslPort;
                            }
                        }

                        // Set network type for bucket configuration
                        bucketConfig.NetworkType = bucketConfiguration.NetworkType;

                        configInfo = new CouchbaseConfigContext(bucketConfig,
                                                                ClientConfig,
                                                                IOServiceFactory,
                                                                ConnectionPoolFactory,
                                                                SaslFactory,
                                                                Transcoder,
                                                                username,
                                                                password);

                        Log.Info("Bootstrap config: {0}", JsonConvert.SerializeObject(bucketConfig));

                        configInfo.LoadConfig(ioService);
                        Configs[bucketName] = configInfo;
                        LogServers(configInfo);
                        break;
                    }

                    var exception = operationResult.Exception;
                    if (exception != null)
                    {
                        exceptions.Add(exception);
                    }

                    //CCCP only supported for Couchbase Buckets
                    if (operationResult.Status == ResponseStatus.UnknownCommand)
                    {
                        const string message = "Config operation returned UnknownCommand.";
                        Log.Info(message);
                        exceptions.Add(new ConfigException(message));
                        break;
                    }

                    Log.Warn("Could not retrieve configuration for {0}. Reason: {1}",
                             bucketName,
                             operationResult.Message);
                }
                catch (AuthenticationException e)
                {
                    const string msg =
                        "A failure to authenticate may mean that the server has not joined the cluster" +
                        " yet or that the Bucket does not exist. Please check that {0} has joined that" +
                        " cluster and that the Bucket '{1}' exists. If using LDAP, please set" +
                        " forceSaslPlain to true.";

                    Log.Warn(msg, endPoint, bucketName);
                    Log.Warn(e);
                    exceptions.Add(e);
                }
                catch (Exception e)
                {
                    Log.Debug("Bootstrapping with {0} failed.", endPoint);
                    Log.Warn(e);
                    exceptions.Add(e);
                }
                finally
                {
                    if (ioService != null && configInfo == null)
                    {
                        ioService.Dispose();
                    }
                }
            }

            //Client cannot bootstrap with this provider
            if (configInfo == null)
            {
                throw new AggregateException("Could not bootstrap with CCCP.", exceptions);
            }

            return(configInfo);
        }