Skip to content

Commit a8b78b2

Browse files
authored
feat: implement environment-variable based config (open-feature#62)
Signed-off-by: Federico Bond <[email protected]>
1 parent 7a7210f commit a8b78b2

File tree

4 files changed

+73
-16
lines changed

4 files changed

+73
-16
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
import typing
3+
4+
5+
def str_to_bool(val: str) -> bool:
6+
return val.lower() == "true"
7+
8+
9+
def env_or_default(env_var, default, cast=None):
10+
val = os.environ.get(env_var)
11+
if val is None:
12+
return default
13+
return val if cast is None else cast(val)
14+
15+
16+
class Config:
17+
def __init__(
18+
self,
19+
host: typing.Optional[str] = None,
20+
port: typing.Optional[str] = None,
21+
tls: typing.Optional[bool] = None,
22+
timeout: typing.Optional[int] = None,
23+
):
24+
self.host = env_or_default("FLAGD_HOST", "localhost") if host is None else host
25+
self.port = (
26+
env_or_default("FLAGD_PORT", 8013, cast=int) if port is None else port
27+
)
28+
self.tls = (
29+
env_or_default("FLAGD_TLS", False, cast=str_to_bool) if tls is None else tls
30+
)
31+
self.timeout = 5 if timeout is None else timeout

providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/defaults.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/provider.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from openfeature.flag_evaluation import FlagEvaluationDetails
4040
from openfeature.provider.provider import AbstractProvider
4141

42-
from .defaults import Defaults
42+
from .config import Config
4343
from .flag_type import FlagType
4444
from .proto.schema.v1 import schema_pb2, schema_pb2_grpc
4545

@@ -54,10 +54,10 @@ class FlagdProvider(AbstractProvider):
5454

5555
def __init__(
5656
self,
57-
host: str = Defaults.HOST,
58-
port: int = Defaults.PORT,
59-
tls: bool = Defaults.TLS,
60-
timeout: int = Defaults.TIMEOUT,
57+
host: typing.Optional[str] = None,
58+
port: typing.Optional[int] = None,
59+
tls: typing.Optional[bool] = None,
60+
timeout: typing.Optional[int] = None,
6161
):
6262
"""
6363
Create an instance of the FlagdProvider
@@ -67,13 +67,15 @@ def __init__(
6767
:param tls: enable/disable secure TLS connectivity
6868
:param timeout: the maximum to wait before a request times out
6969
"""
70-
self.host = host
71-
self.port = port
72-
self.tls = tls
73-
self.timeout = timeout
70+
self.config = Config(
71+
host=host,
72+
port=port,
73+
tls=tls,
74+
timeout=timeout,
75+
)
7476

7577
channel_factory = grpc.secure_channel if tls else grpc.insecure_channel
76-
self.channel = channel_factory(f"{host}:{port}")
78+
self.channel = channel_factory(f"{self.config.host}:{self.config.port}")
7779
self.stub = schema_pb2_grpc.ServiceStub(self.channel)
7880

7981
def shutdown(self):
@@ -131,7 +133,7 @@ def _resolve(
131133
evaluation_context: EvaluationContext,
132134
):
133135
context = self._convert_context(evaluation_context)
134-
call_args = {"timeout": self.timeout}
136+
call_args = {"timeout": self.config.timeout}
135137
try:
136138
if flag_type == FlagType.BOOLEAN:
137139
request = schema_pb2.ResolveBooleanRequest(
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from openfeature.contrib.provider.flagd.config import Config
2+
3+
4+
def test_return_default_values():
5+
config = Config()
6+
assert config.host == "localhost"
7+
assert config.port == 8013
8+
assert config.tls is False
9+
assert config.timeout == 5
10+
11+
12+
def test_overrides_defaults_with_environment(monkeypatch):
13+
monkeypatch.setenv("FLAGD_HOST", "flagd")
14+
monkeypatch.setenv("FLAGD_PORT", "1234")
15+
monkeypatch.setenv("FLAGD_TLS", "true")
16+
17+
config = Config()
18+
assert config.host == "flagd"
19+
assert config.port == 1234
20+
assert config.tls is True
21+
22+
23+
def test_uses_arguments_over_environments_and_defaults(monkeypatch):
24+
monkeypatch.setenv("FLAGD_HOST", "flagd")
25+
26+
config = Config(host="flagd2", port=12345, tls=True)
27+
assert config.host == "flagd2"
28+
assert config.port == 12345
29+
assert config.tls is True

0 commit comments

Comments
 (0)