Skip to content

Commit f15cb75

Browse files
committed
adding tagging service
# Conflicts: # localstack-core/localstack/services/dynamodb/provider.py # localstack-core/localstack/services/dynamodb/v2/provider.py
1 parent 9eb1a8a commit f15cb75

File tree

3 files changed

+22
-38
lines changed

3 files changed

+22
-38
lines changed

localstack-core/localstack/services/dynamodb/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
CrossRegionAttribute,
1919
LocalAttribute,
2020
)
21+
from localstack.utils.tagging import TaggingService
2122

2223

2324
@dataclasses.dataclass
@@ -113,7 +114,7 @@ class DynamoDBStore(BaseStore):
113114
)
114115

115116
# cache table taggings - maps table ARN to tags dict
116-
TABLE_TAGS: dict[str, dict] = CrossRegionAttribute(default=dict)
117+
TAGS: TaggingService = CrossRegionAttribute(default=dict)
117118

118119
# maps table names to cached table definitions
119120
table_definitions: dict[str, TableDescription] = LocalAttribute(default=dict)

localstack-core/localstack/services/dynamodb/provider.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -752,11 +752,8 @@ def create_table(
752752
if "WarmThroughput" in table_description:
753753
table_description["WarmThroughput"]["Status"] = "UPDATING"
754754

755-
tags = table_definitions.pop("Tags", [])
756-
if tags:
757-
get_store(context.account_id, context.region).TABLE_TAGS[table_arn] = {
758-
tag["Key"]: tag["Value"] for tag in tags
759-
}
755+
if tags := table_definitions.pop("Tags", []):
756+
get_store(context.account_id, context.region).TAGS.tag_resource(table_arn, tags)
760757

761758
# remove invalid attributes from result
762759
table_description.pop("Tags", None)
@@ -786,7 +783,7 @@ def delete_table(
786783
dynamodbstreams_api.delete_streams(context.account_id, context.region, table_arn)
787784

788785
store = get_store(context.account_id, context.region)
789-
store.TABLE_TAGS.pop(table_arn, None)
786+
store.TAGS.del_resource(table_arn)
790787
store.REPLICAS.pop(table_name, None)
791788

792789
return result
@@ -1466,10 +1463,8 @@ def execute_statement(
14661463
def tag_resource(
14671464
self, context: RequestContext, resource_arn: ResourceArnString, tags: TagList, **kwargs
14681465
) -> None:
1469-
table_tags = get_store(context.account_id, context.region).TABLE_TAGS
1470-
if resource_arn not in table_tags:
1471-
table_tags[resource_arn] = {}
1472-
table_tags[resource_arn].update({tag["Key"]: tag["Value"] for tag in tags})
1466+
table_tags = get_store(context.account_id, context.region).TAGS
1467+
table_tags.tag_resource(resource_arn, tags)
14731468

14741469
def untag_resource(
14751470
self,
@@ -1478,10 +1473,8 @@ def untag_resource(
14781473
tag_keys: TagKeyList,
14791474
**kwargs,
14801475
) -> None:
1481-
for tag_key in tag_keys or []:
1482-
get_store(context.account_id, context.region).TABLE_TAGS.get(resource_arn, {}).pop(
1483-
tag_key, None
1484-
)
1476+
store = get_store(context.account_id, context.region)
1477+
store.TAGS.untag_resource(resource_arn, tag_keys)
14851478

14861479
def list_tags_of_resource(
14871480
self,
@@ -1490,13 +1483,9 @@ def list_tags_of_resource(
14901483
next_token: NextTokenString = None,
14911484
**kwargs,
14921485
) -> ListTagsOfResourceOutput:
1493-
result = [
1494-
{"Key": k, "Value": v}
1495-
for k, v in get_store(context.account_id, context.region)
1496-
.TABLE_TAGS.get(resource_arn, {})
1497-
.items()
1498-
]
1499-
return ListTagsOfResourceOutput(Tags=result)
1486+
store = get_store(context.account_id, context.region)
1487+
tags = store.TAGS.list_tags_for_resource(resource_arn)
1488+
return ListTagsOfResourceOutput(Tags=tags.get("Tags"))
15001489

15011490
#
15021491
# TTLs

localstack-core/localstack/services/dynamodb/v2/provider.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ def create_table(
561561
if "NumberOfDecreasesToday" not in table_description["ProvisionedThroughput"]:
562562
table_description["ProvisionedThroughput"]["NumberOfDecreasesToday"] = 0
563563

564+
if tags := table_definitions.pop("Tags", []):
565+
get_store(context.account_id, context.region).TAGS.tag_resource(table_arn, tags)
564566
if "WarmThroughput" in table_description:
565567
table_description["WarmThroughput"]["Status"] = "UPDATING"
566568

@@ -597,7 +599,7 @@ def delete_table(
597599
table_arn = self.fix_table_arn(context.account_id, context.region, table_arn)
598600

599601
store = get_store(context.account_id, context.region)
600-
store.TABLE_TAGS.pop(table_arn, None)
602+
store.TAGS.del_resource(table_arn)
601603
store.REPLICAS.pop(table_name, None)
602604

603605
return result
@@ -990,10 +992,8 @@ def execute_statement(
990992
def tag_resource(
991993
self, context: RequestContext, resource_arn: ResourceArnString, tags: TagList, **kwargs
992994
) -> None:
993-
table_tags = get_store(context.account_id, context.region).TABLE_TAGS
994-
if resource_arn not in table_tags:
995-
table_tags[resource_arn] = {}
996-
table_tags[resource_arn].update({tag["Key"]: tag["Value"] for tag in tags})
995+
table_tags = get_store(context.account_id, context.region).TAGS
996+
table_tags.tag_resource(resource_arn, tags)
997997

998998
def untag_resource(
999999
self,
@@ -1002,10 +1002,8 @@ def untag_resource(
10021002
tag_keys: TagKeyList,
10031003
**kwargs,
10041004
) -> None:
1005-
for tag_key in tag_keys or []:
1006-
get_store(context.account_id, context.region).TABLE_TAGS.get(resource_arn, {}).pop(
1007-
tag_key, None
1008-
)
1005+
store = get_store(context.account_id, context.region)
1006+
store.TAGS.untag_resource(resource_arn, tag_keys)
10091007

10101008
def list_tags_of_resource(
10111009
self,
@@ -1014,13 +1012,9 @@ def list_tags_of_resource(
10141012
next_token: NextTokenString = None,
10151013
**kwargs,
10161014
) -> ListTagsOfResourceOutput:
1017-
result = [
1018-
{"Key": k, "Value": v}
1019-
for k, v in get_store(context.account_id, context.region)
1020-
.TABLE_TAGS.get(resource_arn, {})
1021-
.items()
1022-
]
1023-
return ListTagsOfResourceOutput(Tags=result)
1015+
store = get_store(context.account_id, context.region)
1016+
tags = store.TAGS.list_tags_for_resource(resource_arn)
1017+
return ListTagsOfResourceOutput(Tags=tags.get("Tags"))
10241018

10251019
#
10261020
# TTLs

0 commit comments

Comments
 (0)