Skip to content

Commit

Permalink
Fix missing attributes from databricks_job data source (databricks#…
Browse files Browse the repository at this point in the history
…1798)

* re-add expand_tasks to retrieve settings

* fix error messages

* simplify read by Id
  • Loading branch information
nkvuong authored Dec 2, 2022
1 parent 76b720a commit 4869b1f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
14 changes: 11 additions & 3 deletions jobs/data_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ func DataSourceJob() *schema.Resource {
var list []Job
var err error
if data.Name != "" {
list, err = jobsAPI.ListByName(data.Name, false)
// if name is provided, need to list all jobs ny name
list, err = jobsAPI.ListByName(data.Name, true)
} else {
list, err = jobsAPI.List()
// otherwise, just read the job
var job Job
job, err = jobsAPI.Read(data.Id)
if err != nil {
return err
}
data.Job = &job
data.Name = job.Settings.Name
}
if err != nil {
return err
Expand All @@ -39,7 +47,7 @@ func DataSourceJob() *schema.Resource {
}
}
if data.Job == nil {
return fmt.Errorf("no job found with specified name or id")
return fmt.Errorf("no job found with specified name")
}
return nil
})
Expand Down
54 changes: 48 additions & 6 deletions jobs/data_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"fmt"
"testing"

"github.com/databricks/terraform-provider-databricks/common"
"github.com/databricks/terraform-provider-databricks/qa"
)

func commonFixtures(name string) []qa.HTTPFixture {
resource := "/api/2.1/jobs/list?expand_tasks=false&limit=25&offset=0"
if name != "" {
resource = fmt.Sprintf("/api/2.1/jobs/list?expand_tasks=false&limit=25&name=%s&offset=0", name)
resource = fmt.Sprintf("/api/2.1/jobs/list?expand_tasks=true&limit=25&name=%s&offset=0", name)
}
return []qa.HTTPFixture{
{
Expand All @@ -33,12 +34,43 @@ func commonFixtures(name string) []qa.HTTPFixture {
},
},
},
{
Method: "GET",
Resource: resource,
Response: JobListResponse{
Jobs: []Job{
{
JobID: 123,
Settings: &JobSettings{
Name: "First",
},
},
{
JobID: 234,
Settings: &JobSettings{
Name: "Second",
},
},
},
},
},
}

}
func TestDataSourceQueryableJobMatchesId(t *testing.T) {
qa.ResourceFixture{
Fixtures: commonFixtures(""),
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.0/jobs/get?job_id=234",
Response: Job{
JobID: 234,
Settings: &JobSettings{
Name: "Second",
},
},
},
},
Resource: DataSourceJob(),
Read: true,
New: true,
Expand Down Expand Up @@ -70,7 +102,7 @@ func TestDataSourceQueryableJobNoMatchName(t *testing.T) {
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25&name=Third&offset=0",
Resource: "/api/2.1/jobs/list?expand_tasks=true&limit=25&name=Third&offset=0",
Response: JobListResponse{
Jobs: []Job{},
},
Expand All @@ -81,16 +113,26 @@ func TestDataSourceQueryableJobNoMatchName(t *testing.T) {
NonWritable: true,
HCL: `job_name= "Third"`,
ID: "_",
}.ExpectError(t, "no job found with specified name or id")
}.ExpectError(t, "no job found with specified name")
}

func TestDataSourceQueryableJobNoMatchId(t *testing.T) {
qa.ResourceFixture{
Fixtures: commonFixtures(""),
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.0/jobs/get?job_id=567",
Response: common.APIErrorBody{
ErrorCode: "RESOURCE_DOES_NOT_EXIST",
Message: "Job 567 does not exist.",
},
Status: 400,
},
},
Resource: DataSourceJob(),
Read: true,
NonWritable: true,
HCL: `job_id= "567"`,
ID: "_",
}.ExpectError(t, "no job found with specified name or id")
}.ExpectError(t, "Job 567 does not exist.")
}

0 comments on commit 4869b1f

Please sign in to comment.