Skip to content
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

[Tables] Adding azurite support #17944

Merged
5 commits merged into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/tables/azure-data-tables/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 14 additions & 5 deletions sdk/tables/azure-data-tables/azure/data/tables/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
14 changes: 10 additions & 4 deletions sdk/tables/azure-data-tables/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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"
10 changes: 10 additions & 0 deletions sdk/tables/azure-data-tables/tests/test_table_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 3 additions & 3 deletions sdk/tables/azure-data-tables/tests/test_table_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 3 additions & 3 deletions sdk/tables/azure-data-tables/tests/test_table_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down