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
[Task] Update analytics db to use local.env and Remove Dynaconf from Analytics #136
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1a99a71
update config.py
aplybeah 42e2f6e
remove dynaconf settings; add pydantic settings
aplybeah 0a445b5
fix tests
aplybeah 0946598
remove dynaconf references
aplybeah 7d9a5da
lint; update validators
aplybeah bb2908b
Merge branch 'main' into alsia/update-db-conn
aplybeah b1546b5
update package
aplybeah 0fe4fdf
update config
aplybeah 868b50e
update config call
aplybeah 6bec7c2
update call
aplybeah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'd want to use
get_db_settings
here as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chouinar do you mean something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a few other files you do:
You need to do that here as well.
Just doing
DBSettings.user
isn't valid code - a quick test shows that would raise an AttributeError. DBSettings is the class, not an actual settings object.--
Is this bit of code actually running in the tests? It should fail if run as written right now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been testing locally by running the actual make commands which (bizarrely) work. I'm in the process of double checking the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aplybeah any update on the suggested change?