def __init__(self, username, password, host='localhost', port=8091, **kwargs): """Connect to a cluster :param string username: The administrative username for the cluster, this is typically ``Administrator`` :param string password: The administrative password for the cluster, this is the password you entered when Couchbase was installed :param string host: The hostname or IP of one of the nodes which is currently a member of the cluster (or a newly allocated node, if you wish to operate on that) :param int port: The management port for the node :raise: :exc:`couchbase.exceptions.AuthenticationException` if incorrect credentials were supplied :exc:`couchbase.exceptions.ConnectException` if there was a problem establishing a connection to the provided host :return: an instance of :class:`Admin` """ self.__is_6_5 = None connection_string = kwargs.pop('connection_string', None) if not connection_string: connection_string = ConnectionString( hosts=["{0}:{1}".format(host, port)], scheme=kwargs.get('scheme', 'http'), bucket=kwargs.get('bucket', None)) ipv6 = kwargs.pop('ipv6', None) if ipv6: connection_string.set_option('ipv6', ipv6) kwargs.update({ 'username': username, 'password': password, 'connection_string': str(connection_string), '_conntype': LCB.LCB_TYPE_CLUSTER }) super(Admin, self).__init__(**kwargs) self._connect()
def testMultiHost(self): info = self.cluster_info cs = ConnectionString.parse(self.make_connargs()['connection_string']) cs.hosts = [ info.host + ':' + '10', info.host + ':' + str(info.port) ] cb = self.make_connection(connection_string=cs.encode()) d = cb.on_connect() d.addCallback(lambda x: self.assertTrue(cb.connected)) return d
def _get_connstr_and_bucket_name( self, args, # type: List[Any] kwargs): connstr = args.pop(0) if args else kwargs.pop('connection_string') connstr_nobucket = ConnectionString.parse(connstr) bucket = connstr_nobucket.bucket connstr_nobucket.bucket = None return connstr_nobucket, bucket
def test_server_not_found(self): connargs = self.make_connargs() cs = ConnectionString.parse(connargs['connection_string']) cs.hosts = ['example.com'] connargs['connection_string'] = cs.encode() self.assertRaises(TimeoutException, self.factory, **connargs) cs.hosts = [self.cluster_info.host + ':' + str(34567)] self.assertRaises(TimeoutException, self.factory, **connargs)
def test_does_not_encode_slashes(self): connstr = ConnectionString.parse( 'couchbases://10.112.170.101?certpath=/var/rootcert.pem') self.assertTrue('certpath' in connstr.options) self.assertEqual('/var/rootcert.pem', connstr.options.get('certpath')[0]) encoded = connstr.encode() self.assertEqual( 'couchbases://10.112.170.101?certpath=/var/rootcert.pem', encoded)
def test_multi_hosts(self): passwd = self.cluster_info.bucket_password cs = ConnectionString(bucket=None, hosts=[self.cluster_info.host]) if not self.mock: cb = self.factory(str(cs), password=passwd) self.assertTrue(cb.upsert("foo", "bar").success) cs.hosts = [self.cluster_info.host + ':' + str(self.cluster_info.port)] cs.scheme = 'http' cluster = self._instantiate_cluster(cs) cb = cluster.bucket(self.cluster_info.bucket_name) self.assertTrue(cb.upsert("foo", "bar").success) cs.hosts.insert(0, 'localhost:1') cluster = self._instantiate_cluster(cs) cb = cluster.bucket(self.cluster_info.bucket_name) self.assertTrue(cb.upsert("foo", "bar").success)
def setUp(self, **kwargs): super(ClusterTestCase, self).setUp() connargs = self.cluster_info.make_connargs() connstr_abstract = ConnectionString.parse(connargs.pop('connection_string')) bucket_name = connstr_abstract.bucket connstr_abstract.bucket = None connstr_abstract.set_option('enable_collections', 'true') self.cluster = self.cluster_factory(connstr_abstract, ClusterOptions( ClassicAuthenticator(self.cluster_info.admin_username, self.cluster_info.admin_password))) # type: Cluster # self.admin = self.cluster.admin#self.make_admin_connection() self.bucket = self.cluster.bucket(bucket_name, **connargs) self.bucket_name = bucket_name
def _create_cluster_clean(self, authenticator): connargs = self.make_connargs() connstr = ConnectionString.parse(str( connargs.pop('connection_string'))) connstr.clear_option('username') bucket = connstr.bucket connstr.bucket = None password = connargs.get('password', None) keys_to_skip = authenticator.get_credentials(bucket)['options'].keys() for entry in keys_to_skip: connstr.clear_option(entry) cluster = Cluster(connstr, bucket_factory=self.factory) cluster.authenticate(ClassicAuthenticator(buckets={bucket: password})) return cluster, bucket
def _create_cluster(self): connargs = self.make_connargs() connstr = ConnectionString.parse(str( connargs.pop('connection_string'))) connstr.clear_option('username') bucket = connstr.bucket connstr.bucket = None password = connargs.get('password', '') # Can I open a new bucket via open_bucket? cluster = Cluster(connstr, bucket_factory=self.factory) cluster.authenticate( ClassicAuthenticator( buckets={bucket: password}, cluster_password=self.cluster_info.admin_password, cluster_username=self.cluster_info.admin_username)) return cluster, bucket
def test_actions(self): if not self.is_realserver: raise SkipTest('Real server must be used for admin tests') if not os.environ.get('PYCBC_TEST_ADMIN'): raise SkipTest('PYCBC_TEST_ADMIN must be set in the environment') try: # Remove the bucket, if it exists self.admin.bucket_remove('dummy') except CouchbaseError: pass # Need to explicitly enable admin tests.. # Create the bucket self.admin.bucket_create(name='dummy', ram_quota=100, bucket_password='******') self.admin.wait_ready('dummy', timeout=15.0) # All should be OK, ensure we can connect: connstr = ConnectionString.parse( self.make_connargs()['connection_string']) connstr.bucket = 'dummy' connstr = connstr.encode() self.factory(connstr, password='******') # OK, it exists self.assertRaises(CouchbaseError, self.factory, connstr) # Change the password self.admin.bucket_update('dummy', self.admin.bucket_info('dummy'), bucket_password='') self.factory(connstr) # No password # Remove the bucket self.admin.bucket_remove('dummy') self.assertRaises(CouchbaseError, self.factory, connstr)
def test_actions(self): try: # Remove the bucket, if it exists self.bm.drop_bucket('dummy') except CouchbaseError: pass # Need to explicitly enable admin tests.. # Create the bucket self.bm.create_bucket( CreateBucketSettings(name='dummy', ram_quota_mb=100, bucket_password='******')) self.bm._admin_bucket.wait_ready('dummy', timeout=15.0) # All should be OK, ensure we can connect: connstr = ConnectionString.parse( self.make_connargs()['connection_string']) dummy_bucket = 'dummy' connstr = connstr.encode() args = self.make_connargs() args.pop('connection_string', None) args['bucket'] = dummy_bucket self.factory(connstr, **args) # OK, it exists self.assertRaises(CouchbaseError, self.factory, connstr) # Change the password self.bm.update_bucket(BucketSettings(name=dummy_bucket, max_ttl=500)) self.bm._admin_bucket.wait_ready(dummy_bucket, 10) dummy = self.bm.get_bucket('dummy') self.assertEqual(500, dummy.max_ttl) # Remove the bucket self.bm.drop_bucket('dummy') self.assertRaises(CouchbaseError, self.factory, connstr)
def test_pathless_connstr(self): connstr = ConnectionString.parse( 'couchbase://localhost?opt1=val1&opt2=val2') self.assertTrue('opt1' in connstr.options) self.assertTrue('opt2' in connstr.options)
def test_pathless_connstr(self): # Not strictly a cluster test, but relevant connstr = ConnectionString.parse( 'couchbase://localhost?opt1=val1&opt2=val2') self.assertTrue('opt1' in connstr.options) self.assertTrue('opt2' in connstr.options)