Skip to content

Commit ecffe29

Browse files
committed
add yaml support
1 parent 2240267 commit ecffe29

File tree

4 files changed

+64
-24
lines changed

4 files changed

+64
-24
lines changed

.env.example

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ DATA_ACCESS_ROLE_ARN="arn:..."
1010
DB_INSTANCE_TYPE="t3.micro"
1111
DB_ALLOCATED_STORAGE="5"
1212
PUBLIC_DB_SUBNET="FALSE"
13+
14+
# VPC Options
1315
NAT_GATEWAY_COUNT="4"
1416

1517
# Bastion Host Options

config.yaml.example

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
project_id: "eoapi-yo"
2+
stage: "production"
3+
tags: {owner: user_a}
4+
5+
# Ingest Options
6+
auth_provider_jwks_url: 'https://auth.io'
7+
data_access_role_arn: 'arn:...'
8+
9+
# Database Options
10+
db_instance_type: 't3.micro'
11+
db_allocated_storage: 5
12+
public_db_subnet: False
13+
14+
# VPC Options
15+
nat_gateway_count: 4
16+
17+
# Bastion Host Options
18+
bastion_host: True
19+
bastion_host_create_elastic_ip: True
20+
bastion_host_allow_ip_list:
21+
- 'http://0.0.0.0'
22+
bastion_host_user_data: 'path/file.txt'
23+
24+
# Raster Options
25+
raster_buckets:
26+
- 'raster-east'
27+
28+
# STAC Browser Options
29+
stac_browser_version: '0.1.0'
30+
31+
# API Gateway Options
32+
acm_certificate_arn: 'arn:...'
33+
stac_api_custom_domain: 'https://stac.api'
34+
raster_api_custom_domain: 'https://raster.api'
35+
vector_api_custom_domain: 'https://vector.api'
36+
stac_ingestor_api_custom_domain: 'https://ingestor.api'

infrastructure/config.py

+25-22
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
from typing import Any, Dict, List, Optional, Union
1+
from typing import Dict, List, Optional, Tuple, Type, Union
22

3-
import yaml
43
from aws_cdk import aws_ec2
54
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+
)
711
from typing_extensions import Self
812

913

@@ -65,7 +69,7 @@ class AppConfig(BaseSettings):
6569
equals `False`.""",
6670
default=[],
6771
)
68-
bastion_host_user_data: Union[Dict[str, Any], aws_ec2.UserData] = Field(
72+
bastion_host_user_data: Union[str, aws_ec2.UserData] = Field(
6973
description="""Path to file containing user data for the bastion host.
7074
Ignored if `bastion_host` equals `False`.""",
7175
default=aws_ec2.UserData.for_linux(),
@@ -109,7 +113,7 @@ class AppConfig(BaseSettings):
109113
default=None,
110114
)
111115

112-
model_config = SettingsConfigDict(env_file=".env")
116+
model_config = SettingsConfigDict(env_file=".env", yaml_file="config.yaml")
113117

114118
@field_validator("tags")
115119
def default_tags(cls, v, info: ValidationInfo):
@@ -154,20 +158,19 @@ def validate_model(self) -> Self:
154158
def build_service_name(self, service_id: str) -> str:
155159
return f"{self.project_id}-{self.stage}-{service_id}"
156160

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+
)

requirements.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
eoapi-cdk==7.1.0
22
pydantic==2.7
3-
pydantic-settings==2.2.1
3+
pydantic-settings["yaml"]==2.2.1
44
boto3==1.24.15
5-
pyyaml==6.0
65
typing-extensions

0 commit comments

Comments
 (0)