-
Notifications
You must be signed in to change notification settings - Fork 2
Refactor loading of configuration #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zaro0508
merged 10 commits into
Sage-Bionetworks-IT:main
from
zaro0508:refactor-config-handler
Oct 30, 2025
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6bfa489
Refactor loading of configuration
zaro0508 47d4378
refactor tests to load configs from file
zaro0508 65323d1
update doc
zaro0508 d5c41b8
fix from review
zaro0508 9e2680c
fix stack dependency
zaro0508 7f8251c
fix conflicts
zaro0508 0d3e00a
minor updates
zaro0508 e667def
merge with latest
zaro0508 2f8b5dc
Merge branch 'main' into refactor-config-handler
zaro0508 ffc2a34
use test parameterization
zaro0508 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| TAGS: | ||
| CostCenter: NO PROGRAM / 000000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| VPC_CIDR: 10.254.172.0/24 | ||
| FQDN: dev.mydomain.io | ||
| CERTIFICATE_ARN: >- | ||
| arn:aws:acm:us-east-1:XXXXXXXXX:certificate/e8093404-7db1-4042-90d0-01eb5bde1ffc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| VPC_CIDR: 10.254.174.0/24 | ||
| FQDN: prod.mydomain.io | ||
| CERTIFICATE_ARN: >- | ||
| arn:aws:acm:us-east-1:XXXXXXXXX:certificate/69b3ba97-b382-4648-8f94-a250b77b4994 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| VPC_CIDR: 10.254.173.0/24 | ||
| FQDN: stage.mydomain.io | ||
| CERTIFICATE_ARN: >- | ||
| arn:aws:acm:us-east-1:XXXXXXXXX:certificate/69b3ba97-b382-4648-8f94-a250b77b4994 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| aws-cdk-lib==2.139.0 | ||
| constructs>=10.0.0,<11.0.0 | ||
| boto3>=1.34.1 | ||
| pyyaml>=6.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import json | ||
| import yaml | ||
| from pathlib import Path | ||
| from typing import Any, Dict | ||
|
|
||
|
|
||
| def load_context_config(env_name: str, config_dir: str = "config") -> Dict[str, Any]: | ||
| """ | ||
| Load AWS CDK context configuration from a YAML or JSON file. | ||
|
|
||
| Supports: | ||
| - .yaml/.yml OR .json (but not both) | ||
| - base.yaml merged with environment config | ||
| - Only 'dev', 'stage', or 'prod' environments are valid | ||
|
|
||
| Raises: | ||
| - ValueError if environment is invalid or multiple config files exist | ||
| - FileNotFoundError if expected config file not found | ||
| """ | ||
|
|
||
| # ✅ Validate environment name | ||
| valid_envs = {"dev", "stage", "prod"} | ||
| if env_name not in valid_envs: | ||
| raise ValueError( | ||
| f"Invalid environment '{env_name}'. " | ||
| f"Must be one of: {', '.join(sorted(valid_envs))}" | ||
| ) | ||
|
|
||
| # Define possible config paths | ||
| base_path = Path(config_dir) / "base.yaml" | ||
| env_yaml = Path(config_dir) / f"{env_name}.yaml" | ||
| env_yml = Path(config_dir) / f"{env_name}.yml" | ||
| env_json = Path(config_dir) / f"{env_name}.json" | ||
|
|
||
| def read_file(path: Path) -> Dict[str, Any]: | ||
| """Read YAML or JSON file into a dictionary.""" | ||
| with open(path, "r") as f: | ||
| if path.suffix in (".yaml", ".yml"): | ||
| return yaml.safe_load(f) or {} | ||
| elif path.suffix == ".json": | ||
| return json.load(f) | ||
| else: | ||
| raise ValueError(f"Unsupported config file type: {path.suffix}") | ||
|
|
||
| # Load base config (optional) | ||
| base_config = {} | ||
| if base_path.exists(): | ||
| base_config = read_file(base_path) | ||
|
|
||
| # Detect existing env-specific file(s) | ||
| env_files = [p for p in [env_yaml, env_yml, env_json] if p.exists()] | ||
|
|
||
| if not env_files: | ||
| raise FileNotFoundError( | ||
| f"No config file found for environment '{env_name}' " | ||
| f"in {config_dir}. Expected one of: {env_yaml}, {env_yml}, {env_json}" | ||
| ) | ||
|
|
||
| if len(env_files) > 1: | ||
| raise ValueError( | ||
| f"Multiple config files found for environment '{env_name}': {env_files}. " | ||
| f"Use only one (.yaml/.yml OR .json)." | ||
| ) | ||
|
|
||
| # Load and merge configs | ||
| env_config = read_file(env_files[0]) | ||
| merged_config = {**base_config, **env_config} | ||
|
|
||
| return merged_config |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.