This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
forked from HHS/simpler-grants-gov
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Task] Update analytics db to use local.env and Remove Dynaconf from …
…Analytics (#136) ## Summary Fixes #107 Fixes #115 ### Time to review: __5 mins__ ## Changes proposed * removed the `*.toml` files related to dynaconf * removed references to Dynaconf (e.g. in Docstrings, gitignore) * use Pydantic for loading ## Context for reviewers > After getting feedback for #107, the consensus was to reevaluate the way the database loader works for more uniformity. Changes will need to be made primarily in db.py and cli.py > > With the PR #84 , the env settings for the db are stored in settings.toml. The config settings should be updated to use the existing local.env file ## Additional information > Screenshots, GIF demos, code examples or output to help show the changes working as expected.
- Loading branch information
Showing
9 changed files
with
81 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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,4 +1,2 @@ | ||
data | ||
|
||
# Ignore dynaconf secret files | ||
.secrets.* |
This file contains 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,35 +1,23 @@ | ||
"""Loads configuration variables from settings files and settings files | ||
"""Loads configuration variables from settings files | ||
Dynaconf provides a few valuable features for configuration management: | ||
- Load variables from env vars and files with predictable overrides | ||
- Validate the existence and format of required configs | ||
- Connect with secrets managers like HashiCorp's Vault server | ||
- Load different configs based on environment (e.g. DEV, PROD, STAGING) | ||
For more information visit: https://www.dynaconf.com/ | ||
""" | ||
import os | ||
from typing import Optional | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
from pydantic import Field | ||
|
||
from dynaconf import Dynaconf, Validator, ValidationError | ||
# reads environment variables from .env files defaulting to "local.env" | ||
class PydanticBaseEnvConfig(BaseSettings): | ||
model_config = SettingsConfigDict(env_file="%s.env" % os.getenv("ENVIRONMENT", "local"), extra="ignore") # set extra to ignore so that it ignores variables irrelevant to the database config (e.g. metabase settings) | ||
|
||
settings = Dynaconf( | ||
# set env vars with `export ANALYTICS_FOO=bar` | ||
envvar_prefix="ANALYTICS", | ||
# looks for config vars in the following files | ||
# with vars in .secrets.toml overriding vars in settings.toml | ||
settings_files=["settings.toml", ".secrets.toml"], | ||
# merge the settings found in all files | ||
merge_enabled= True, | ||
# add validators for our required config vars | ||
validators=[ | ||
Validator("SLACK_BOT_TOKEN", must_exist=True), | ||
Validator("REPORTING_CHANNEL_ID", must_exist=True), | ||
], | ||
) | ||
class DBSettings(PydanticBaseEnvConfig): | ||
db_host: str = Field(alias="DB_HOST") | ||
port: int = Field(5432,alias="DB_PORT") | ||
user: str = Field (alias="DB_USER") | ||
password: str = Field(alias="DB_PASSWORD") | ||
ssl_mode: str = Field(alias="DB_SSL_MODE") | ||
slack_bot_token: str = Field(alias="ANALYTICS_SLACK_BOT_TOKEN") | ||
reporting_channel_id: str = Field(alias="ANALYTICS_REPORTING_CHANNEL_ID") | ||
|
||
# raises after all possible errors are evaluated | ||
try: | ||
settings.validators.validate_all() | ||
except ValidationError as error: | ||
list_of_all_errors = error.details | ||
print(list_of_all_errors) | ||
raise | ||
def get_db_settings() -> DBSettings: | ||
return DBSettings() |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 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 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