Skip to content

Commit ab053ef

Browse files
authored
Added webhook_notifications support to databricks_job (#1674)
* Add support for job webhooks * Do not sort empty webhooks * Move logic to WebhookNotifications receiver
1 parent 97eda0b commit ab053ef

File tree

2 files changed

+124
-5
lines changed

2 files changed

+124
-5
lines changed

jobs/resource_job.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type DbtTask struct {
8888
WarehouseId string `json:"warehouse_id,omitempty"`
8989
}
9090

91-
// EmailNotifications contains the information for email notifications after job completion
91+
// EmailNotifications contains the information for email notifications after job or task run start or completion
9292
type EmailNotifications struct {
9393
OnStart []string `json:"on_start,omitempty"`
9494
OnSuccess []string `json:"on_success,omitempty"`
@@ -97,6 +97,31 @@ type EmailNotifications struct {
9797
AlertOnLastAttempt bool `json:"alert_on_last_attempt,omitempty"`
9898
}
9999

100+
// WebhookNotifications contains the information for webhook notifications sent after job start or completion.
101+
type WebhookNotifications struct {
102+
OnStart []Webhook `json:"on_start,omitempty"`
103+
OnSuccess []Webhook `json:"on_success,omitempty"`
104+
OnFailure []Webhook `json:"on_failure,omitempty"`
105+
}
106+
107+
func (wn *WebhookNotifications) Sort() {
108+
if wn == nil {
109+
return
110+
}
111+
112+
notifs := [][]Webhook{wn.OnStart, wn.OnFailure, wn.OnSuccess}
113+
for _, ns := range notifs {
114+
sort.Slice(ns, func(i, j int) bool {
115+
return ns[i].ID < ns[j].ID
116+
})
117+
}
118+
}
119+
120+
// Webhook contains a reference by id to one of the centrally configured webhooks.
121+
type Webhook struct {
122+
ID string `json:"id"`
123+
}
124+
100125
// CronSchedule contains the information for the quartz cron expression
101126
type CronSchedule struct {
102127
QuartzCronExpression string `json:"quartz_cron_expression"`
@@ -179,10 +204,11 @@ type JobSettings struct {
179204
GitSource *GitSource `json:"git_source,omitempty"`
180205
// END Jobs + Repo integration preview
181206

182-
Schedule *CronSchedule `json:"schedule,omitempty"`
183-
MaxConcurrentRuns int32 `json:"max_concurrent_runs,omitempty"`
184-
EmailNotifications *EmailNotifications `json:"email_notifications,omitempty" tf:"suppress_diff"`
185-
Tags map[string]string `json:"tags,omitempty"`
207+
Schedule *CronSchedule `json:"schedule,omitempty"`
208+
MaxConcurrentRuns int32 `json:"max_concurrent_runs,omitempty"`
209+
EmailNotifications *EmailNotifications `json:"email_notifications,omitempty" tf:"suppress_diff"`
210+
WebhookNotifications *WebhookNotifications `json:"webhook_notifications,omitempty" tf:"suppress_diff"`
211+
Tags map[string]string `json:"tags,omitempty"`
186212
}
187213

188214
func (js *JobSettings) isMultiTask() bool {
@@ -195,6 +221,10 @@ func (js *JobSettings) sortTasksByKey() {
195221
})
196222
}
197223

224+
func (js *JobSettings) sortWebhooksByID() {
225+
js.WebhookNotifications.Sort()
226+
}
227+
198228
// JobList returns a list of all jobs
199229
type JobList struct {
200230
Jobs []Job `json:"jobs"`
@@ -381,6 +411,7 @@ func (a JobsAPI) Restart(id string, timeout time.Duration) error {
381411
func (a JobsAPI) Create(jobSettings JobSettings) (Job, error) {
382412
var job Job
383413
jobSettings.sortTasksByKey()
414+
jobSettings.sortWebhooksByID()
384415
var gitSource *GitSource = jobSettings.GitSource
385416
if gitSource != nil && gitSource.Provider == "" {
386417
gitSource.Provider = repos.GetGitProviderFromUrl(gitSource.Url)
@@ -418,6 +449,7 @@ func (a JobsAPI) Read(id string) (job Job, err error) {
418449
}, &job), id)
419450
if job.Settings != nil {
420451
job.Settings.sortTasksByKey()
452+
job.Settings.sortWebhooksByID()
421453
}
422454
return
423455
}

jobs/resource_job_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,93 @@ func TestResourceJobCreateNWorkers(t *testing.T) {
543543
assert.Equal(t, "789", d.Id())
544544
}
545545

546+
func TestResourceJobCreateWithWebhooks(t *testing.T) {
547+
d, err := qa.ResourceFixture{
548+
Fixtures: []qa.HTTPFixture{
549+
{
550+
Method: "POST",
551+
Resource: "/api/2.0/jobs/create",
552+
ExpectedRequest: JobSettings{
553+
ExistingClusterID: "abc",
554+
MaxConcurrentRuns: 1,
555+
SparkJarTask: &SparkJarTask{
556+
MainClassName: "com.labs.BarMain",
557+
},
558+
Libraries: []libraries.Library{
559+
{
560+
Jar: "dbfs://aa/bb/cc.jar",
561+
},
562+
},
563+
Name: "Featurizer",
564+
WebhookNotifications: &WebhookNotifications{
565+
OnStart: []Webhook{{ID: "id1"}, {ID: "id2"}, {ID: "id3"}},
566+
OnSuccess: []Webhook{{ID: "id2"}},
567+
OnFailure: []Webhook{{ID: "id3"}},
568+
},
569+
},
570+
Response: Job{
571+
JobID: 789,
572+
},
573+
},
574+
{
575+
Method: "GET",
576+
Resource: "/api/2.0/jobs/get?job_id=789",
577+
Response: Job{
578+
JobID: 789,
579+
Settings: &JobSettings{
580+
ExistingClusterID: "abc",
581+
MaxConcurrentRuns: 1,
582+
SparkJarTask: &SparkJarTask{
583+
MainClassName: "com.labs.BarMain",
584+
},
585+
Libraries: []libraries.Library{
586+
{
587+
Jar: "dbfs://aa/bb/cc.jar",
588+
},
589+
},
590+
Name: "Featurizer",
591+
WebhookNotifications: &WebhookNotifications{
592+
OnStart: []Webhook{{ID: "id1"}, {ID: "id2"}, {ID: "id3"}},
593+
OnSuccess: []Webhook{{ID: "id2"}},
594+
OnFailure: []Webhook{{ID: "id3"}},
595+
},
596+
},
597+
},
598+
},
599+
},
600+
Create: true,
601+
Resource: ResourceJob(),
602+
HCL: `existing_cluster_id = "abc"
603+
name = "Featurizer"
604+
max_concurrent_runs = 1
605+
spark_jar_task {
606+
main_class_name = "com.labs.BarMain"
607+
}
608+
library {
609+
jar = "dbfs://aa/bb/cc.jar"
610+
}
611+
webhook_notifications {
612+
on_start {
613+
id = "id3"
614+
}
615+
on_start {
616+
id = "id1"
617+
}
618+
on_start {
619+
id = "id2"
620+
}
621+
on_success {
622+
id = "id2"
623+
}
624+
on_failure {
625+
id = "id3"
626+
}
627+
}`,
628+
}.Apply(t)
629+
assert.NoError(t, err, err)
630+
assert.Equal(t, "789", d.Id())
631+
}
632+
546633
func TestResourceJobCreateFromGitSource(t *testing.T) {
547634
qa.ResourceFixture{
548635
Fixtures: []qa.HTTPFixture{

0 commit comments

Comments
 (0)