Skip to content

Commit 0752280

Browse files
authored
PYTHON-2480: Add MongoClient helper to access the current TopologyDescription (#583)
1 parent 94f4de1 commit 0752280

File tree

7 files changed

+32
-10
lines changed

7 files changed

+32
-10
lines changed

doc/api/pymongo/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Sub-modules:
4848
read_preferences
4949
results
5050
server_api
51+
server_description
52+
topology_description
5153
uri_parser
5254
write_concern
5355
event_loggers

doc/api/pymongo/mongo_client.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
Raises :class:`~pymongo.errors.InvalidName` if an invalid database name is used.
1616

1717
.. autoattribute:: event_listeners
18+
.. autoattribute:: topology_description
1819
.. autoattribute:: address
1920
.. autoattribute:: primary
2021
.. autoattribute:: secondaries

doc/api/pymongo/server_description.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,4 @@
66
.. automodule:: pymongo.server_description
77

88
.. autoclass:: pymongo.server_description.ServerDescription()
9-
10-
.. autoattribute:: address
11-
.. autoattribute:: all_hosts
12-
.. autoattribute:: server_type
13-
.. autoattribute:: server_type_name
9+
:members:

doc/api/pymongo/topology_description.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,5 @@
66
.. automodule:: pymongo.topology_description
77

88
.. autoclass:: pymongo.topology_description.TopologyDescription()
9+
:members:
910

10-
.. automethod:: has_readable_server(read_preference=ReadPreference.PRIMARY)
11-
.. automethod:: has_writable_server
12-
.. automethod:: server_descriptions
13-
.. autoattribute:: topology_type
14-
.. autoattribute:: topology_type_name

doc/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ changes. For example, all APIs deprecated in PyMongo 3.X have been removed.
1111
Be sure to read the changes listed below and the :doc:`migrate-to-pymongo4`
1212
before upgrading from PyMongo 3.x.
1313

14+
- Added :attr:`pymongo.mongo_client.MongoClient.topology_description`.
15+
1416
Breaking Changes in 4.0
1517
.......................
1618

pymongo/mongo_client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,28 @@ def event_listeners(self):
891891
"""
892892
return self._event_listeners.event_listeners
893893

894+
@property
895+
def topology_description(self):
896+
"""The description of the connected MongoDB deployment.
897+
898+
>>> client.topology_description
899+
<TopologyDescription id: 605a7b04e76489833a7c6113, topology_type: ReplicaSetWithPrimary, servers: [<ServerDescription ('localhost', 27017) server_type: RSPrimary, rtt: 0.0007973677999995488>, <ServerDescription ('localhost', 27018) server_type: RSSecondary, rtt: 0.0005540556000003249>, <ServerDescription ('localhost', 27019) server_type: RSSecondary, rtt: 0.0010367483999999649>]>
900+
>>> client.topology_description.topology_type_name
901+
'ReplicaSetWithPrimary'
902+
903+
Note that the description is periodically updated in the background
904+
but the returned object itself is immutable. Access this property again
905+
to get a more recent
906+
:class:`~pymongo.topology_description.TopologyDescription`.
907+
908+
:Returns:
909+
An instance of
910+
:class:`~pymongo.topology_description.TopologyDescription`.
911+
912+
.. versionadded:: 4.0
913+
"""
914+
return self._topology.description
915+
894916
@property
895917
def address(self):
896918
"""(host, port) of the current standalone, primary, or mongos, or None.

test/test_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
from pymongo.server_type import SERVER_TYPE
6363
from pymongo.settings import TOPOLOGY_TYPE
6464
from pymongo.topology import _ErrorContext
65+
from pymongo.topology_description import TopologyDescription
6566
from pymongo.srv_resolver import _HAVE_DNSPYTHON
6667
from pymongo.write_concern import WriteConcern
6768
from test import (client_context,
@@ -597,6 +598,8 @@ def test_init_disconnected(self):
597598
self.assertFalse(c.secondaries)
598599
c = rs_or_single_client(connect=False)
599600
self.assertIsInstance(c.max_write_batch_size, int)
601+
self.assertIsInstance(c.topology_description, TopologyDescription)
602+
self.assertEqual(c.topology_description, c._topology._description)
600603

601604
if client_context.is_rs:
602605
# The primary's host and port are from the replica set config.

0 commit comments

Comments
 (0)