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

[Feature] Allow to filter jobs by name in databricks_jobs data source #3395

Merged
merged 5 commits into from
Dec 9, 2024
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
8 changes: 8 additions & 0 deletions docs/data-sources/jobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Granting view [databricks_permissions](../resources/permissions.md) to all [data
```hcl
data "databricks_jobs" "this" {}

data "databricks_jobs" "tests" {
job_name_contains = "test"
}

resource "databricks_permissions" "everyone_can_view_all_jobs" {
for_each = data.databricks_jobs.this.ids
job_id = each.value
Expand All @@ -38,6 +42,10 @@ output "x" {
}
```

## Argument Reference

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

## Attribute Reference

This data source exports the following attributes:
Expand Down
37 changes: 22 additions & 15 deletions jobs/data_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +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 {
Ids map[string]string `json:"ids,omitempty" tf:"computed"`
}
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()
if err != nil {
return err
}
response.Ids = map[string]string{}
for _, v := range list {
name := v.Settings.Name
_, duplicateName := response.Ids[name]
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"`
}, 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",
},
})
}
3 changes: 1 addition & 2 deletions jobs/resource_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,8 +677,7 @@ func (a JobsAPI) ListByName(name string, expandTasks bool) ([]Job, error) {

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

// RunsList returns a job runs list
Expand Down
Loading