-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathjob.go
673 lines (627 loc) · 20.1 KB
/
job.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
package resources
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/samber/lo"
)
var (
scheduleTypes = []string{
"every_day",
"days_of_week",
"custom_cron",
}
)
var jobSchema = map[string]*schema.Schema{
"project_id": &schema.Schema{
Type: schema.TypeInt,
Required: true,
Description: "Project ID to create the job in",
},
"environment_id": &schema.Schema{
Type: schema.TypeInt,
Required: true,
Description: "Environment ID to create the job in",
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Job name",
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Description for the job",
},
"execute_steps": &schema.Schema{
Type: schema.TypeList,
MinItems: 1,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "List of commands to execute for the job",
},
"dbt_version": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Version number of dbt to use in this job, usually in the format 1.2.0-latest rather than core versions",
},
"is_active": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Flag for whether the job is marked active or deleted. To create/keep a job in a 'deactivated' state, check the `triggers` config.",
},
"triggers": &schema.Schema{
Type: schema.TypeMap,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeBool,
Optional: false,
Default: false,
},
Description: "Flags for which types of triggers to use, the values are `github_webhook`, `git_provider_webhook`, and `schedule`. <br>`custom_branch_only` used to be allowed but has been deprecated from the API. The jobs will use the custom branch of the environment. Please remove the `custom_branch_only` from your config. <br>To create a job in a 'deactivated' state, set all to `false`.",
},
"num_threads": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 1,
Description: "Number of threads to use in the job",
},
"target_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "default",
Description: "Target name for the dbt profile",
},
"generate_docs": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Flag for whether the job should generate documentation",
},
"run_generate_sources": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Flag for whether the job should add a `dbt source freshness` step to the job. The difference between manually adding a step with `dbt source freshness` in the job steps or using this flag is that with this flag, a failed freshness will still allow the following steps to run.",
},
"schedule_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "every_day",
Description: "Type of schedule to use, one of every_day/ days_of_week/ custom_cron",
ValidateFunc: validation.StringInSlice(scheduleTypes, false),
},
"schedule_interval": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 1,
Description: "Number of hours between job executions if running on a schedule",
ValidateFunc: validation.IntBetween(1, 23),
ConflictsWith: []string{"schedule_hours", "schedule_cron"},
},
"schedule_hours": &schema.Schema{
Type: schema.TypeList,
MinItems: 1,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Description: "List of hours to execute the job at if running on a schedule",
ConflictsWith: []string{"schedule_interval", "schedule_cron"},
},
"schedule_days": &schema.Schema{
Type: schema.TypeList,
MinItems: 1,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Description: "List of days of week as numbers (0 = Sunday, 7 = Saturday) to execute the job at if running on a schedule",
},
"schedule_cron": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Custom cron expression for schedule",
ConflictsWith: []string{"schedule_interval", "schedule_hours"},
},
"deferring_job_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Description: "Job identifier that this job defers to (legacy deferring approach)",
ConflictsWith: []string{"self_deferring", "deferring_environment_id"},
},
"deferring_environment_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Description: "Environment identifier that this job defers to (new deferring approach)",
ConflictsWith: []string{"self_deferring", "deferring_job_id"},
},
"self_deferring": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Description: "Whether this job defers on a previous run of itself",
ConflictsWith: []string{"deferring_job_id"},
},
"timeout_seconds": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 0,
Description: "Number of seconds to allow the job to run before timing out",
},
"triggers_on_draft_pr": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether the CI job should be automatically triggered on draft PRs",
},
"job_completion_trigger_condition": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
// using a set or a list with 1 item is the way in the SDKv2 to define nested objects
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"job_id": {
Type: schema.TypeInt,
Required: true,
Description: "The ID of the job that would trigger this job after completion.",
},
"project_id": {
Type: schema.TypeInt,
Required: true,
Description: "The ID of the project where the trigger job is running in.",
},
"statuses": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "List of statuses to trigger the job on. Possible values are `success`, `error` and `canceled`.",
},
},
},
Description: "Which other job should trigger this job when it finishes, and on which conditions (sometimes referred as 'job chaining').",
},
}
func ResourceJob() *schema.Resource {
return &schema.Resource{
CreateContext: resourceJobCreate,
ReadContext: resourceJobRead,
UpdateContext: resourceJobUpdate,
DeleteContext: resourceJobDelete,
Schema: jobSchema,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func resourceJobRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*dbt_cloud.Client)
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics
jobId := d.Id()
job, err := c.GetJob(jobId)
if err != nil {
if strings.HasPrefix(err.Error(), "resource-not-found") {
d.SetId("")
return diags
}
return diag.FromErr(err)
}
if err := d.Set("project_id", job.Project_Id); err != nil {
return diag.FromErr(err)
}
if err := d.Set("environment_id", job.Environment_Id); err != nil {
return diag.FromErr(err)
}
if err := d.Set("name", job.Name); err != nil {
return diag.FromErr(err)
}
if err := d.Set("description", job.Description); err != nil {
return diag.FromErr(err)
}
if err := d.Set("execute_steps", job.Execute_Steps); err != nil {
return diag.FromErr(err)
}
if err := d.Set("dbt_version", job.Dbt_Version); err != nil {
return diag.FromErr(err)
}
if err := d.Set("is_active", job.State == 1); err != nil {
return diag.FromErr(err)
}
if err := d.Set("num_threads", job.Settings.Threads); err != nil {
return diag.FromErr(err)
}
if err := d.Set("target_name", job.Settings.Target_Name); err != nil {
return diag.FromErr(err)
}
if err := d.Set("generate_docs", job.Generate_Docs); err != nil {
return diag.FromErr(err)
}
if err := d.Set("run_generate_sources", job.Run_Generate_Sources); err != nil {
return diag.FromErr(err)
}
if err := d.Set("schedule_type", job.Schedule.Date.Type); err != nil {
return diag.FromErr(err)
}
schedule := 1
if job.Schedule.Time.Interval > 0 {
schedule = job.Schedule.Time.Interval
}
if err := d.Set("schedule_interval", schedule); err != nil {
return diag.FromErr(err)
}
if err := d.Set("schedule_hours", job.Schedule.Time.Hours); err != nil {
return diag.FromErr(err)
}
if err := d.Set("schedule_days", job.Schedule.Date.Days); err != nil {
return diag.FromErr(err)
}
if err := d.Set("schedule_cron", job.Schedule.Date.Cron); err != nil {
return diag.FromErr(err)
}
selfDeferring := job.Deferring_Job_Id != nil && strconv.Itoa(*job.Deferring_Job_Id) == jobId
if !selfDeferring {
if err := d.Set("deferring_job_id", job.Deferring_Job_Id); err != nil {
return diag.FromErr(err)
}
}
if err := d.Set("deferring_environment_id", job.DeferringEnvironmentId); err != nil {
return diag.FromErr(err)
}
if err := d.Set("self_deferring", selfDeferring); err != nil {
return diag.FromErr(err)
}
if err := d.Set("timeout_seconds", job.Execution.Timeout_Seconds); err != nil {
return diag.FromErr(err)
}
var triggers map[string]interface{}
triggersInput, _ := json.Marshal(job.Triggers)
json.Unmarshal(triggersInput, &triggers)
// for now, we allow people to keep the triggers.custom_branch_only config even if the parameter was deprecated in the API
// we set the state to the current config value, so it doesn't do anything
listedTriggers := d.Get("triggers").(map[string]interface{})
listedCustomBranchOnly, ok := listedTriggers["custom_branch_only"].(bool)
if ok {
triggers["custom_branch_only"] = listedCustomBranchOnly
}
if err := d.Set("triggers", triggers); err != nil {
return diag.FromErr(err)
}
if err := d.Set("triggers_on_draft_pr", job.TriggersOnDraftPR); err != nil {
return diag.FromErr(err)
}
if job.JobCompletionTrigger == nil {
if err := d.Set("job_completion_trigger_condition", nil); err != nil {
return diag.FromErr(err)
}
} else {
triggerCondition := job.JobCompletionTrigger.Condition
statusesNames := lo.Map(triggerCondition.Statuses, func(status int, idx int) any {
return utils.JobCompletionTriggerConditionsMappingCodeHuman[status]
})
triggerConditionMap := map[string]any{
"job_id": triggerCondition.JobID,
"project_id": triggerCondition.ProjectID,
"statuses": statusesNames,
}
triggerConditionSet := utils.JobConditionMapToSet(triggerConditionMap)
if err := d.Set("job_completion_trigger_condition", triggerConditionSet); err != nil {
return diag.FromErr(err)
}
}
return diags
}
func resourceJobCreate(
ctx context.Context,
d *schema.ResourceData,
m interface{},
) diag.Diagnostics {
c := m.(*dbt_cloud.Client)
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics
projectId := d.Get("project_id").(int)
environmentId := d.Get("environment_id").(int)
name := d.Get("name").(string)
description := d.Get("description").(string)
executeSteps := d.Get("execute_steps").([]interface{})
dbtVersion := d.Get("dbt_version").(string)
isActive := d.Get("is_active").(bool)
triggers := d.Get("triggers").(map[string]interface{})
numThreads := d.Get("num_threads").(int)
targetName := d.Get("target_name").(string)
generateDocs := d.Get("generate_docs").(bool)
runGenerateSources := d.Get("run_generate_sources").(bool)
scheduleType := d.Get("schedule_type").(string)
scheduleInterval := d.Get("schedule_interval").(int)
scheduleHours := d.Get("schedule_hours").([]interface{})
scheduleDays := d.Get("schedule_days").([]interface{})
scheduleCron := d.Get("schedule_cron").(string)
deferringJobId := d.Get("deferring_job_id").(int)
deferringEnvironmentID := d.Get("deferring_environment_id").(int)
selfDeferring := d.Get("self_deferring").(bool)
timeoutSeconds := d.Get("timeout_seconds").(int)
triggersOnDraftPR := d.Get("triggers_on_draft_pr").(bool)
var jobCompletionTrigger map[string]any
empty, completionJobID, completionProjectID, completionStatuses := utils.ExtractJobConditionSet(
d,
)
if !empty {
jobCompletionTrigger = map[string]any{
"job_id": completionJobID,
"project_id": completionProjectID,
"statuses": completionStatuses,
}
}
steps := []string{}
for _, step := range executeSteps {
steps = append(steps, step.(string))
}
hours := []int{}
for _, hour := range scheduleHours {
hours = append(hours, hour.(int))
}
days := []int{}
for _, day := range scheduleDays {
days = append(days, day.(int))
}
j, err := c.CreateJob(
projectId,
environmentId,
name,
description,
steps,
dbtVersion,
isActive,
triggers,
numThreads,
targetName,
generateDocs,
runGenerateSources,
scheduleType,
scheduleInterval,
hours,
days,
scheduleCron,
deferringJobId,
deferringEnvironmentID,
selfDeferring,
timeoutSeconds,
triggersOnDraftPR,
jobCompletionTrigger,
)
if err != nil {
return diag.FromErr(err)
}
d.SetId(strconv.Itoa(*j.ID))
resourceJobRead(ctx, d, m)
return diags
}
func resourceJobUpdate(
ctx context.Context,
d *schema.ResourceData,
m interface{},
) diag.Diagnostics {
c := m.(*dbt_cloud.Client)
jobId := d.Id()
if d.HasChange("project_id") ||
d.HasChange("environment_id") ||
d.HasChange("name") ||
d.HasChange("description") ||
d.HasChange("dbt_version") ||
d.HasChange("num_threads") ||
d.HasChange("target_name") ||
d.HasChange("execute_steps") ||
d.HasChange("run_generate_sources") ||
d.HasChange("generate_docs") ||
d.HasChange("triggers") ||
d.HasChange("schedule_type") ||
d.HasChange("schedule_interval") ||
d.HasChange("schedule_hours") ||
d.HasChange("schedule_days") ||
d.HasChange("schedule_cron") ||
d.HasChange("deferring_job_id") ||
d.HasChange("deferring_environment_id") ||
d.HasChange("self_deferring") ||
d.HasChange("timeout_seconds") ||
d.HasChange("triggers_on_drat_pr") ||
d.HasChange("job_completion_trigger_condition") {
job, err := c.GetJob(jobId)
if err != nil {
return diag.FromErr(err)
}
if d.HasChange("project_id") {
projectID := d.Get("project_id").(int)
job.Project_Id = projectID
}
if d.HasChange("environment_id") {
envID := d.Get("environment_id").(int)
job.Environment_Id = envID
}
if d.HasChange("name") {
name := d.Get("name").(string)
job.Name = name
}
if d.HasChange("description") {
description := d.Get("description").(string)
job.Description = description
}
if d.HasChange("dbt_version") {
dbtVersion := d.Get("dbt_version").(string)
job.Dbt_Version = &dbtVersion
}
if d.HasChange("num_threads") {
numThreads := d.Get("num_threads").(int)
job.Settings.Threads = numThreads
}
if d.HasChange("target_name") {
targetName := d.Get("target_name").(string)
job.Settings.Target_Name = targetName
}
if d.HasChange("run_generate_sources") {
runGenerateSources := d.Get("run_generate_sources").(bool)
job.Run_Generate_Sources = runGenerateSources
}
if d.HasChange("generate_docs") {
generateDocs := d.Get("generate_docs").(bool)
job.Generate_Docs = generateDocs
}
if d.HasChange("execute_steps") {
executeSteps := make([]string, len(d.Get("execute_steps").([]interface{})))
for i, step := range d.Get("execute_steps").([]interface{}) {
executeSteps[i] = step.(string)
}
job.Execute_Steps = executeSteps
}
if d.HasChange("triggers") {
var ok bool
newTriggers := d.Get("triggers").(map[string]interface{})
job.Triggers.Github_Webhook, ok = newTriggers["github_webhook"].(bool)
if !ok {
return diag.FromErr(fmt.Errorf("github_webhook was not provided"))
}
job.Triggers.GitProviderWebhook, ok = newTriggers["git_provider_webhook"].(bool)
if !ok {
return diag.FromErr(fmt.Errorf("git_provider_webhook was not provided"))
}
job.Triggers.Schedule, ok = newTriggers["schedule"].(bool)
if !ok {
return diag.FromErr(fmt.Errorf("schedule was not provided"))
}
}
if d.HasChange("schedule_interval") {
scheduleInterval := d.Get("schedule_interval").(int)
job.Schedule.Time.Interval = scheduleInterval
}
if d.HasChange("schedule_hours") {
scheduleHours := make([]int, len(d.Get("schedule_hours").([]interface{})))
for i, hour := range d.Get("schedule_hours").([]interface{}) {
scheduleHours[i] = hour.(int)
}
if len(d.Get("schedule_hours").([]interface{})) > 0 {
job.Schedule.Time.Hours = &scheduleHours
job.Schedule.Time.Type = "at_exact_hours"
job.Schedule.Time.Interval = 0
} else {
job.Schedule.Time.Hours = nil
job.Schedule.Time.Interval = d.Get("schedule_interval").(int)
job.Schedule.Time.Type = "every_hour"
}
}
if d.HasChange("schedule_days") {
scheduleDays := make([]int, len(d.Get("schedule_days").([]interface{})))
for i, day := range d.Get("schedule_days").([]interface{}) {
scheduleDays[i] = day.(int)
}
if len(d.Get("schedule_days").([]interface{})) > 0 {
job.Schedule.Date.Days = &scheduleDays
}
}
if d.HasChange("schedule_cron") {
scheduleCron := d.Get("schedule_cron").(string)
job.Schedule.Date.Cron = &scheduleCron
}
// we set this after the subfields to remove the fields not matching the schedule type
// if it was before, some of those fields would be set again
if d.HasChange("schedule_type") {
scheduleType := d.Get("schedule_type").(string)
job.Schedule.Date.Type = scheduleType
if scheduleType == "days_of_week" || scheduleType == "every_day" {
job.Schedule.Date.Cron = nil
}
if scheduleType == "custom_cron" || scheduleType == "every_day" {
job.Schedule.Date.Days = nil
}
}
if d.HasChange("deferring_job_id") {
deferringJobId := d.Get("deferring_job_id").(int)
if deferringJobId != 0 {
job.Deferring_Job_Id = &deferringJobId
} else {
job.Deferring_Job_Id = nil
}
}
if d.HasChange("deferring_environment_id") {
deferringEnvironmentId := d.Get("deferring_environment_id").(int)
if deferringEnvironmentId != 0 {
job.DeferringEnvironmentId = &deferringEnvironmentId
} else {
job.DeferringEnvironmentId = nil
}
}
// If self_deferring has been toggled to true, set deferring_job_id as own ID
// Otherwise, set it back to what deferring_job_id specifies it to be
if d.HasChange("self_deferring") {
if d.Get("self_deferring") == true {
deferringJobID := *job.ID
job.Deferring_Job_Id = &deferringJobID
} else {
deferringJobId := d.Get("deferring_job_id").(int)
if deferringJobId != 0 {
job.Deferring_Job_Id = &deferringJobId
} else {
job.Deferring_Job_Id = nil
}
}
}
if d.HasChange("timeout_seconds") {
timeoutSeconds := d.Get("timeout_seconds").(int)
job.Execution.Timeout_Seconds = timeoutSeconds
}
if d.HasChange("triggers_on_draft_pr") {
triggersOnDraftPR := d.Get("triggers_on_draft_pr").(bool)
job.TriggersOnDraftPR = triggersOnDraftPR
}
if d.HasChange("job_completion_trigger_condition") {
empty, completionJobID, completionProjectID, completionStatuses := utils.ExtractJobConditionSet(
d,
)
if empty {
job.JobCompletionTrigger = nil
} else {
jobCondTrigger := dbt_cloud.JobCompletionTrigger{
Condition: dbt_cloud.JobCompletionTriggerCondition{
JobID: completionJobID,
ProjectID: completionProjectID,
Statuses: completionStatuses,
},
}
job.JobCompletionTrigger = &jobCondTrigger
}
}
_, err = c.UpdateJob(jobId, *job)
if err != nil {
return diag.FromErr(err)
}
}
return resourceJobRead(ctx, d, m)
}
func resourceJobDelete(
ctx context.Context,
d *schema.ResourceData,
m interface{},
) diag.Diagnostics {
c := m.(*dbt_cloud.Client)
jobId := d.Id()
var diags diag.Diagnostics
job, err := c.GetJob(jobId)
if err != nil {
return diag.FromErr(err)
}
job.State = dbt_cloud.STATE_DELETED
_, err = c.UpdateJob(jobId, *job)
if err != nil {
return diag.FromErr(err)
}
return diags
}