Skip to content

Commit

Permalink
added create_table_if_not_exists method to table service client (#13385)
Browse files Browse the repository at this point in the history
* added create_table_if_not_exists method to table service client, eseentially create table wrapped in try/except block

* lint fix

* fixing up issy's comments

* table_name not name, fixed test bug
  • Loading branch information
seankane-msft authored Aug 31, 2020
1 parent c77746b commit 43d7ebf
Show file tree
Hide file tree
Showing 10 changed files with 515 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
from ._deserialize import _convert_to_entity, _trim_service_metadata
from ._entity import TableEntity
from ._generated import AzureTable
from ._generated.models import AccessPolicy, SignedIdentifier, TableProperties, QueryOptions
from ._generated.models import (
AccessPolicy,
SignedIdentifier,
TableProperties,
QueryOptions
)
from ._serialize import _get_match_headers, _add_entity_properties
from ._base_client import parse_connection_str
from ._table_client_base import TableClientBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import functools
from typing import Any, Union
from azure.core.exceptions import HttpResponseError
from azure.core.exceptions import HttpResponseError, ResourceExistsError
from azure.core.paging import ItemPaged
from azure.core.tracing.decorator import distributed_trace
from azure.core.pipeline import Pipeline
Expand Down Expand Up @@ -154,6 +154,30 @@ def create_table(
table.create_table(**kwargs)
return table

@distributed_trace
def create_table_if_not_exists(
self,
table_name, # type: str
**kwargs # type: Any
):
# type: (...) -> TableClient
"""Creates a new table if it does not currently exist.
If the table currently exists, the current table is
returned.
:param table_name: The Table name.
:type table_name: str
:return: TableClient
:rtype: ~azure.data.tables.TableClient
:raises: ~azure.core.exceptions.HttpResponseError
"""
table = self.get_table_client(table_name=table_name)
try:
table.create_table(**kwargs)
except ResourceExistsError:
pass
return table

@distributed_trace
def delete_table(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)

from azure.core.async_paging import AsyncItemPaged
from azure.core.exceptions import HttpResponseError
from azure.core.exceptions import HttpResponseError, ResourceExistsError
from azure.core.pipeline import AsyncPipeline
from azure.core.tracing.decorator import distributed_trace
from azure.core.tracing.decorator_async import distributed_trace_async
Expand Down Expand Up @@ -196,6 +196,30 @@ async def create_table(
await table.create_table(**kwargs)
return table

@distributed_trace_async
async def create_table_if_not_exists(
self,
table_name, # type: str
**kwargs # type: Any
):
# type: (...) -> TableClient
"""Creates a new table if it does not currently exist.
If the table currently exists, the current table is
returned.
:param table_name: The Table name.
:type table_name: str
:return: TableClient
:rtype: ~azure.data.tables.aio.TableClient
:raises: ~azure.core.exceptions.HttpResponseError
"""
table = self.get_table_client(table_name=table_name)
try:
await table.create_table(**kwargs)
except ResourceExistsError:
pass
return table

@distributed_trace_async
async def delete_table(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ interactions:
DataServiceVersion:
- '3.0'
Date:
- Wed, 19 Aug 2020 21:18:00 GMT
- Thu, 27 Aug 2020 21:28:19 GMT
User-Agent:
- azsdk-python-storage-table/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
- azsdk-python-data-tables/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
x-ms-date:
- Wed, 19 Aug 2020 21:18:00 GMT
- Thu, 27 Aug 2020 21:28:19 GMT
x-ms-version:
- '2019-07-07'
method: POST
Expand All @@ -33,7 +33,7 @@ interactions:
content-type:
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
date:
- Wed, 19 Aug 2020 21:18:00 GMT
- Thu, 27 Aug 2020 21:28:20 GMT
location:
- https://storagename.table.core.windows.net/Tables('pytablesync6d7c1113')
server:
Expand Down Expand Up @@ -63,26 +63,26 @@ interactions:
DataServiceVersion:
- '3.0'
Date:
- Wed, 19 Aug 2020 21:18:00 GMT
- Thu, 27 Aug 2020 21:28:20 GMT
User-Agent:
- azsdk-python-storage-table/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
- azsdk-python-data-tables/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
x-ms-date:
- Wed, 19 Aug 2020 21:18:00 GMT
- Thu, 27 Aug 2020 21:28:20 GMT
x-ms-version:
- '2019-07-07'
method: POST
uri: https://storagename.table.core.windows.net/Tables
response:
body:
string: '{"odata.error":{"code":"TableAlreadyExists","message":{"lang":"en-US","value":"The
table specified already exists.\nRequestId:fbae76e2-b002-009f-206e-7649d9000000\nTime:2020-08-19T21:18:00.8893697Z"}}}'
table specified already exists.\nRequestId:2e7b208e-d002-003b-7bb8-7c685f000000\nTime:2020-08-27T21:28:21.0100931Z"}}}'
headers:
cache-control:
- no-cache
content-type:
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
date:
- Wed, 19 Aug 2020 21:18:00 GMT
- Thu, 27 Aug 2020 21:28:20 GMT
server:
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
transfer-encoding:
Expand All @@ -106,11 +106,11 @@ interactions:
Content-Length:
- '0'
Date:
- Wed, 19 Aug 2020 21:18:00 GMT
- Thu, 27 Aug 2020 21:28:20 GMT
User-Agent:
- azsdk-python-storage-table/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
- azsdk-python-data-tables/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
x-ms-date:
- Wed, 19 Aug 2020 21:18:00 GMT
- Thu, 27 Aug 2020 21:28:20 GMT
x-ms-version:
- '2019-07-07'
method: DELETE
Expand All @@ -124,7 +124,7 @@ interactions:
content-length:
- '0'
date:
- Wed, 19 Aug 2020 21:18:00 GMT
- Thu, 27 Aug 2020 21:28:20 GMT
server:
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
x-content-type-options:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
interactions:
- request:
body: '{"TableName": "pytablesync2c5a0f7d"}'
headers:
Accept:
- application/json;odata=minimalmetadata
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '36'
Content-Type:
- application/json;odata=nometadata
DataServiceVersion:
- '3.0'
Date:
- Thu, 27 Aug 2020 21:42:12 GMT
User-Agent:
- azsdk-python-data-tables/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
x-ms-date:
- Thu, 27 Aug 2020 21:42:12 GMT
x-ms-version:
- '2019-07-07'
method: POST
uri: https://storagename.table.core.windows.net/Tables
response:
body:
string: '{"odata.metadata":"https://storagename.table.core.windows.net/$metadata#Tables/@Element","TableName":"pytablesync2c5a0f7d"}'
headers:
cache-control:
- no-cache
content-type:
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
date:
- Thu, 27 Aug 2020 21:42:12 GMT
location:
- https://storagename.table.core.windows.net/Tables('pytablesync2c5a0f7d')
server:
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
transfer-encoding:
- chunked
x-content-type-options:
- nosniff
x-ms-version:
- '2019-07-07'
status:
code: 201
message: Created
- request:
body: '{"TableName": "pytablesync2c5a0f7d"}'
headers:
Accept:
- application/json;odata=minimalmetadata
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '36'
Content-Type:
- application/json;odata=nometadata
DataServiceVersion:
- '3.0'
Date:
- Thu, 27 Aug 2020 21:42:12 GMT
User-Agent:
- azsdk-python-data-tables/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
x-ms-date:
- Thu, 27 Aug 2020 21:42:12 GMT
x-ms-version:
- '2019-07-07'
method: POST
uri: https://storagename.table.core.windows.net/Tables
response:
body:
string: '{"odata.error":{"code":"TableAlreadyExists","message":{"lang":"en-US","value":"The
table specified already exists.\nRequestId:64cfdefd-d002-0057-80ba-7c2831000000\nTime:2020-08-27T21:42:13.5449901Z"}}}'
headers:
cache-control:
- no-cache
content-type:
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
date:
- Thu, 27 Aug 2020 21:42:13 GMT
server:
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
transfer-encoding:
- chunked
x-content-type-options:
- nosniff
x-ms-version:
- '2019-07-07'
status:
code: 409
message: Conflict
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '0'
Date:
- Thu, 27 Aug 2020 21:42:12 GMT
User-Agent:
- azsdk-python-data-tables/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
x-ms-date:
- Thu, 27 Aug 2020 21:42:12 GMT
x-ms-version:
- '2019-07-07'
method: DELETE
uri: https://storagename.table.core.windows.net/Tables('pytablesync2c5a0f7d')
response:
body:
string: ''
headers:
cache-control:
- no-cache
content-length:
- '0'
date:
- Thu, 27 Aug 2020 21:42:13 GMT
server:
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
x-content-type-options:
- nosniff
x-ms-version:
- '2019-07-07'
status:
code: 204
message: No Content
version: 1
Loading

0 comments on commit 43d7ebf

Please sign in to comment.