Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
Fix/organisations restore data (#1749)
Nahuel Nucera80c8c08f6f04
diff --git a/ministack/services/cloudfront_keyvaluestore.py b/ministack/services/cloudfront_keyvaluestore.pyindex e5c4a09c..838a03b4 100644--- a/ministack/services/cloudfront_keyvaluestore.py+++ b/ministack/services/cloudfront_keyvaluestore.py@@ -54,8 +54,11 @@ def _restore_state(data):if not data:return_stores.clear()- for k, v in (data.get("stores") or {}).items():- _stores[k] = v+ # update() carries every account's entries; iterating a scoped dict yields+ # only the caller's, and `or {}` discards it entirely, since both its+ # truthiness and its iteration are account-scoped and the loader runs at+ # boot with no request scope.+ _stores.update(data.get("stores", {}))diff --git a/ministack/services/cur.py b/ministack/services/cur.pyindex b081c7ba..efcc3d91 100644--- a/ministack/services/cur.py+++ b/ministack/services/cur.py@@ -43,10 +43,12 @@ def get_state():def _restore_state(data):if not data:return+ # No `or {}`: a scoped dict holding only other accounts' entries is falsy+ # here, and the loader runs at boot with no request scope._report_definitions.clear()- _report_definitions.update(data.get("report_definitions") or {})+ _report_definitions.update(data.get("report_definitions", {}))_report_tags.clear()- _report_tags.update(data.get("report_tags") or {})+ _report_tags.update(data.get("report_tags", {}))def load_persisted_state(data):diff --git a/ministack/services/elasticache.py b/ministack/services/elasticache.pyindex 653e9013..81a2e1ee 100644--- a/ministack/services/elasticache.py+++ b/ministack/services/elasticache.py@@ -215,7 +215,6 @@ def load_persisted_state(data):def _restore_state(data):if not data:- default_state()return_restore_replication_groups(data.get("replication_groups", {}))_subnet_groups.update(data.get("subnet_groups", {}))@@ -1787,6 +1786,11 @@ def _default_params_for_family(family):}+# The default parameter groups exist on a fresh AWS account, so they are seeded+# at import rather than from the restore path — the central loader only calls+# load_persisted_state when a state file exists, and a boot without one still+# has to answer DescribeCacheParameterGroups.+default_state()# ---- Engine Versions ----diff --git a/ministack/services/opensearch.py b/ministack/services/opensearch.pyindex ab9ed31b..00940b57 100644--- a/ministack/services/opensearch.py+++ b/ministack/services/opensearch.py@@ -168,18 +168,21 @@ def load_persisted_state(data) -> None:def _restore_state(data):if not data:return- _domains.update(data.get("domains") or {})+ # No `or {}` on any of these: a scoped dict holding only other accounts'+ # entries is falsy, and the loader runs at boot with no request scope, so+ # the guard would drop every account's state but the default one's.+ _domains.update(data.get("domains", {}))domain_regions = {(account_id, name): regionfor (account_id, region, name), _rec in _domains.all_items()}_restore_domain_child_store(_change_progress,- data.get("change_progress") or {},+ data.get("change_progress", {}),domain_regions,key_to_domain=lambda key: key,)- domain_packages = data.get("domain_packages") or {}+ domain_packages = data.get("domain_packages", {})_restore_domain_child_store(_domain_packages,domain_packages,@@ -189,8 +192,8 @@ def _restore_state(data):package_regions = _package_regions_from_domain_packages(domain_packages, domain_regions)- _restore_package_store(_packages, data.get("packages") or {}, package_regions)- _tags.update(data.get("tags") or {})+ _restore_package_store(_packages, data.get("packages", {}), package_regions)+ _tags.update(data.get("tags", {}))def _legacy_items(restored):diff --git a/ministack/services/organizations.py b/ministack/services/organizations.pyindex 07e726b9..599419cf 100644--- a/ministack/services/organizations.py+++ b/ministack/services/organizations.py@@ -67,9 +67,15 @@ def _restore_state(data):(_orgs, "orgs"), (_accounts, "accounts"),(_ous, "ous"), (_roots, "roots"), (_tags, "tags")):+ # update() copies every account's entries; iterating an+ # AccountScopedDict yields only the caller's, and the loader runs at+ # boot with no request scope, so a per-key loop would drop every+ # account but the default one. `or {}` is wrong here for the same+ # reason: the truthiness of a scoped dict is account-scoped too.+ restored = data.get(key)store.clear()- for k, v in (data.get(key) or {}).items():- store[k] = v+ if restored is not None:+ store.update(restored)def _json(status, body):diff --git a/tests/test_lambda.py b/tests/test_lambda.pyindex 0c3f0889..211b545e 100644--- a/tests/test_lambda.py+++ b/tests/test_lambda.py@@ -4997,7 +4997,7 @@ def test_lambda_deleted_shared_layer_retention_and_restore(isolated_layers):state = svc.get_state()svc._layers.clear()svc._functions.clear()- svc.restore_state(state)+ svc.load_persisted_state(state)with request_scope(_CALLER_ACCOUNT, _FOREIGN_REGION):assert svc._resolve_layer_zip(arn) == layer_zipassert svc._layer_unzipped_size(arn) == len("retained bytes")@@ -5056,7 +5056,7 @@ def test_lambda_cfn_deletes_and_restore_reap_unreferenced_layer_versions(isolatewith request_scope(_FOREIGN_ACCOUNT, _FOREIGN_REGION):assert svc._layers["cfn-layer"]["versions"] == []svc._layers.clear()- svc.restore_state(state)+ svc.load_persisted_state(state)assert svc._layers["cfn-layer"]["versions"] == []