Skip to content

Commit

Permalink
Added support for subscriptions in dashboards & alert SQL tasks in `d…
Browse files Browse the repository at this point in the history
…atabricks_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
  • Loading branch information
alexott authored Jul 6, 2023
1 parent b83a8c0 commit 47857a6
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 4 deletions.
35 changes: 33 additions & 2 deletions docs/resources/job.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,15 @@ One of the `query`, `dashboard` or `alert` needs to be provided.
* `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.
* `parameters` - (Optional) (Map) parameters to be used for each run of this task. The SQL alert task does not support custom parameters.
* `query` - (Optional) block consisting of single string field: `query_id` - identifier of the Databricks SQL Query ([databricks_sql_query](sql_query.md)).
* `dashboard` - (Optional) block consisting of single string field: `dashboard_id` - identifier of the Databricks SQL Dashboard [databricks_sql_dashboard](sql_dashboard.md).
* `alert` - (Optional) block consisting of single string field: `alert_id` - identifier of the Databricks SQL Alert.
* `dashboard` - (Optional) block consisting of following fields:
* `dashboard_id` - (Required) (String) identifier of the Databricks SQL Dashboard [databricks_sql_dashboard](sql_dashboard.md).
* `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.
* `custom_subject` - (Optional) string specifying a custom subject of email sent.
* `pause_subscriptions` - (Optional) flag that specifies if subscriptions are paused or not.
* `alert` - (Optional) block consisting of following fields:
* `alert_id` - (Required) (String) identifier of the Databricks SQL Alert.
* `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.
* `pause_subscriptions` - (Optional) flag that specifies if subscriptions are paused or not.
* `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*.

Example
Expand All @@ -272,6 +279,30 @@ resource "databricks_job" "sql_aggregation_job" {
}
}
}
task {
task_key = "run_dashboard"
sql_task {
warehouse_id = databricks_sql_endpoint.sql_job_warehouse.id
dashboard {
dashboard_id = databricks_sql_dashboard.dash.id
subscriptions {
user_name = "user@domain.com"
}
}
}
}
task {
task_key = "run_alert"
sql_task {
warehouse_id = databricks_sql_endpoint.sql_job_warehouse.id
alert {
alert_id = databricks_sql_alert.alert.id
subscriptions {
user_name = "user@domain.com"
}
}
}
}
}
```

Expand Down
14 changes: 12 additions & 2 deletions jobs/resource_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,22 @@ type SqlQueryTask struct {
QueryID string `json:"query_id"`
}

type SqlSubscription struct {
UserName string `json:"user_name,omitempty"`
DestinationID string `json:"destination_id,omitempty"`
}

type SqlDashboardTask struct {
DashboardID string `json:"dashboard_id"`
DashboardID string `json:"dashboard_id"`
Subscriptions []SqlSubscription `json:"subscriptions,omitempty"`
CustomSubject string `json:"custom_subject,omitempty"`
PauseSubscriptions bool `json:"pause_subscriptions,omitempty"`
}

type SqlAlertTask struct {
AlertID string `json:"alert_id"`
AlertID string `json:"alert_id"`
Subscriptions []SqlSubscription `json:"subscriptions"`
PauseSubscriptions bool `json:"pause_subscriptions,omitempty"`
}

type SqlFileTask struct {
Expand Down
130 changes: 130 additions & 0 deletions jobs/resource_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,136 @@ func TestResourceJobCreate_JobCompute(t *testing.T) {
assert.Equal(t, "18", d.Id())
}

func TestResourceJobCreate_SqlSubscriptions(t *testing.T) {
d, err := qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
{
Method: "POST",
Resource: "/api/2.1/jobs/create",
ExpectedRequest: JobSettings{
Name: "TF SQL task subscriptions",
MaxConcurrentRuns: 1,
Tasks: []JobTaskSettings{
{
TaskKey: "a",
SqlTask: &SqlTask{
WarehouseID: "dca3a0ba199040eb",
Alert: &SqlAlertTask{
AlertID: "3cf91a42-6217-4f3c-a6f0-345d489051b9",
Subscriptions: []SqlSubscription{
{UserName: "user@domain.com"},
{DestinationID: "Test"},
},
PauseSubscriptions: true,
},
},
},
{
TaskKey: "d",
SqlTask: &SqlTask{
WarehouseID: "dca3a0ba199040eb",
Dashboard: &SqlDashboardTask{
DashboardID: "d81a7760-7fd2-443e-bf41-95a60c2f4c7c",
Subscriptions: []SqlSubscription{
{UserName: "user@domain.com"},
{DestinationID: "Test"},
},
CustomSubject: "test",
},
},
},
},
},
Response: Job{
JobID: 789,
},
},
{
Method: "GET",
Resource: "/api/2.1/jobs/get?job_id=789",
Response: Job{
JobID: 789,
Settings: &JobSettings{
Name: "TF SQL task subscriptions",
Tasks: []JobTaskSettings{
{
TaskKey: "a",
SqlTask: &SqlTask{
WarehouseID: "dca3a0ba199040eb",
Alert: &SqlAlertTask{
AlertID: "3cf91a42-6217-4f3c-a6f0-345d489051b9",
Subscriptions: []SqlSubscription{
{UserName: "user@domain.com"},
{DestinationID: "Test"},
},
PauseSubscriptions: true,
},
},
},
{
TaskKey: "d",
SqlTask: &SqlTask{
WarehouseID: "dca3a0ba199040eb",
Dashboard: &SqlDashboardTask{
DashboardID: "d81a7760-7fd2-443e-bf41-95a60c2f4c7c",
Subscriptions: []SqlSubscription{
{UserName: "user@domain.com"},
{DestinationID: "Test"},
},
CustomSubject: "test",
},
},
},
},
},
},
},
},
Create: true,
Resource: ResourceJob(),
HCL: `name = "TF SQL task subscriptions"
task {
task_key = "a"
sql_task {
warehouse_id = "dca3a0ba199040eb"
alert {
subscriptions {
user_name = "user@domain.com"
}
subscriptions {
destination_id = "Test"
}
pause_subscriptions = true
alert_id = "3cf91a42-6217-4f3c-a6f0-345d489051b9"
}
}
}
task {
task_key = "d"
sql_task {
warehouse_id = "dca3a0ba199040eb"
dashboard {
subscriptions {
user_name = "user@domain.com"
}
subscriptions {
destination_id = "Test"
}
pause_subscriptions = false
dashboard_id = "d81a7760-7fd2-443e-bf41-95a60c2f4c7c"
custom_subject = "test"
}
}
}`,
}.Apply(t)
assert.NoError(t, err)
assert.Equal(t, "789", d.Id())
}

func TestResourceJobCreate_AlwaysRunning(t *testing.T) {
d, err := qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
Expand Down

0 comments on commit 47857a6

Please sign in to comment.