Skip to content

Commit

Permalink
Updates common settings
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Jun 27, 2021
1 parent 728e8f9 commit 98262b5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 68 deletions.
50 changes: 21 additions & 29 deletions packages/settings-library/src/settings_library/rabbit.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,38 @@
from typing import Dict, Optional
from functools import cached_property
from typing import Dict

from pydantic import BaseSettings, Extra, validator
from pydantic.networks import AnyUrl
from pydantic.types import PositiveInt, SecretStr
from pydantic.types import SecretStr

from .base import BaseCustomSettings
from .basic_types import PortInt


class RabbitDsn(AnyUrl):
allowed_schemes = {"amqp"}


class RabbitConfig(BaseSettings):
class RabbitConfig(BaseCustomSettings):
# host
host: str = "rabbit"
port: PositiveInt = 5672
RABBIT_HOST: str = "rabbit"
RABBIT_PORT: PortInt = 5672

# auth
user: str = "simcore"
password: SecretStr = SecretStr("simcore")

dsn: Optional[RabbitDsn] = None
RABBIT_USER: str = "simcore"
RABBIT_PASSWORD: SecretStr = SecretStr("simcore")

# channels
channels: Dict[str, str] = {
RABBIT_CHANNELS: Dict[str, str] = {
"log": "comp.backend.channels.log",
"instrumentation": "comp.backend.channels.instrumentation",
}

@validator("dsn", pre=True)
@classmethod
def autofill_dsn(cls, v, values):
if not v and all(
key in values for key in cls.__fields__ if key not in ["dsn", "channels"]
):
return RabbitDsn.build(
scheme="amqp",
user=values["user"],
password=values["password"].get_secret_value(),
host=values["host"],
port=f"{values['port']}",
)
return v

class Config:
env_prefix = "RABBIT_"
extra = Extra.forbid
@cached_property
def dsn(self) -> str:
return RabbitDsn.build(
scheme="amqp",
user=self.RABBIT_USER,
password=self.RABBIT_PASSWORD.get_secret_value(),
host=self.RABBIT_HOST,
port=f"{self.RABBIT_PORT}",
)
51 changes: 23 additions & 28 deletions packages/settings-library/src/settings_library/redis.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
from functools import cached_property
from typing import Optional

from pydantic import BaseSettings, PositiveInt, validator
from pydantic.networks import RedisDsn
from pydantic.types import SecretStr

from .base import BaseCustomSettings
from .basic_types import PortInt

class RedisConfig(BaseSettings):

class RedisConfig(BaseCustomSettings):
# host
host: str = "redis"
port: PositiveInt = 6789
REDIS_HOST: str = "redis"
REDIS_PORT: PortInt = 6789

# auth
user: Optional[str] = None
password: Optional[SecretStr] = None
REDIS_USER: Optional[str] = None
REDIS_PASSWORD: Optional[SecretStr] = None

# db
db: Optional[str] = "0"

dsn: Optional[RedisDsn] = None

@validator("dsn", pre=True)
@classmethod
def autofill_dsn(cls, v, values):
if not v and all(key in values for key in cls.__fields__ if key != "dsn"):
return RedisDsn.build(
scheme="redis",
user=values["user"] or None,
password=values["password"].get_secret_value()
if values["password"]
else None,
host=values["host"],
port=f"{values['port']}",
path=f"/{values['db']}",
)
return v

class Config:
env_prefix = "REDIS_"
REDIS_DB: Optional[str] = "0"

@cached_property
def dsn(self) -> str:
return RedisDsn.build(
scheme="redis",
user=self.REDIS_USER or None,
password=self.REDIS_PASSWORD.get_secret_value()
if self.REDIS_PASSWORD
else None,
host=self.REDIS_HOST,
port=f"{self.REDIS_PORT}",
path=f"/{self.REDIS_DB}",
)
18 changes: 7 additions & 11 deletions packages/settings-library/src/settings_library/s3.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
from pydantic import BaseSettings
from .base import BaseCustomSettings


class S3Config(BaseSettings):
endpoint: str = "minio:9000"
access_key: str = "12345678"
secret_key: str = "12345678"
bucket_name: str = "simcore"
secure: bool = False

class Config:
case_sensitive = False
env_prefix = "S3_"
class S3Config(BaseCustomSettings):
S3_ENDPOINT: str = "minio:9000"
S3_ACCESS_KEY: str = "12345678"
S3_SECRET_KEY: str = "12345678"
S3_BUCKET_NAME: str = "simcore"
S3_SECURE: bool = False
3 changes: 3 additions & 0 deletions packages/settings-library/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,8 @@ def _get_all_settings_classes():
"settings_cls", _get_all_settings_classes(), ids=lambda cls: cls.__name__
)
def test_settings_class_policies(settings_cls):
# must inherit
assert issubclass(settings_cls, BaseCustomSettings)

# all fields UPPER
assert all(key == key.upper() for key in settings_cls.__fields__.keys())

0 comments on commit 98262b5

Please sign in to comment.