forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_dataproc_job.go.erb
1296 lines (1150 loc) · 40.8 KB
/
resource_dataproc_job.go.erb
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<% autogen_exception -%>
package google
import (
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
<% if version == "ga" -%>
"google.golang.org/api/dataproc/v1"
<% else -%>
dataproc "google.golang.org/api/dataproc/v1beta2"
<% end -%>
)
var jobTypes = []string{"pyspark_config", "spark_config", "hadoop_config", "hive_config", "pig_config", "sparksql_config", "presto_config"}
func resourceDataprocJob() *schema.Resource {
return &schema.Resource{
Create: resourceDataprocJobCreate,
Update: resourceDataprocJobUpdate,
Read: resourceDataprocJobRead,
Delete: resourceDataprocJobDelete,
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
},
Schema: map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: `The project in which the cluster can be found and jobs subsequently run against. If it is not provided, the provider project is used.`,
},
// Ref: https://cloud.google.com/dataproc/docs/reference/rest/v1/projects.regions.jobs#JobReference
"region": {
Type: schema.TypeString,
Optional: true,
Default: "global",
ForceNew: true,
Description: `The Cloud Dataproc region. This essentially determines which clusters are available for this job to be submitted to. If not specified, defaults to global.`,
},
// If a job is still running, trying to delete a job will fail. Setting
// this flag to true however will force the deletion by first cancelling
// the job and then deleting it
"force_delete": {
Type: schema.TypeBool,
Default: false,
Optional: true,
Description: `By default, you can only delete inactive jobs within Dataproc. Setting this to true, and calling destroy, will ensure that the job is first cancelled before issuing the delete.`,
},
"reference": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Description: `The reference of the job`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"job_id": {
Type: schema.TypeString,
Description: "The job ID, which must be unique within the project. The job ID is generated by the server upon job submission or provided by the user as a means to perform retries without creating duplicate jobs",
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validateRegexp("^[a-zA-Z0-9_-]{1,100}$"),
},
},
},
},
"placement": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Description: `The config of job placement.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cluster_name": {
Type: schema.TypeString,
Description: "The name of the cluster where the job will be submitted",
Required: true,
ForceNew: true,
},
"cluster_uuid": {
Type: schema.TypeString,
Computed: true,
Description: "Output-only. A cluster UUID generated by the Cloud Dataproc service when the job is submitted",
},
},
},
},
"status": {
Type: schema.TypeList,
Computed: true,
Description: `The status of the job.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"state": {
Type: schema.TypeString,
Description: "Output-only. A state message specifying the overall job state",
Computed: true,
},
"details": {
Type: schema.TypeString,
Description: "Output-only. Optional job state details, such as an error description if the state is ERROR",
Computed: true,
},
"state_start_time": {
Type: schema.TypeString,
Description: "Output-only. The time when this state was entered",
Computed: true,
},
"substate": {
Type: schema.TypeString,
Description: "Output-only. Additional state information, which includes status reported by the agent",
Computed: true,
},
},
},
},
"driver_output_resource_uri": {
Type: schema.TypeString,
Description: "Output-only. A URI pointing to the location of the stdout of the job's driver program",
Computed: true,
},
"driver_controls_files_uri": {
Type: schema.TypeString,
Description: "Output-only. If present, the location of miscellaneous control files which may be used as part of job setup and handling. If not present, control files may be placed in the same location as driver_output_uri.",
Computed: true,
},
"labels": {
Type: schema.TypeMap,
Description: "Optional. The labels to associate with this job.",
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"scheduling": {
Type: schema.TypeList,
Description: "Optional. Job scheduling configuration.",
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"max_failures_per_hour": {
Type: schema.TypeInt,
Description: "Maximum number of times per hour a driver may be restarted as a result of driver exiting with non-zero code before job is reported failed.",
Required: true,
ForceNew: true,
ValidateFunc: validation.IntAtMost(10),
},
"max_failures_total": {
Type: schema.TypeInt,
Description: "Maximum number of times in total a driver may be restarted as a result of driver exiting with non-zero code before job is reported failed.",
Required: true,
ForceNew: true,
ValidateFunc: validation.IntAtMost(240),
},
},
},
},
"pyspark_config": pySparkSchema,
"spark_config": sparkSchema,
"hadoop_config": hadoopSchema,
"hive_config": hiveSchema,
"pig_config": pigSchema,
"sparksql_config": sparkSqlSchema,
"presto_config": prestoSchema,
},
UseJSONNumber: true,
}
}
func resourceDataprocJobUpdate(d *schema.ResourceData, meta interface{}) error {
// The only updatable value is currently 'force_delete' which is a local
// only value therefore we don't need to make any GCP calls to update this.
return resourceDataprocJobRead(d, meta)
}
func resourceDataprocJobCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
project, err := getProject(d, config)
if err != nil {
return err
}
clusterName := d.Get("placement.0.cluster_name").(string)
region := d.Get("region").(string)
submitReq := &dataproc.SubmitJobRequest{
Job: &dataproc.Job{
Placement: &dataproc.JobPlacement{
ClusterName: clusterName,
},
Reference: &dataproc.JobReference{
ProjectId: project,
},
},
}
if v, ok := d.GetOk("reference.0.job_id"); ok {
submitReq.Job.Reference.JobId = v.(string)
}
if v, ok := d.GetOk("scheduling"); ok {
config := extractFirstMapConfig(v.([]interface{}))
submitReq.Job.Scheduling = expandJobScheduling(config)
}
if _, ok := d.GetOk("labels"); ok {
submitReq.Job.Labels = expandLabels(d)
}
if v, ok := d.GetOk("pyspark_config"); ok {
config := extractFirstMapConfig(v.([]interface{}))
submitReq.Job.PysparkJob = expandPySparkJob(config)
}
if v, ok := d.GetOk("spark_config"); ok {
config := extractFirstMapConfig(v.([]interface{}))
submitReq.Job.SparkJob = expandSparkJob(config)
}
if v, ok := d.GetOk("hadoop_config"); ok {
config := extractFirstMapConfig(v.([]interface{}))
submitReq.Job.HadoopJob = expandHadoopJob(config)
}
if v, ok := d.GetOk("hive_config"); ok {
config := extractFirstMapConfig(v.([]interface{}))
submitReq.Job.HiveJob = expandHiveJob(config)
}
if v, ok := d.GetOk("pig_config"); ok {
config := extractFirstMapConfig(v.([]interface{}))
submitReq.Job.PigJob = expandPigJob(config)
}
if v, ok := d.GetOk("sparksql_config"); ok {
config := extractFirstMapConfig(v.([]interface{}))
submitReq.Job.SparkSqlJob = expandSparkSqlJob(config)
}
if v, ok := d.GetOk("presto_config"); ok {
config := extractFirstMapConfig(v.([]interface{}))
submitReq.Job.PrestoJob = expandPrestoJob(config)
}
// Submit the job
job, err := config.NewDataprocClient(userAgent).Projects.Regions.Jobs.Submit(
project, region, submitReq).Do()
if err != nil {
return err
}
d.SetId(fmt.Sprintf("projects/%s/regions/%s/jobs/%s", project, region, job.Reference.JobId))
waitErr := dataprocJobOperationWait(config, region, project, job.Reference.JobId,
"Creating Dataproc job", userAgent, d.Timeout(schema.TimeoutCreate))
if waitErr != nil {
return waitErr
}
log.Printf("[INFO] Dataproc job %s has been submitted", job.Reference.JobId)
return resourceDataprocJobRead(d, meta)
}
func resourceDataprocJobRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
region := d.Get("region").(string)
project, err := getProject(d, config)
if err != nil {
return err
}
parts := strings.Split(d.Id(), "/")
jobId := parts[len(parts)-1]
job, err := config.NewDataprocClient(userAgent).Projects.Regions.Jobs.Get(
project, region, jobId).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Dataproc Job %q", jobId))
}
if err := d.Set("force_delete", d.Get("force_delete")); err != nil {
return fmt.Errorf("Error setting force_delete: %s", err)
}
if err := d.Set("labels", job.Labels); err != nil {
return fmt.Errorf("Error setting labels: %s", err)
}
if err := d.Set("driver_output_resource_uri", job.DriverOutputResourceUri); err != nil {
return fmt.Errorf("Error setting driver_output_resource_uri: %s", err)
}
if err := d.Set("driver_controls_files_uri", job.DriverControlFilesUri); err != nil {
return fmt.Errorf("Error setting driver_controls_files_uri: %s", err)
}
if err := d.Set("placement", flattenJobPlacement(job.Placement)); err != nil {
return fmt.Errorf("Error setting placement: %s", err)
}
if err := d.Set("status", flattenJobStatus(job.Status)); err != nil {
return fmt.Errorf("Error setting status: %s", err)
}
if err := d.Set("reference", flattenJobReference(job.Reference)); err != nil {
return fmt.Errorf("Error setting reference: %s", err)
}
if err := d.Set("scheduling", flattenJobScheduling(job.Scheduling)); err != nil {
return fmt.Errorf("Error setting reference: %s", err)
}
if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error setting project: %s", err)
}
if job.PysparkJob != nil {
if err := d.Set("pyspark_config", flattenPySparkJob(job.PysparkJob)); err != nil {
return fmt.Errorf("Error setting pyspark_config: %s", err)
}
}
if job.SparkJob != nil {
if err := d.Set("spark_config", flattenSparkJob(job.SparkJob)); err != nil {
return fmt.Errorf("Error setting spark_config: %s", err)
}
}
if job.HadoopJob != nil {
if err := d.Set("hadoop_config", flattenHadoopJob(job.HadoopJob)); err != nil {
return fmt.Errorf("Error setting hadoop_config: %s", err)
}
}
if job.HiveJob != nil {
if err := d.Set("hive_config", flattenHiveJob(job.HiveJob)); err != nil {
return fmt.Errorf("Error setting hive_config: %s", err)
}
}
if job.PigJob != nil {
if err := d.Set("pig_config", flattenPigJob(job.PigJob)); err != nil {
return fmt.Errorf("Error setting pig_config: %s", err)
}
}
if job.SparkSqlJob != nil {
if err := d.Set("sparksql_config", flattenSparkSqlJob(job.SparkSqlJob)); err != nil {
return fmt.Errorf("Error setting sparksql_config: %s", err)
}
}
if job.PrestoJob != nil {
if err := d.Set("presto_config", flattenPrestoJob(job.PrestoJob)); err != nil {
return fmt.Errorf("Error setting presto_config: %s", err)
}
}
return nil
}
func resourceDataprocJobDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
project, err := getProject(d, config)
if err != nil {
return err
}
region := d.Get("region").(string)
forceDelete := d.Get("force_delete").(bool)
parts := strings.Split(d.Id(), "/")
jobId := parts[len(parts)-1]
if forceDelete {
log.Printf("[DEBUG] Attempting to first cancel Dataproc job %s if it's still running ...", d.Id())
// ignore error if we get one - job may be finished already and not need to
// be cancelled. We do however wait for the state to be one that is
// at least not active
_, _ = config.NewDataprocClient(userAgent).Projects.Regions.Jobs.Cancel(project, region, jobId, &dataproc.CancelJobRequest{}).Do()
waitErr := dataprocJobOperationWait(config, region, project, jobId,
"Cancelling Dataproc job", userAgent, d.Timeout(schema.TimeoutDelete))
if waitErr != nil {
return waitErr
}
}
log.Printf("[DEBUG] Deleting Dataproc job %s", d.Id())
_, err = config.NewDataprocClient(userAgent).Projects.Regions.Jobs.Delete(
project, region, jobId).Do()
if err != nil {
return err
}
waitErr := dataprocDeleteOperationWait(config, region, project, jobId,
"Deleting Dataproc job", userAgent, d.Timeout(schema.TimeoutDelete))
if waitErr != nil {
return waitErr
}
log.Printf("[INFO] Dataproc job %s has been deleted", d.Id())
d.SetId("")
return nil
}
// ---- PySpark Job ----
var loggingConfig = &schema.Schema{
Type: schema.TypeList,
Description: "The runtime logging config of the job",
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"driver_log_levels": {
Type: schema.TypeMap,
Description: "Optional. The per-package log levels for the driver. This may include 'root' package name to configure rootLogger. Examples: 'com.google = FATAL', 'root = INFO', 'org.apache = DEBUG'.",
Required: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
}
var pySparkSchema = &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Description: `The config of pySpark job.`,
ExactlyOneOf: jobTypes,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"main_python_file_uri": {
Type: schema.TypeString,
Description: "Required. The HCFS URI of the main Python file to use as the driver. Must be a .py file",
Required: true,
ForceNew: true,
},
"args": {
Type: schema.TypeList,
Description: "Optional. The arguments to pass to the driver. Do not include arguments, such as --conf, that can be set as job properties, since a collision may occur that causes an incorrect job submission",
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"python_file_uris": {
Type: schema.TypeList,
Description: "Optional. HCFS file URIs of Python files to pass to the PySpark framework. Supported file types: .py, .egg, and .zip",
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"jar_file_uris": {
Type: schema.TypeList,
Description: "Optional. HCFS URIs of jar files to add to the CLASSPATHs of the Python driver and tasks",
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"file_uris": {
Type: schema.TypeList,
Description: "Optional. HCFS URIs of files to be copied to the working directory of Python drivers and distributed tasks. Useful for naively parallel tasks",
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"archive_uris": {
Type: schema.TypeList,
Description: "Optional. HCFS URIs of archives to be extracted in the working directory of .jar, .tar, .tar.gz, .tgz, and .zip",
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"properties": {
Type: schema.TypeMap,
Description: "Optional. A mapping of property names to values, used to configure PySpark. Properties that conflict with values set by the Cloud Dataproc API may be overwritten. Can include properties set in /etc/spark/conf/spark-defaults.conf and classes in user code",
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"logging_config": loggingConfig,
},
},
}
func flattenPySparkJob(job *dataproc.PySparkJob) []map[string]interface{} {
return []map[string]interface{}{
{
"main_python_file_uri": job.MainPythonFileUri,
"args": job.Args,
"python_file_uris": job.PythonFileUris,
"jar_file_uris": job.JarFileUris,
"file_uris": job.FileUris,
"archive_uris": job.ArchiveUris,
"properties": job.Properties,
"logging_config": flattenLoggingConfig(job.LoggingConfig),
},
}
}
func expandPySparkJob(config map[string]interface{}) *dataproc.PySparkJob {
job := &dataproc.PySparkJob{}
if v, ok := config["main_python_file_uri"]; ok {
job.MainPythonFileUri = v.(string)
}
if v, ok := config["args"]; ok {
job.Args = convertStringArr(v.([]interface{}))
}
if v, ok := config["python_file_uris"]; ok {
job.PythonFileUris = convertStringArr(v.([]interface{}))
}
if v, ok := config["jar_file_uris"]; ok {
job.JarFileUris = convertStringArr(v.([]interface{}))
}
if v, ok := config["file_uris"]; ok {
job.FileUris = convertStringArr(v.([]interface{}))
}
if v, ok := config["archive_uris"]; ok {
job.ArchiveUris = convertStringArr(v.([]interface{}))
}
if v, ok := config["properties"]; ok {
job.Properties = convertStringMap(v.(map[string]interface{}))
}
if v, ok := config["logging_config"]; ok {
config := extractFirstMapConfig(v.([]interface{}))
job.LoggingConfig = expandLoggingConfig(config)
}
return job
}
func expandJobScheduling(config map[string]interface{}) *dataproc.JobScheduling {
jobScheduling := &dataproc.JobScheduling{}
if v, ok := config["max_failures_per_hour"]; ok {
jobScheduling.MaxFailuresPerHour = int64(v.(int))
}
if v, ok := config["max_failures_total"]; ok {
jobScheduling.MaxFailuresTotal = int64(v.(int))
}
return jobScheduling
}
// ---- Spark Job ----
var sparkSchema = &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Description: `The config of the Spark job.`,
ExactlyOneOf: jobTypes,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// main driver: can be only one of the class | jar_file
"main_class": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The class containing the main method of the driver. Must be in a provided jar or jar that is already on the classpath. Conflicts with main_jar_file_uri`,
ExactlyOneOf: []string{"spark_config.0.main_class", "spark_config.0.main_jar_file_uri"},
},
"main_jar_file_uri": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The HCFS URI of jar file containing the driver jar. Conflicts with main_class`,
ExactlyOneOf: []string{"spark_config.0.main_jar_file_uri", "spark_config.0.main_class"},
},
"args": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `The arguments to pass to the driver.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"jar_file_uris": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `HCFS URIs of jar files to add to the CLASSPATHs of the Spark driver and tasks.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"file_uris": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `HCFS URIs of files to be copied to the working directory of Spark drivers and distributed tasks. Useful for naively parallel tasks.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"archive_uris": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `HCFS URIs of archives to be extracted in the working directory of .jar, .tar, .tar.gz, .tgz, and .zip.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"properties": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Description: `A mapping of property names to values, used to configure Spark. Properties that conflict with values set by the Cloud Dataproc API may be overwritten. Can include properties set in /etc/spark/conf/spark-defaults.conf and classes in user code.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"logging_config": loggingConfig,
},
},
}
func flattenSparkJob(job *dataproc.SparkJob) []map[string]interface{} {
return []map[string]interface{}{
{
"main_class": job.MainClass,
"main_jar_file_uri": job.MainJarFileUri,
"args": job.Args,
"jar_file_uris": job.JarFileUris,
"file_uris": job.FileUris,
"archive_uris": job.ArchiveUris,
"properties": job.Properties,
"logging_config": flattenLoggingConfig(job.LoggingConfig),
},
}
}
func expandSparkJob(config map[string]interface{}) *dataproc.SparkJob {
job := &dataproc.SparkJob{}
if v, ok := config["main_class"]; ok {
job.MainClass = v.(string)
}
if v, ok := config["main_jar_file_uri"]; ok {
job.MainJarFileUri = v.(string)
}
if v, ok := config["args"]; ok {
job.Args = convertStringArr(v.([]interface{}))
}
if v, ok := config["jar_file_uris"]; ok {
job.JarFileUris = convertStringArr(v.([]interface{}))
}
if v, ok := config["file_uris"]; ok {
job.FileUris = convertStringArr(v.([]interface{}))
}
if v, ok := config["archive_uris"]; ok {
job.ArchiveUris = convertStringArr(v.([]interface{}))
}
if v, ok := config["properties"]; ok {
job.Properties = convertStringMap(v.(map[string]interface{}))
}
if v, ok := config["logging_config"]; ok {
config := extractFirstMapConfig(v.([]interface{}))
job.LoggingConfig = expandLoggingConfig(config)
}
return job
}
// ---- Hadoop Job ----
var hadoopSchema = &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Description: `The config of Hadoop job`,
ExactlyOneOf: jobTypes,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// main driver: can be only one of the main_class | main_jar_file_uri
"main_class": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The class containing the main method of the driver. Must be in a provided jar or jar that is already on the classpath. Conflicts with main_jar_file_uri`,
ExactlyOneOf: []string{"hadoop_config.0.main_jar_file_uri", "hadoop_config.0.main_class"},
},
"main_jar_file_uri": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The HCFS URI of jar file containing the driver jar. Conflicts with main_class`,
ExactlyOneOf: []string{"hadoop_config.0.main_jar_file_uri", "hadoop_config.0.main_class"},
},
"args": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `The arguments to pass to the driver.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"jar_file_uris": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `HCFS URIs of jar files to add to the CLASSPATHs of the Spark driver and tasks.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"file_uris": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `HCFS URIs of files to be copied to the working directory of Spark drivers and distributed tasks. Useful for naively parallel tasks.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"archive_uris": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `HCFS URIs of archives to be extracted in the working directory of .jar, .tar, .tar.gz, .tgz, and .zip.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"properties": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Description: `A mapping of property names to values, used to configure Spark. Properties that conflict with values set by the Cloud Dataproc API may be overwritten. Can include properties set in /etc/spark/conf/spark-defaults.conf and classes in user code.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"logging_config": loggingConfig,
},
},
}
func flattenHadoopJob(job *dataproc.HadoopJob) []map[string]interface{} {
return []map[string]interface{}{
{
"main_class": job.MainClass,
"main_jar_file_uri": job.MainJarFileUri,
"args": job.Args,
"jar_file_uris": job.JarFileUris,
"file_uris": job.FileUris,
"archive_uris": job.ArchiveUris,
"properties": job.Properties,
"logging_config": flattenLoggingConfig(job.LoggingConfig),
},
}
}
func expandHadoopJob(config map[string]interface{}) *dataproc.HadoopJob {
job := &dataproc.HadoopJob{}
if v, ok := config["main_class"]; ok {
job.MainClass = v.(string)
}
if v, ok := config["main_jar_file_uri"]; ok {
job.MainJarFileUri = v.(string)
}
if v, ok := config["args"]; ok {
job.Args = convertStringArr(v.([]interface{}))
}
if v, ok := config["jar_file_uris"]; ok {
job.JarFileUris = convertStringArr(v.([]interface{}))
}
if v, ok := config["file_uris"]; ok {
job.FileUris = convertStringArr(v.([]interface{}))
}
if v, ok := config["archive_uris"]; ok {
job.ArchiveUris = convertStringArr(v.([]interface{}))
}
if v, ok := config["properties"]; ok {
job.Properties = convertStringMap(v.(map[string]interface{}))
}
if v, ok := config["logging_config"]; ok {
config := extractFirstMapConfig(v.([]interface{}))
job.LoggingConfig = expandLoggingConfig(config)
}
return job
}
// ---- Hive Job ----
var hiveSchema = &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Description: `The config of hive job`,
ExactlyOneOf: jobTypes,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// main query: can be only one of query_list | query_file_uri
"query_list": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `The list of Hive queries or statements to execute as part of the job. Conflicts with query_file_uri`,
Elem: &schema.Schema{Type: schema.TypeString},
ExactlyOneOf: []string{"hive_config.0.query_file_uri", "hive_config.0.query_list"},
},
"query_file_uri": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `HCFS URI of file containing Hive script to execute as the job. Conflicts with query_list`,
ExactlyOneOf: []string{"hive_config.0.query_file_uri", "hive_config.0.query_list"},
},
"continue_on_failure": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `Whether to continue executing queries if a query fails. The default value is false. Setting to true can be useful when executing independent parallel queries. Defaults to false.`,
},
"script_variables": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Description: `Mapping of query variable names to values (equivalent to the Hive command: SET name="value";).`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"properties": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Description: `A mapping of property names and values, used to configure Hive. Properties that conflict with values set by the Cloud Dataproc API may be overwritten. Can include properties set in /etc/hadoop/conf/*-site.xml, /etc/hive/conf/hive-site.xml, and classes in user code.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"jar_file_uris": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `HCFS URIs of jar files to add to the CLASSPATH of the Hive server and Hadoop MapReduce (MR) tasks. Can contain Hive SerDes and UDFs.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
}
func flattenHiveJob(job *dataproc.HiveJob) []map[string]interface{} {
queries := []string{}
if job.QueryList != nil {
queries = job.QueryList.Queries
}
return []map[string]interface{}{
{
"query_list": queries,
"query_file_uri": job.QueryFileUri,
"continue_on_failure": job.ContinueOnFailure,
"script_variables": job.ScriptVariables,
"properties": job.Properties,
"jar_file_uris": job.JarFileUris,
},
}
}
func expandHiveJob(config map[string]interface{}) *dataproc.HiveJob {
job := &dataproc.HiveJob{}
if v, ok := config["query_file_uri"]; ok {
job.QueryFileUri = v.(string)
}
if v, ok := config["query_list"]; ok {
job.QueryList = &dataproc.QueryList{
Queries: convertStringArr(v.([]interface{})),
}
}
if v, ok := config["continue_on_failure"]; ok {
job.ContinueOnFailure = v.(bool)
}
if v, ok := config["script_variables"]; ok {
job.ScriptVariables = convertStringMap(v.(map[string]interface{}))
}
if v, ok := config["jar_file_uris"]; ok {
job.JarFileUris = convertStringArr(v.([]interface{}))
}
if v, ok := config["properties"]; ok {
job.Properties = convertStringMap(v.(map[string]interface{}))
}
return job
}
// ---- Pig Job ----
var pigSchema = &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Description: `The config of pag job.`,
ExactlyOneOf: jobTypes,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// main query: can be only one of query_list | query_file_uri
"query_list": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `The list of Hive queries or statements to execute as part of the job. Conflicts with query_file_uri`,
Elem: &schema.Schema{Type: schema.TypeString},
ExactlyOneOf: []string{"pig_config.0.query_file_uri", "pig_config.0.query_list"},
},
"query_file_uri": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `HCFS URI of file containing Hive script to execute as the job. Conflicts with query_list`,
ExactlyOneOf: []string{"pig_config.0.query_file_uri", "pig_config.0.query_list"},
},
"continue_on_failure": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `Whether to continue executing queries if a query fails. The default value is false. Setting to true can be useful when executing independent parallel queries. Defaults to false.`,
},
"script_variables": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Description: `Mapping of query variable names to values (equivalent to the Pig command: name=[value]).`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"properties": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Description: `A mapping of property names to values, used to configure Pig. Properties that conflict with values set by the Cloud Dataproc API may be overwritten. Can include properties set in /etc/hadoop/conf/*-site.xml, /etc/pig/conf/pig.properties, and classes in user code.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"jar_file_uris": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `HCFS URIs of jar files to add to the CLASSPATH of the Pig Client and Hadoop MapReduce (MR) tasks. Can contain Pig UDFs.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"logging_config": loggingConfig,
},
},
}
func flattenPigJob(job *dataproc.PigJob) []map[string]interface{} {
queries := []string{}
if job.QueryList != nil {
queries = job.QueryList.Queries
}
return []map[string]interface{}{
{
"query_list": queries,
"query_file_uri": job.QueryFileUri,