Skip to content

Commit

Permalink
Add tag support to table creation (#811)
Browse files Browse the repository at this point in the history
  • Loading branch information
matllubos authored Nov 20, 2020
1 parent a365bc8 commit b10da8e
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 4 deletions.
13 changes: 11 additions & 2 deletions pynamodb/connection/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
RETURN_VALUES_ON_CONDITION_FAILURE_VALUES, RETURN_VALUES_ON_CONDITION_FAILURE,
AVAILABLE_BILLING_MODES, DEFAULT_BILLING_MODE, BILLING_MODE, PAY_PER_REQUEST_BILLING_MODE,
PROVISIONED_BILLING_MODE,
TIME_TO_LIVE_SPECIFICATION, ENABLED, UPDATE_TIME_TO_LIVE
TIME_TO_LIVE_SPECIFICATION, ENABLED, UPDATE_TIME_TO_LIVE, TAGS, VALUE
)
from pynamodb.exceptions import (
TableError, QueryError, PutError, DeleteError, UpdateError, GetError, ScanError, TableDoesNotExist,
Expand Down Expand Up @@ -568,7 +568,8 @@ def create_table(
global_secondary_indexes: Optional[Any] = None,
local_secondary_indexes: Optional[Any] = None,
stream_specification: Optional[Dict] = None,
billing_mode: str = DEFAULT_BILLING_MODE
billing_mode: str = DEFAULT_BILLING_MODE,
tags: Optional[Dict[str, str]] = None,
) -> Dict:
"""
Performs the CreateTable operation
Expand Down Expand Up @@ -638,6 +639,14 @@ def create_table(
STREAM_VIEW_TYPE: stream_specification['stream_view_type']
}

if tags:
operation_kwargs[TAGS] = [
{
KEY: k,
VALUE: v
} for k, v in tags.items()
]

try:
data = self.dispatch(CREATE_TABLE, operation_kwargs)
except BOTOCORE_EXCEPTIONS as e:
Expand Down
4 changes: 3 additions & 1 deletion pynamodb/connection/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ def create_table(
local_secondary_indexes: Optional[Any] = None,
stream_specification: Optional[Dict] = None,
billing_mode: str = DEFAULT_BILLING_MODE,
tags: Optional[Dict[str, str]] = None,
) -> Dict:
"""
Performs the CreateTable operation and returns the result
Expand All @@ -315,5 +316,6 @@ def create_table(
global_secondary_indexes=global_secondary_indexes,
local_secondary_indexes=local_secondary_indexes,
stream_specification=stream_specification,
billing_mode=billing_mode
billing_mode=billing_mode,
tags=tags
)
1 change: 1 addition & 0 deletions pynamodb/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
LIMIT = 'Limit'
ITEMS = 'Items'
ITEM = 'Item'
TAGS = 'Tags'
KEYS = 'Keys'
UTC = 'UTC'
KEY = 'Key'
Expand Down
5 changes: 4 additions & 1 deletion pynamodb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
BATCH_WRITE_PAGE_LIMIT,
META_CLASS_NAME, REGION, HOST, NULL,
COUNT, ITEM_COUNT, KEY, UNPROCESSED_ITEMS, STREAM_VIEW_TYPE,
STREAM_SPECIFICATION, STREAM_ENABLED, BILLING_MODE, PAY_PER_REQUEST_BILLING_MODE
STREAM_SPECIFICATION, STREAM_ENABLED, BILLING_MODE, PAY_PER_REQUEST_BILLING_MODE, TAGS
)

_T = TypeVar('_T', bound='Model')
Expand Down Expand Up @@ -185,6 +185,7 @@ class MetaProtocol(Protocol):
aws_secret_access_key: Optional[str]
aws_session_token: Optional[str]
billing_mode: Optional[str]
tags: Optional[Dict[str, str]]
stream_view_type: Optional[str]


Expand Down Expand Up @@ -788,6 +789,8 @@ def create_table(
}
if hasattr(cls.Meta, 'billing_mode'):
schema['billing_mode'] = cls.Meta.billing_mode
if hasattr(cls.Meta, 'tags'):
schema['tags'] = cls.Meta.tags
if read_capacity_units is not None:
schema['read_capacity_units'] = read_capacity_units
if write_capacity_units is not None:
Expand Down
75 changes: 75 additions & 0 deletions tests/test_table_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,81 @@ def test_create_table(self):
kwargs = req.call_args[0][1]
self.assertEqual(kwargs, params)

def test_create_table_with_tags(self):
conn = TableConnection(self.test_table_name)
kwargs = {
'read_capacity_units': 1,
'write_capacity_units': 1,
'attribute_definitions': [
{
'attribute_name': 'key1',
'attribute_type': 'S'
},
{
'attribute_name': 'key2',
'attribute_type': 'S'
}
],
'key_schema': [
{
'attribute_name': 'key1',
'key_type': 'hash'
},
{
'attribute_name': 'key2',
'key_type': 'range'
}
],
'tags': {
'tag-key1': 'tag-value1',
'tag-key2': 'tag-value2',
}
}
params = {
'TableName': 'ci-table',
'ProvisionedThroughput': {
'WriteCapacityUnits': 1,
'ReadCapacityUnits': 1
},
'AttributeDefinitions': [
{
'AttributeType': 'S',
'AttributeName': 'key1'
},
{
'AttributeType': 'S',
'AttributeName': 'key2'
}
],
'KeySchema': [
{
'KeyType': 'HASH',
'AttributeName': 'key1'
},
{
'KeyType': 'RANGE',
'AttributeName': 'key2'
}
],
'Tags': [
{
'Key': 'tag-key1',
'Value': 'tag-value1'
},
{
'Key': 'tag-key2',
'Value': 'tag-value2'
}
]
}
with patch(PATCH_METHOD) as req:
req.return_value = {}
conn.create_table(
**kwargs
)
kwargs = req.call_args[0][1]
self.assertEqual(kwargs, params)

def test_update_time_to_live(self):
"""
TableConnection.update_time_to_live
Expand Down

0 comments on commit b10da8e

Please sign in to comment.