Skip to content

Commit

Permalink
simplify read by Id
Browse files Browse the repository at this point in the history
  • Loading branch information
nkvuong committed Nov 25, 2022
1 parent 34b3b4a commit 23345e2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
26 changes: 7 additions & 19 deletions jobs/data_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,17 @@ func DataSourceJob() *schema.Resource {
var list []Job
var err error
if data.Name != "" {
// if name is provided, need to list all jobs ny name
list, err = jobsAPI.ListByName(data.Name, true)
} else {
// if name is not provided, need to list all the jobs and search by id
// does not expand tasks to limit the results size
list, err = jobsAPI.List()
// otherwise, just read the job
var job Job
job, err = jobsAPI.Read(data.Id)
if err != nil {
return err
}
jobName := ""
for _, job := range list {
currentJob := job // De-referencing the temp variable used by the loop
currentJobId := currentJob.ID()
if currentJobId == data.Id {
jobName = currentJob.Settings.Name
break // break the loop after we found the job
}
}
if jobName == "" {
return fmt.Errorf("no job found with specified id")
}

// if a matching job id is found, list it by name with expandTasks
list, err = jobsAPI.ListByName(jobName, true)
data.Job = &job
data.Name = job.Settings.Name
}
if err != nil {
return err
Expand All @@ -59,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
30 changes: 26 additions & 4 deletions jobs/data_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

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

Expand Down Expand Up @@ -58,7 +59,18 @@ func commonFixtures(name string) []qa.HTTPFixture {
}
func TestDataSourceQueryableJobMatchesId(t *testing.T) {
qa.ResourceFixture{
Fixtures: append(commonFixtures(""), commonFixtures("Second")...),
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 @@ -101,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 id")
}.ExpectError(t, "Job 567 does not exist.")
}

0 comments on commit 23345e2

Please sign in to comment.