Created
December 3, 2024 22:22
-
-
Save ramannanda9/7bd7778182552e02d44a79a8f3283f51 to your computer and use it in GitHub Desktop.
using environment for build
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
| class EnvironmentConfig(BaseModel): | |
| environment: str = "development" | |
| @classmethod | |
| def from_env(cls, explicit_env: Optional[str] = None): | |
| """ | |
| Create an instance of the config, prioritizing explicitly provided environment | |
| over environment variable, with a default fallback. | |
| """ | |
| # Check for explicitly provided environment | |
| if explicit_env: | |
| return cls(environment=explicit_env) | |
| # Try to read from environment variable | |
| env_from_os = os.getenv('APP_ENVIRONMENT') | |
| if env_from_os: | |
| return cls(environment=env_from_os) | |
| # Return default if no environment specified | |
| return cls() | |
| @field_validator('environment') | |
| @classmethod | |
| def validate_environment(cls, v: str): | |
| """ | |
| Validate the environment value | |
| """ | |
| valid_envs = ['development', 'staging', 'production'] | |
| if v.lower() not in valid_envs: | |
| raise ValueError(f'Invalid environment. Must be one of {valid_envs}') | |
| return v.lower() | |
| # Example usage | |
| class MyApplication: | |
| def __init__(self, config: Optional[EnvironmentConfig] = None): | |
| # If no config provided, create a default one | |
| self.config = config or EnvironmentConfig.from_env() | |
| # Now you can use the validated environment | |
| print(f"Running in {self.config.environment} environment") |
p.s. w.r.t. environment, the contract exposed to users is that:
- they supply environment variables in Leicester via
conduis portal env - those variables will be made available via the
AbstractPipeline.get_app_envmethod
platform environment information such as bootstrap servers, etc, are not made available. I'm open to changing that if needed, though.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For that kind of environment validation, I'd expect it to happen within the framework, for example here
What public API will CFS expose? For example, pipelines need a
get_data_sourcesandprocessmethods, example