diff --git a/sdk/tables/azure-data-tables/CHANGELOG.md b/sdk/tables/azure-data-tables/CHANGELOG.md index 0ab5c6b93f33..1759ecd5dd20 100644 --- a/sdk/tables/azure-data-tables/CHANGELOG.md +++ b/sdk/tables/azure-data-tables/CHANGELOG.md @@ -11,6 +11,7 @@ * Removed legacy Storage policies from pipeline. * Removed unused legacy client-side encryption attributes from client classes. * Fixed sharing of pipeline between service/table clients. +* Added support for Azurite storage emulator ## 12.0.0b6 (2021-04-06) * Updated deserialization of datetime fields in entities to support preservation of the service format with additional decimal place. diff --git a/sdk/tables/azure-data-tables/azure/data/tables/_base_client.py b/sdk/tables/azure-data-tables/azure/data/tables/_base_client.py index d1fc6fd5a909..b0dd23960755 100644 --- a/sdk/tables/azure-data-tables/azure/data/tables/_base_client.py +++ b/sdk/tables/azure-data-tables/azure/data/tables/_base_client.py @@ -96,13 +96,22 @@ def __init__( self._hosts = kwargs.get("_hosts") self.scheme = parsed_url.scheme self._cosmos_endpoint = _is_cosmos_endpoint(parsed_url.hostname) + if ".core." in parsed_url.netloc or ".cosmos." in parsed_url.netloc: + account = parsed_url.netloc.split(".table.core.") + if "cosmos" in parsed_url.netloc: + account = parsed_url.netloc.split(".table.cosmos.") + self.account_name = account[0] if len(account) > 1 else None + else: + path_account_name = parsed_url.path.split("/") + if len(path_account_name) > 1: + self.account_name = path_account_name[1] + account = [self.account_name, parsed_url.netloc] + else: + # If format doesn't fit Azurite, default to standard parsing + account = parsed_url.netloc.split(".table.core.") + self.account_name = account[0] if len(account) > 1 else None - account = parsed_url.netloc.split(".table.core.") - if "cosmos" in parsed_url.netloc: - account = parsed_url.netloc.split(".table.cosmos.") - self.account_name = account[0] if len(account) > 1 else None secondary_hostname = None - self.credential = format_shared_key_credential(account, credential) if self.scheme.lower() != "https" and hasattr(self.credential, "get_token"): raise ValueError("Token credential is only supported with HTTPS.") diff --git a/sdk/tables/azure-data-tables/tests/test_table.py b/sdk/tables/azure-data-tables/tests/test_table.py index a6873ad45384..f1df1f1721c8 100644 --- a/sdk/tables/azure-data-tables/tests/test_table.py +++ b/sdk/tables/azure-data-tables/tests/test_table.py @@ -472,10 +472,6 @@ def test_account_sas(self, tables_storage_account_name, tables_primary_storage_a service = self.create_client_from_credential(TableServiceClient, token, account_url=account_url) # Act - # service = TableServiceClient( - # self.account_url(tables_storage_account_name, "table"), - # credential=token, - # ) sas_table = service.get_table_client(table.table_name) entities = list(sas_table.list_entities()) @@ -529,3 +525,13 @@ def test_delete_table_invalid_name(self): assert "Table names must be alphanumeric, cannot begin with a number, and must be between 3-63 characters long.""" in str( excinfo) + + def test_azurite_url(self): + account_url = "https://127.0.0.1:10002/my_account" + tsc = TableServiceClient(account_url, credential=self.tables_primary_storage_account_key) + + assert tsc.account_name == "my_account" + assert tsc.url == "https://127.0.0.1:10002/my_account" + assert tsc.location_mode == "primary" + assert tsc.credential.account_key == self.tables_primary_storage_account_key + assert tsc.credential.account_name == "my_account" \ No newline at end of file diff --git a/sdk/tables/azure-data-tables/tests/test_table_async.py b/sdk/tables/azure-data-tables/tests/test_table_async.py index 285d4d479c0d..e8d2a93df773 100644 --- a/sdk/tables/azure-data-tables/tests/test_table_async.py +++ b/sdk/tables/azure-data-tables/tests/test_table_async.py @@ -438,3 +438,13 @@ async def test_delete_table_invalid_name(self): assert "Table names must be alphanumeric, cannot begin with a number, and must be between 3-63 characters long.""" in str( excinfo) + + def test_azurite_url(self): + account_url = "https://127.0.0.1:10002/my_account" + tsc = TableServiceClient(account_url, credential=self.tables_primary_storage_account_key) + + assert tsc.account_name == "my_account" + assert tsc.url == "https://127.0.0.1:10002/my_account" + assert tsc.location_mode == "primary" + assert tsc.credential.account_key == self.tables_primary_storage_account_key + assert tsc.credential.account_name == "my_account" diff --git a/sdk/tables/azure-data-tables/tests/test_table_client.py b/sdk/tables/azure-data-tables/tests/test_table_client.py index 67eaf3c6791a..af993338e060 100644 --- a/sdk/tables/azure-data-tables/tests/test_table_client.py +++ b/sdk/tables/azure-data-tables/tests/test_table_client.py @@ -399,20 +399,20 @@ def test_create_service_with_custom_account_endpoint_path(self): assert service._primary_hostname == 'local-machine:11002/custom/account/path' service = TableServiceClient(account_url=custom_account_url) - assert service.account_name == None + assert service.account_name == "custom" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' assert service.url.startswith('http://local-machine:11002/custom/account/path') service = TableClient(account_url=custom_account_url, table_name="foo") - assert service.account_name == None + assert service.account_name == "custom" assert service.table_name == "foo" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' assert service.url.startswith('http://local-machine:11002/custom/account/path') service = TableClient.from_table_url("http://local-machine:11002/custom/account/path/foo" + token) - assert service.account_name == None + assert service.account_name == "custom" assert service.table_name == "foo" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' diff --git a/sdk/tables/azure-data-tables/tests/test_table_client_async.py b/sdk/tables/azure-data-tables/tests/test_table_client_async.py index 166c4a0ab217..f989d2df8a27 100644 --- a/sdk/tables/azure-data-tables/tests/test_table_client_async.py +++ b/sdk/tables/azure-data-tables/tests/test_table_client_async.py @@ -428,20 +428,20 @@ async def test_create_service_with_custom_account_endpoint_path_async(self): assert service._primary_hostname == 'local-machine:11002/custom/account/path' service = TableServiceClient(account_url=custom_account_url) - assert service.account_name == None + assert service.account_name == "custom" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' assert service.url.startswith('http://local-machine:11002/custom/account/path') service = TableClient(account_url=custom_account_url, table_name="foo") - assert service.account_name == None + assert service.account_name == "custom" assert service.table_name == "foo" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' assert service.url.startswith('http://local-machine:11002/custom/account/path') service = TableClient.from_table_url("http://local-machine:11002/custom/account/path/foo" + token) - assert service.account_name == None + assert service.account_name == "custom" assert service.table_name == "foo" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' diff --git a/sdk/tables/azure-data-tables/tests/test_table_client_cosmos.py b/sdk/tables/azure-data-tables/tests/test_table_client_cosmos.py index 6c5465aea362..8d8da85ef85c 100644 --- a/sdk/tables/azure-data-tables/tests/test_table_client_cosmos.py +++ b/sdk/tables/azure-data-tables/tests/test_table_client_cosmos.py @@ -439,21 +439,21 @@ def test_create_service_with_custom_account_endpoint_path(self): assert service._primary_hostname == 'local-machine:11002/custom/account/path' service = TableServiceClient(account_url=custom_account_url) - assert service.account_name == None + assert service.account_name == "custom" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' # mine doesnt have a question mark at the end assert service.url.startswith('http://local-machine:11002/custom/account/path') service = TableClient(account_url=custom_account_url, table_name="foo") - assert service.account_name == None + assert service.account_name == "custom" assert service.table_name == "foo" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' assert service.url.startswith('http://local-machine:11002/custom/account/path') service = TableClient.from_table_url("http://local-machine:11002/custom/account/path/foo" + self.sas_token) - assert service.account_name == None + assert service.account_name == "custom" assert service.table_name == "foo" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' diff --git a/sdk/tables/azure-data-tables/tests/test_table_client_cosmos_async.py b/sdk/tables/azure-data-tables/tests/test_table_client_cosmos_async.py index f010b863e7f0..8952b69490f7 100644 --- a/sdk/tables/azure-data-tables/tests/test_table_client_cosmos_async.py +++ b/sdk/tables/azure-data-tables/tests/test_table_client_cosmos_async.py @@ -424,30 +424,30 @@ async def test_create_service_with_custom_account_endpoint_path_async(self): service = service_type[0].from_connection_string(conn_string, table_name="foo") # Assert - assert service.account_name == self.tables_cosmos_account_name - assert service.credential.account_name == self.tables_cosmos_account_name - assert service.credential.account_key == self.tables_primary_cosmos_account_key - assert service._primary_hostname == 'local-machine:11002/custom/account/path' + assert service.account_name == self.tables_cosmos_account_name + assert service.credential.account_name == self.tables_cosmos_account_name + assert service.credential.account_key == self.tables_primary_cosmos_account_key + assert service._primary_hostname == 'local-machine:11002/custom/account/path' service = TableServiceClient(account_url=custom_account_url) - assert service.account_name == None - assert service.credential == None - assert service._primary_hostname == 'local-machine:11002/custom/account/path' + assert service.account_name == "custom" + assert service.credential == None + assert service._primary_hostname == 'local-machine:11002/custom/account/path' # mine doesnt have a question mark at the end assert service.url.startswith('http://local-machine:11002/custom/account/path') service = TableClient(account_url=custom_account_url, table_name="foo") - assert service.account_name == None - assert service.table_name == "foo" - assert service.credential == None - assert service._primary_hostname == 'local-machine:11002/custom/account/path' + assert service.account_name == "custom" + assert service.table_name == "foo" + assert service.credential == None + assert service._primary_hostname == 'local-machine:11002/custom/account/path' assert service.url.startswith('http://local-machine:11002/custom/account/path') service = TableClient.from_table_url("http://local-machine:11002/custom/account/path/foo" + self.sas_token) - assert service.account_name == None - assert service.table_name == "foo" - assert service.credential == None - assert service._primary_hostname == 'local-machine:11002/custom/account/path' + assert service.account_name == "custom" + assert service.table_name == "foo" + assert service.credential == None + assert service._primary_hostname == 'local-machine:11002/custom/account/path' assert service.url.startswith('http://local-machine:11002/custom/account/path') @pytest.mark.asyncio