Skip to content

Commit 34d894e

Browse files
committed
port fix to v2
1 parent 750ab0e commit 34d894e

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,34 @@ def create_table(
687687
)
688688
table_description["TableClassSummary"] = {"TableClass": table_class}
689689

690+
if "GlobalSecondaryIndexes" in table_description:
691+
gsis = copy.deepcopy(table_description["GlobalSecondaryIndexes"])
692+
# update the different values, as DynamoDB-local v2 has a regression around GSI and does not return anything
693+
# anymore
694+
for gsi in gsis:
695+
index_name = gsi.get("IndexName", "")
696+
gsi.update(
697+
{
698+
"IndexArn": f"{table_arn}/index/{index_name}",
699+
"IndexSizeBytes": 0,
700+
"IndexStatus": "ACTIVE",
701+
"ItemCount": 0,
702+
}
703+
)
704+
gsi_provisioned_throughput = gsi.setdefault("ProvisionedThroughput", {})
705+
gsi_provisioned_throughput["NumberOfDecreasesToday"] = 0
706+
707+
if billing_mode == BillingMode.PAY_PER_REQUEST:
708+
gsi_provisioned_throughput["ReadCapacityUnits"] = 0
709+
gsi_provisioned_throughput["WriteCapacityUnits"] = 0
710+
711+
# table_definitions["GlobalSecondaryIndexes"] = gsis
712+
table_description["GlobalSecondaryIndexes"] = gsis
713+
714+
if "ProvisionedThroughput" in table_description:
715+
if "NumberOfDecreasesToday" not in table_description["ProvisionedThroughput"]:
716+
table_description["ProvisionedThroughput"]["NumberOfDecreasesToday"] = 0
717+
690718
tags = table_definitions.pop("Tags", [])
691719
if tags:
692720
get_store(context.account_id, context.region).TABLE_TAGS[table_arn] = {
@@ -765,6 +793,17 @@ def describe_table(
765793
"TableClass": table_definitions["TableClass"]
766794
}
767795

796+
if "GlobalSecondaryIndexes" in table_description:
797+
for gsi in table_description["GlobalSecondaryIndexes"]:
798+
default_values = {
799+
"NumberOfDecreasesToday": 0,
800+
"ReadCapacityUnits": 0,
801+
"WriteCapacityUnits": 0,
802+
}
803+
# even if the billing mode is PAY_PER_REQUEST, AWS returns the Read and Write Capacity Units
804+
# Terraform depends on this parity for update operations
805+
gsi["ProvisionedThroughput"] = default_values | gsi.get("ProvisionedThroughput", {})
806+
768807
return DescribeTableOutput(
769808
Table=select_from_typed_dict(TableDescription, table_description)
770809
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
SCHEMA_CACHE = TTLCache(maxsize=50, ttl=20)
3434

3535
_ddb_local_arn_pattern = re.compile(
36-
r'("TableArn"|"LatestStreamArn"|"StreamArn"|"ShardIterator")\s*:\s*"arn:[a-z-]+:dynamodb:ddblocal:000000000000:([^"]+)"'
36+
r'("TableArn"|"LatestStreamArn"|"StreamArn"|"ShardIterator"|"IndexArn")\s*:\s*"arn:[a-z-]+:dynamodb:ddblocal:000000000000:([^"]+)"'
3737
)
3838
_ddb_local_region_pattern = re.compile(r'"awsRegion"\s*:\s*"([^"]+)"')
3939
_ddb_local_exception_arn_pattern = re.compile(r'arn:[a-z-]+:dynamodb:ddblocal:000000000000:([^"]+)')

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import json
23
import logging
34
import os
@@ -526,6 +527,34 @@ def create_table(
526527
)
527528
table_description["TableClassSummary"] = {"TableClass": table_class}
528529

530+
if "GlobalSecondaryIndexes" in table_description:
531+
gsis = copy.deepcopy(table_description["GlobalSecondaryIndexes"])
532+
# update the different values, as DynamoDB-local v2 has a regression around GSI and does not return anything
533+
# anymore
534+
for gsi in gsis:
535+
index_name = gsi.get("IndexName", "")
536+
gsi.update(
537+
{
538+
"IndexArn": f"{table_arn}/index/{index_name}",
539+
"IndexSizeBytes": 0,
540+
"IndexStatus": "ACTIVE",
541+
"ItemCount": 0,
542+
}
543+
)
544+
gsi_provisioned_throughput = gsi.setdefault("ProvisionedThroughput", {})
545+
gsi_provisioned_throughput["NumberOfDecreasesToday"] = 0
546+
547+
if billing_mode == BillingMode.PAY_PER_REQUEST:
548+
gsi_provisioned_throughput["ReadCapacityUnits"] = 0
549+
gsi_provisioned_throughput["WriteCapacityUnits"] = 0
550+
551+
# table_definitions["GlobalSecondaryIndexes"] = gsis
552+
table_description["GlobalSecondaryIndexes"] = gsis
553+
554+
if "ProvisionedThroughput" in table_description:
555+
if "NumberOfDecreasesToday" not in table_description["ProvisionedThroughput"]:
556+
table_description["ProvisionedThroughput"]["NumberOfDecreasesToday"] = 0
557+
529558
tags = table_definitions.pop("Tags", [])
530559
if tags:
531560
get_store(context.account_id, context.region).TABLE_TAGS[table_arn] = {
@@ -603,6 +632,17 @@ def describe_table(
603632
"TableClass": table_definitions["TableClass"]
604633
}
605634

635+
if "GlobalSecondaryIndexes" in table_description:
636+
for gsi in table_description["GlobalSecondaryIndexes"]:
637+
default_values = {
638+
"NumberOfDecreasesToday": 0,
639+
"ReadCapacityUnits": 0,
640+
"WriteCapacityUnits": 0,
641+
}
642+
# even if the billing mode is PAY_PER_REQUEST, AWS returns the Read and Write Capacity Units
643+
# Terraform depends on this parity for update operations
644+
gsi["ProvisionedThroughput"] = default_values | gsi.get("ProvisionedThroughput", {})
645+
606646
return DescribeTableOutput(
607647
Table=select_from_typed_dict(TableDescription, table_description)
608648
)

tests/aws/services/dynamodb/test_dynamodb.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,6 +2341,14 @@ def _get_records_amount(record_amount: int):
23412341

23422342
@markers.aws.validated
23432343
@pytest.mark.parametrize("billing_mode", ["PAY_PER_REQUEST", "PROVISIONED"])
2344+
@markers.snapshot.skip_snapshot_verify(
2345+
paths=[
2346+
# LS returns those and not AWS, probably because no changes happened there yet
2347+
"$..ProvisionedThroughput.LastDecreaseDateTime",
2348+
"$..ProvisionedThroughput.LastIncreaseDateTime",
2349+
"$..TableDescription.BillingModeSummary.LastUpdateToPayPerRequestDateTime",
2350+
]
2351+
)
23442352
def test_gsi_with_billing_mode(
23452353
self, aws_client, dynamodb_create_table_with_parameters, snapshot, billing_mode
23462354
):

0 commit comments

Comments
 (0)