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] Update TableAccessPolicy deserialization #19143

Merged
merged 6 commits into from
Jun 8, 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
2 changes: 2 additions & 0 deletions sdk/tables/azure-data-tables/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
`TableAccessPolicy`, `TableMetrics`, `TableRetentionPolicy`, `TableCorsRule`
* All parameters for `TableServiceClient.set_service_properties` are now keyword-only.
* The `credential` parameter for all Clients is now keyword-only.
* The method `TableClient.get_access_policy` will now return `None` where previously it returned an "empty" access policy object.
* Timestamp properties on `TableAccessPolicy` instances returned from `TableClient.get_access_policy` will now be deserialized to `datetime` instances.

**Fixes**
* Fixed support for Cosmos emulator endpoint, via URL/credential or connection string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ def clean_up_dotnet_timestamps(value):
return value[0]


def deserialize_iso(value):
if not value:
return value
return _from_entity_datetime(value)


def _from_entity_guid(value):
return UUID(value)
Expand Down
34 changes: 24 additions & 10 deletions sdk/tables/azure-data-tables/azure/data/tables/_table_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# --------------------------------------------------------------------------

import functools
from typing import Optional, Any, TYPE_CHECKING, Union, List, Tuple, Dict, Mapping, Iterable, overload
from typing import Optional, Any, TYPE_CHECKING, Union, List, Tuple, Dict, Mapping, Iterable, overload, cast
try:
from urllib.parse import urlparse, unquote
except ImportError:
Expand Down Expand Up @@ -33,7 +33,7 @@
from ._serialize import _get_match_headers, _add_entity_properties
from ._base_client import parse_connection_str, TablesBaseClient
from ._serialize import serialize_iso, _parameter_filter_substitution
from ._deserialize import _return_headers_and_deserialized
from ._deserialize import deserialize_iso, _return_headers_and_deserialized
from ._table_batch import TableBatchOperations
from ._models import (
TableEntityPropertiesPaged,
Expand Down Expand Up @@ -169,12 +169,12 @@ def from_table_url(cls, table_url, **kwargs):
def get_table_access_policy(
self, **kwargs # type: Any
):
# type: (...) -> Dict[str, TableAccessPolicy]
# type: (...) -> Dict[str, Optional[TableAccessPolicy]]
"""Retrieves details about any stored access policies specified on the table that may be
used with Shared Access Signatures.

:return: Dictionary of SignedIdentifiers
:rtype: Dict[str, :class:`~azure.data.tables.TableAccessPolicy`]
:rtype: Dict[str, Optional[:class:`~azure.data.tables.TableAccessPolicy`]]
:raises: :class:`~azure.core.exceptions.HttpResponseError`
"""
timeout = kwargs.pop("timeout", None)
Expand All @@ -187,29 +187,43 @@ def get_table_access_policy(
)
except HttpResponseError as error:
_process_table_error(error)
return {s.id: s.access_policy or TableAccessPolicy() for s in identifiers} # type: ignore
output = {} # type: Dict[str, Optional[TableAccessPolicy]]
for identifier in cast(List[SignedIdentifier], identifiers):
if identifier.access_policy:
output[identifier.id] = TableAccessPolicy(
start=deserialize_iso(identifier.access_policy.start),
expiry=deserialize_iso(identifier.access_policy.expiry),
permission=identifier.access_policy.permission
)
else:
output[identifier.id] = None
return output

@distributed_trace
def set_table_access_policy(
self,
signed_identifiers, # type: Dict[str, TableAccessPolicy]
signed_identifiers, # type: Dict[str, Optional[TableAccessPolicy]]
**kwargs
):
# type: (...) -> None
"""Sets stored access policies for the table that may be used with Shared Access Signatures.

:param signed_identifiers: Access policies to set for the table
:type signed_identifiers: Dict[str, :class:`~azure.data.tables.TableAccessPolicy`]
:type signed_identifiers: Dict[str, Optional[:class:`~azure.data.tables.TableAccessPolicy`]]
:return: None
:rtype: None
:raises: :class:`~azure.core.exceptions.HttpResponseError`
"""
identifiers = []
for key, value in signed_identifiers.items():
payload = None
if value:
value.start = serialize_iso(value.start)
value.expiry = serialize_iso(value.expiry)
identifiers.append(SignedIdentifier(id=key, access_policy=value))
payload = TableAccessPolicy(
start=serialize_iso(value.start),
expiry=serialize_iso(value.expiry),
permission=value.permission
)
identifiers.append(SignedIdentifier(id=key, access_policy=payload))
signed_identifiers = identifiers # type: ignore
try:
self._client.table.set_access_policy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# license information.
# --------------------------------------------------------------------------
import functools
from typing import List, Union, Any, Optional, Mapping, Iterable, Dict, overload
from typing import List, Union, Any, Optional, Mapping, Iterable, Dict, overload, cast
try:
from urllib.parse import urlparse, unquote
except ImportError:
Expand All @@ -23,7 +23,7 @@
from .._generated.models import SignedIdentifier, TableProperties, QueryOptions
from .._models import TableAccessPolicy, TableItem
from .._serialize import serialize_iso, _parameter_filter_substitution
from .._deserialize import _return_headers_and_deserialized
from .._deserialize import deserialize_iso, _return_headers_and_deserialized
from .._error import (
_process_table_error,
_validate_table_name,
Expand Down Expand Up @@ -158,13 +158,13 @@ def from_table_url(
return cls(endpoint, table_name=table_name, **kwargs)

@distributed_trace_async
async def get_table_access_policy(self, **kwargs) -> Mapping[str, TableAccessPolicy]:
async def get_table_access_policy(self, **kwargs) -> Mapping[str, Optional[TableAccessPolicy]]:
"""
Retrieves details about any stored access policies specified on the table that may be
used with Shared Access Signatures.

:return: Dictionary of SignedIdentifiers
:rtype: Dict[str, :class:`~azure.data.tables.TableAccessPolicy`]
:rtype: Dict[str, Optional[:class:`~azure.data.tables.TableAccessPolicy`]]
:raises: :class:`~azure.core.exceptions.HttpResponseError`
"""
timeout = kwargs.pop("timeout", None)
Expand All @@ -177,16 +177,22 @@ async def get_table_access_policy(self, **kwargs) -> Mapping[str, TableAccessPol
)
except HttpResponseError as error:
_process_table_error(error)
return {
s.id: s.access_policy
or TableAccessPolicy(start=None, expiry=None, permission=None)
for s in identifiers # type: ignore
}
output = {} # type: Dict[str, Optional[TableAccessPolicy]]
for identifier in cast(List[SignedIdentifier], identifiers):
if identifier.access_policy:
output[identifier.id] = TableAccessPolicy(
start=deserialize_iso(identifier.access_policy.start),
expiry=deserialize_iso(identifier.access_policy.expiry),
permission=identifier.access_policy.permission
)
else:
output[identifier.id] = None
return output

@distributed_trace_async
async def set_table_access_policy(
self,
signed_identifiers: Mapping[str, TableAccessPolicy],
signed_identifiers: Mapping[str, Optional[TableAccessPolicy]],
**kwargs
) -> None:
"""Sets stored access policies for the table that may be used with Shared Access Signatures.
Expand All @@ -199,10 +205,14 @@ async def set_table_access_policy(
"""
identifiers = []
for key, value in signed_identifiers.items():
payload = None
if value:
value.start = serialize_iso(value.start)
value.expiry = serialize_iso(value.expiry)
identifiers.append(SignedIdentifier(id=key, access_policy=value))
payload = TableAccessPolicy(
start=serialize_iso(value.start),
expiry=serialize_iso(value.expiry),
permission=value.permission
)
identifiers.append(SignedIdentifier(id=key, access_policy=payload))
try:
await self._client.table.set_access_policy(
table=self.table_name, table_acl=identifiers or None, **kwargs # type: ignore
Expand Down
9 changes: 9 additions & 0 deletions sdk/tables/azure-data-tables/tests/_shared/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,15 @@ def _assert_properties_default(self, prop):
self._assert_metrics_equal(prop["minute_metrics"], TableMetrics())
self._assert_cors_equal(prop["cors"], list())

def _assert_policy_datetime(self, val1, val2):
assert isinstance(val2, datetime)
assert val1.year == val2.year
assert val1.month == val2.month
assert val1.day == val2.day
assert val1.hour == val2.hour
assert val1.minute == val2.minute
assert val1.second == val2.second

def _assert_logging_equal(self, log1, log2):
if log1 is None or log2 is None:
assert log1 == log2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ interactions:
DataServiceVersion:
- '3.0'
Date:
- Wed, 14 Apr 2021 18:10:59 GMT
- Tue, 08 Jun 2021 02:27:57 GMT
User-Agent:
- azsdk-python-data-tables/12.0.0b7 Python/3.7.4 (Windows-10-10.0.19041-SP0)
- azsdk-python-data-tables/12.0.0 Python/3.7.4 (Windows-10-10.0.19041-SP0)
x-ms-date:
- Wed, 14 Apr 2021 18:10:59 GMT
- Tue, 08 Jun 2021 02:27:57 GMT
x-ms-version:
- '2019-02-02'
method: POST
Expand All @@ -33,7 +33,7 @@ interactions:
content-type:
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
date:
- Wed, 14 Apr 2021 18:10:59 GMT
- Tue, 08 Jun 2021 02:27:57 GMT
location:
- https://fake_table_account.table.core.windows.net/Tables('pytablesync6f17111b')
server:
Expand Down Expand Up @@ -63,11 +63,11 @@ interactions:
Content-Type:
- application/xml
Date:
- Wed, 14 Apr 2021 18:11:00 GMT
- Tue, 08 Jun 2021 02:27:58 GMT
User-Agent:
- azsdk-python-data-tables/12.0.0b7 Python/3.7.4 (Windows-10-10.0.19041-SP0)
- azsdk-python-data-tables/12.0.0 Python/3.7.4 (Windows-10-10.0.19041-SP0)
x-ms-date:
- Wed, 14 Apr 2021 18:11:00 GMT
- Tue, 08 Jun 2021 02:27:58 GMT
x-ms-version:
- '2019-02-02'
method: PUT
Expand All @@ -77,16 +77,16 @@ interactions:
string: '<?xml version="1.0" encoding="utf-8"?><m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><m:code>InvalidXmlDocument</m:code><m:message
xml:lang="en-US">XML specified is not syntactically valid.

RequestId:e4afd8b3-1002-0010-1959-311f4c000000
RequestId:e4d09754-d002-00a6-300d-5c6d3a000000

Time:2021-04-14T18:11:00.9932670Z</m:message></m:error>'
Time:2021-06-08T02:27:58.1749862Z</m:message></m:error>'
headers:
content-length:
- '327'
content-type:
- application/xml
date:
- Wed, 14 Apr 2021 18:11:00 GMT
- Tue, 08 Jun 2021 02:27:58 GMT
server:
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
x-ms-error-code:
Expand All @@ -108,11 +108,11 @@ interactions:
Content-Length:
- '0'
Date:
- Wed, 14 Apr 2021 18:11:01 GMT
- Tue, 08 Jun 2021 02:27:58 GMT
User-Agent:
- azsdk-python-data-tables/12.0.0b7 Python/3.7.4 (Windows-10-10.0.19041-SP0)
- azsdk-python-data-tables/12.0.0 Python/3.7.4 (Windows-10-10.0.19041-SP0)
x-ms-date:
- Wed, 14 Apr 2021 18:11:01 GMT
- Tue, 08 Jun 2021 02:27:58 GMT
x-ms-version:
- '2019-02-02'
method: DELETE
Expand All @@ -126,7 +126,7 @@ interactions:
content-length:
- '0'
date:
- Wed, 14 Apr 2021 18:11:00 GMT
- Tue, 08 Jun 2021 02:27:58 GMT
server:
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
x-content-type-options:
Expand Down
Loading