Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/posit/workbench/external/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,30 @@ def text():
"""

def __init__(self, config: Optional[Config] = None):
self._config = config or Config(profile="workbench")
self.__config = config

def auth_type(self) -> str:
return POSIT_WORKBENCH_AUTH_TYPE

@property
def _config(self) -> Config:
"""The Databricks SDK `Config` object used by this strategy.

Returns
-------
Config
The provided `Config` object, defaulting to a new `Config` with the profile set to "workbench" if not provided.
"""
if self.__config is None:
# Do not create this configuration object until it is needed.
# This avoids failing if the 'workbench' profile is not defined in the user's
# `~/.databrickscfg` file until this strategy is actually used.
self.__config = Config(profile="workbench")

return self.__config

def __call__(self, *args, **kwargs) -> CredentialsProvider: # noqa: ARG002
if self._config.token is None:
raise ValueError("Missing value for field 'token' in Config.")

def cp():
return {"Authorization": f"Bearer {self._config.token}"}

return cp
return lambda: {"Authorization": f"Bearer {self._config.token}"}
9 changes: 6 additions & 3 deletions tests/posit/workbench/external/test_databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@


class TestPositCredentialsHelpers:
def test_workbench_strategy(self):
# default will attempt to load the workbench profile
def test_default_workbench_strategy(self):
# By default, the WorkbenchStrategy should use "workbench" profile.
strategy = WorkbenchStrategy()

with pytest.raises(ValueError, match="profile=workbench"):
WorkbenchStrategy()
strategy()

def test_workbench_strategy(self):
# providing a Config is allowed
cs = WorkbenchStrategy(
config=Config(host="https://databricks.com/workspace", token="token") # pyright: ignore[reportPossiblyUnboundVariable]
Expand Down