Skip to content

Commit 8d94bd1

Browse files
authored
Fix crash when error happens during reading databricks_job (#5110)
## Changes <!-- Summary of your changes that are easy to understand --> The crash occurred when reading a job and backend returned an error, so the `job` became `nil`, but we still tried to access fields inside the struct, instead of returning the error immediately. ## Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [x] `make test` run locally - [ ] relevant change in `docs/` folder - [ ] covered with integration tests in `internal/acceptance` - [ ] using Go SDK - [ ] using TF Plugin Framework - [x] has entry in `NEXT_CHANGELOG.md` file
1 parent 9a30cf2 commit 8d94bd1

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

NEXT_CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bug Fixes
1010

11+
* Fix crash when error happens during reading `databricks_job` ([#5110](https://github.com/databricks/terraform-provider-databricks/pull/5110))
12+
1113
### Documentation
1214

1315
* Document `git_email` in `databricks_git_credential` resource ([#5099](https://github.com/databricks/terraform-provider-databricks/pull/5099))

jobs/jobs_api_go_sdk.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ func Read(jobID int64, w *databricks.WorkspaceClient, ctx context.Context) (job
313313
JobId: jobID,
314314
})
315315
err = wrapMissingJobError(err, fmt.Sprintf("%d", jobID))
316+
if err != nil {
317+
return
318+
}
316319
if job.Settings != nil {
317320
js := JobSettingsResource{JobSettings: *job.Settings}
318321
js.adjustTasks()

jobs/resource_job_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,6 +2372,37 @@ func TestResourceJobRead_NotFound(t *testing.T) {
23722372
}.ApplyNoError(t)
23732373
}
23742374

2375+
func TestResourceJobReadMultiTask_NotFound(t *testing.T) {
2376+
qa.ResourceFixture{
2377+
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
2378+
e := w.GetMockJobsAPI().EXPECT()
2379+
e.Get(mock.Anything, jobs.GetJobRequest{
2380+
JobId: 789,
2381+
}).Return(nil, apierr.ErrNotFound)
2382+
},
2383+
Resource: ResourceJob(),
2384+
HCL: `
2385+
name = "Featurizer"
2386+
task {
2387+
task_key = "a"
2388+
spark_jar_task {
2389+
main_class_name = "com.labs.BarMain"
2390+
}
2391+
}
2392+
task {
2393+
task_key = "b"
2394+
notebook_task {
2395+
notebook_path = "/Stuff"
2396+
}
2397+
}`,
2398+
2399+
Read: true,
2400+
New: true,
2401+
Removed: true,
2402+
ID: "789",
2403+
}.ApplyNoError(t)
2404+
}
2405+
23752406
func TestResourceJobRead_Error(t *testing.T) {
23762407
d, err := qa.ResourceFixture{
23772408
Fixtures: []qa.HTTPFixture{

0 commit comments

Comments
 (0)