-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
resource_dataproc_cluster.go
1515 lines (1342 loc) · 52.8 KB
/
resource_dataproc_cluster.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
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
package google
import (
"errors"
"fmt"
"log"
"regexp"
"strconv"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
dataproc "google.golang.org/api/dataproc/v1beta2"
)
var (
resolveDataprocImageVersion = regexp.MustCompile(`(?P<Major>[^\s.-]+)\.(?P<Minor>[^\s.-]+)(?:\.(?P<Subminor>[^\s.-]+))?(?:\-(?P<Distr>[^\s.-]+))?`)
gceClusterConfigKeys = []string{
"cluster_config.0.gce_cluster_config.0.zone",
"cluster_config.0.gce_cluster_config.0.network",
"cluster_config.0.gce_cluster_config.0.subnetwork",
"cluster_config.0.gce_cluster_config.0.tags",
"cluster_config.0.gce_cluster_config.0.service_account",
"cluster_config.0.gce_cluster_config.0.service_account_scopes",
"cluster_config.0.gce_cluster_config.0.internal_ip_only",
"cluster_config.0.gce_cluster_config.0.metadata",
}
preemptibleWorkerDiskConfigKeys = []string{
"cluster_config.0.preemptible_worker_config.0.disk_config.0.num_local_ssds",
"cluster_config.0.preemptible_worker_config.0.disk_config.0.boot_disk_size_gb",
"cluster_config.0.preemptible_worker_config.0.disk_config.0.boot_disk_type",
}
clusterSoftwareConfigKeys = []string{
"cluster_config.0.software_config.0.image_version",
"cluster_config.0.software_config.0.override_properties",
"cluster_config.0.software_config.0.optional_components",
}
clusterConfigKeys = []string{
"cluster_config.0.staging_bucket",
"cluster_config.0.gce_cluster_config",
"cluster_config.0.master_config",
"cluster_config.0.worker_config",
"cluster_config.0.preemptible_worker_config",
"cluster_config.0.security_config",
"cluster_config.0.software_config",
"cluster_config.0.initialization_action",
"cluster_config.0.encryption_config",
"cluster_config.0.autoscaling_config",
}
)
func resourceDataprocCluster() *schema.Resource {
return &schema.Resource{
Create: resourceDataprocClusterCreate,
Read: resourceDataprocClusterRead,
Update: resourceDataprocClusterUpdate,
Delete: resourceDataprocClusterDelete,
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Update: schema.DefaultTimeout(20 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The name of the cluster, unique within the project and zone.`,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 55 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 55 characters", k))
}
if !regexp.MustCompile("^[a-z0-9-]+$").MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q can only contain lowercase letters, numbers and hyphens", k))
}
if !regexp.MustCompile("^[a-z]").MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must start with a letter", k))
}
if !regexp.MustCompile("[a-z0-9]$").MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must end with a number or a letter", k))
}
return
},
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: `The ID of the project in which the cluster will exist. If it is not provided, the provider project is used.`,
},
"region": {
Type: schema.TypeString,
Optional: true,
Default: "global",
ForceNew: true,
Description: `The region in which the cluster and associated nodes will be created in. Defaults to global.`,
},
"graceful_decommission_timeout": {
Type: schema.TypeString,
Optional: true,
Default: "0s",
Description: `The timeout duration which allows graceful decomissioning when you change the number of worker nodes directly through a terraform apply`,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
// GCP automatically adds two labels
// 'goog-dataproc-cluster-uuid'
// 'goog-dataproc-cluster-name'
Computed: true,
Description: `The list of labels (key/value pairs) to be applied to instances in the cluster. GCP generates some itself including goog-dataproc-cluster-name which is the name of the cluster.`,
},
"cluster_config": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Description: `Allows you to configure various aspects of the cluster.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"staging_bucket": {
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: clusterConfigKeys,
ForceNew: true,
Description: `The Cloud Storage staging bucket used to stage files, such as Hadoop jars, between client machines and the cluster. Note: If you don't explicitly specify a staging_bucket then GCP will auto create / assign one for you. However, you are not guaranteed an auto generated bucket which is solely dedicated to your cluster; it may be shared with other clusters in the same region/zone also choosing to use the auto generation option.`,
},
// If the user does not specify a staging bucket, GCP will allocate one automatically.
// The staging_bucket field provides a way for the user to supply their own
// staging bucket. The bucket field is purely a computed field which details
// the definitive bucket allocated and in use (either the user supplied one via
// staging_bucket, or the GCP generated one)
"bucket": {
Type: schema.TypeString,
Computed: true,
Description: ` The name of the cloud storage bucket ultimately used to house the staging data for the cluster. If staging_bucket is specified, it will contain this value, otherwise it will be the auto generated name.`,
},
"gce_cluster_config": {
Type: schema.TypeList,
Optional: true,
AtLeastOneOf: clusterConfigKeys,
Computed: true,
MaxItems: 1,
Description: `Common config settings for resources of Google Compute Engine cluster instances, applicable to all instances in the cluster.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"zone": {
Type: schema.TypeString,
Optional: true,
Computed: true,
AtLeastOneOf: gceClusterConfigKeys,
ForceNew: true,
Description: `The GCP zone where your data is stored and used (i.e. where the master and the worker nodes will be created in). If region is set to 'global' (default) then zone is mandatory, otherwise GCP is able to make use of Auto Zone Placement to determine this automatically for you. Note: This setting additionally determines and restricts which computing resources are available for use with other configs such as cluster_config.master_config.machine_type and cluster_config.worker_config.machine_type.`,
},
"network": {
Type: schema.TypeString,
Optional: true,
Computed: true,
AtLeastOneOf: gceClusterConfigKeys,
ForceNew: true,
ConflictsWith: []string{"cluster_config.0.gce_cluster_config.0.subnetwork"},
DiffSuppressFunc: compareSelfLinkOrResourceName,
Description: `The name or self_link of the Google Compute Engine network to the cluster will be part of. Conflicts with subnetwork. If neither is specified, this defaults to the "default" network.`,
},
"subnetwork": {
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: gceClusterConfigKeys,
ForceNew: true,
ConflictsWith: []string{"cluster_config.0.gce_cluster_config.0.network"},
DiffSuppressFunc: compareSelfLinkOrResourceName,
Description: `The name or self_link of the Google Compute Engine subnetwork the cluster will be part of. Conflicts with network.`,
},
"tags": {
Type: schema.TypeSet,
Optional: true,
AtLeastOneOf: gceClusterConfigKeys,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The list of instance tags applied to instances in the cluster. Tags are used to identify valid sources or targets for network firewalls.`,
},
"service_account": {
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: gceClusterConfigKeys,
ForceNew: true,
Description: `The service account to be used by the Node VMs. If not specified, the "default" service account is used.`,
},
"service_account_scopes": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
AtLeastOneOf: gceClusterConfigKeys,
ForceNew: true,
Description: `The set of Google API scopes to be made available on all of the node VMs under the service_account specified. These can be either FQDNs, or scope aliases.`,
Elem: &schema.Schema{
Type: schema.TypeString,
StateFunc: func(v interface{}) string {
return canonicalizeServiceScope(v.(string))
},
},
Set: stringScopeHashcode,
},
"internal_ip_only": {
Type: schema.TypeBool,
Optional: true,
AtLeastOneOf: gceClusterConfigKeys,
ForceNew: true,
Default: false,
Description: `By default, clusters are not restricted to internal IP addresses, and will have ephemeral external IP addresses assigned to each instance. If set to true, all instances in the cluster will only have internal IP addresses. Note: Private Google Access (also known as privateIpGoogleAccess) must be enabled on the subnetwork that the cluster will be launched in.`,
},
"metadata": {
Type: schema.TypeMap,
Optional: true,
AtLeastOneOf: gceClusterConfigKeys,
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: true,
Description: `A map of the Compute Engine metadata entries to add to all instances`,
},
},
},
},
"master_config": instanceConfigSchema("master_config"),
"worker_config": instanceConfigSchema("worker_config"),
// preemptible_worker_config has a slightly different config
"preemptible_worker_config": {
Type: schema.TypeList,
Optional: true,
AtLeastOneOf: clusterConfigKeys,
Computed: true,
MaxItems: 1,
Description: `The Google Compute Engine config settings for the additional (aka preemptible) instances in a cluster.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"num_instances": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: `Specifies the number of preemptible nodes to create. Defaults to 0.`,
AtLeastOneOf: []string{
"cluster_config.0.preemptible_worker_config.0.num_instances",
"cluster_config.0.preemptible_worker_config.0.disk_config",
},
},
// API does not honour this if set ...
// It always uses whatever is specified for the worker_config
// "machine_type": { ... }
// "min_cpu_platform": { ... }
"disk_config": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: `Disk Config`,
AtLeastOneOf: []string{
"cluster_config.0.preemptible_worker_config.0.num_instances",
"cluster_config.0.preemptible_worker_config.0.disk_config",
},
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"num_local_ssds": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
AtLeastOneOf: preemptibleWorkerDiskConfigKeys,
ForceNew: true,
Description: `The amount of local SSD disks that will be attached to each preemptible worker node. Defaults to 0.`,
},
"boot_disk_size_gb": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
AtLeastOneOf: preemptibleWorkerDiskConfigKeys,
ForceNew: true,
ValidateFunc: validation.IntAtLeast(10),
Description: `Size of the primary disk attached to each preemptible worker node, specified in GB. The smallest allowed disk size is 10GB. GCP will default to a predetermined computed value if not set (currently 500GB). Note: If SSDs are not attached, it also contains the HDFS data blocks and Hadoop working directories.`,
},
"boot_disk_type": {
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: preemptibleWorkerDiskConfigKeys,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"pd-standard", "pd-ssd", ""}, false),
Default: "pd-standard",
Description: `The disk type of the primary disk attached to each preemptible worker node. One of "pd-ssd" or "pd-standard". Defaults to "pd-standard".`,
},
},
},
},
"instance_names": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `List of preemptible instance names which have been assigned to the cluster.`,
},
},
},
},
"security_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: `Security related configuration.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"kerberos_config": {
Type: schema.TypeList,
Required: true,
Description: "Kerberos related configuration",
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cross_realm_trust_admin_server": {
Type: schema.TypeString,
Optional: true,
Description: `The admin server (IP or hostname) for the remote trusted realm in a cross realm trust relationship.`,
},
"cross_realm_trust_kdc": {
Type: schema.TypeString,
Optional: true,
Description: `The KDC (IP or hostname) for the remote trusted realm in a cross realm trust relationship.`,
},
"cross_realm_trust_realm": {
Type: schema.TypeString,
Optional: true,
Description: `The remote realm the Dataproc on-cluster KDC will trust, should the user enable cross realm trust.`,
},
"cross_realm_trust_shared_password_uri": {
Type: schema.TypeString,
Optional: true,
Description: `The Cloud Storage URI of a KMS encrypted file containing the shared password between the on-cluster
Kerberos realm and the remote trusted realm, in a cross realm trust relationship.`,
},
"enable_kerberos": {
Type: schema.TypeBool,
Optional: true,
Description: `Flag to indicate whether to Kerberize the cluster.`,
},
"kdc_db_key_uri": {
Type: schema.TypeString,
Optional: true,
Description: `The Cloud Storage URI of a KMS encrypted file containing the master key of the KDC database.`,
},
"key_password_uri": {
Type: schema.TypeString,
Optional: true,
Description: `The Cloud Storage URI of a KMS encrypted file containing the password to the user provided key. For the self-signed certificate, this password is generated by Dataproc.`,
},
"keystore_uri": {
Type: schema.TypeString,
Optional: true,
Description: `The Cloud Storage URI of the keystore file used for SSL encryption. If not provided, Dataproc will provide a self-signed certificate.`,
},
"keystore_password_uri": {
Type: schema.TypeString,
Optional: true,
Description: `The Cloud Storage URI of a KMS encrypted file containing
the password to the user provided keystore. For the self-signed certificate, this password is generated
by Dataproc`,
},
"kms_key_uri": {
Type: schema.TypeString,
Required: true,
Description: `The uri of the KMS key used to encrypt various sensitive files.`,
},
"realm": {
Type: schema.TypeString,
Optional: true,
Description: `The name of the on-cluster Kerberos realm. If not specified, the uppercased domain of hostnames will be the realm.`,
},
"root_principal_password_uri": {
Type: schema.TypeString,
Required: true,
Description: `The cloud Storage URI of a KMS encrypted file containing the root principal password.`,
},
"tgt_lifetime_hours": {
Type: schema.TypeInt,
Optional: true,
Description: `The lifetime of the ticket granting ticket, in hours.`,
},
"truststore_password_uri": {
Type: schema.TypeString,
Optional: true,
Description: `The Cloud Storage URI of a KMS encrypted file containing the password to the user provided truststore. For the self-signed certificate, this password is generated by Dataproc.`,
},
"truststore_uri": {
Type: schema.TypeString,
Optional: true,
Description: `The Cloud Storage URI of the truststore file used for SSL encryption. If not provided, Dataproc will provide a self-signed certificate.`,
},
},
},
},
},
},
},
"software_config": {
Type: schema.TypeList,
Optional: true,
AtLeastOneOf: clusterConfigKeys,
Computed: true,
MaxItems: 1,
Description: `The config settings for software inside the cluster.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"image_version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
AtLeastOneOf: clusterSoftwareConfigKeys,
ForceNew: true,
DiffSuppressFunc: dataprocImageVersionDiffSuppress,
Description: `The Cloud Dataproc image version to use for the cluster - this controls the sets of software versions installed onto the nodes when you create clusters. If not specified, defaults to the latest version.`,
},
"override_properties": {
Type: schema.TypeMap,
Optional: true,
AtLeastOneOf: clusterSoftwareConfigKeys,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `A list of override and additional properties (key/value pairs) used to modify various aspects of the common configuration files used when creating a cluster.`,
},
"properties": {
Type: schema.TypeMap,
Computed: true,
Description: `A list of the properties used to set the daemon config files. This will include any values supplied by the user via cluster_config.software_config.override_properties`,
},
// We have two versions of the properties field here because by default
// dataproc will set a number of default properties for you out of the
// box. If you want to override one or more, if we only had one field,
// you would need to add in all these values as well otherwise you would
// get a diff. To make this easier, 'properties' simply contains the computed
// values (including overrides) for all properties, whilst override_properties
// is only for properties the user specifically wants to override. If nothing
// is overridden, this will be empty.
"optional_components": {
Type: schema.TypeSet,
Optional: true,
AtLeastOneOf: clusterSoftwareConfigKeys,
Description: `The set of optional components to activate on the cluster.`,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{"COMPONENT_UNSPECIFIED", "ANACONDA", "DOCKER", "DRUID", "HBASE", "FLINK",
"HIVE_WEBHCAT", "JUPYTER", "KERBEROS", "PRESTO", "RANGER", "SOLR", "ZEPPELIN", "ZOOKEEPER"}, false),
},
},
},
},
},
"initialization_action": {
Type: schema.TypeList,
Optional: true,
AtLeastOneOf: clusterConfigKeys,
ForceNew: true,
Description: `Commands to execute on each node after config is completed. You can specify multiple versions of these.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"script": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The script to be executed during initialization of the cluster. The script must be a GCS file with a gs:// prefix.`,
},
"timeout_sec": {
Type: schema.TypeInt,
Optional: true,
Default: 300,
ForceNew: true,
Description: `The maximum duration (in seconds) which script is allowed to take to execute its action. GCP will default to a predetermined computed value if not set (currently 300).`,
},
},
},
},
"encryption_config": {
Type: schema.TypeList,
Optional: true,
AtLeastOneOf: clusterConfigKeys,
MaxItems: 1,
Description: `The Customer managed encryption keys settings for the cluster.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"kms_key_name": {
Type: schema.TypeString,
Required: true,
Description: `The Cloud KMS key name to use for PD disk encryption for all instances in the cluster.`,
},
},
},
},
"autoscaling_config": {
Type: schema.TypeList,
Optional: true,
AtLeastOneOf: clusterConfigKeys,
MaxItems: 1,
Description: `The autoscaling policy config associated with the cluster.`,
DiffSuppressFunc: emptyOrUnsetBlockDiffSuppress,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"policy_uri": {
Type: schema.TypeString,
Required: true,
Description: `The autoscaling policy used by the cluster.`,
DiffSuppressFunc: locationDiffSuppress,
},
},
},
},
},
},
},
},
}
}
func instanceConfigSchema(parent string) *schema.Schema {
var instanceConfigKeys = []string{
"cluster_config.0." + parent + ".0.num_instances",
"cluster_config.0." + parent + ".0.image_uri",
"cluster_config.0." + parent + ".0.machine_type",
"cluster_config.0." + parent + ".0.min_cpu_platform",
"cluster_config.0." + parent + ".0.disk_config",
"cluster_config.0." + parent + ".0.accelerators",
}
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
Computed: true,
AtLeastOneOf: clusterConfigKeys,
MaxItems: 1,
Description: `The Google Compute Engine config settings for the master/worker instances in a cluster.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"num_instances": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: `Specifies the number of master/worker nodes to create. If not specified, GCP will default to a predetermined computed value.`,
AtLeastOneOf: instanceConfigKeys,
},
"image_uri": {
Type: schema.TypeString,
Optional: true,
Computed: true,
AtLeastOneOf: instanceConfigKeys,
ForceNew: true,
Description: `The URI for the image to use for this master/worker`,
},
"machine_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
AtLeastOneOf: instanceConfigKeys,
ForceNew: true,
Description: `The name of a Google Compute Engine machine type to create for the master/worker`,
},
"min_cpu_platform": {
Type: schema.TypeString,
Optional: true,
Computed: true,
AtLeastOneOf: instanceConfigKeys,
ForceNew: true,
Description: `The name of a minimum generation of CPU family for the master/worker. If not specified, GCP will default to a predetermined computed value for each zone.`,
},
"disk_config": {
Type: schema.TypeList,
Optional: true,
Computed: true,
AtLeastOneOf: instanceConfigKeys,
MaxItems: 1,
Description: `Disk Config`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"num_local_ssds": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: `The amount of local SSD disks that will be attached to each master cluster node. Defaults to 0.`,
AtLeastOneOf: []string{
"cluster_config.0." + parent + ".0.disk_config.0.num_local_ssds",
"cluster_config.0." + parent + ".0.disk_config.0.boot_disk_size_gb",
"cluster_config.0." + parent + ".0.disk_config.0.boot_disk_type",
},
ForceNew: true,
},
"boot_disk_size_gb": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: `Size of the primary disk attached to each node, specified in GB. The primary disk contains the boot volume and system libraries, and the smallest allowed disk size is 10GB. GCP will default to a predetermined computed value if not set (currently 500GB). Note: If SSDs are not attached, it also contains the HDFS data blocks and Hadoop working directories.`,
AtLeastOneOf: []string{
"cluster_config.0." + parent + ".0.disk_config.0.num_local_ssds",
"cluster_config.0." + parent + ".0.disk_config.0.boot_disk_size_gb",
"cluster_config.0." + parent + ".0.disk_config.0.boot_disk_type",
},
ForceNew: true,
ValidateFunc: validation.IntAtLeast(10),
},
"boot_disk_type": {
Type: schema.TypeString,
Optional: true,
Description: `The disk type of the primary disk attached to each node. One of "pd-ssd" or "pd-standard". Defaults to "pd-standard".`,
AtLeastOneOf: []string{
"cluster_config.0." + parent + ".0.disk_config.0.num_local_ssds",
"cluster_config.0." + parent + ".0.disk_config.0.boot_disk_size_gb",
"cluster_config.0." + parent + ".0.disk_config.0.boot_disk_type",
},
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"pd-standard", "pd-ssd", ""}, false),
Default: "pd-standard",
},
},
},
},
// Note: preemptible workers don't support accelerators
"accelerators": {
Type: schema.TypeSet,
Optional: true,
AtLeastOneOf: instanceConfigKeys,
ForceNew: true,
Elem: acceleratorsSchema(),
Description: `The Compute Engine accelerator (GPU) configuration for these instances. Can be specified multiple times.`,
},
"instance_names": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `List of master/worker instance names which have been assigned to the cluster.`,
},
},
},
}
}
// We need to pull accelerators' schema out so we can use it to make a set hash func
func acceleratorsSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"accelerator_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The short name of the accelerator type to expose to this instance. For example, nvidia-tesla-k80.`,
},
"accelerator_count": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: `The number of the accelerator cards of this type exposed to this instance. Often restricted to one of 1, 2, 4, or 8.`,
},
},
}
}
func resourceDataprocClusterCreate(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)
cluster := &dataproc.Cluster{
ClusterName: d.Get("name").(string),
ProjectId: project,
}
cluster.Config, err = expandClusterConfig(d, config)
if err != nil {
return err
}
if _, ok := d.GetOk("labels"); ok {
cluster.Labels = expandLabels(d)
}
// Checking here caters for the case where the user does not specify cluster_config
// at all, as well where it is simply missing from the gce_cluster_config
if region == "global" && cluster.Config.GceClusterConfig.ZoneUri == "" {
return errors.New("zone is mandatory when region is set to 'global'")
}
// Create the cluster
op, err := config.NewDataprocBetaClient(userAgent).Projects.Regions.Clusters.Create(
project, region, cluster).Do()
if err != nil {
return fmt.Errorf("Error creating Dataproc cluster: %s", err)
}
d.SetId(fmt.Sprintf("projects/%s/regions/%s/clusters/%s", project, region, cluster.ClusterName))
// Wait until it's created
waitErr := dataprocClusterOperationWait(config, op, "creating Dataproc cluster", userAgent, d.Timeout(schema.TimeoutCreate))
if waitErr != nil {
// The resource didn't actually create
// Note that we do not remove the ID here - this resource tends to leave
// partially created clusters behind, so we'll let the next Read remove
// it.
return waitErr
}
log.Printf("[INFO] Dataproc cluster %s has been created", cluster.ClusterName)
return resourceDataprocClusterRead(d, meta)
}
func expandClusterConfig(d *schema.ResourceData, config *Config) (*dataproc.ClusterConfig, error) {
conf := &dataproc.ClusterConfig{
// SDK requires GceClusterConfig to be specified,
// even if no explicit values specified
GceClusterConfig: &dataproc.GceClusterConfig{},
}
if v, ok := d.GetOk("cluster_config"); ok {
confs := v.([]interface{})
if (len(confs)) == 0 {
return conf, nil
}
}
if v, ok := d.GetOk("cluster_config.0.staging_bucket"); ok {
conf.ConfigBucket = v.(string)
}
c, err := expandGceClusterConfig(d, config)
if err != nil {
return nil, err
}
conf.GceClusterConfig = c
if cfg, ok := configOptions(d, "cluster_config.0.security_config"); ok {
conf.SecurityConfig = expandSecurityConfig(cfg)
}
if cfg, ok := configOptions(d, "cluster_config.0.software_config"); ok {
conf.SoftwareConfig = expandSoftwareConfig(cfg)
}
if v, ok := d.GetOk("cluster_config.0.initialization_action"); ok {
conf.InitializationActions = expandInitializationActions(v)
}
if cfg, ok := configOptions(d, "cluster_config.0.encryption_config"); ok {
conf.EncryptionConfig = expandEncryptionConfig(cfg)
}
if cfg, ok := configOptions(d, "cluster_config.0.autoscaling_config"); ok {
conf.AutoscalingConfig = expandAutoscalingConfig(cfg)
}
if cfg, ok := configOptions(d, "cluster_config.0.master_config"); ok {
log.Println("[INFO] got master_config")
conf.MasterConfig = expandInstanceGroupConfig(cfg)
}
if cfg, ok := configOptions(d, "cluster_config.0.worker_config"); ok {
log.Println("[INFO] got worker config")
conf.WorkerConfig = expandInstanceGroupConfig(cfg)
}
if cfg, ok := configOptions(d, "cluster_config.0.preemptible_worker_config"); ok {
log.Println("[INFO] got preemptible worker config")
conf.SecondaryWorkerConfig = expandPreemptibleInstanceGroupConfig(cfg)
if conf.SecondaryWorkerConfig.NumInstances > 0 {
conf.SecondaryWorkerConfig.IsPreemptible = true
}
}
return conf, nil
}
func expandGceClusterConfig(d *schema.ResourceData, config *Config) (*dataproc.GceClusterConfig, error) {
conf := &dataproc.GceClusterConfig{}
v, ok := d.GetOk("cluster_config.0.gce_cluster_config")
if !ok {
return conf, nil
}
cfg := v.([]interface{})[0].(map[string]interface{})
if v, ok := cfg["zone"]; ok {
conf.ZoneUri = v.(string)
}
if v, ok := cfg["network"]; ok {
nf, err := ParseNetworkFieldValue(v.(string), d, config)
if err != nil {
return nil, fmt.Errorf("cannot determine self_link for network %q: %s", v, err)
}
conf.NetworkUri = nf.RelativeLink()
}
if v, ok := cfg["subnetwork"]; ok {
snf, err := ParseSubnetworkFieldValue(v.(string), d, config)
if err != nil {
return nil, fmt.Errorf("cannot determine self_link for subnetwork %q: %s", v, err)
}
conf.SubnetworkUri = snf.RelativeLink()
}
if v, ok := cfg["tags"]; ok {
conf.Tags = convertStringSet(v.(*schema.Set))
}
if v, ok := cfg["service_account"]; ok {
conf.ServiceAccount = v.(string)
}
if scopes, ok := cfg["service_account_scopes"]; ok {
scopesSet := scopes.(*schema.Set)
scopes := make([]string, scopesSet.Len())
for i, scope := range scopesSet.List() {
scopes[i] = canonicalizeServiceScope(scope.(string))
}
conf.ServiceAccountScopes = scopes
}
if v, ok := cfg["internal_ip_only"]; ok {
conf.InternalIpOnly = v.(bool)
}
if v, ok := cfg["metadata"]; ok {
conf.Metadata = convertStringMap(v.(map[string]interface{}))
}
return conf, nil
}
func expandSecurityConfig(cfg map[string]interface{}) *dataproc.SecurityConfig {
conf := &dataproc.SecurityConfig{}
if kfg, ok := cfg["kerberos_config"]; ok {
conf.KerberosConfig = expandKerberosConfig(kfg.([]interface{})[0].(map[string]interface{}))
}
return conf
}
func expandKerberosConfig(cfg map[string]interface{}) *dataproc.KerberosConfig {
conf := &dataproc.KerberosConfig{}
if v, ok := cfg["enable_kerberos"]; ok {
conf.EnableKerberos = v.(bool)
}
if v, ok := cfg["root_principal_password_uri"]; ok {
conf.RootPrincipalPasswordUri = v.(string)
}
if v, ok := cfg["kms_key_uri"]; ok {
conf.KmsKeyUri = v.(string)
}
if v, ok := cfg["keystore_uri"]; ok {
conf.KeystoreUri = v.(string)
}
if v, ok := cfg["truststore_uri"]; ok {
conf.TruststoreUri = v.(string)
}
if v, ok := cfg["keystore_password_uri"]; ok {
conf.KeystorePasswordUri = v.(string)
}
if v, ok := cfg["key_password_uri"]; ok {
conf.KeyPasswordUri = v.(string)
}
if v, ok := cfg["truststore_password_uri"]; ok {
conf.TruststorePasswordUri = v.(string)
}
if v, ok := cfg["cross_realm_trust_realm"]; ok {
conf.CrossRealmTrustRealm = v.(string)
}
if v, ok := cfg["cross_realm_trust_kdc"]; ok {
conf.CrossRealmTrustKdc = v.(string)
}
if v, ok := cfg["cross_realm_trust_admin_server"]; ok {
conf.CrossRealmTrustAdminServer = v.(string)
}
if v, ok := cfg["cross_realm_trust_shared_password_uri"]; ok {
conf.CrossRealmTrustSharedPasswordUri = v.(string)
}
if v, ok := cfg["kdc_db_key_uri"]; ok {
conf.KdcDbKeyUri = v.(string)
}
if v, ok := cfg["tgt_lifetime_hours"]; ok {
conf.TgtLifetimeHours = int64(v.(int))
}
if v, ok := cfg["realm"]; ok {
conf.Realm = v.(string)
}
return conf
}
func expandSoftwareConfig(cfg map[string]interface{}) *dataproc.SoftwareConfig {
conf := &dataproc.SoftwareConfig{}
if v, ok := cfg["override_properties"]; ok {
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
conf.Properties = m
}
if v, ok := cfg["image_version"]; ok {
conf.ImageVersion = v.(string)
}
if components, ok := cfg["optional_components"]; ok {
compSet := components.(*schema.Set)
components := make([]string, compSet.Len())
for i, component := range compSet.List() {
components[i] = component.(string)
}
conf.OptionalComponents = components
}
return conf
}
func expandEncryptionConfig(cfg map[string]interface{}) *dataproc.EncryptionConfig {
conf := &dataproc.EncryptionConfig{}
if v, ok := cfg["kms_key_name"]; ok {
conf.GcePdKmsKeyName = v.(string)
}
return conf
}
func expandAutoscalingConfig(cfg map[string]interface{}) *dataproc.AutoscalingConfig {
conf := &dataproc.AutoscalingConfig{}
if v, ok := cfg["policy_uri"]; ok {
conf.PolicyUri = v.(string)
}
return conf
}
func expandInitializationActions(v interface{}) []*dataproc.NodeInitializationAction {
actionList := v.([]interface{})
actions := []*dataproc.NodeInitializationAction{}
for _, v1 := range actionList {
actionItem := v1.(map[string]interface{})
action := &dataproc.NodeInitializationAction{
ExecutableFile: actionItem["script"].(string),
}
if x, ok := actionItem["timeout_sec"]; ok {
action.ExecutionTimeout = strconv.Itoa(x.(int)) + "s"
}
actions = append(actions, action)
}
return actions
}
func expandPreemptibleInstanceGroupConfig(cfg map[string]interface{}) *dataproc.InstanceGroupConfig {
icg := &dataproc.InstanceGroupConfig{}
if v, ok := cfg["num_instances"]; ok {
icg.NumInstances = int64(v.(int))
}
if dc, ok := cfg["disk_config"]; ok {