Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing attributes from databricks_job data source #1798

Merged
merged 5 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.")
}