Skip to content

Commit

Permalink
set api key via env var (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
edublancas authored Nov 17, 2023
1 parent b7702e5 commit 4effa85
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 0.2.16dev

* [Feature] Allow setting API key via the `PLOOMBER_CLOUD_KEY` environment variable

## 0.2.15 (2023-10-25)

* [Fix] Removes `started` telemetry event
Expand Down
11 changes: 11 additions & 0 deletions src/ploomber_core/telemetry/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class UserSettings(Config):
"""User-customizable settings"""

version_check_enabled: bool = True

# important: do not get the key like this! use UserSettings().get_cloud_key()
cloud_key: str = None
user_email: str = None
stats_enabled: bool = True
Expand All @@ -84,6 +86,15 @@ def path(cls):
location = location / CONF_DIR
return location / DEFAULT_USER_CONF

def get_cloud_key(self):
"""Get the cloud key. Returns the value in PLOOMBER_CLOUD_KEY if set,
otherwise returns the value in the config file
"""
if "PLOOMBER_CLOUD_KEY" in os.environ:
return os.environ["PLOOMBER_CLOUD_KEY"]
else:
return self.cloud_key


class Internal(Config):
"""
Expand Down
30 changes: 30 additions & 0 deletions tests/telemetry/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,36 @@ def test_user_settings_create_file(tmp_directory, monkeypatch):
assert settings.stats_enabled


def test_user_settings_get_cloud_key_from_file(tmp_directory, monkeypatch):
monkeypatch.setattr(telemetry, "DEFAULT_HOME_DIR", str(Path().absolute()))
parent = Path("stats")
parent.mkdir()
(parent / "config.yaml").write_text(
"""
cloud_key: some-cloud-key
"""
)
settings = telemetry.UserSettings()

assert settings.get_cloud_key() == "some-cloud-key"


def test_user_settings_get_cloud_key_from_env_var(tmp_directory, monkeypatch):
monkeypatch.setattr(telemetry, "DEFAULT_HOME_DIR", str(Path().absolute()))
monkeypatch.setenv("PLOOMBER_CLOUD_KEY", "another-cloud-key")

parent = Path("stats")
parent.mkdir()
(parent / "config.yaml").write_text(
"""
cloud_key: some-cloud-key
"""
)
settings = telemetry.UserSettings()

assert settings.get_cloud_key() == "another-cloud-key"


def test_internal_create_file(tmp_directory, monkeypatch):
monkeypatch.setattr(telemetry, "DEFAULT_HOME_DIR", str(Path().absolute()))
monkeypatch.setattr(telemetry, "uuid4", lambda: "some-unique-uuid")
Expand Down

0 comments on commit 4effa85

Please sign in to comment.