Skip to content

Commit 75ddfe3

Browse files
[Tables] remove client side validation of entity (Azure#18267)
* removed client side validation of entity * fixes to try/catch * removing comments * using _decode_error
1 parent 6bd591d commit 75ddfe3

16 files changed

+478
-130
lines changed

sdk/tables/azure-data-tables/azure/data/tables/_table_client.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@
1212
from urlparse import urlparse # type: ignore
1313
from urllib2 import unquote # type: ignore
1414

15-
from azure.core.exceptions import HttpResponseError, ResourceNotFoundError
15+
from azure.core.exceptions import HttpResponseError
1616
from azure.core.paging import ItemPaged
1717
from azure.core.tracing.decorator import distributed_trace
1818

1919
from ._deserialize import _convert_to_entity, _trim_service_metadata
2020
from ._entity import TableEntity
21-
from ._error import _process_table_error, _validate_table_name
21+
from ._error import (
22+
_process_table_error,
23+
_validate_table_name,
24+
_reraise_error,
25+
_decode_error
26+
)
2227
from ._generated.models import (
2328
SignedIdentifier,
2429
TableProperties,
@@ -379,10 +384,7 @@ def create_entity(
379384
:dedent: 12
380385
:caption: Creating and adding an entity to a Table
381386
"""
382-
if "PartitionKey" in entity and "RowKey" in entity:
383-
entity = _add_entity_properties(entity)
384-
else:
385-
raise ValueError("PartitionKey and RowKey were not provided in entity")
387+
entity = _add_entity_properties(entity)
386388
try:
387389
metadata, _ = self._client.table.insert_entity(
388390
table=self.table_name,
@@ -391,8 +393,14 @@ def create_entity(
391393
**kwargs
392394
)
393395
return _trim_service_metadata(metadata)
394-
except ResourceNotFoundError as error:
395-
_process_table_error(error)
396+
except HttpResponseError as error:
397+
decoded = _decode_error(error.response, error.message)
398+
if decoded.error_code == "PropertiesNeedValue":
399+
if entity.get("PartitionKey") is None:
400+
raise ValueError("PartitionKey must be present in an entity")
401+
if entity.get("RowKey") is None:
402+
raise ValueError("RowKey must be present in an entity")
403+
_reraise_error(error)
396404

397405
@distributed_trace
398406
def update_entity(

sdk/tables/azure-data-tables/azure/data/tables/aio/_table_client_async.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from azure.core.credentials import AzureNamedKeyCredential, AzureSasCredential
1515
from azure.core.async_paging import AsyncItemPaged
16-
from azure.core.exceptions import ResourceNotFoundError, HttpResponseError
16+
from azure.core.exceptions import HttpResponseError
1717
from azure.core.tracing.decorator import distributed_trace
1818
from azure.core.tracing.decorator_async import distributed_trace_async
1919

@@ -23,14 +23,19 @@
2323
from .._models import AccessPolicy
2424
from .._serialize import serialize_iso, _parameter_filter_substitution
2525
from .._deserialize import _return_headers_and_deserialized
26-
from .._error import _process_table_error, _validate_table_name
26+
from .._error import (
27+
_process_table_error,
28+
_validate_table_name,
29+
_decode_error,
30+
_reraise_error
31+
)
2732
from .._models import UpdateMode
2833
from .._deserialize import _convert_to_entity, _trim_service_metadata
2934
from .._serialize import _add_entity_properties, _get_match_headers
35+
from .._table_client import EntityType, TransactionOperationType
3036
from ._base_client_async import AsyncTablesBaseClient
3137
from ._models import TableEntityPropertiesPaged
3238
from ._table_batch_async import TableBatchOperations
33-
from .._table_client import EntityType, TransactionOperationType
3439

3540

3641
class TableClient(AsyncTablesBaseClient):
@@ -368,10 +373,7 @@ async def create_entity(
368373
:dedent: 8
369374
:caption: Adding an entity to a Table
370375
"""
371-
if "PartitionKey" in entity and "RowKey" in entity:
372-
entity = _add_entity_properties(entity)
373-
else:
374-
raise ValueError("PartitionKey and RowKey were not provided in entity")
376+
entity = _add_entity_properties(entity)
375377
try:
376378
metadata, _ = await self._client.table.insert_entity(
377379
table=self.table_name,
@@ -380,8 +382,15 @@ async def create_entity(
380382
**kwargs
381383
)
382384
return _trim_service_metadata(metadata)
383-
except ResourceNotFoundError as error:
384-
_process_table_error(error)
385+
except HttpResponseError as error:
386+
decoded = _decode_error(error.response, error.message)
387+
if decoded.error_code == "PropertiesNeedValue":
388+
if entity.get("PartitionKey") is None:
389+
raise ValueError("PartitionKey must be present in an entity")
390+
if entity.get("RowKey") is None:
391+
raise ValueError("RowKey must be present in an entity")
392+
_reraise_error(error)
393+
385394

386395
@distributed_trace_async
387396
async def update_entity(

sdk/tables/azure-data-tables/tests/recordings/test_table_entity.test_insert_entity_missing_pk.yaml

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ interactions:
1515
DataServiceVersion:
1616
- '3.0'
1717
Date:
18-
- Fri, 18 Dec 2020 17:09:08 GMT
18+
- Fri, 23 Apr 2021 17:54:53 GMT
1919
User-Agent:
20-
- azsdk-python-data-tables/12.0.0b4 Python/3.9.0rc1 (Windows-10-10.0.19041-SP0)
20+
- azsdk-python-data-tables/12.0.0b7 Python/3.9.0rc1 (Windows-10-10.0.19041-SP0)
2121
x-ms-date:
22-
- Fri, 18 Dec 2020 17:09:08 GMT
22+
- Fri, 23 Apr 2021 17:54:53 GMT
2323
x-ms-version:
2424
- '2019-02-02'
2525
method: POST
@@ -33,7 +33,7 @@ interactions:
3333
content-type:
3434
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
3535
date:
36-
- Fri, 18 Dec 2020 17:09:08 GMT
36+
- Fri, 23 Apr 2021 17:54:53 GMT
3737
location:
3838
- https://fake_table_account.table.core.windows.net/Tables('uttabled41f1395')
3939
server:
@@ -47,6 +47,53 @@ interactions:
4747
status:
4848
code: 201
4949
message: Created
50+
- request:
51+
body: '{"RowKey": "rk", "RowKey@odata.type": "Edm.String"}'
52+
headers:
53+
Accept:
54+
- application/json;odata=minimalmetadata
55+
Accept-Encoding:
56+
- gzip, deflate
57+
Connection:
58+
- keep-alive
59+
Content-Length:
60+
- '51'
61+
Content-Type:
62+
- application/json;odata=nometadata
63+
DataServiceVersion:
64+
- '3.0'
65+
Date:
66+
- Fri, 23 Apr 2021 17:54:54 GMT
67+
User-Agent:
68+
- azsdk-python-data-tables/12.0.0b7 Python/3.9.0rc1 (Windows-10-10.0.19041-SP0)
69+
x-ms-date:
70+
- Fri, 23 Apr 2021 17:54:54 GMT
71+
x-ms-version:
72+
- '2019-02-02'
73+
method: POST
74+
uri: https://fake_table_account.table.core.windows.net/uttabled41f1395
75+
response:
76+
body:
77+
string: '{"odata.error":{"code":"PropertiesNeedValue","message":{"lang":"en-US","value":"The
78+
values are not specified for all properties in the entity.\nRequestId:e81fe51f-c002-003c-2969-38f3e3000000\nTime:2021-04-23T17:54:54.3641239Z"}}}'
79+
headers:
80+
cache-control:
81+
- no-cache
82+
content-type:
83+
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
84+
date:
85+
- Fri, 23 Apr 2021 17:54:53 GMT
86+
server:
87+
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
88+
transfer-encoding:
89+
- chunked
90+
x-content-type-options:
91+
- nosniff
92+
x-ms-version:
93+
- '2019-02-02'
94+
status:
95+
code: 400
96+
message: Bad Request
5097
- request:
5198
body: null
5299
headers:
@@ -59,11 +106,11 @@ interactions:
59106
Content-Length:
60107
- '0'
61108
Date:
62-
- Fri, 18 Dec 2020 17:09:08 GMT
109+
- Fri, 23 Apr 2021 17:54:54 GMT
63110
User-Agent:
64-
- azsdk-python-data-tables/12.0.0b4 Python/3.9.0rc1 (Windows-10-10.0.19041-SP0)
111+
- azsdk-python-data-tables/12.0.0b7 Python/3.9.0rc1 (Windows-10-10.0.19041-SP0)
65112
x-ms-date:
66-
- Fri, 18 Dec 2020 17:09:08 GMT
113+
- Fri, 23 Apr 2021 17:54:54 GMT
67114
x-ms-version:
68115
- '2019-02-02'
69116
method: DELETE
@@ -77,7 +124,7 @@ interactions:
77124
content-length:
78125
- '0'
79126
date:
80-
- Fri, 18 Dec 2020 17:09:08 GMT
127+
- Fri, 23 Apr 2021 17:54:53 GMT
81128
server:
82129
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
83130
x-content-type-options:

sdk/tables/azure-data-tables/tests/recordings/test_table_entity.test_insert_entity_missing_rk.yaml

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ interactions:
1515
DataServiceVersion:
1616
- '3.0'
1717
Date:
18-
- Fri, 18 Dec 2020 17:09:08 GMT
18+
- Fri, 23 Apr 2021 17:57:34 GMT
1919
User-Agent:
20-
- azsdk-python-data-tables/12.0.0b4 Python/3.9.0rc1 (Windows-10-10.0.19041-SP0)
20+
- azsdk-python-data-tables/12.0.0b7 Python/3.9.0rc1 (Windows-10-10.0.19041-SP0)
2121
x-ms-date:
22-
- Fri, 18 Dec 2020 17:09:08 GMT
22+
- Fri, 23 Apr 2021 17:57:34 GMT
2323
x-ms-version:
2424
- '2019-02-02'
2525
method: POST
@@ -33,7 +33,7 @@ interactions:
3333
content-type:
3434
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
3535
date:
36-
- Fri, 18 Dec 2020 17:09:09 GMT
36+
- Fri, 23 Apr 2021 17:57:35 GMT
3737
location:
3838
- https://fake_table_account.table.core.windows.net/Tables('uttabled4231397')
3939
server:
@@ -47,6 +47,53 @@ interactions:
4747
status:
4848
code: 201
4949
message: Created
50+
- request:
51+
body: '{"PartitionKey": "pk", "PartitionKey@odata.type": "Edm.String"}'
52+
headers:
53+
Accept:
54+
- application/json;odata=minimalmetadata
55+
Accept-Encoding:
56+
- gzip, deflate
57+
Connection:
58+
- keep-alive
59+
Content-Length:
60+
- '63'
61+
Content-Type:
62+
- application/json;odata=nometadata
63+
DataServiceVersion:
64+
- '3.0'
65+
Date:
66+
- Fri, 23 Apr 2021 17:57:35 GMT
67+
User-Agent:
68+
- azsdk-python-data-tables/12.0.0b7 Python/3.9.0rc1 (Windows-10-10.0.19041-SP0)
69+
x-ms-date:
70+
- Fri, 23 Apr 2021 17:57:35 GMT
71+
x-ms-version:
72+
- '2019-02-02'
73+
method: POST
74+
uri: https://fake_table_account.table.core.windows.net/uttabled4231397
75+
response:
76+
body:
77+
string: '{"odata.error":{"code":"PropertiesNeedValue","message":{"lang":"en-US","value":"The
78+
values are not specified for all properties in the entity.\nRequestId:ea3a5369-2002-001b-5b6a-38e427000000\nTime:2021-04-23T17:57:35.5932700Z"}}}'
79+
headers:
80+
cache-control:
81+
- no-cache
82+
content-type:
83+
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
84+
date:
85+
- Fri, 23 Apr 2021 17:57:35 GMT
86+
server:
87+
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
88+
transfer-encoding:
89+
- chunked
90+
x-content-type-options:
91+
- nosniff
92+
x-ms-version:
93+
- '2019-02-02'
94+
status:
95+
code: 400
96+
message: Bad Request
5097
- request:
5198
body: null
5299
headers:
@@ -59,11 +106,11 @@ interactions:
59106
Content-Length:
60107
- '0'
61108
Date:
62-
- Fri, 18 Dec 2020 17:09:09 GMT
109+
- Fri, 23 Apr 2021 17:57:35 GMT
63110
User-Agent:
64-
- azsdk-python-data-tables/12.0.0b4 Python/3.9.0rc1 (Windows-10-10.0.19041-SP0)
111+
- azsdk-python-data-tables/12.0.0b7 Python/3.9.0rc1 (Windows-10-10.0.19041-SP0)
65112
x-ms-date:
66-
- Fri, 18 Dec 2020 17:09:09 GMT
113+
- Fri, 23 Apr 2021 17:57:35 GMT
67114
x-ms-version:
68115
- '2019-02-02'
69116
method: DELETE
@@ -77,7 +124,7 @@ interactions:
77124
content-length:
78125
- '0'
79126
date:
80-
- Fri, 18 Dec 2020 17:09:09 GMT
127+
- Fri, 23 Apr 2021 17:57:35 GMT
81128
server:
82129
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
83130
x-content-type-options:

sdk/tables/azure-data-tables/tests/recordings/test_table_entity.test_insert_entity_property_name_too_long.yaml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ interactions:
1515
DataServiceVersion:
1616
- '3.0'
1717
Date:
18-
- Fri, 18 Dec 2020 17:09:09 GMT
18+
- Fri, 23 Apr 2021 17:43:03 GMT
1919
User-Agent:
20-
- azsdk-python-data-tables/12.0.0b4 Python/3.9.0rc1 (Windows-10-10.0.19041-SP0)
20+
- azsdk-python-data-tables/12.0.0b7 Python/3.9.0rc1 (Windows-10-10.0.19041-SP0)
2121
x-ms-date:
22-
- Fri, 18 Dec 2020 17:09:09 GMT
22+
- Fri, 23 Apr 2021 17:43:03 GMT
2323
x-ms-version:
2424
- '2019-02-02'
2525
method: POST
@@ -33,7 +33,7 @@ interactions:
3333
content-type:
3434
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
3535
date:
36-
- Fri, 18 Dec 2020 17:09:09 GMT
36+
- Fri, 23 Apr 2021 17:43:03 GMT
3737
location:
3838
- https://fake_table_account.table.core.windows.net/Tables('uttablee10d18a6')
3939
server:
@@ -66,26 +66,26 @@ interactions:
6666
DataServiceVersion:
6767
- '3.0'
6868
Date:
69-
- Fri, 18 Dec 2020 17:09:09 GMT
69+
- Fri, 23 Apr 2021 17:43:04 GMT
7070
User-Agent:
71-
- azsdk-python-data-tables/12.0.0b4 Python/3.9.0rc1 (Windows-10-10.0.19041-SP0)
71+
- azsdk-python-data-tables/12.0.0b7 Python/3.9.0rc1 (Windows-10-10.0.19041-SP0)
7272
x-ms-date:
73-
- Fri, 18 Dec 2020 17:09:09 GMT
73+
- Fri, 23 Apr 2021 17:43:04 GMT
7474
x-ms-version:
7575
- '2019-02-02'
7676
method: POST
7777
uri: https://fake_table_account.table.core.windows.net/uttablee10d18a6
7878
response:
7979
body:
8080
string: '{"odata.error":{"code":"PropertyNameTooLong","message":{"lang":"en-US","value":"The
81-
property name exceeds the maximum allowed length (255).\nRequestId:f5282dfd-6002-0035-3f60-d5cf0d000000\nTime:2020-12-18T17:09:10.2909098Z"}}}'
81+
property name exceeds the maximum allowed length (255).\nRequestId:a1de1e90-d002-007d-6b68-38ab07000000\nTime:2021-04-23T17:43:04.3558935Z"}}}'
8282
headers:
8383
cache-control:
8484
- no-cache
8585
content-type:
8686
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
8787
date:
88-
- Fri, 18 Dec 2020 17:09:09 GMT
88+
- Fri, 23 Apr 2021 17:43:03 GMT
8989
server:
9090
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
9191
transfer-encoding:
@@ -109,11 +109,11 @@ interactions:
109109
Content-Length:
110110
- '0'
111111
Date:
112-
- Fri, 18 Dec 2020 17:09:09 GMT
112+
- Fri, 23 Apr 2021 17:43:04 GMT
113113
User-Agent:
114-
- azsdk-python-data-tables/12.0.0b4 Python/3.9.0rc1 (Windows-10-10.0.19041-SP0)
114+
- azsdk-python-data-tables/12.0.0b7 Python/3.9.0rc1 (Windows-10-10.0.19041-SP0)
115115
x-ms-date:
116-
- Fri, 18 Dec 2020 17:09:09 GMT
116+
- Fri, 23 Apr 2021 17:43:04 GMT
117117
x-ms-version:
118118
- '2019-02-02'
119119
method: DELETE
@@ -127,7 +127,7 @@ interactions:
127127
content-length:
128128
- '0'
129129
date:
130-
- Fri, 18 Dec 2020 17:09:09 GMT
130+
- Fri, 23 Apr 2021 17:43:04 GMT
131131
server:
132132
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
133133
x-content-type-options:

0 commit comments

Comments
 (0)