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
1 change: 1 addition & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### New Features and Improvements

### Bug Fixes
- Improving the error message that is shown when the unsupported `dbutils.credentials.getServiceCredentialsProvider` method is used. This method can only be used inside of a notebook.

### Documentation

Expand Down
17 changes: 17 additions & 0 deletions databricks/sdk/dbutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,17 @@ def get_local_notebook_path():
return value


def not_supported_method_err_msg(methodName):
return f"Method '{methodName}' is not supported in the SDK version of DBUtils"


class _OverrideProxyUtil:

@classmethod
def new(cls, path: str):
if path in cls.not_supported_override_paths:
raise ValueError(cls.not_supported_override_paths[path])

if len(cls.__get_matching_overrides(path)) > 0:
return _OverrideProxyUtil(path)
return None
Expand All @@ -301,6 +308,16 @@ def __init__(self, name: str):
"notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get()": get_local_notebook_path,
}

# These paths work the same as 'proxy_override_paths' but instead of using a local implementation we raise an exception.
not_supported_override_paths = {
# The object returned by 'credentials.getServiceCredentialProvider()' can't be serialized to JSON.
# Without this override, the command would fail with an error 'TypeError: Object of type Session is not JSON serializable'.
# We override it to show a better error message
"credentials.getServiceCredentialsProvider": not_supported_method_err_msg(
"credentials.getServiceCredentialsProvider"
),
}

@classmethod
def __get_matching_overrides(cls, path: str):
return [x for x in cls.proxy_override_paths.keys() if x.startswith(path)]
Expand Down
10 changes: 10 additions & 0 deletions tests/test_dbutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ def test_dbutils_proxy_overrides(dbutils, mocker, restorable_env):
assert dbutils.notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get() == "test_source_file"


@raises("Method 'credentials.getServiceCredentialsProvider' is not supported in the SDK version of DBUtils")
def test_dbutils_credentials_get_service_credential_provider(config, mocker):
from databricks.sdk.dbutils import RemoteDbUtils

config.cluster_id = "test_cluster_id"
dbutils = RemoteDbUtils(config)

dbutils.credentials.getServiceCredentialsProvider("creds")


def test_dbutils_adds_user_agent(config):
from databricks.sdk.dbutils import RemoteDbUtils

Expand Down
Loading