Skip to content

Commit 04d744a

Browse files
Add support for generate for alerts (#4108)
## Changes Adds support for `databricks bundle generate alert` command to generate bundle configuration from existing SQL alerts. ## Why This allows users to import existing alerts into bundles ## Usage ### Generate configuration from existing alert ```bash databricks bundle generate alert --existing-id <alert-id> ``` This creates: - `resources/<alert-name>.alert.yml` - Bundle configuration with alert settings - `src/<alert-name>.dbalert.json` - Complete alert definition ### Update existing bundle resource ```bash databricks bundle generate alert --resource my_alert --force ``` ## Tests - Added acceptance tests for successful generation and error cases Note: It should be merged after: #3602
1 parent c10fd97 commit 04d744a

File tree

16 files changed

+292
-0
lines changed

16 files changed

+292
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"display_name": "test alert",
3+
"parent_path": "/Workspace/test-$UNIQUE_NAME",
4+
"query_text": "SELECT 1\n as value",
5+
"warehouse_id": "$TEST_DEFAULT_WAREHOUSE_ID",
6+
"evaluation": {
7+
"comparison_operator": "GREATER_THAN",
8+
"source": {
9+
"name": "value"
10+
},
11+
"threshold": {
12+
"value": {
13+
"double_value": 0.0
14+
}
15+
}
16+
},
17+
"schedule": {
18+
"quartz_cron_schedule": "0 0 * * * ?",
19+
"timezone_id": "UTC"
20+
}
21+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bundle:
2+
name: alert-generate

acceptance/bundle/generate/alert/out.test.toml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"evaluation": {
3+
"source": {
4+
"name": "value"
5+
},
6+
"comparison_operator": "GREATER_THAN",
7+
"threshold": {
8+
"value": {
9+
"double_value": 0.0
10+
}
11+
},
12+
"notification": {}
13+
},
14+
"schedule": {
15+
"quartz_cron_schedule": "0 0 * * * ?",
16+
"timezone_id": "UTC"
17+
},
18+
"query_lines": [
19+
"SELECT 1",
20+
" as value"
21+
]
22+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
resources:
2+
alerts:
3+
test_alert:
4+
display_name: "test alert"
5+
warehouse_id: [TEST_DEFAULT_WAREHOUSE_ID]
6+
file_path: ../alert/test_alert.dbalert.json
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
>>> [CLI] workspace mkdirs /Workspace/test-[UNIQUE_NAME]
3+
4+
>>> [CLI] bundle generate alert --existing-id [NUMID] --source-dir out/alert --config-dir out/resource
5+
Alert configuration successfully saved to [TEST_TMP_DIR]/out/resource/test_alert.alert.yml
6+
Serialized alert definition to [TEST_TMP_DIR]/out/alert/test_alert.dbalert.json
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
trace $CLI workspace mkdirs /Workspace/test-$UNIQUE_NAME
2+
3+
# create an alert to import
4+
envsubst < alert.json.tmpl > alert.json
5+
alert_id=$($CLI alerts-v2 create-alert --json @alert.json | jq -r '.id')
6+
rm alert.json
7+
8+
trace $CLI bundle generate alert --existing-id $alert_id --source-dir out/alert --config-dir out/resource
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Cloud = true
2+
Local = false
3+
4+
[Env]
5+
# MSYS2 automatically converts absolute paths like /Users/$username/$UNIQUE_NAME to
6+
# C:/Program Files/Git/Users/$username/UNIQUE_NAME before passing it to the CLI
7+
# Setting this environment variable prevents that conversion on windows.
8+
MSYS_NO_PATHCONV = "1"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bundle:
2+
name: test-alert-existing-id-not-found

acceptance/bundle/generate/alert_existing_id_not_found/out.test.toml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)