Skip to content

Commit 9128e16

Browse files
committed
Add API to get the host metadata associated with the control connection node
1 parent 6700d3a commit 9128e16

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

cassandra/cluster.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,14 @@ def _target_type_from_refresh_args(keyspace, table, usertype, function, aggregat
13601360
return SchemaTargetType.KEYSPACE
13611361
return None
13621362

1363+
def get_control_connection_host(self):
1364+
"""
1365+
Returns the control connection host metadata.
1366+
"""
1367+
connection = getattr(self.control_connection, '_connection')
1368+
host = connection.host if connection else None
1369+
return self.metadata.get_host(host) if host else None
1370+
13631371
def refresh_schema_metadata(self, max_schema_agreement_wait=None):
13641372
"""
13651373
Synchronously refresh all schema metadata.

docs/api/cassandra/cluster.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@
8989

9090
.. automethod:: set_max_connections_per_host
9191

92+
.. automethod:: get_control_connection_host
93+
9294
.. automethod:: refresh_schema_metadata
9395

9496
.. automethod:: refresh_keyspace_metadata

tests/integration/standard/test_control_connection.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def setUp(self):
3939
"Native protocol 3,0+ is required for UDTs using %r"
4040
% (PROTOCOL_VERSION,))
4141
self.cluster = Cluster(protocol_version=PROTOCOL_VERSION)
42-
self.session = self.cluster.connect()
4342

4443
def tearDown(self):
4544
try:
@@ -65,6 +64,7 @@ def test_drop_keyspace(self):
6564
@test_category connection
6665
"""
6766

67+
self.session = self.cluster.connect()
6868
self.session.execute("""
6969
CREATE KEYSPACE keyspacetodrop
7070
WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1' }
@@ -76,3 +76,18 @@ def test_drop_keyspace(self):
7676
self.session.execute("DROP KEYSPACE keyspacetodrop")
7777
cc_id_post_drop = id(self.cluster.control_connection._connection)
7878
self.assertEqual(cc_id_post_drop, cc_id_pre_drop)
79+
80+
def test_get_control_connection_host(self):
81+
"""
82+
Test to validate Cluster.get_control_connection_host() metadata
83+
"""
84+
85+
host = self.cluster.get_control_connection_host()
86+
self.assertEqual(host, None)
87+
88+
self.session = self.cluster.connect()
89+
cc_host = self.cluster.control_connection._connection.host
90+
91+
host = self.cluster.get_control_connection_host()
92+
self.assertEqual(host.address, cc_host)
93+
self.assertEqual(host.is_up, True)

0 commit comments

Comments
 (0)