Skip to content

Commit 2f6b3d2

Browse files
authored
[Datalake] Fix connection string parsing (Azure#24779)
* Fix dfs conn str parsing * Added unittest * Some more tests
1 parent dcf2803 commit 2f6b3d2

File tree

6 files changed

+64
-6
lines changed

6 files changed

+64
-6
lines changed

sdk/storage/azure-storage-blob/azure/storage/blob/_shared/base_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ def parse_connection_str(conn_str, credential, service):
402402
raise ValueError("Connection string missing required connection details.")
403403
if service == "dfs":
404404
primary = primary.replace(".blob.", ".dfs.")
405-
secondary = secondary.replace(".blob.", ".dfs.")
405+
if secondary:
406+
secondary = secondary.replace(".blob.", ".dfs.")
406407
return primary, secondary, credential
407408

408409

sdk/storage/azure-storage-file-datalake/azure/storage/filedatalake/_shared/base_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ def parse_connection_str(conn_str, credential, service):
401401
raise ValueError("Connection string missing required connection details.")
402402
if service == "dfs":
403403
primary = primary.replace(".blob.", ".dfs.")
404-
secondary = secondary.replace(".blob.", ".dfs.")
404+
if secondary:
405+
secondary = secondary.replace(".blob.", ".dfs.")
405406
return primary, secondary, credential
406407

407408

sdk/storage/azure-storage-file-datalake/tests/test_datalake_service_client.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
from azure.core.exceptions import HttpResponseError
1111

1212

13-
from azure.storage.filedatalake import DataLakeServiceClient
13+
from azure.storage.filedatalake import (
14+
DataLakeServiceClient,
15+
FileSystemClient,
16+
DataLakeFileClient,
17+
DataLakeDirectoryClient
18+
)
1419

1520
from settings.testcase import DataLakePreparer
1621
from devtools_testutils.storage import StorageTestCase
@@ -321,3 +326,25 @@ def test_set_cors(self, datalake_storage_account_name, datalake_storage_account_
321326
# Assert
322327
received_props = self.dsc.get_service_properties()
323328
self._assert_cors_equal(received_props['cors'], cors)
329+
330+
def test_connectionstring_without_secondary(self):
331+
test_conn_str = "DefaultEndpointsProtocol=https;AccountName=foo;AccountKey=bar"
332+
client = DataLakeServiceClient.from_connection_string(test_conn_str)
333+
assert client.url == 'https://foo.dfs.core.windows.net/'
334+
assert client.primary_hostname == 'foo.dfs.core.windows.net'
335+
assert not client.secondary_hostname
336+
337+
client = FileSystemClient.from_connection_string(test_conn_str, "fsname")
338+
assert client.url == 'https://foo.dfs.core.windows.net/fsname'
339+
assert client.primary_hostname == 'foo.dfs.core.windows.net'
340+
assert not client.secondary_hostname
341+
342+
client = DataLakeFileClient.from_connection_string(test_conn_str, "fsname", "fpath")
343+
assert client.url == 'https://foo.dfs.core.windows.net/fsname/fpath'
344+
assert client.primary_hostname == 'foo.dfs.core.windows.net'
345+
assert not client.secondary_hostname
346+
347+
client = DataLakeDirectoryClient.from_connection_string(test_conn_str, "fsname", "dname")
348+
assert client.url == 'https://foo.dfs.core.windows.net/fsname/dname'
349+
assert client.primary_hostname == 'foo.dfs.core.windows.net'
350+
assert not client.secondary_hostname

sdk/storage/azure-storage-file-datalake/tests/test_datalake_service_client_async.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
from azure.core.exceptions import HttpResponseError
1212

1313

14-
from azure.storage.filedatalake.aio import DataLakeServiceClient
14+
from azure.storage.filedatalake.aio import (
15+
DataLakeServiceClient,
16+
FileSystemClient,
17+
DataLakeFileClient,
18+
DataLakeDirectoryClient
19+
)
1520
from devtools_testutils.storage.aio import AsyncStorageTestCase as StorageTestCase
1621
from settings.testcase import DataLakePreparer
1722

@@ -316,3 +321,25 @@ async def test_set_cors(self, datalake_storage_account_name, datalake_storage_ac
316321
# Assert
317322
received_props = await self.dsc.get_service_properties()
318323
self._assert_cors_equal(received_props['cors'], cors)
324+
325+
async def test_connectionstring_without_secondary(self):
326+
test_conn_str = "DefaultEndpointsProtocol=https;AccountName=foo;AccountKey=bar"
327+
client = DataLakeServiceClient.from_connection_string(test_conn_str)
328+
assert client.url == 'https://foo.dfs.core.windows.net/'
329+
assert client.primary_hostname == 'foo.dfs.core.windows.net'
330+
assert not client.secondary_hostname
331+
332+
client = FileSystemClient.from_connection_string(test_conn_str, "fsname")
333+
assert client.url == 'https://foo.dfs.core.windows.net/fsname'
334+
assert client.primary_hostname == 'foo.dfs.core.windows.net'
335+
assert not client.secondary_hostname
336+
337+
client = DataLakeFileClient.from_connection_string(test_conn_str, "fsname", "fpath")
338+
assert client.url == 'https://foo.dfs.core.windows.net/fsname/fpath'
339+
assert client.primary_hostname == 'foo.dfs.core.windows.net'
340+
assert not client.secondary_hostname
341+
342+
client = DataLakeDirectoryClient.from_connection_string(test_conn_str, "fsname", "dname")
343+
assert client.url == 'https://foo.dfs.core.windows.net/fsname/dname'
344+
assert client.primary_hostname == 'foo.dfs.core.windows.net'
345+
assert not client.secondary_hostname

sdk/storage/azure-storage-file-share/azure/storage/fileshare/_shared/base_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ def parse_connection_str(conn_str, credential, service):
401401
raise ValueError("Connection string missing required connection details.")
402402
if service == "dfs":
403403
primary = primary.replace(".blob.", ".dfs.")
404-
secondary = secondary.replace(".blob.", ".dfs.")
404+
if secondary:
405+
secondary = secondary.replace(".blob.", ".dfs.")
405406
return primary, secondary, credential
406407

407408

sdk/storage/azure-storage-queue/azure/storage/queue/_shared/base_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ def parse_connection_str(conn_str, credential, service):
401401
raise ValueError("Connection string missing required connection details.")
402402
if service == "dfs":
403403
primary = primary.replace(".blob.", ".dfs.")
404-
secondary = secondary.replace(".blob.", ".dfs.")
404+
if secondary:
405+
secondary = secondary.replace(".blob.", ".dfs.")
405406
return primary, secondary, credential
406407

407408

0 commit comments

Comments
 (0)