|
1 |
| -from typing import Any, Dict, List, Optional, Union |
| 1 | +from typing import Dict, List, Optional, Tuple, Type, Union |
2 | 2 |
|
3 |
| -import yaml |
4 | 3 | from aws_cdk import aws_ec2
|
5 | 4 | from pydantic import Field, ValidationInfo, field_validator, model_validator
|
6 |
| -from pydantic_settings import BaseSettings, SettingsConfigDict |
| 5 | +from pydantic_settings import ( |
| 6 | + BaseSettings, |
| 7 | + PydanticBaseSettingsSource, |
| 8 | + SettingsConfigDict, |
| 9 | + YamlConfigSettingsSource, |
| 10 | +) |
7 | 11 | from typing_extensions import Self
|
8 | 12 |
|
9 | 13 |
|
@@ -65,7 +69,7 @@ class AppConfig(BaseSettings):
|
65 | 69 | equals `False`.""",
|
66 | 70 | default=[],
|
67 | 71 | )
|
68 |
| - bastion_host_user_data: Union[Dict[str, Any], aws_ec2.UserData] = Field( |
| 72 | + bastion_host_user_data: Union[str, aws_ec2.UserData] = Field( |
69 | 73 | description="""Path to file containing user data for the bastion host.
|
70 | 74 | Ignored if `bastion_host` equals `False`.""",
|
71 | 75 | default=aws_ec2.UserData.for_linux(),
|
@@ -109,7 +113,7 @@ class AppConfig(BaseSettings):
|
109 | 113 | default=None,
|
110 | 114 | )
|
111 | 115 |
|
112 |
| - model_config = SettingsConfigDict(env_file=".env") |
| 116 | + model_config = SettingsConfigDict(env_file=".env", yaml_file="config.yaml") |
113 | 117 |
|
114 | 118 | @field_validator("tags")
|
115 | 119 | def default_tags(cls, v, info: ValidationInfo):
|
@@ -154,20 +158,19 @@ def validate_model(self) -> Self:
|
154 | 158 | def build_service_name(self, service_id: str) -> str:
|
155 | 159 | return f"{self.project_id}-{self.stage}-{service_id}"
|
156 | 160 |
|
157 |
| - |
158 |
| -def build_app_config() -> AppConfig: |
159 |
| - """Builds the AppConfig object from config.yaml file if exists, |
160 |
| - otherwise use defaults""" |
161 |
| - try: |
162 |
| - with open("config.yaml") as f: |
163 |
| - print("Loading config from config.yaml") |
164 |
| - app_config = yaml.safe_load(f) |
165 |
| - app_config = ( |
166 |
| - {} if app_config is None else app_config |
167 |
| - ) # if config is empty, set it to an empty dict |
168 |
| - app_config = AppConfig(**app_config) |
169 |
| - except FileNotFoundError: |
170 |
| - # if no config at the expected path, using defaults |
171 |
| - app_config = AppConfig() |
172 |
| - |
173 |
| - return app_config |
| 161 | + @classmethod |
| 162 | + def settings_customise_sources( |
| 163 | + cls, |
| 164 | + settings_cls: Type[BaseSettings], |
| 165 | + init_settings: PydanticBaseSettingsSource, |
| 166 | + env_settings: PydanticBaseSettingsSource, |
| 167 | + dotenv_settings: PydanticBaseSettingsSource, |
| 168 | + file_secret_settings: PydanticBaseSettingsSource, |
| 169 | + ) -> Tuple[PydanticBaseSettingsSource, ...]: |
| 170 | + return ( |
| 171 | + init_settings, |
| 172 | + env_settings, |
| 173 | + dotenv_settings, |
| 174 | + file_secret_settings, |
| 175 | + YamlConfigSettingsSource(settings_cls), |
| 176 | + ) |
0 commit comments