Skip to content

Commit 03c71d0

Browse files
authored
Add terraform support for periodic triggers (#3700)
* Add periodic triggers * Add acceptance test for periodic triggers * Fix typo
1 parent 483eb99 commit 03c71d0

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

docs/resources/job.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ This block describes the queue settings of the job:
345345
### trigger Configuration Block
346346

347347
* `pause_status` - (Optional) Indicate whether this trigger is paused or not. Either `PAUSED` or `UNPAUSED`. When the `pause_status` field is omitted in the block, the server will default to using `UNPAUSED` as a value for `pause_status`.
348+
* `periodic` - (Optional) configuration block to define a trigger for Periodic Triggers consisting of the following attributes:
349+
* `interval` - (Required) Specifies the interval at which the job should run. This value is required.
350+
* `unit` - (Required) Options are {"DAYS", "HOURS", "WEEKS"}.
351+
348352
* `file_arrival` - (Optional) configuration block to define a trigger for [File Arrival events](https://learn.microsoft.com/en-us/azure/databricks/workflows/jobs/file-arrival-triggers) consisting of following attributes:
349353
* `url` - (Required) URL to be monitored for file arrivals. The path must point to the root or a subpath of the external location. Please note that the URL must have a trailing slash character (`/`).
350354
* `min_time_between_triggers_seconds` - (Optional) If set, the trigger starts a run only after the specified amount of time passed since the last time the trigger fired. The minimum allowed value is 60 seconds.

internal/acceptance/job_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,36 @@ func TestAccRemoveWebhooks(t *testing.T) {
413413
`,
414414
})
415415
}
416+
417+
func TestAccPeriodicTrigger(t *testing.T) {
418+
workspaceLevel(t, step{
419+
Template: `
420+
resource "databricks_job" "this" {
421+
name = "{var.RANDOM}"
422+
423+
trigger {
424+
pause_status = "UNPAUSED"
425+
periodic {
426+
interval = 17
427+
unit = "HOURS"
428+
}
429+
}
430+
}`,
431+
Check: resourceCheck("databricks_job.this", func(ctx context.Context, client *common.DatabricksClient, id string) error {
432+
w, err := client.WorkspaceClient()
433+
assert.NoError(t, err)
434+
435+
jobID, err := strconv.ParseInt(id, 10, 64)
436+
assert.NoError(t, err)
437+
438+
res, err := w.Jobs.GetByJobId(ctx, jobID)
439+
assert.NoError(t, err)
440+
441+
assert.Equal(t, jobs.PauseStatus("UNPAUSED"), res.Settings.Trigger.PauseStatus)
442+
assert.Equal(t, 17, res.Settings.Trigger.Periodic.Interval)
443+
assert.Equal(t, jobs.PeriodicTriggerConfigurationTimeUnit("HOURS"), res.Settings.Trigger.Periodic.Unit)
444+
445+
return nil
446+
}),
447+
})
448+
}

jobs/resource_job.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,15 @@ type TableUpdate struct {
272272
WaitAfterLastChangeSeconds int32 `json:"wait_after_last_change_seconds,omitempty"`
273273
}
274274

275+
type Periodic struct {
276+
Interval int32 `json:"interval"`
277+
Unit string `json:"unit"`
278+
}
279+
275280
type Trigger struct {
276281
FileArrival *FileArrival `json:"file_arrival,omitempty"`
277282
TableUpdate *TableUpdate `json:"table_update,omitempty"`
283+
Periodic *Periodic `json:"periodic,omitempty"`
278284
PauseStatus string `json:"pause_status,omitempty" tf:"default:UNPAUSED"`
279285
}
280286

@@ -566,9 +572,10 @@ func (JobSettingsResource) CustomizeSchema(s *common.CustomizableSchema) *common
566572
s.SchemaPath("continuous").SetConflictsWith([]string{"schedule", "trigger"})
567573
s.SchemaPath("trigger").SetConflictsWith([]string{"continuous", "schedule"})
568574

569-
trigger_eoo := []string{"trigger.0.file_arrival", "trigger.0.table_update"}
575+
trigger_eoo := []string{"trigger.0.file_arrival", "trigger.0.table_update", "trigger.0.periodic"}
570576
s.SchemaPath("trigger", "file_arrival").SetExactlyOneOf(trigger_eoo)
571577
s.SchemaPath("trigger", "table_update").SetExactlyOneOf(trigger_eoo)
578+
s.SchemaPath("trigger", "periodic").SetExactlyOneOf(trigger_eoo)
572579

573580
// Deprecated Job API 2.0 attributes
574581
var topLevelDeprecatedAttr = []string{

jobs/resource_job_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,62 @@ func TestResourceJobCreate_Trigger_TableUpdateCreate(t *testing.T) {
13811381
}.ApplyNoError(t)
13821382
}
13831383

1384+
func TestResourceJobCreate_Trigger_PeriodicCreate(t *testing.T) {
1385+
qa.ResourceFixture{
1386+
Create: true,
1387+
Resource: ResourceJob(),
1388+
Fixtures: []qa.HTTPFixture{
1389+
{
1390+
Method: "POST",
1391+
Resource: "/api/2.0/jobs/create",
1392+
ExpectedRequest: JobSettings{
1393+
MaxConcurrentRuns: 1,
1394+
Name: "Test",
1395+
Trigger: &Trigger{
1396+
PauseStatus: "UNPAUSED",
1397+
Periodic: &Periodic{
1398+
Interval: 4,
1399+
Unit: "HOURS",
1400+
},
1401+
},
1402+
},
1403+
Response: Job{
1404+
JobID: 1042,
1405+
},
1406+
},
1407+
{
1408+
Method: "GET",
1409+
Resource: "/api/2.0/jobs/get?job_id=1042",
1410+
Response: Job{
1411+
JobID: 1042,
1412+
Settings: &JobSettings{
1413+
MaxConcurrentRuns: 1,
1414+
Name: "Test",
1415+
Trigger: &Trigger{
1416+
PauseStatus: "UNPAUSED",
1417+
Periodic: &Periodic{
1418+
Interval: 4,
1419+
Unit: "HOURS",
1420+
},
1421+
},
1422+
},
1423+
},
1424+
},
1425+
},
1426+
HCL: `
1427+
trigger {
1428+
pause_status = "UNPAUSED"
1429+
periodic {
1430+
interval = 4
1431+
unit = "HOURS"
1432+
}
1433+
}
1434+
max_concurrent_runs = 1
1435+
name = "Test"
1436+
`,
1437+
}.ApplyNoError(t)
1438+
}
1439+
13841440
func TestResourceJobUpdate_ControlRunState_ContinuousUpdateRunNow(t *testing.T) {
13851441
qa.ResourceFixture{
13861442
Update: true,

0 commit comments

Comments
 (0)