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 .nextchanges/bundles/6553.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Added PyDABs (Python) support for secrets: `Resources.add_secret` and the `secret_mutator` decorator. ([#6553](https://github.com/databricks/cli/pull/6553))
23 changes: 23 additions & 0 deletions acceptance/bundle/python/secrets-support/databricks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
bundle:
name: my_project

sync: {paths: []} # don't need to copy files

variables:
secret_value:
description: The value of the secret

python:
resources:
- "resources:load_resources"
mutators:
- "mutators:update_secret"

resources:
secrets:
my_secret_1:
catalog_name: main
schema_name: default
name: my_secret_1
value: ${var.secret_value}
expire_time: "2030-01-01T00:00:00Z"
11 changes: 11 additions & 0 deletions acceptance/bundle/python/secrets-support/mutators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from dataclasses import replace

from databricks.bundles.secrets import Secret
from databricks.bundles.core import secret_mutator


@secret_mutator
def update_secret(secret: Secret) -> Secret:
assert isinstance(secret.name, str)

return replace(secret, name=f"{secret.name} (updated)")
4 changes: 4 additions & 0 deletions acceptance/bundle/python/secrets-support/out.test.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions acceptance/bundle/python/secrets-support/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

>>> uv run [UV_ARGS] -q [CLI] bundle validate --var secret_value=my-secret-value --output json
{
"experimental": {
"python": {
"mutators": [
"mutators:update_secret"
],
"resources": [
"resources:load_resources"
]
}
},
"resources": {
"secrets": {
"my_secret_1": {
"catalog_name": "main",
"expire_time": "[TIMESTAMP]",
"name": "my_secret_1 (updated)",
"schema_name": "default",
"value": "[redacted]"
},
"my_secret_2": {
"catalog_name": "main",
"expire_time": "[TIMESTAMP]",
"name": "my_secret_2 (updated)",
"schema_name": "default",
"value": "[redacted]"
}
}
}
}
18 changes: 18 additions & 0 deletions acceptance/bundle/python/secrets-support/resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from databricks.bundles.core import Resources


def load_resources() -> Resources:
resources = Resources()

resources.add_secret(
"my_secret_2",
{
"catalog_name": "main",
"schema_name": "default",
"name": "my_secret_2",
"value": "${var.secret_value}",
"expire_time": "2030-01-01T00:00:00Z",
},
)

return resources
6 changes: 6 additions & 0 deletions acceptance/bundle/python/secrets-support/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

# secrets require value to be a variable reference, so supply it at validate time.
trace uv run $UV_ARGS -q $CLI bundle validate --var secret_value=my-secret-value --output json | \
jq "pick(.experimental.python, .resources)"

rm -fr .databricks __pycache__
7 changes: 7 additions & 0 deletions acceptance/bundle/python/secrets-support/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Cloud = false # tests don't interact with APIs

# secrets are only supported in the current version of the wheel
EnvMatrix.PYDAB_VERSION = ["current"]

# secrets are only supported on the direct deployment engine
EnvMatrix.DATABRICKS_BUNDLE_ENGINE = ["direct"]
5 changes: 4 additions & 1 deletion python/codegen/codegen/generated_test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _is_composite(ref: str) -> bool:


def _synth_scalar(name: str, hint: str) -> _Scalar:
"""Placeholder value for a primitive (str -> hint, int -> 0, float -> 0.0, bool -> True).
"""Placeholder value for a primitive (str -> hint, int -> 0, float -> 0.0, bool -> True, time.Time -> RFC3339 string).

:param name: the primitive's schema name, e.g. "string", "int", "boolean".
:param hint: enclosing field name, used as the string placeholder so examples read meaningfully.
Expand All @@ -117,6 +117,9 @@ def _synth_scalar(name: str, hint: str) -> _Scalar:
return _Scalar("0.0", "0.0")
if name in ("boolean", "bool"):
return _Scalar("True", "True")
# time.Time is generated as a str (see packages.RENAMES); serialized as RFC3339.
if name == "time.Time":
return _Scalar('"2020-01-01T00:00:00Z"', '"2020-01-01T00:00:00Z"')

raise ValueError(f"Unknown primitive: {name}")

Expand Down
10 changes: 9 additions & 1 deletion python/codegen/codegen/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"resources.ClusterPolicy", # interface{}
"resources.Dashboard", # interface{}
"resources.GenieSpace", # interface{}
"resources.Secret", # time.Time
}

# Only GA and public-preview resources are generated; later stages may still change.
Expand Down Expand Up @@ -63,6 +62,9 @@ def _load_resource_namespace() -> dict[str, str]:
RESOURCE_TYPES = list(RESOURCE_NAMESPACE.keys())

RENAMES = {
# time.Time is a scalar serialized as an RFC3339 string; the Go side models
# it as a string too (see libs/dyn/convert/sdk_native_types.go).
"time.Time": "str",
"string": "str",
"boolean": "bool",
"integer": "int",
Expand All @@ -72,6 +74,8 @@ def _load_resource_namespace() -> dict[str, str]:
}

PRIMITIVES = [
# Treated as str
"time.Time",
"string",
"boolean",
"integer",
Expand All @@ -85,6 +89,10 @@ def _load_resource_namespace() -> dict[str, str]:

def get_class_name(ref: str) -> str:
name = ref.split("/")[-1]

if name in RENAMES:
return RENAMES[name]

name = name.split(".")[-1]

return RENAMES.get(name, name)
Expand Down
1 change: 1 addition & 0 deletions python/codegen/codegen_tests/test_generated_test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def test_is_composite():
("float64", _Scalar("0.0", "0.0")),
("boolean", _Scalar("True", "True")),
("bool", _Scalar("True", "True")),
("time.Time", _Scalar('"2020-01-01T00:00:00Z"', '"2020-01-01T00:00:00Z"')),
],
)
def test_synth_scalar(name, expected):
Expand Down
2 changes: 2 additions & 0 deletions python/databricks/bundles/core/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions python/databricks/bundles/core/_generated/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 115 additions & 0 deletions python/databricks/bundles/core/_generated/secrets.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions python/databricks/bundles/secrets/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading