Skip to content

Commit 7a3f01e

Browse files
authored
Feature - Add deferring_job_id (#65)
1 parent c861ae9 commit 7a3f01e

File tree

9 files changed

+112
-10
lines changed

9 files changed

+112
-10
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.77
1+
0.0.81

docs/data-sources/dbt_cloud_job.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ description: |-
1717

1818
### Required
1919

20+
- `deferring_job_id` (Number) ID of the job this job defers to
2021
- `job_id` (Number) ID of the job
2122
- `project_id` (Number) ID of the project the job is in
2223

docs/resources/dbt_cloud_job.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ description: |-
2626
### Optional
2727

2828
- `dbt_version` (String) Version number of DBT to use in this job
29+
- `deferring_job_id` (Number) Job identifier that this job defers to
2930
- `generate_docs` (Boolean) Flag for whether the job should generate documentation
3031
- `is_active` (Boolean) Flag for whether the job is marked active or deleted
3132
- `num_threads` (Number) Number of threads to use in the job

go.mod

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ require (
2626
github.com/hashicorp/go-multierror v1.1.1 // indirect
2727
github.com/hashicorp/go-plugin v1.4.3 // indirect
2828
github.com/hashicorp/go-uuid v1.0.3 // indirect
29-
github.com/hashicorp/go-version v1.5.0 // indirect
30-
github.com/hashicorp/hc-install v0.3.2 // indirect
29+
github.com/hashicorp/go-version v1.6.0 // indirect
30+
github.com/hashicorp/hc-install v0.4.0 // indirect
3131
github.com/hashicorp/hcl/v2 v2.11.1 // indirect
3232
github.com/hashicorp/logutils v1.0.0 // indirect
33-
github.com/hashicorp/terraform-exec v0.16.1 // indirect
33+
github.com/hashicorp/terraform-exec v0.17.1 // indirect
3434
github.com/hashicorp/terraform-json v0.14.0 // indirect
35-
github.com/hashicorp/terraform-plugin-docs v0.10.1 // indirect
35+
github.com/hashicorp/terraform-plugin-docs v0.12.0 // indirect
3636
github.com/hashicorp/terraform-plugin-go v0.8.0 // indirect
3737
github.com/hashicorp/terraform-plugin-log v0.3.0 // indirect
3838
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896 // indirect
@@ -57,9 +57,9 @@ require (
5757
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
5858
github.com/vmihailenco/tagparser v0.1.1 // indirect
5959
github.com/zclconf/go-cty v1.10.0 // indirect
60-
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
60+
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
6161
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
62-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
62+
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect
6363
golang.org/x/text v0.3.7 // indirect
6464
google.golang.org/appengine v1.6.6 // indirect
6565
google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d // indirect

go.sum

+12
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,23 @@ github.com/hashicorp/go-version v1.4.0 h1:aAQzgqIrRKRa7w75CKpbBxYsmUoPjzVm1W59ca
145145
github.com/hashicorp/go-version v1.4.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
146146
github.com/hashicorp/go-version v1.5.0 h1:O293SZ2Eg+AAYijkVK3jR786Am1bhDEh2GHT0tIVE5E=
147147
github.com/hashicorp/go-version v1.5.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
148+
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
149+
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
148150
github.com/hashicorp/hc-install v0.3.1 h1:VIjllE6KyAI1A244G8kTaHXy+TL5/XYzvrtFi8po/Yk=
149151
github.com/hashicorp/hc-install v0.3.1/go.mod h1:3LCdWcCDS1gaHC9mhHCGbkYfoY6vdsKohGjugbZdZak=
150152
github.com/hashicorp/hc-install v0.3.2 h1:oiQdJZvXmkNcRcEOOfM5n+VTsvNjWQeOjfAoO6dKSH8=
151153
github.com/hashicorp/hc-install v0.3.2/go.mod h1:xMG6Tr8Fw1WFjlxH0A9v61cW15pFwgEGqEz0V4jisHs=
154+
github.com/hashicorp/hc-install v0.4.0 h1:cZkRFr1WVa0Ty6x5fTvL1TuO1flul231rWkGH92oYYk=
155+
github.com/hashicorp/hc-install v0.4.0/go.mod h1:5d155H8EC5ewegao9A4PUTMNPZaq+TbOzkJJZ4vrXeI=
152156
github.com/hashicorp/hcl/v2 v2.11.1 h1:yTyWcXcm9XB0TEkyU/JCRU6rYy4K+mgLtzn2wlrJbcc=
153157
github.com/hashicorp/hcl/v2 v2.11.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
154158
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
155159
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
156160
github.com/hashicorp/terraform-exec v0.16.0/go.mod h1:wB5JHmjxZ/YVNZuv9npAXKmz5pGyxy8PSi0GRR0+YjA=
157161
github.com/hashicorp/terraform-exec v0.16.1 h1:NAwZFJW2L2SaCBVZoVaH8LPImLOGbPLkSHy0IYbs2uE=
158162
github.com/hashicorp/terraform-exec v0.16.1/go.mod h1:aj0lVshy8l+MHhFNoijNHtqTJQI3Xlowv5EOsEaGO7M=
163+
github.com/hashicorp/terraform-exec v0.17.1 h1:Hd7Z0WfPYiwFZwzczSnkCJCxOensv0x93Zq9KhkPfQA=
164+
github.com/hashicorp/terraform-exec v0.17.1/go.mod h1:0J3KZWwroWCS+ObFZMEUSMD9GztU+CS8eLnUm+vfl5k=
159165
github.com/hashicorp/terraform-json v0.13.0 h1:Li9L+lKD1FO5RVFRM1mMMIBDoUHslOniyEi5CM+FWGY=
160166
github.com/hashicorp/terraform-json v0.13.0/go.mod h1:y5OdLBCT+rxbwnpxZs9kGL7R9ExU76+cpdY8zHwoazk=
161167
github.com/hashicorp/terraform-json v0.14.0 h1:sh9iZ1Y8IFJLx+xQiKHGud6/TSUCM0N8e17dKDpqV7s=
@@ -164,6 +170,8 @@ github.com/hashicorp/terraform-plugin-docs v0.8.0 h1:qNuHNTEqVCT258+h1GsejtfgW6q
164170
github.com/hashicorp/terraform-plugin-docs v0.8.0/go.mod h1:MjeyK5CEI/jZbTQXq1Ay0UwTGdqxEcQexqhHP1WcLZc=
165171
github.com/hashicorp/terraform-plugin-docs v0.10.1 h1:jiVYfhJ/hVXDAQN2XjLK3WH1A/YHgFCrFXPpxibvmjc=
166172
github.com/hashicorp/terraform-plugin-docs v0.10.1/go.mod h1:47ZcsxMUJxAjGzHf+dZ9q78oYf4PeJxO1N+i5XDtXBc=
173+
github.com/hashicorp/terraform-plugin-docs v0.12.0 h1:EAvFVEoV/wj15t/VSeKVpnAd+BBnIxzYepAnScBWrU4=
174+
github.com/hashicorp/terraform-plugin-docs v0.12.0/go.mod h1:HVn60yjtl4XxLINPgNmPCwX8SQ4T99Ut9CTD/ac6i5w=
167175
github.com/hashicorp/terraform-plugin-go v0.8.0 h1:MvY43PcDj9VlBjYifBWCO/6j1wf106xU8d5Tob/WRs0=
168176
github.com/hashicorp/terraform-plugin-go v0.8.0/go.mod h1:E3GuvfX0Pz2Azcl6BegD6t51StXsVZMOYQoGO8mkHM0=
169177
github.com/hashicorp/terraform-plugin-log v0.3.0 h1:NPENNOjaJSVX0f7JJTl4f/2JKRPQ7S2ZN9B4NSqq5kA=
@@ -308,6 +316,8 @@ golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91
308316
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
309317
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM=
310318
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
319+
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
320+
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
311321
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
312322
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
313323
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
@@ -364,6 +374,8 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYe
364374
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
365375
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
366376
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
377+
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b h1:2n253B2r0pYSmEV+UNCQoPfU/FiaizQEK5Gu4Bq4JE8=
378+
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
367379
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
368380
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
369381
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

pkg/data_sources/job.go

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ var jobSchema = map[string]*schema.Schema{
3131
Required: true,
3232
Description: "ID of the job",
3333
},
34+
"deferring_job_id": &schema.Schema{
35+
Type: schema.TypeInt,
36+
Computed: true,
37+
Description: "ID of the job this job defers to",
38+
},
3439
"triggers": &schema.Schema{
3540
Type: schema.TypeMap,
3641
Computed: true,
@@ -74,6 +79,9 @@ func datasourceJobRead(ctx context.Context, d *schema.ResourceData, m interface{
7479
if err := d.Set("job_id", job.ID); err != nil {
7580
return diag.FromErr(err)
7681
}
82+
if err := d.Set("deferring_job_id", job.Deferring_Job_Id); err != nil {
83+
return diag.FromErr(err)
84+
}
7785
var triggers map[string]interface{}
7886
triggersInput, _ := json.Marshal(job.Triggers)
7987
json.Unmarshal(triggersInput, &triggers)

pkg/dbt_cloud/job.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type Job struct {
5757
Generate_Docs bool `json:"generate_docs"`
5858
Schedule JobSchedule `json:"schedule"`
5959
Run_Generate_Sources bool `json:"run_generate_sources"`
60+
Deferring_Job_Id *int `json:"deferring_job_definition_id"`
6061
}
6162

6263
func (c *Client) GetJob(jobID string) (*Job, error) {
@@ -79,7 +80,7 @@ func (c *Client) GetJob(jobID string) (*Job, error) {
7980
return &jobResponse.Data, nil
8081
}
8182

82-
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) (*Job, error) {
83+
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) (*Job, error) {
8384
state := STATE_ACTIVE
8485
if !isActive {
8586
state = STATE_DELETED
@@ -153,6 +154,9 @@ func (c *Client) CreateJob(projectId int, environmentId int, name string, execut
153154
if dbtVersion != "" {
154155
newJob.Dbt_Version = &dbtVersion
155156
}
157+
if deferringJobId != 0 {
158+
newJob.Deferring_Job_Id = &deferringJobId
159+
}
156160
newJobData, err := json.Marshal(newJob)
157161
if err != nil {
158162
return nil, err

pkg/resources/job.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ var jobSchema = map[string]*schema.Schema{
128128
Optional: true,
129129
Description: "Custom cron expression for schedule",
130130
},
131+
"deferring_job_id": &schema.Schema{
132+
Type: schema.TypeInt,
133+
Optional: true,
134+
Description: "Job identifier that this job defers to",
135+
},
131136
}
132137

133138
func ResourceJob() *schema.Resource {
@@ -208,6 +213,9 @@ func resourceJobRead(ctx context.Context, d *schema.ResourceData, m interface{})
208213
if err := d.Set("schedule_cron", job.Schedule.Date.Cron); err != nil {
209214
return diag.FromErr(err)
210215
}
216+
if err := d.Set("deferring_job_id", job.Deferring_Job_Id); err != nil {
217+
return diag.FromErr(err)
218+
}
211219

212220
var triggers map[string]interface{}
213221
triggersInput, _ := json.Marshal(job.Triggers)
@@ -241,6 +249,7 @@ func resourceJobCreate(ctx context.Context, d *schema.ResourceData, m interface{
241249
scheduleHours := d.Get("schedule_hours").([]interface{})
242250
scheduleDays := d.Get("schedule_days").([]interface{})
243251
scheduleCron := d.Get("schedule_cron").(string)
252+
deferringJobId := d.Get("deferring_job_id").(int)
244253

245254
steps := []string{}
246255
for _, step := range executeSteps {
@@ -255,7 +264,7 @@ func resourceJobCreate(ctx context.Context, d *schema.ResourceData, m interface{
255264
days = append(days, day.(int))
256265
}
257266

258-
j, err := c.CreateJob(projectId, environmentId, name, steps, dbtVersion, isActive, triggers, numThreads, targetName, generateDocs, runGenerateSources, scheduleType, scheduleInterval, hours, days, scheduleCron)
267+
j, err := c.CreateJob(projectId, environmentId, name, steps, dbtVersion, isActive, triggers, numThreads, targetName, generateDocs, runGenerateSources, scheduleType, scheduleInterval, hours, days, scheduleCron, deferringJobId)
259268
if err != nil {
260269
return diag.FromErr(err)
261270
}
@@ -275,7 +284,7 @@ func resourceJobUpdate(ctx context.Context, d *schema.ResourceData, m interface{
275284
d.HasChange("target_name") || d.HasChange("execute_steps") || d.HasChange("run_generate_sources") ||
276285
d.HasChange("generate_docs") || d.HasChange("triggers") || d.HasChange("schedule_type") ||
277286
d.HasChange("schedule_interval") || d.HasChange("schedule_hours") || d.HasChange("schedule_days") ||
278-
d.HasChange("schedule_cron") {
287+
d.HasChange("schedule_cron") || d.HasChange("deferringJobId") {
279288
job, err := c.GetJob(jobId)
280289
if err != nil {
281290
return diag.FromErr(err)
@@ -347,6 +356,10 @@ func resourceJobUpdate(ctx context.Context, d *schema.ResourceData, m interface{
347356
scheduleCron := d.Get("schedule_cron").(string)
348357
job.Schedule.Date.Cron = &scheduleCron
349358
}
359+
if d.HasChange("deferring_job_id") {
360+
deferringJobId := d.Get("deferring_job_id").(int)
361+
job.Deferring_Job_Id = &deferringJobId
362+
}
350363

351364
_, err = c.UpdateJob(jobId, *job)
352365
if err != nil {

pkg/resources/job_acceptance_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ func TestAccDbtCloudJobResource(t *testing.T) {
5555
resource.TestCheckResourceAttrSet("dbt_cloud_job.test_job", "generate_docs"),
5656
),
5757
},
58+
// DEFERRING JOBS
59+
{
60+
Config: testAccDbtCloudJobResourceDeferringJobConfig(jobName, jobName2, projectName, environmentName),
61+
Check: resource.ComposeTestCheckFunc(
62+
testAccCheckDbtCloudJobExists("dbt_cloud_job.test_job"),
63+
testAccCheckDbtCloudJobExists("dbt_cloud_job.test_job_2"),
64+
resource.TestCheckResourceAttrSet("dbt_cloud_job.test_job_2", "deferring_job_id"),
65+
),
66+
},
5867
// IMPORT
5968
{
6069
ResourceName: "dbt_cloud_job.test_job",
@@ -134,6 +143,60 @@ resource "dbt_cloud_job" "test_job" {
134143
`, projectName, environmentName, jobName)
135144
}
136145

146+
func testAccDbtCloudJobResourceDeferringJobConfig(jobName, jobName2, projectName, environmentName string) string {
147+
return fmt.Sprintf(`
148+
resource "dbt_cloud_project" "test_job_project" {
149+
name = "%s"
150+
}
151+
152+
resource "dbt_cloud_environment" "test_job_environment" {
153+
project_id = dbt_cloud_project.test_job_project.id
154+
name = "%s"
155+
dbt_version = "0.21.0"
156+
type = "development"
157+
}
158+
159+
resource "dbt_cloud_job" "test_job" {
160+
name = "%s"
161+
project_id = dbt_cloud_project.test_job_project.id
162+
environment_id = dbt_cloud_environment.test_job_environment.environment_id
163+
dbt_version = "0.20.2"
164+
execute_steps = [
165+
"dbt test"
166+
]
167+
triggers = {
168+
"github_webhook": false,
169+
"git_provider_webhook": false,
170+
"schedule": true,
171+
"custom_branch_only": false,
172+
}
173+
is_active = true
174+
num_threads = 37
175+
target_name = "test"
176+
run_generate_sources = true
177+
generate_docs = true
178+
schedule_type = "every_day"
179+
schedule_hours = [9, 17]
180+
}
181+
182+
resource "dbt_cloud_job" "test_job_2" {
183+
name = "%s"
184+
project_id = dbt_cloud_project.test_job_project.id
185+
environment_id = dbt_cloud_environment.test_job_environment.environment_id
186+
execute_steps = [
187+
"dbt test"
188+
]
189+
triggers = {
190+
"github_webhook": false,
191+
"git_provider_webhook": false,
192+
"schedule": false,
193+
"custom_branch_only": false,
194+
}
195+
deferring_job_id = dbt_cloud_job.test_job.id
196+
}
197+
`, projectName, environmentName, jobName, jobName2)
198+
}
199+
137200
func testAccCheckDbtCloudJobExists(resource string) resource.TestCheckFunc {
138201
return func(state *terraform.State) error {
139202
rs, ok := state.RootModule().Resources[resource]

0 commit comments

Comments
 (0)