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

Added databricks_job data resource #1509

Merged
merged 18 commits into from
Sep 30, 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
| [databricks_instance_profile](docs/resources/instance_profile.md)
| [databricks_ip_access_list](docs/resources/ip_access_list.md)
| [databricks_job](docs/resources/job.md)
| [databricks_job](docs/data-sources/job.md) data
| [databricks_jobs](docs/data-sources/jobs.md)
| [databricks_library](docs/resources/library.md)
| [databricks_metastore](docs/resources/metastore.md)
Expand Down
38 changes: 38 additions & 0 deletions docs/data-sources/job.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
subcategory: "Compute"
---
# databricks_job Data Source

-> **Note** If you have a fully automated setup with workspaces created by [databricks_mws_workspaces](../resources/mws_workspaces.md) or [azurerm_databricks_workspace](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/databricks_workspace), please make sure to add [depends_on attribute](../index.md#data-resources-and-authentication-is-not-configured-errors) in order to prevent _authentication is not configured for provider_ errors.

Retrieves the settings of [databricks_job](../resources/job.md) by name or by id. Complements the feature of the [databricks_jobs](jobs.md) data source.

## Example Usage

Getting the existing cluster id of specific [databricks_job](../resources/job.md) by name or by id:

```hcl
data "databricks_job" "this" {
job_name = "My job"
}

output "cluster_id" {
value = data.databricks_job.job_settings.existing_cluster_id
sensitive = false
}
```

## Attribute Reference

This data source exports the following attributes:

* `job_id` - the id of [databricks_job](../resources/job.md) if the resource was matched by name.
* `job_name` - the job name of [databricks_job](../resources/job.md) if the resource was matched by id.
* `job_settings` - the job settings of [databricks_job](../resources/job.md).

## Related Resources

The following resources are used in the same context:

* [databricks_jobs](jobs.md) data to get all jobs and their names from a workspace.
* [databricks_job](../resources/job.md) to manage [Databricks Jobs](https://docs.databricks.com/jobs.html) to run non-interactive code in a [databricks_cluster](../resources/cluster.md).
66 changes: 66 additions & 0 deletions jobs/acceptance/data_job_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package acceptance

import (
"github.com/databricks/terraform-provider-databricks/internal/acceptance"

"testing"
)

func TestAccDataSourceQueryableJob(t *testing.T) {
acceptance.Test(t, []acceptance.Step{
{
Template: `
data "databricks_current_user" "me" {}
data "databricks_spark_version" "latest" {}
data "databricks_node_type" "smallest" {
local_disk = true
}

resource "databricks_notebook" "this" {
path = "${data.databricks_current_user.me.home}/Terraform{var.RANDOM}"
language = "PYTHON"
content_base64 = base64encode(<<-EOT
# created from ${abspath(path.module)}
display(spark.range(10))
EOT
)
}

resource "databricks_job" "this" {
name = "job-datasource-acceptance-test"

job_cluster {
job_cluster_key = "j"
new_cluster {
num_workers = 20
spark_version = data.databricks_spark_version.latest.id
node_type_id = data.databricks_node_type.smallest.id
}
}

task {
task_key = "a"

new_cluster {
num_workers = 1
spark_version = data.databricks_spark_version.latest.id
node_type_id = data.databricks_node_type.smallest.id
}

notebook_task {
notebook_path = databricks_notebook.this.path
}
}

}

data "databricks_job" "this" {
job_name = databricks_job.this.name
}

output "cluster_workers" {
value = data.databricks_job.this.job_settings[0].settings[0].new_cluster[0].num_workers
}`,
},
})
}
39 changes: 39 additions & 0 deletions jobs/data_job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package jobs

import (
"context"
"fmt"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DataSourceJob() *schema.Resource {
type queryableJobData struct {
Id string `json:"job_id,omitempty" tf:"computed"`
Name string `json:"job_name,omitempty" tf:"computed"`
Job *Job `json:"job_settings,omitempty" tf:"computed"`
}
return common.DataResource(queryableJobData{}, func(ctx context.Context, e any, c *common.DatabricksClient) error {
data := e.(*queryableJobData)
jobsAPI := NewJobsAPI(ctx, c)
list, err := jobsAPI.List()
if err != nil {
return err
}
for _, job := range list.Jobs {
currentJob := job // De-referencing the temp variable used by the loop
currentJobId := currentJob.ID()
currentJobName := currentJob.Settings.Name
if currentJobName == data.Name || currentJobId == data.Id {
data.Job = &currentJob
data.Name = currentJobName
data.Id = currentJobId
nfx marked this conversation as resolved.
Show resolved Hide resolved
return nil // break the loop after we found the job
}
}
if data.Job == nil {
return fmt.Errorf("no job found with specified name or id")
}
return nil
})
}
82 changes: 82 additions & 0 deletions jobs/data_job_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package jobs

import (
"github.com/databricks/terraform-provider-databricks/qa"
"testing"
)

func commonFixtures() []qa.HTTPFixture {
return []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.0/jobs/list",
Response: JobList{
Jobs: []Job{
{
JobID: 123,
Settings: &JobSettings{
Name: "First",
},
},
{
JobID: 234,
Settings: &JobSettings{
Name: "Second",
},
},
},
},
},
}

}
func TestDataSourceQueryableJobMatchesId(t *testing.T) {
qa.ResourceFixture{
Fixtures: commonFixtures(),
Resource: DataSourceJob(),
Read: true,
New: true,
NonWritable: true,
HCL: `job_id = "234"`,
ID: "_",
}.ApplyAndExpectData(t, map[string]any{
"job_id": "234",
"job_settings.0.settings.0.name": "Second",
})
}

func TestDataSourceQueryableJobMatchesName(t *testing.T) {
qa.ResourceFixture{
Fixtures: commonFixtures(),
Resource: DataSourceJob(),
Read: true,
nfx marked this conversation as resolved.
Show resolved Hide resolved
NonWritable: true,
HCL: `job_name = "First"`,
ID: "_",
}.ApplyAndExpectData(t, map[string]any{
"job_id": "123",
"job_settings.0.settings.0.name": "First",
})
}

func TestDataSourceQueryableJobNoMatchName(t *testing.T) {
qa.ResourceFixture{
Fixtures: commonFixtures(),
Resource: DataSourceJob(),
Read: true,
NonWritable: true,
HCL: `job_name= "Third"`,
ID: "_",
}.ExpectError(t, "no job found with specified name or id")
}

func TestDataSourceQueryableJobNoMatchId(t *testing.T) {
qa.ResourceFixture{
Fixtures: commonFixtures(),
Resource: DataSourceJob(),
Read: true,
NonWritable: true,
HCL: `job_id= "567"`,
ID: "_",
}.ExpectError(t, "no job found with specified name or id")
}
1 change: 1 addition & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func DatabricksProvider() *schema.Provider {
"databricks_dbfs_file_paths": storage.DataSourceDbfsFilePaths(),
"databricks_group": scim.DataSourceGroup(),
"databricks_jobs": jobs.DataSourceJobs(),
"databricks_job": jobs.DataSourceJob(),
"databricks_mws_workspaces": mws.DataSourceMwsWorkspaces(),
"databricks_node_type": clusters.DataSourceNodeType(),
"databricks_notebook": workspace.DataSourceNotebook(),
Expand Down