Skip to content

Commit 47857a6

Browse files
authored
Added support for subscriptions in dashboards & alert SQL tasks in databricks_job (#2447)
* Add ability to subscribe to dashboards & alert SQL tasks in `databricks_job` It's now possible to subscribe to updates of individual dashboards & alerts in the SQL tasks of Databricks Workflows This fixes #2436 * Add unit test
1 parent b83a8c0 commit 47857a6

File tree

3 files changed

+175
-4
lines changed

3 files changed

+175
-4
lines changed

docs/resources/job.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,15 @@ One of the `query`, `dashboard` or `alert` needs to be provided.
254254
* `warehouse_id` - (Required) ID of the (the [databricks_sql_endpoint](sql_endpoint.md)) that will be used to execute the task. Only Serverless & Pro warehouses are supported right now.
255255
* `parameters` - (Optional) (Map) parameters to be used for each run of this task. The SQL alert task does not support custom parameters.
256256
* `query` - (Optional) block consisting of single string field: `query_id` - identifier of the Databricks SQL Query ([databricks_sql_query](sql_query.md)).
257-
* `dashboard` - (Optional) block consisting of single string field: `dashboard_id` - identifier of the Databricks SQL Dashboard [databricks_sql_dashboard](sql_dashboard.md).
258-
* `alert` - (Optional) block consisting of single string field: `alert_id` - identifier of the Databricks SQL Alert.
257+
* `dashboard` - (Optional) block consisting of following fields:
258+
* `dashboard_id` - (Required) (String) identifier of the Databricks SQL Dashboard [databricks_sql_dashboard](sql_dashboard.md).
259+
* `subscriptions` - (Optional) a list of subscription blocks consisting out of one of the required fields: `user_name` for user emails or `destination_id` - for Alert destination's identifier.
260+
* `custom_subject` - (Optional) string specifying a custom subject of email sent.
261+
* `pause_subscriptions` - (Optional) flag that specifies if subscriptions are paused or not.
262+
* `alert` - (Optional) block consisting of following fields:
263+
* `alert_id` - (Required) (String) identifier of the Databricks SQL Alert.
264+
* `subscriptions` - (Required) a list of subscription blocks consisting out of one of the required fields: `user_name` for user emails or `destination_id` - for Alert destination's identifier.
265+
* `pause_subscriptions` - (Optional) flag that specifies if subscriptions are paused or not.
259266
* `file` - (Optional) block consisting of single string field: `path` - a relative path to the file (inside the Git repository) with SQL commands to execute. *Requires `git_source` configuration block*.
260267

261268
Example
@@ -272,6 +279,30 @@ resource "databricks_job" "sql_aggregation_job" {
272279
}
273280
}
274281
}
282+
task {
283+
task_key = "run_dashboard"
284+
sql_task {
285+
warehouse_id = databricks_sql_endpoint.sql_job_warehouse.id
286+
dashboard {
287+
dashboard_id = databricks_sql_dashboard.dash.id
288+
subscriptions {
289+
user_name = "[email protected]"
290+
}
291+
}
292+
}
293+
}
294+
task {
295+
task_key = "run_alert"
296+
sql_task {
297+
warehouse_id = databricks_sql_endpoint.sql_job_warehouse.id
298+
alert {
299+
alert_id = databricks_sql_alert.alert.id
300+
subscriptions {
301+
user_name = "[email protected]"
302+
}
303+
}
304+
}
305+
}
275306
}
276307
```
277308

jobs/resource_job.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,22 @@ type SqlQueryTask struct {
6666
QueryID string `json:"query_id"`
6767
}
6868

69+
type SqlSubscription struct {
70+
UserName string `json:"user_name,omitempty"`
71+
DestinationID string `json:"destination_id,omitempty"`
72+
}
73+
6974
type SqlDashboardTask struct {
70-
DashboardID string `json:"dashboard_id"`
75+
DashboardID string `json:"dashboard_id"`
76+
Subscriptions []SqlSubscription `json:"subscriptions,omitempty"`
77+
CustomSubject string `json:"custom_subject,omitempty"`
78+
PauseSubscriptions bool `json:"pause_subscriptions,omitempty"`
7179
}
7280

7381
type SqlAlertTask struct {
74-
AlertID string `json:"alert_id"`
82+
AlertID string `json:"alert_id"`
83+
Subscriptions []SqlSubscription `json:"subscriptions"`
84+
PauseSubscriptions bool `json:"pause_subscriptions,omitempty"`
7585
}
7686

7787
type SqlFileTask struct {

jobs/resource_job_test.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,136 @@ func TestResourceJobCreate_JobCompute(t *testing.T) {
405405
assert.Equal(t, "18", d.Id())
406406
}
407407

408+
func TestResourceJobCreate_SqlSubscriptions(t *testing.T) {
409+
d, err := qa.ResourceFixture{
410+
Fixtures: []qa.HTTPFixture{
411+
{
412+
Method: "POST",
413+
Resource: "/api/2.1/jobs/create",
414+
ExpectedRequest: JobSettings{
415+
Name: "TF SQL task subscriptions",
416+
MaxConcurrentRuns: 1,
417+
Tasks: []JobTaskSettings{
418+
{
419+
TaskKey: "a",
420+
SqlTask: &SqlTask{
421+
WarehouseID: "dca3a0ba199040eb",
422+
Alert: &SqlAlertTask{
423+
AlertID: "3cf91a42-6217-4f3c-a6f0-345d489051b9",
424+
Subscriptions: []SqlSubscription{
425+
{UserName: "[email protected]"},
426+
{DestinationID: "Test"},
427+
},
428+
PauseSubscriptions: true,
429+
},
430+
},
431+
},
432+
{
433+
TaskKey: "d",
434+
SqlTask: &SqlTask{
435+
WarehouseID: "dca3a0ba199040eb",
436+
Dashboard: &SqlDashboardTask{
437+
DashboardID: "d81a7760-7fd2-443e-bf41-95a60c2f4c7c",
438+
Subscriptions: []SqlSubscription{
439+
{UserName: "[email protected]"},
440+
{DestinationID: "Test"},
441+
},
442+
CustomSubject: "test",
443+
},
444+
},
445+
},
446+
},
447+
},
448+
Response: Job{
449+
JobID: 789,
450+
},
451+
},
452+
{
453+
Method: "GET",
454+
Resource: "/api/2.1/jobs/get?job_id=789",
455+
Response: Job{
456+
JobID: 789,
457+
Settings: &JobSettings{
458+
Name: "TF SQL task subscriptions",
459+
Tasks: []JobTaskSettings{
460+
{
461+
TaskKey: "a",
462+
SqlTask: &SqlTask{
463+
WarehouseID: "dca3a0ba199040eb",
464+
Alert: &SqlAlertTask{
465+
AlertID: "3cf91a42-6217-4f3c-a6f0-345d489051b9",
466+
Subscriptions: []SqlSubscription{
467+
{UserName: "[email protected]"},
468+
{DestinationID: "Test"},
469+
},
470+
PauseSubscriptions: true,
471+
},
472+
},
473+
},
474+
{
475+
TaskKey: "d",
476+
SqlTask: &SqlTask{
477+
WarehouseID: "dca3a0ba199040eb",
478+
Dashboard: &SqlDashboardTask{
479+
DashboardID: "d81a7760-7fd2-443e-bf41-95a60c2f4c7c",
480+
Subscriptions: []SqlSubscription{
481+
{UserName: "[email protected]"},
482+
{DestinationID: "Test"},
483+
},
484+
CustomSubject: "test",
485+
},
486+
},
487+
},
488+
},
489+
},
490+
},
491+
},
492+
},
493+
Create: true,
494+
Resource: ResourceJob(),
495+
HCL: `name = "TF SQL task subscriptions"
496+
497+
task {
498+
task_key = "a"
499+
500+
sql_task {
501+
warehouse_id = "dca3a0ba199040eb"
502+
alert {
503+
subscriptions {
504+
user_name = "[email protected]"
505+
}
506+
subscriptions {
507+
destination_id = "Test"
508+
}
509+
pause_subscriptions = true
510+
alert_id = "3cf91a42-6217-4f3c-a6f0-345d489051b9"
511+
}
512+
}
513+
}
514+
515+
task {
516+
task_key = "d"
517+
518+
sql_task {
519+
warehouse_id = "dca3a0ba199040eb"
520+
dashboard {
521+
subscriptions {
522+
user_name = "[email protected]"
523+
}
524+
subscriptions {
525+
destination_id = "Test"
526+
}
527+
pause_subscriptions = false
528+
dashboard_id = "d81a7760-7fd2-443e-bf41-95a60c2f4c7c"
529+
custom_subject = "test"
530+
}
531+
}
532+
}`,
533+
}.Apply(t)
534+
assert.NoError(t, err)
535+
assert.Equal(t, "789", d.Id())
536+
}
537+
408538
func TestResourceJobCreate_AlwaysRunning(t *testing.T) {
409539
d, err := qa.ResourceFixture{
410540
Fixtures: []qa.HTTPFixture{

0 commit comments

Comments
 (0)