Skip to content

Commit c724344

Browse files
committed
model filtering
1 parent 4d7ce9d commit c724344

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

src/lambda/update_configuration/index.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,66 @@ def resolve_content(content: Union[str, Dict[str, Any]]) -> Union[Dict[str, Any]
7171
return content
7272

7373

74+
def get_current_region() -> str:
75+
"""Get the current AWS region"""
76+
return boto3.Session().region_name
77+
78+
79+
def is_eu_region(region: str) -> bool:
80+
"""Check if the region is an EU region"""
81+
return region.startswith("eu-")
82+
83+
84+
def is_us_region(region: str) -> bool:
85+
"""Check if the region is a US region"""
86+
return region.startswith("us-")
87+
88+
89+
def _should_include_model(model_id: str, region_type: str) -> bool:
90+
"""Check if a model should be included for the given region type"""
91+
# Non-string items are always included
92+
if not isinstance(model_id, str):
93+
return True
94+
95+
# For other regions or non-prefixed models, include everything
96+
if region_type not in ["us", "eu"] or not (
97+
model_id.startswith("us.") or model_id.startswith("eu.")
98+
):
99+
return True
100+
101+
# For US regions: exclude EU models
102+
if region_type == "us" and model_id.startswith("eu."):
103+
return False
104+
105+
# For EU regions: exclude US models
106+
if region_type == "eu" and model_id.startswith("us."):
107+
return False
108+
109+
return True
110+
111+
112+
def filter_models_by_region(data: Any, region_type: str) -> Any:
113+
"""Filter out models that don't match the region type"""
114+
if isinstance(data, dict):
115+
return {
116+
key: (
117+
[item for item in value if _should_include_model(item, region_type)]
118+
if isinstance(value, list)
119+
and any(
120+
isinstance(item, str) and ("us." in item or "eu." in item)
121+
for item in value
122+
)
123+
else filter_models_by_region(value, region_type)
124+
)
125+
for key, value in data.items()
126+
}
127+
128+
if isinstance(data, list):
129+
return [filter_models_by_region(item, region_type) for item in data]
130+
131+
return data
132+
133+
74134
def generate_physical_id(stack_id: str, logical_id: str) -> str:
75135
"""
76136
Generates a consistent physical ID for the custom resource
@@ -96,13 +156,32 @@ def handler(event: Dict[str, Any], context: Any) -> None:
96156
# Remove ServiceToken from properties as it's not needed in DynamoDB
97157
properties.pop("ServiceToken", None)
98158

159+
# Detect region type
160+
current_region = get_current_region()
161+
region_type = (
162+
"eu"
163+
if is_eu_region(current_region)
164+
else "us"
165+
if is_us_region(current_region)
166+
else "other"
167+
)
168+
logger.info(f"Detected region: {current_region}, region type: {region_type}")
169+
99170
# Initialize ConfigurationManager for all database operations
100171
manager = ConfigurationManager()
101172

102173
if request_type in ["Create", "Update"]:
103174
# Update Schema configuration
104175
if "Schema" in properties:
105176
resolved_schema = resolve_content(properties["Schema"])
177+
178+
# Filter models based on region
179+
if region_type in ["us", "eu"]:
180+
resolved_schema = filter_models_by_region(
181+
resolved_schema, region_type
182+
)
183+
logger.info(f"Filtered schema models for {region_type} region")
184+
106185
# New API: save_configuration() accepts dict and converts to IDPConfig internally
107186
# ConfigurationManager handles migration automatically
108187
manager.save_configuration("Schema", {"Schema": resolved_schema})

0 commit comments

Comments
 (0)