Skip to content

Commit

Permalink
Rework PR to address issues
Browse files Browse the repository at this point in the history
  • Loading branch information
alexott committed Dec 6, 2024
1 parent 8f4c339 commit a491279
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 32 deletions.
2 changes: 1 addition & 1 deletion docs/data-sources/jobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ output "x" {

## Argument Reference

* `job_name_contains` - (Optional) Only return [databricks_job](../resources/job.md#) ids that match the given name string.
* `job_name_contains` - (Optional) Only return [databricks_job](../resources/job.md#) ids that match the given name string (case-insensitive).

## Attribute Reference

Expand Down
36 changes: 21 additions & 15 deletions jobs/data_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,36 @@ package jobs
import (
"context"
"fmt"
"strconv"
"strings"

"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/service/jobs"
"github.com/databricks/terraform-provider-databricks/common"
)

func DataSourceJobs() common.Resource {
type jobsData struct {
return common.WorkspaceData(func(ctx context.Context, data *struct {
Ids map[string]string `json:"ids,omitempty" tf:"computed"`
NameFilter string `json:"job_name_contains,omitempty" tf:"optional"`
}
return common.DataResource(jobsData{}, func(ctx context.Context, e any, c *common.DatabricksClient) error {
response := e.(*jobsData)
jobsAPI := NewJobsAPI(ctx, c)
list, err := jobsAPI.List(response.NameFilter)
if err != nil {
return err
}
response.Ids = map[string]string{}
for _, v := range list {
name := v.Settings.Name
_, duplicateName := response.Ids[name]
NameFilter string `json:"job_name_contains,omitempty"`
}, w *databricks.WorkspaceClient) error {
iter := w.Jobs.List(ctx, jobs.ListJobsRequest{ExpandTasks: false, Limit: 100})
data.Ids = map[string]string{}
nameFilter := strings.ToLower(data.NameFilter)
for iter.HasNext(ctx) {
job, err := iter.Next(ctx)
if err != nil {
return err
}
name := job.Settings.Name
if nameFilter != "" && !strings.Contains(strings.ToLower(name), nameFilter) {
continue
}
_, duplicateName := data.Ids[name]
if duplicateName {
return fmt.Errorf("duplicate job name detected: %s", name)
}
response.Ids[name] = v.ID()
data.Ids[name] = strconv.FormatInt(job.JobId, 10)
}
return nil
})
Expand Down
38 changes: 37 additions & 1 deletion jobs/data_jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestJobsData(t *testing.T) {
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25",
Resource: "/api/2.1/jobs/list?limit=100",
Response: JobListResponse{
Jobs: []Job{
{
Expand Down Expand Up @@ -41,3 +41,39 @@ func TestJobsData(t *testing.T) {
},
})
}

func TestJobsDataWithFilter(t *testing.T) {
qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.1/jobs/list?limit=100",
Response: JobListResponse{
Jobs: []Job{
{
JobID: 123,
Settings: &JobSettings{
Name: "First",
},
},
{
JobID: 234,
Settings: &JobSettings{
Name: "Second",
},
},
},
},
},
},
Resource: DataSourceJobs(),
Read: true,
NonWritable: true,
ID: "_",
HCL: `job_name_contains = "Sec"`,
}.ApplyAndExpectData(t, map[string]any{
"ids": map[string]any{
"Second": "234",
},
})
}
17 changes: 2 additions & 15 deletions jobs/resource_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,21 +676,8 @@ func (a JobsAPI) ListByName(name string, expandTasks bool) ([]Job, error) {
}

// List all jobs
func (a JobsAPI) List(nameFilter string) (l []Job, err error) {
if nameFilter == "" {
return a.ListByName("", false)
}

allJobs, err := a.ListByName("", false)
if err != nil {
return l, err
}
for _, job := range allJobs {
if strings.Contains(job.Settings.Name, nameFilter) {
l = append(l, job)
}
}
return
func (a JobsAPI) List() (l []Job, err error) {
return a.ListByName("", false)
}

// RunsList returns a job runs list
Expand Down

0 comments on commit a491279

Please sign in to comment.