-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-2823 Allow custom service names with srvServiceName URI option #749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4f4ce7f
9a89ed4
ef4db92
d88f1eb
fcaf49b
3e63bd5
029f222
9fdfb38
944786e
90466a4
1b48a20
459f5e7
be53d49
4a8b1d0
94e44c2
9c93a7a
ce2f48b
ad366dd
241e105
f415705
70dcb1f
c148364
57c19be
d3b210f
ec58a75
2cbd7ce
891446b
f54b120
e5c7e9d
b9dc33a
8a31961
e593759
edec74e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
from urllib.parse import unquote_plus | ||
|
||
from pymongo.common import ( | ||
SRV_SERVICE_NAME, | ||
get_validated_options, INTERNAL_URI_OPTION_NAME_MAP, | ||
URI_OPTIONS_DEPRECATION_MAP, _CaseInsensitiveDictionary) | ||
from pymongo.errors import ConfigurationError, InvalidURI | ||
|
@@ -373,7 +374,7 @@ def _check_options(nodes, options): | |
|
||
|
||
def parse_uri(uri, default_port=DEFAULT_PORT, validate=True, warn=False, | ||
normalize=True, connect_timeout=None): | ||
normalize=True, connect_timeout=None, srv_service_name=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update the docstring below to reflect the new srv_service_name param. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
"""Parse and validate a MongoDB URI. | ||
|
||
Returns a dict of the form:: | ||
|
@@ -405,6 +406,7 @@ def parse_uri(uri, default_port=DEFAULT_PORT, validate=True, warn=False, | |
to their internally-used names. Default: ``True``. | ||
- `connect_timeout` (optional): The maximum time in milliseconds to | ||
wait for a response from the DNS server. | ||
- 'srv_service_name` (optional): A custom SRV service name | ||
|
||
.. versionchanged:: 3.9 | ||
Added the ``normalize`` parameter. | ||
|
@@ -468,6 +470,9 @@ def parse_uri(uri, default_port=DEFAULT_PORT, validate=True, warn=False, | |
if opts: | ||
options.update(split_options(opts, validate, warn, normalize)) | ||
|
||
if srv_service_name is None: | ||
srv_service_name = options.get("srvServiceName", SRV_SERVICE_NAME) | ||
|
||
if '@' in host_part: | ||
userinfo, _, hosts = host_part.rpartition('@') | ||
user, passwd = parse_userinfo(userinfo) | ||
|
@@ -499,7 +504,7 @@ def parse_uri(uri, default_port=DEFAULT_PORT, validate=True, warn=False, | |
# Use the connection timeout. connectTimeoutMS passed as a keyword | ||
# argument overrides the same option passed in the connection string. | ||
connect_timeout = connect_timeout or options.get("connectTimeoutMS") | ||
dns_resolver = _SrvResolver(fqdn, connect_timeout=connect_timeout) | ||
dns_resolver = _SrvResolver(fqdn, connect_timeout, srv_service_name) | ||
nodes = dns_resolver.get_hosts() | ||
dns_options = dns_resolver.get_options() | ||
if dns_options: | ||
|
@@ -514,6 +519,9 @@ def parse_uri(uri, default_port=DEFAULT_PORT, validate=True, warn=False, | |
options[opt] = val | ||
if "tls" not in options and "ssl" not in options: | ||
options["tls"] = True if validate else 'true' | ||
elif not is_srv and options.get("srvServiceName") is not None: | ||
raise ConfigurationError("The srvServiceName option is only allowed " | ||
"with 'mongodb+srv://' URIs") | ||
else: | ||
nodes = split_hosts(hosts, default_port=default_port) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"uri": "mongodb+srv://test4.test.build.10gen.cc/?loadBalanced=true", | ||
"seeds": [], | ||
"hosts": [], | ||
"error": true, | ||
"comment": "Should fail because no SRV records are present for this URI." | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"uri": "mongodb+srv://test22.test.build.10gen.cc/?srvServiceName=customname", | ||
"seeds": [ | ||
"localhost.test.build.10gen.cc:27017", | ||
"localhost.test.build.10gen.cc:27018" | ||
], | ||
"hosts": [ | ||
"localhost:27017", | ||
"localhost:27018", | ||
"localhost:27019" | ||
], | ||
"options": { | ||
"ssl": true, | ||
"srvServiceName": "customname" | ||
ShaneHarvey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1591,6 +1591,27 @@ def test_network_error_message(self): | |
with self.assertRaisesRegex(AutoReconnect, expected): | ||
client.pymongo_test.test.find_one({}) | ||
|
||
@unittest.skipUnless( | ||
_HAVE_DNSPYTHON, "DNS-related tests need dnspython to be installed") | ||
def test_service_name_from_kwargs(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
client = MongoClient( | ||
'mongodb+srv://user:[email protected]', | ||
srvServiceName='customname', connect=False) | ||
self.assertEqual(client._topology_settings._srv_service_name, | ||
'customname') | ||
client = MongoClient( | ||
'mongodb+srv://user:[email protected]' | ||
'/?srvServiceName=shouldbeoverriden', | ||
srvServiceName='customname', connect=False) | ||
self.assertEqual(client._topology_settings._srv_service_name, | ||
'customname') | ||
client = MongoClient( | ||
'mongodb+srv://user:[email protected]' | ||
'/?srvServiceName=customname', | ||
connect=False) | ||
self.assertEqual(client._topology_settings._srv_service_name, | ||
'customname') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a test which actually checks that the kwarg overrides the URI. Like this: client = MongoClient(
'mongodb+srv://user:[email protected]'
'/?srvServiceName=shouldBeOverridden',
srvServiceName='customname',
connect=False)
self.assertEqual(client._topology_settings._srv_service_name,
'customname') There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
|
||
class TestExhaustCursor(IntegrationTest): | ||
"""Test that clients properly handle errors from exhaust cursors.""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"tests": [ | ||
{ | ||
"description": "SRV URI with custom srvServiceName", | ||
"uri": "mongodb+srv://test22.test.build.10gen.cc/?srvServiceName=customname", | ||
"valid": true, | ||
"warning": false, | ||
"hosts": null, | ||
"auth": null, | ||
"options": { | ||
"srvServiceName": "customname" | ||
ShaneHarvey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}, | ||
{ | ||
"description": "Non-SRV URI with custom srvServiceName", | ||
"uri": "mongodb://example.com/?srvServiceName=customname", | ||
"valid": false, | ||
"warning": true, | ||
"hosts": null, | ||
"auth": null, | ||
"options": {} | ||
} | ||
] | ||
} |
Uh oh!
There was an error while loading. Please reload this page.