11import datetime
22from functools import cached_property
3- from typing import Any , ClassVar , Final , Literal , cast
3+ from typing import Final , Literal , cast
44
55from aws_library .ec2 import EC2InstanceBootSpecific , EC2Tags
66from fastapi import FastAPI
1212)
1313from models_library .clusters import InternalClusterAuthentication
1414from pydantic import (
15+ AliasChoices ,
1516 Field ,
1617 NonNegativeFloat ,
1718 NonNegativeInt ,
1819 PositiveInt ,
1920 SecretStr ,
20- parse_obj_as ,
21- validator ,
21+ TypeAdapter ,
22+ field_validator ,
2223)
24+ from pytest_simcore .helpers .dict_tools import ConfigDict
2325from settings_library .base import BaseCustomSettings
2426from settings_library .docker_registry import RegistrySettings
2527from settings_library .ec2 import EC2Settings
3436
3537
3638class ClustersKeeperEC2Settings (EC2Settings ):
37- class Config (EC2Settings .Config ):
38- env_prefix = CLUSTERS_KEEPER_ENV_PREFIX
39-
40- schema_extra : ClassVar [dict [str , Any ]] = { # type: ignore[misc]
39+ model_config = ConfigDict (
40+ env_prefix = CLUSTERS_KEEPER_ENV_PREFIX ,
41+ json_schema_extra = {
4142 "examples" : [
4243 {
4344 f"{ CLUSTERS_KEEPER_ENV_PREFIX } EC2_ACCESS_KEY_ID" : "my_access_key_id" ,
@@ -46,7 +47,8 @@ class Config(EC2Settings.Config):
4647 f"{ CLUSTERS_KEEPER_ENV_PREFIX } EC2_SECRET_ACCESS_KEY" : "my_secret_access_key" ,
4748 }
4849 ],
49- }
50+ },
51+ )
5052
5153
5254class WorkersEC2InstancesSettings (BaseCustomSettings ):
@@ -77,7 +79,7 @@ class WorkersEC2InstancesSettings(BaseCustomSettings):
7779 # NAME PREFIX is not exposed since we override it anyway
7880 WORKERS_EC2_INSTANCES_SECURITY_GROUP_IDS : list [str ] = Field (
7981 ...,
80- min_items = 1 ,
82+ min_length = 1 ,
8183 description = "A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic"
8284 " (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-security-groups.html), "
8385 " this is required to start a new EC2 instance" ,
@@ -108,14 +110,14 @@ class WorkersEC2InstancesSettings(BaseCustomSettings):
108110 "a tag must have a key and an optional value. see [https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html]" ,
109111 )
110112
111- @validator ("WORKERS_EC2_INSTANCES_ALLOWED_TYPES" )
113+ @field_validator ("WORKERS_EC2_INSTANCES_ALLOWED_TYPES" )
112114 @classmethod
113115 def check_valid_instance_names (
114116 cls , value : dict [str , EC2InstanceBootSpecific ]
115117 ) -> dict [str , EC2InstanceBootSpecific ]:
116118 # NOTE: needed because of a flaw in BaseCustomSettings
117119 # issubclass raises TypeError if used on Aliases
118- parse_obj_as (list [InstanceTypeType ], list (value ))
120+ TypeAdapter (list [InstanceTypeType ]). validate_python ( list (value ))
119121 return value
120122
121123
@@ -130,7 +132,7 @@ class PrimaryEC2InstancesSettings(BaseCustomSettings):
130132 )
131133 PRIMARY_EC2_INSTANCES_SECURITY_GROUP_IDS : list [str ] = Field (
132134 ...,
133- min_items = 1 ,
135+ min_length = 1 ,
134136 description = "A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic"
135137 " (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-security-groups.html), "
136138 " this is required to start a new EC2 instance" ,
@@ -182,17 +184,17 @@ class PrimaryEC2InstancesSettings(BaseCustomSettings):
182184 "that take longer than this time will be terminated as sometimes it happens that EC2 machine fail on start." ,
183185 )
184186
185- @validator ("PRIMARY_EC2_INSTANCES_ALLOWED_TYPES" )
187+ @field_validator ("PRIMARY_EC2_INSTANCES_ALLOWED_TYPES" )
186188 @classmethod
187189 def check_valid_instance_names (
188190 cls , value : dict [str , EC2InstanceBootSpecific ]
189191 ) -> dict [str , EC2InstanceBootSpecific ]:
190192 # NOTE: needed because of a flaw in BaseCustomSettings
191193 # issubclass raises TypeError if used on Aliases
192- parse_obj_as (list [InstanceTypeType ], list (value ))
194+ TypeAdapter (list [InstanceTypeType ]). validate_python ( list (value ))
193195 return value
194196
195- @validator ("PRIMARY_EC2_INSTANCES_ALLOWED_TYPES" )
197+ @field_validator ("PRIMARY_EC2_INSTANCES_ALLOWED_TYPES" )
196198 @classmethod
197199 def check_only_one_value (
198200 cls , value : dict [str , EC2InstanceBootSpecific ]
@@ -231,45 +233,54 @@ class ApplicationSettings(BaseCustomSettings, MixinLoggingSettings):
231233
232234 # RUNTIME -----------------------------------------------------------
233235 CLUSTERS_KEEPER_DEBUG : bool = Field (
234- default = False , description = "Debug mode" , env = ["CLUSTERS_KEEPER_DEBUG" , "DEBUG" ]
236+ default = False ,
237+ description = "Debug mode" ,
238+ validation_alias = AliasChoices ("CLUSTERS_KEEPER_DEBUG" , "DEBUG" ),
235239 )
236240 CLUSTERS_KEEPER_LOGLEVEL : LogLevel = Field (
237- LogLevel .INFO , env = ["CLUSTERS_KEEPER_LOGLEVEL" , "LOG_LEVEL" , "LOGLEVEL" ]
241+ LogLevel .INFO ,
242+ validation_alias = AliasChoices (
243+ "CLUSTERS_KEEPER_LOGLEVEL" , "LOG_LEVEL" , "LOGLEVEL"
244+ ),
238245 )
239246 CLUSTERS_KEEPER_LOG_FORMAT_LOCAL_DEV_ENABLED : bool = Field (
240247 default = False ,
241- env = [
248+ validation_alias = AliasChoices (
242249 "CLUSTERS_KEEPER_LOG_FORMAT_LOCAL_DEV_ENABLED" ,
243250 "LOG_FORMAT_LOCAL_DEV_ENABLED" ,
244- ] ,
251+ ) ,
245252 description = "Enables local development log format. WARNING: make sure it is disabled if you want to have structured logs!" ,
246253 )
247254
248255 CLUSTERS_KEEPER_EC2_ACCESS : ClustersKeeperEC2Settings | None = Field (
249- auto_default_from_env = True
256+ json_schema_extra = { "auto_default_from_env" : True }
250257 )
251258
252259 CLUSTERS_KEEPER_PRIMARY_EC2_INSTANCES : PrimaryEC2InstancesSettings | None = Field (
253- auto_default_from_env = True
260+ json_schema_extra = { "auto_default_from_env" : True }
254261 )
255262
256263 CLUSTERS_KEEPER_WORKERS_EC2_INSTANCES : WorkersEC2InstancesSettings | None = Field (
257- auto_default_from_env = True
264+ json_schema_extra = { "auto_default_from_env" : True }
258265 )
259266
260267 CLUSTERS_KEEPER_EC2_INSTANCES_PREFIX : str = Field (
261268 ...,
262269 description = "set a prefix to all machines created (useful for testing)" ,
263270 )
264271
265- CLUSTERS_KEEPER_RABBITMQ : RabbitSettings | None = Field (auto_default_from_env = True )
272+ CLUSTERS_KEEPER_RABBITMQ : RabbitSettings | None = Field (
273+ json_schema_extra = {"auto_default_from_env" : True }
274+ )
266275
267276 CLUSTERS_KEEPER_PROMETHEUS_INSTRUMENTATION_ENABLED : bool = True
268277
269- CLUSTERS_KEEPER_REDIS : RedisSettings = Field (auto_default_from_env = True )
278+ CLUSTERS_KEEPER_REDIS : RedisSettings = Field (
279+ json_schema_extra = {"auto_default_from_env" : True }
280+ )
270281
271282 CLUSTERS_KEEPER_REGISTRY : RegistrySettings | None = Field (
272- auto_default_from_env = True
283+ json_schema_extra = { "auto_default_from_env" : True }
273284 )
274285
275286 CLUSTERS_KEEPER_TASK_INTERVAL : datetime .timedelta = Field (
@@ -320,7 +331,7 @@ class ApplicationSettings(BaseCustomSettings, MixinLoggingSettings):
320331 def LOG_LEVEL (self ) -> LogLevel : # noqa: N802
321332 return self .CLUSTERS_KEEPER_LOGLEVEL
322333
323- @validator ("CLUSTERS_KEEPER_LOGLEVEL" )
334+ @field_validator ("CLUSTERS_KEEPER_LOGLEVEL" )
324335 @classmethod
325336 def valid_log_level (cls , value : str ) -> str :
326337 return cls .validate_log_level (value )
0 commit comments