Skip to content

Commit e9ca6cf

Browse files
authored
Merge pull request #195 from dbt-labs/release-0.2.8
Release 0.2.8
2 parents 2e98f13 + a79f5da commit e9ca6cf

File tree

12 files changed

+161
-39
lines changed

12 files changed

+161
-39
lines changed

.github/workflows/unit.yml

+1
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ jobs:
3737
DBT_CLOUD_ACCOUNT_ID: ${{ secrets.TEST_DBT_CLOUD_ACCOUNT_ID }}
3838
DBT_CLOUD_TOKEN: ${{ secrets.TEST_DBT_CLOUD_TOKEN }}
3939
DBT_CLOUD_HOST_URL: ${{ secrets.TEST_DBT_CLOUD_HOST_URL }}
40+
DBT_LEGACY_JOB_DEFERRAL: 1

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
All notable changes to this project will be documented in this file.
44

5-
## [Unreleased](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.2.7...HEAD)
5+
## [Unreleased](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.2.8...HEAD)
6+
7+
## [0.2.8](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.2.7...v0.2.8)
8+
9+
## Fix
10+
11+
- [#190](https://github.com/dbt-labs/terraform-provider-dbtcloud/issues/190) - Allow setting deferral for jobs at the environment level rather than at the job level. This is due to changes in CI in dbt Cloud. Add docs about those changes on the dbtcloud_job resource page
612

713
## [0.2.7](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.2.6...v0.2.7)
814

docs/data-sources/job.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ description: |-
2222

2323
### Read-Only
2424

25+
- `deferring_environment_id` (Number) ID of the environment this job defers to
2526
- `deferring_job_id` (Number) ID of the job this job defers to
2627
- `environment_id` (Number) ID of the environment the job is in
2728
- `id` (String) The ID of this resource.

docs/resources/job.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ description: |-
77

88
# dbtcloud_job (Resource)
99

10-
11-
10+
~> As of September 2023, some CI improvements are being rolled out to dbt Cloud with minor impacts to some jobs: [more info](https://docs.getdbt.com/docs/dbt-versions/release-notes/june-2023/ci-updates-phase1-rn).
11+
<br/>
12+
<br/>
13+
Those improvements include modifications to deferral which was historically set at the job level and will now be set at the environment level.
14+
Deferral can still be set to "self" by setting `self_deferring` to `true` but with the new approach, deferral to other runs need to be done with `deferring_environment_id` instead of `deferring_job_id`.
1215

1316
## Example Usage
1417

@@ -54,7 +57,8 @@ resource "dbtcloud_job" "test" {
5457
### Optional
5558

5659
- `dbt_version` (String) Version number of dbt to use in this job, usually in the format 1.2.0-latest rather than core versions
57-
- `deferring_job_id` (Number) Job identifier that this job defers to
60+
- `deferring_environment_id` (Number) Environment identifier that this job defers to (new deferring approach)
61+
- `deferring_job_id` (Number) Job identifier that this job defers to (legacy deferring approach)
5862
- `generate_docs` (Boolean) Flag for whether the job should generate documentation
5963
- `is_active` (Boolean) Flag for whether the job is marked active or deleted
6064
- `num_threads` (Number) Number of threads to use in the job
@@ -80,4 +84,4 @@ Import is supported using the following syntax:
8084
# Import using a job ID found in the URL or via the API.
8185
terraform import dbtcloud_job.test_job "job_id"
8286
terraform import dbtcloud_job.test_job 12345
83-
```
87+
```

docs/resources/repository.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ resource "dbtcloud_repository" "github_repo" {
3737
3838
# repo cloned via the GitHub integration, with auto-retrieval of the `github_installation_id`
3939
# here, we assume that `token` and `host_url` are respectively accessible via `var.dbt_token` and `var.dbt_host_url`
40+
# NOTE: the following requires connecting via a user token and can't be retrieved with a service token
4041
data "http" "github_installations_response" {
4142
url = format("%s/v2/integrations/github/installations/", var.dbt_host_url)
4243
request_headers = {
@@ -57,6 +58,7 @@ resource "dbtcloud_repository" "github_repo_other" {
5758
5859
5960
# repo cloned via the GitLab integration
61+
# as of 15 Sept 2023 this resource requires using a user token and can't be set with a service token
6062
resource "dbtcloud_repository" "gitlab_repo" {
6163
project_id = dbtcloud_project.my_project_2.id
6264
remote_url = "<gitlab-group>/<gitlab-project>"

examples/resources/dbtcloud_repository/resource.tf

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ resource "dbtcloud_repository" "github_repo" {
1111

1212
# repo cloned via the GitHub integration, with auto-retrieval of the `github_installation_id`
1313
# here, we assume that `token` and `host_url` are respectively accessible via `var.dbt_token` and `var.dbt_host_url`
14+
# NOTE: the following requires connecting via a user token and can't be retrieved with a service token
1415
data "http" "github_installations_response" {
1516
url = format("%s/v2/integrations/github/installations/", var.dbt_host_url)
1617
request_headers = {
@@ -31,6 +32,7 @@ resource "dbtcloud_repository" "github_repo_other" {
3132

3233

3334
# repo cloned via the GitLab integration
35+
# as of 15 Sept 2023 this resource requires using a user token and can't be set with a service token
3436
resource "dbtcloud_repository" "gitlab_repo" {
3537
project_id = dbtcloud_project.my_project_2.id
3638
remote_url = "<gitlab-group>/<gitlab-project>"

pkg/data_sources/job.go

+8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ var jobSchema = map[string]*schema.Schema{
3636
Computed: true,
3737
Description: "ID of the job this job defers to",
3838
},
39+
"deferring_environment_id": &schema.Schema{
40+
Type: schema.TypeInt,
41+
Computed: true,
42+
Description: "ID of the environment this job defers to",
43+
},
3944
"self_deferring": &schema.Schema{
4045
Type: schema.TypeBool,
4146
Computed: true,
@@ -95,6 +100,9 @@ func datasourceJobRead(ctx context.Context, d *schema.ResourceData, m interface{
95100
return diag.FromErr(err)
96101
}
97102
}
103+
if err := d.Set("deferring_environment_id", job.DeferringEnvironmentId); err != nil {
104+
return diag.FromErr(err)
105+
}
98106
if err := d.Set("self_deferring", selfDeferring); err != nil {
99107
return diag.FromErr(err)
100108
}

pkg/dbt_cloud/job.go

+24-16
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,22 @@ type JobExecution struct {
4848
}
4949

5050
type Job struct {
51-
ID *int `json:"id"`
52-
Account_Id int `json:"account_id"`
53-
Project_Id int `json:"project_id"`
54-
Environment_Id int `json:"environment_id"`
55-
Name string `json:"name"`
56-
Execute_Steps []string `json:"execute_steps"`
57-
Dbt_Version *string `json:"dbt_version"`
58-
Triggers JobTrigger `json:"triggers"`
59-
Settings JobSettings `json:"settings"`
60-
State int `json:"state"`
61-
Generate_Docs bool `json:"generate_docs"`
62-
Schedule JobSchedule `json:"schedule"`
63-
Run_Generate_Sources bool `json:"run_generate_sources"`
64-
Deferring_Job_Id *int `json:"deferring_job_definition_id"`
65-
Execution JobExecution `json:"execution"`
51+
ID *int `json:"id"`
52+
Account_Id int `json:"account_id"`
53+
Project_Id int `json:"project_id"`
54+
Environment_Id int `json:"environment_id"`
55+
Name string `json:"name"`
56+
Execute_Steps []string `json:"execute_steps"`
57+
Dbt_Version *string `json:"dbt_version"`
58+
Triggers JobTrigger `json:"triggers"`
59+
Settings JobSettings `json:"settings"`
60+
State int `json:"state"`
61+
Generate_Docs bool `json:"generate_docs"`
62+
Schedule JobSchedule `json:"schedule"`
63+
Run_Generate_Sources bool `json:"run_generate_sources"`
64+
Deferring_Job_Id *int `json:"deferring_job_definition_id"`
65+
DeferringEnvironmentId *int `json:"deferring_environment_id"`
66+
Execution JobExecution `json:"execution"`
6667
}
6768

6869
func (c *Client) GetJob(jobID string) (*Job, error) {
@@ -85,7 +86,7 @@ func (c *Client) GetJob(jobID string) (*Job, error) {
8586
return &jobResponse.Data, nil
8687
}
8788

88-
func (c *Client) CreateJob(projectId int, environmentId int, name string, executeSteps []string, dbtVersion string, isActive bool, triggers map[string]interface{}, numThreads int, targetName string, generateDocs bool, runGenerateSources bool, scheduleType string, scheduleInterval int, scheduleHours []int, scheduleDays []int, scheduleCron string, deferringJobId int, selfDeferring bool, timeoutSeconds int) (*Job, error) {
89+
func (c *Client) CreateJob(projectId int, environmentId int, name string, executeSteps []string, dbtVersion string, isActive bool, triggers map[string]interface{}, numThreads int, targetName string, generateDocs bool, runGenerateSources bool, scheduleType string, scheduleInterval int, scheduleHours []int, scheduleDays []int, scheduleCron string, deferringJobId int, deferringEnvironmentID int, selfDeferring bool, timeoutSeconds int) (*Job, error) {
8990
state := STATE_ACTIVE
9091
if !isActive {
9192
state = STATE_DELETED
@@ -165,6 +166,13 @@ func (c *Client) CreateJob(projectId int, environmentId int, name string, execut
165166
}
166167
if deferringJobId != 0 {
167168
newJob.Deferring_Job_Id = &deferringJobId
169+
} else {
170+
newJob.Deferring_Job_Id = nil
171+
}
172+
if deferringEnvironmentID != 0 {
173+
newJob.DeferringEnvironmentId = &deferringEnvironmentID
174+
} else {
175+
newJob.DeferringEnvironmentId = nil
168176
}
169177
newJobData, err := json.Marshal(newJob)
170178
if err != nil {

pkg/resources/job.go

+32-5
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,14 @@ var jobSchema = map[string]*schema.Schema{
132132
"deferring_job_id": &schema.Schema{
133133
Type: schema.TypeInt,
134134
Optional: true,
135-
Description: "Job identifier that this job defers to",
136-
ConflictsWith: []string{"self_deferring"},
135+
Description: "Job identifier that this job defers to (legacy deferring approach)",
136+
ConflictsWith: []string{"self_deferring", "deferring_environment_id"},
137+
},
138+
"deferring_environment_id": &schema.Schema{
139+
Type: schema.TypeInt,
140+
Optional: true,
141+
Description: "Environment identifier that this job defers to (new deferring approach)",
142+
ConflictsWith: []string{"self_deferring", "deferring_job_id"},
137143
},
138144
"self_deferring": &schema.Schema{
139145
Type: schema.TypeBool,
@@ -237,6 +243,9 @@ func resourceJobRead(ctx context.Context, d *schema.ResourceData, m interface{})
237243
return diag.FromErr(err)
238244
}
239245
}
246+
if err := d.Set("deferring_environment_id", job.DeferringEnvironmentId); err != nil {
247+
return diag.FromErr(err)
248+
}
240249
if err := d.Set("self_deferring", selfDeferring); err != nil {
241250
return diag.FromErr(err)
242251
}
@@ -278,6 +287,7 @@ func resourceJobCreate(ctx context.Context, d *schema.ResourceData, m interface{
278287
scheduleDays := d.Get("schedule_days").([]interface{})
279288
scheduleCron := d.Get("schedule_cron").(string)
280289
deferringJobId := d.Get("deferring_job_id").(int)
290+
deferringEnvironmentID := d.Get("deferring_environment_id").(int)
281291
selfDeferring := d.Get("self_deferring").(bool)
282292
timeoutSeconds := d.Get("timeout_seconds").(int)
283293

@@ -294,7 +304,7 @@ func resourceJobCreate(ctx context.Context, d *schema.ResourceData, m interface{
294304
days = append(days, day.(int))
295305
}
296306

297-
j, err := c.CreateJob(projectId, environmentId, name, steps, dbtVersion, isActive, triggers, numThreads, targetName, generateDocs, runGenerateSources, scheduleType, scheduleInterval, hours, days, scheduleCron, deferringJobId, selfDeferring, timeoutSeconds)
307+
j, err := c.CreateJob(projectId, environmentId, name, steps, dbtVersion, isActive, triggers, numThreads, targetName, generateDocs, runGenerateSources, scheduleType, scheduleInterval, hours, days, scheduleCron, deferringJobId, deferringEnvironmentID, selfDeferring, timeoutSeconds)
298308
if err != nil {
299309
return diag.FromErr(err)
300310
}
@@ -326,6 +336,7 @@ func resourceJobUpdate(ctx context.Context, d *schema.ResourceData, m interface{
326336
d.HasChange("schedule_days") ||
327337
d.HasChange("schedule_cron") ||
328338
d.HasChange("deferring_job_id") ||
339+
d.HasChange("deferring_environment_id") ||
329340
d.HasChange("self_deferring") ||
330341
d.HasChange("timeout_seconds") {
331342
job, err := c.GetJob(jobId)
@@ -417,7 +428,19 @@ func resourceJobUpdate(ctx context.Context, d *schema.ResourceData, m interface{
417428
}
418429
if d.HasChange("deferring_job_id") {
419430
deferringJobId := d.Get("deferring_job_id").(int)
420-
job.Deferring_Job_Id = &deferringJobId
431+
if deferringJobId != 0 {
432+
job.Deferring_Job_Id = &deferringJobId
433+
} else {
434+
job.Deferring_Job_Id = nil
435+
}
436+
}
437+
if d.HasChange("deferring_environment_id") {
438+
deferringEnvironmentId := d.Get("deferring_environment_id").(int)
439+
if deferringEnvironmentId != 0 {
440+
job.DeferringEnvironmentId = &deferringEnvironmentId
441+
} else {
442+
job.DeferringEnvironmentId = nil
443+
}
421444
}
422445
// If self_deferring has been toggled to true, set deferring_job_id as own ID
423446
// Otherwise, set it back to what deferring_job_id specifies it to be
@@ -427,7 +450,11 @@ func resourceJobUpdate(ctx context.Context, d *schema.ResourceData, m interface{
427450
job.Deferring_Job_Id = &deferringJobID
428451
} else {
429452
deferringJobId := d.Get("deferring_job_id").(int)
430-
job.Deferring_Job_Id = &deferringJobId
453+
if deferringJobId != 0 {
454+
job.Deferring_Job_Id = &deferringJobId
455+
} else {
456+
job.Deferring_Job_Id = nil
457+
}
431458
}
432459
}
433460
if d.HasChange("timeout_seconds") {

pkg/resources/job_acceptance_test.go

+48-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package resources_test
22

33
import (
44
"fmt"
5+
"os"
56
"regexp"
67
"strings"
78
"testing"
@@ -20,6 +21,29 @@ func TestAccDbtCloudJobResource(t *testing.T) {
2021
projectName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
2122
environmentName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
2223

24+
var configDeferral string
25+
var checkDeferral resource.TestCheckFunc
26+
27+
// this logic is used as not all accounts are on the new CI approach of deferring at the env level
28+
if os.Getenv("DBT_LEGACY_JOB_DEFERRAL") != "" {
29+
configDeferral = testAccDbtCloudJobResourceDeferringConfig(jobName, jobName2, jobName3, projectName, environmentName, "self")
30+
checkDeferral = resource.ComposeTestCheckFunc(
31+
testAccCheckDbtCloudJobExists("dbtcloud_job.test_job"),
32+
testAccCheckDbtCloudJobExists("dbtcloud_job.test_job_2"),
33+
testAccCheckDbtCloudJobExists("dbtcloud_job.test_job_3"),
34+
resource.TestCheckResourceAttrSet("dbtcloud_job.test_job_2", "deferring_job_id"),
35+
resource.TestCheckResourceAttrSet("dbtcloud_job.test_job_3", "self_deferring"),
36+
)
37+
} else {
38+
configDeferral = testAccDbtCloudJobResourceDeferringConfig(jobName, jobName2, jobName3, projectName, environmentName, "env")
39+
checkDeferral = resource.ComposeTestCheckFunc(
40+
testAccCheckDbtCloudJobExists("dbtcloud_job.test_job"),
41+
testAccCheckDbtCloudJobExists("dbtcloud_job.test_job_2"),
42+
testAccCheckDbtCloudJobExists("dbtcloud_job.test_job_3"),
43+
resource.TestCheckResourceAttrSet("dbtcloud_job.test_job_2", "deferring_environment_id"),
44+
)
45+
}
46+
2347
resource.Test(t, resource.TestCase{
2448
PreCheck: func() { testAccPreCheck(t) },
2549
Providers: testAccProviders,
@@ -57,15 +81,26 @@ func TestAccDbtCloudJobResource(t *testing.T) {
5781
resource.TestCheckResourceAttrSet("dbtcloud_job.test_job", "generate_docs"),
5882
),
5983
},
60-
// DEFERRING JOBS
84+
// DEFERRING JOBS (depends on whether DBT_LEGACY_JOB_DEFERRAL is set, e.g. whether the new CI is set)
85+
{
86+
Config: configDeferral,
87+
Check: checkDeferral,
88+
},
89+
// REMOVE DEFERRAL
6190
{
62-
Config: testAccDbtCloudJobResourceDeferringJobConfig(jobName, jobName2, jobName3, projectName, environmentName, false),
91+
Config: testAccDbtCloudJobResourceFullConfig(jobName2, projectName, environmentName),
6392
Check: resource.ComposeTestCheckFunc(
6493
testAccCheckDbtCloudJobExists("dbtcloud_job.test_job"),
65-
testAccCheckDbtCloudJobExists("dbtcloud_job.test_job_2"),
66-
testAccCheckDbtCloudJobExists("dbtcloud_job.test_job_3"),
67-
resource.TestCheckResourceAttrSet("dbtcloud_job.test_job_2", "deferring_job_id"),
68-
resource.TestCheckResourceAttrSet("dbtcloud_job.test_job_3", "self_deferring"),
94+
resource.TestCheckResourceAttr("dbtcloud_job.test_job", "name", jobName2),
95+
resource.TestCheckResourceAttr("dbtcloud_job.test_job", "dbt_version", DBT_CLOUD_VERSION),
96+
resource.TestCheckResourceAttr("dbtcloud_job.test_job", "target_name", "test"),
97+
resource.TestCheckResourceAttr("dbtcloud_job.test_job", "timeout_seconds", "180"),
98+
resource.TestCheckResourceAttrSet("dbtcloud_job.test_job", "project_id"),
99+
resource.TestCheckResourceAttrSet("dbtcloud_job.test_job", "environment_id"),
100+
resource.TestCheckResourceAttrSet("dbtcloud_job.test_job", "is_active"),
101+
resource.TestCheckResourceAttrSet("dbtcloud_job.test_job", "num_threads"),
102+
resource.TestCheckResourceAttrSet("dbtcloud_job.test_job", "run_generate_sources"),
103+
resource.TestCheckResourceAttrSet("dbtcloud_job.test_job", "generate_docs"),
69104
),
70105
},
71106
// IMPORT
@@ -155,10 +190,12 @@ resource "dbtcloud_job" "test_job" {
155190
`, projectName, environmentName, DBT_CLOUD_VERSION, environmentName, DBT_CLOUD_VERSION, jobName, DBT_CLOUD_VERSION)
156191
}
157192

158-
func testAccDbtCloudJobResourceDeferringJobConfig(jobName, jobName2, jobName3, projectName, environmentName string, selfDeferring bool) string {
159-
deferParam := "deferring_job_id = dbtcloud_job.test_job.id"
160-
if selfDeferring {
193+
func testAccDbtCloudJobResourceDeferringConfig(jobName, jobName2, jobName3, projectName, environmentName string, deferring string) string {
194+
deferParam := ""
195+
if deferring == "self" {
161196
deferParam = "self_deferring = true"
197+
} else if deferring == "env" {
198+
deferParam = "deferring_environment_id = dbtcloud_environment.test_job_environment_new.environment_id"
162199
}
163200
return fmt.Sprintf(`
164201
resource "dbtcloud_project" "test_job_project" {
@@ -224,9 +261,9 @@ resource "dbtcloud_job" "test_job_3" {
224261
"schedule": false,
225262
"custom_branch_only": false,
226263
}
227-
self_deferring = true
264+
%s
228265
}
229-
`, projectName, environmentName, DBT_CLOUD_VERSION, jobName, DBT_CLOUD_VERSION, jobName2, deferParam, jobName3)
266+
`, projectName, environmentName, DBT_CLOUD_VERSION, jobName, DBT_CLOUD_VERSION, jobName2, deferParam, jobName3, deferParam)
230267
}
231268

232269
func testAccCheckDbtCloudJobExists(resource string) resource.TestCheckFunc {

pkg/resources/license_map_acceptance_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func TestAccDbtCloudLicenseMapResource(t *testing.T) {
3838
testAccCheckDbtCloudLicenseMapExists("dbtcloud_license_map.test_license_map"),
3939
resource.TestCheckResourceAttr("dbtcloud_license_map.test_license_map", "license_type", "developer"),
4040
resource.TestCheckResourceAttr("dbtcloud_license_map.test_license_map", "sso_license_mapping_groups.#", "2"),
41-
resource.TestCheckResourceAttr("dbtcloud_license_map.test_license_map", "sso_license_mapping_groups.0", groupName),
42-
resource.TestCheckResourceAttr("dbtcloud_license_map.test_license_map", "sso_license_mapping_groups.1", groupName2),
41+
resource.TestCheckTypeSetElemAttr("dbtcloud_license_map.test_license_map", "sso_license_mapping_groups.*", groupName),
42+
resource.TestCheckTypeSetElemAttr("dbtcloud_license_map.test_license_map", "sso_license_mapping_groups.*", groupName2),
4343
),
4444
},
4545
// IMPORT

templates/resources/job.md.tmpl

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
page_title: "{{.Name}} {{.Type}} - {{.RenderedProviderName}}"
3+
subcategory: ""
4+
description: |-
5+
{{ .Description | plainmarkdown | trimspace | prefixlines " " }}
6+
---
7+
8+
# {{.Name}} ({{.Type}})
9+
10+
~> As of September 2023, some CI improvements are being rolled out to dbt Cloud with minor impacts to some jobs: [more info](https://docs.getdbt.com/docs/dbt-versions/release-notes/june-2023/ci-updates-phase1-rn).
11+
<br/>
12+
<br/>
13+
Those improvements include modifications to deferral which was historically set at the job level and will now be set at the environment level.
14+
Deferral can still be set to "self" by setting `self_deferring` to `true` but with the new approach, deferral to other runs need to be done with `deferring_environment_id` instead of `deferring_job_id`.
15+
16+
## Example Usage
17+
18+
{{ tffile (printf "%s%s%s" "examples/resources/" .Name "/resource.tf") }}
19+
20+
{{ .SchemaMarkdown | trimspace }}
21+
22+
## Import
23+
24+
Import is supported using the following syntax:
25+
26+
{{ codefile "shell" (printf "%s%s%s" "examples/resources/" .Name "/import.sh") }}

0 commit comments

Comments
 (0)