Skip to content

Commit 7c9e98d

Browse files
dsfredericdsfrederic-cgkalexott
authored
[Feature] Allow to filter jobs by name in databricks_jobs data source (#3395)
## Changes <!-- Summary of your changes that are easy to understand --> I've added a filter to obtain only job ids if the name contains a certain string. Inspired by the logic applied at databricks_clusters ![image](https://github.com/databricks/terraform-provider-databricks/assets/28486949/8233e9c2-1f5c-47cd-a5fd-a2d2bbfba370) ## Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [ ] `make test` run locally - [x] relevant change in `docs/` folder - [ ] covered with integration tests in `internal/acceptance` - [ ] relevant acceptance tests are passing - [ ] using Go SDK --------- Co-authored-by: Frédéric De Smet <[email protected]> Co-authored-by: Alex Ott <[email protected]>
1 parent 2f4b570 commit 7c9e98d

File tree

4 files changed

+68
-18
lines changed

4 files changed

+68
-18
lines changed

docs/data-sources/jobs.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Granting view [databricks_permissions](../resources/permissions.md) to all [data
1616
```hcl
1717
data "databricks_jobs" "this" {}
1818
19+
data "databricks_jobs" "tests" {
20+
job_name_contains = "test"
21+
}
22+
1923
resource "databricks_permissions" "everyone_can_view_all_jobs" {
2024
for_each = data.databricks_jobs.this.ids
2125
job_id = each.value
@@ -38,6 +42,10 @@ output "x" {
3842
}
3943
```
4044

45+
## Argument Reference
46+
47+
* `job_name_contains` - (Optional) Only return [databricks_job](../resources/job.md#) ids that match the given name string (case-insensitive).
48+
4149
## Attribute Reference
4250

4351
This data source exports the following attributes:

jobs/data_jobs.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,36 @@ package jobs
33
import (
44
"context"
55
"fmt"
6+
"strconv"
7+
"strings"
68

9+
"github.com/databricks/databricks-sdk-go"
10+
"github.com/databricks/databricks-sdk-go/service/jobs"
711
"github.com/databricks/terraform-provider-databricks/common"
812
)
913

1014
func DataSourceJobs() common.Resource {
11-
type jobsData struct {
12-
Ids map[string]string `json:"ids,omitempty" tf:"computed"`
13-
}
14-
return common.DataResource(jobsData{}, func(ctx context.Context, e any, c *common.DatabricksClient) error {
15-
response := e.(*jobsData)
16-
jobsAPI := NewJobsAPI(ctx, c)
17-
list, err := jobsAPI.List()
18-
if err != nil {
19-
return err
20-
}
21-
response.Ids = map[string]string{}
22-
for _, v := range list {
23-
name := v.Settings.Name
24-
_, duplicateName := response.Ids[name]
15+
return common.WorkspaceData(func(ctx context.Context, data *struct {
16+
Ids map[string]string `json:"ids,omitempty" tf:"computed"`
17+
NameFilter string `json:"job_name_contains,omitempty"`
18+
}, w *databricks.WorkspaceClient) error {
19+
iter := w.Jobs.List(ctx, jobs.ListJobsRequest{ExpandTasks: false, Limit: 100})
20+
data.Ids = map[string]string{}
21+
nameFilter := strings.ToLower(data.NameFilter)
22+
for iter.HasNext(ctx) {
23+
job, err := iter.Next(ctx)
24+
if err != nil {
25+
return err
26+
}
27+
name := job.Settings.Name
28+
if nameFilter != "" && !strings.Contains(strings.ToLower(name), nameFilter) {
29+
continue
30+
}
31+
_, duplicateName := data.Ids[name]
2532
if duplicateName {
2633
return fmt.Errorf("duplicate job name detected: %s", name)
2734
}
28-
response.Ids[name] = v.ID()
35+
data.Ids[name] = strconv.FormatInt(job.JobId, 10)
2936
}
3037
return nil
3138
})

jobs/data_jobs_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestJobsData(t *testing.T) {
1111
Fixtures: []qa.HTTPFixture{
1212
{
1313
Method: "GET",
14-
Resource: "/api/2.1/jobs/list?expand_tasks=false&limit=25",
14+
Resource: "/api/2.1/jobs/list?limit=100",
1515
Response: JobListResponse{
1616
Jobs: []Job{
1717
{
@@ -41,3 +41,39 @@ func TestJobsData(t *testing.T) {
4141
},
4242
})
4343
}
44+
45+
func TestJobsDataWithFilter(t *testing.T) {
46+
qa.ResourceFixture{
47+
Fixtures: []qa.HTTPFixture{
48+
{
49+
Method: "GET",
50+
Resource: "/api/2.1/jobs/list?limit=100",
51+
Response: JobListResponse{
52+
Jobs: []Job{
53+
{
54+
JobID: 123,
55+
Settings: &JobSettings{
56+
Name: "First",
57+
},
58+
},
59+
{
60+
JobID: 234,
61+
Settings: &JobSettings{
62+
Name: "Second",
63+
},
64+
},
65+
},
66+
},
67+
},
68+
},
69+
Resource: DataSourceJobs(),
70+
Read: true,
71+
NonWritable: true,
72+
ID: "_",
73+
HCL: `job_name_contains = "Sec"`,
74+
}.ApplyAndExpectData(t, map[string]any{
75+
"ids": map[string]any{
76+
"Second": "234",
77+
},
78+
})
79+
}

jobs/resource_job.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,8 +677,7 @@ func (a JobsAPI) ListByName(name string, expandTasks bool) ([]Job, error) {
677677

678678
// List all jobs
679679
func (a JobsAPI) List() (l []Job, err error) {
680-
l, err = a.ListByName("", false)
681-
return
680+
return a.ListByName("", false)
682681
}
683682

684683
// RunsList returns a job runs list

0 commit comments

Comments
 (0)