This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
forked from yandex-cloud/terraform-provider-yandex
-
Notifications
You must be signed in to change notification settings - Fork 6
/
resource_yandex_dataproc_cluster.go
707 lines (592 loc) · 19.3 KB
/
resource_yandex_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
package yandex
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"google.golang.org/genproto/protobuf/field_mask"
"github.com/yandex-cloud/go-genproto/yandex/cloud/dataproc/v1"
)
const (
yandexDataprocClusterCreateTimeout = 60 * time.Minute
yandexDataprocClusterDeleteTimeout = 60 * time.Minute
yandexDataprocClusterUpdateTimeout = 60 * time.Minute
)
func resourceYandexDataprocCluster() *schema.Resource {
return &schema.Resource{
Create: resourceYandexDataprocClusterCreate,
Read: resourceYandexDataprocClusterRead,
Update: resourceYandexDataprocClusterUpdate,
Delete: resourceYandexDataprocClusterDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(yandexDataprocClusterCreateTimeout),
Update: schema.DefaultTimeout(yandexDataprocClusterUpdateTimeout),
Delete: schema.DefaultTimeout(yandexDataprocClusterDeleteTimeout),
},
SchemaVersion: 0,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"service_account_id": {
Type: schema.TypeString,
Required: true,
},
"cluster_config": {
Type: schema.TypeList,
MaxItems: 1,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subcluster_spec": {
Type: schema.TypeList,
MinItems: 1,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"role": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(
[]string{"MASTERNODE", "DATANODE", "COMPUTENODE"}, false),
},
"resources": {
Type: schema.TypeList,
MaxItems: 1,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"resource_preset_id": {
Type: schema.TypeString,
Required: true,
},
"disk_size": {
Type: schema.TypeInt,
Required: true,
},
"disk_type_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "network-hdd",
},
},
},
},
"subnet_id": {
Type: schema.TypeString,
Required: true,
},
"hosts_count": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntAtLeast(1),
},
"id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"hadoop": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"services": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(dataprocServiceNames(), false),
},
},
"properties": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"ssh_public_keys": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"version_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
},
},
"bucket": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"folder_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
},
"zone_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceYandexDataprocClusterCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
req, err := prepareDataprocCreateClusterRequest(d, config)
if err != nil {
return err
}
ctx, cancel := config.ContextWithTimeout(d.Timeout(schema.TimeoutCreate))
defer cancel()
op, err := config.sdk.WrapOperation(config.sdk.Dataproc().Cluster().Create(ctx, req))
if err != nil {
return fmt.Errorf("error while requesting API to create Data Proc Cluster: %s", err)
}
protoMetadata, err := op.Metadata()
if err != nil {
return fmt.Errorf("error while getting Data Proc Cluster create operation metadata: %s", err)
}
md, ok := protoMetadata.(*dataproc.CreateClusterMetadata)
if !ok {
return fmt.Errorf("could not get Data Proc Cluster ID from create operation metadata")
}
d.SetId(md.ClusterId)
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("error while waiting for operation to create Data Proc Cluster: %s", err)
}
if _, err := op.Response(); err != nil {
return fmt.Errorf("failed to create Data Proc Cluster: %s", err)
}
return resourceYandexDataprocClusterRead(d, meta)
}
func resourceYandexDataprocClusterRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx, cancel := config.ContextWithTimeout(d.Timeout(schema.TimeoutRead))
defer cancel()
cluster, err := config.sdk.Dataproc().Cluster().Get(ctx, &dataproc.GetClusterRequest{
ClusterId: d.Id(),
})
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("cluster %q", d.Id()))
}
return populateDataprocClusterResourceData(d, config, cluster)
}
func populateDataprocClusterResourceData(d *schema.ResourceData, config *Config, cluster *dataproc.Cluster) error {
ctx, cancel := config.ContextWithTimeout(d.Timeout(schema.TimeoutRead))
defer cancel()
subclusters, err := listDataprocSubclusters(ctx, config, cluster.Id)
if err != nil {
return err
}
subclusters = reorderDataprocSubclusters(d, subclusters)
d.Set("folder_id", cluster.FolderId)
d.Set("name", cluster.Name)
d.Set("description", cluster.Description)
d.Set("zone_id", cluster.ZoneId)
d.Set("service_account_id", cluster.ServiceAccountId)
d.Set("bucket", cluster.Bucket)
err = d.Set("labels", cluster.Labels)
if err != nil {
return err
}
err = d.Set("cluster_config", flattenDataprocClusterConfig(cluster, subclusters))
if err != nil {
return err
}
createdAt, err := getTimestamp(cluster.CreatedAt)
if err != nil {
return err
}
d.Set("created_at", createdAt)
return nil
}
func reorderDataprocSubclusters(d *schema.ResourceData, subclusters []*dataproc.Subcluster) []*dataproc.Subcluster {
subclustersReordered := make([]*dataproc.Subcluster, len(subclusters))
subclusterSpecs := d.Get("cluster_config.0.subcluster_spec").([]interface{})
for i, value := range subclusterSpecs {
subclusterSpec := value.(map[string]interface{})
var id, name string
if val, ok := subclusterSpec["id"]; ok {
id = val.(string)
}
if val, ok := subclusterSpec["name"]; ok {
name = val.(string)
}
for j, subcluster := range subclusters {
if subcluster != nil && (subcluster.Id == id || subcluster.Name == name) {
subclustersReordered[i] = subcluster
subclusters[j] = nil
break
}
}
}
j := 0
for i, s := range subclustersReordered {
if s == nil {
subcluster := (*dataproc.Subcluster)(nil)
for ; subcluster == nil; j++ {
subcluster = subclusters[j]
}
subclustersReordered[i] = subcluster
}
}
return subclustersReordered
}
func getDataprocZoneID(d *schema.ResourceData, config *Config) (string, error) {
res, ok := d.GetOk("zone_id")
if !ok {
if config.Zone != "" {
return config.Zone, nil
}
return "", fmt.Errorf("cannot determine zone: please set 'zone_id' key in this resource or at provider level")
}
return res.(string), nil
}
func prepareDataprocCreateClusterRequest(d *schema.ResourceData, meta *Config) (*dataproc.CreateClusterRequest, error) {
folderID, err := getFolderID(d, meta)
if err != nil {
return nil, fmt.Errorf("error getting folder ID while creating Data Proc Cluster: %s", err)
}
zoneID, err := getDataprocZoneID(d, meta)
if err != nil {
return nil, fmt.Errorf("error getting zone while creating Data Proc Cluster: %s", err)
}
labels, err := expandLabels(d.Get("labels"))
if err != nil {
return nil, fmt.Errorf("error while expanding labels on Data Proc Cluster create: %s", err)
}
req := dataproc.CreateClusterRequest{
FolderId: folderID,
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Labels: labels,
ConfigSpec: expandDataprocCreateClusterConfigSpec(d),
ZoneId: zoneID,
ServiceAccountId: d.Get("service_account_id").(string),
Bucket: d.Get("bucket").(string),
}
return &req, nil
}
func listDataprocSubclusters(ctx context.Context, config *Config, id string) ([]*dataproc.Subcluster, error) {
var subclusters []*dataproc.Subcluster
pageToken := ""
for {
resp, err := config.sdk.Dataproc().Subcluster().List(ctx, &dataproc.ListSubclustersRequest{
ClusterId: id,
PageSize: defaultMDBPageSize,
PageToken: pageToken,
})
if err != nil {
return nil, fmt.Errorf("error while getting list of subclusters for Data Proc Cluster %q: %s", id, err)
}
subclusters = append(subclusters, resp.Subclusters...)
if resp.NextPageToken == "" {
break
}
pageToken = resp.NextPageToken
}
return subclusters, nil
}
func resourceYandexDataprocClusterDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
log.Printf("[DEBUG] Deleting Data Proc Cluster %q", d.Id())
req := &dataproc.DeleteClusterRequest{
ClusterId: d.Id(),
}
ctx, cancel := config.ContextWithTimeout(d.Timeout(schema.TimeoutDelete))
defer cancel()
op, err := config.sdk.WrapOperation(config.sdk.Dataproc().Cluster().Delete(ctx, req))
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Data Proc Cluster %q", d.Get("name").(string)))
}
err = op.Wait(ctx)
if err != nil {
return err
}
_, err = op.Response()
if err != nil {
return err
}
log.Printf("[DEBUG] Finished deleting Data Proc Cluster %q", d.Id())
return nil
}
func resourceYandexDataprocClusterUpdate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Updating Data Proc Cluster %q", d.Id())
d.Partial(true)
if err := updateDataprocClusterParams(d, meta); err != nil {
return err
}
if d.HasChange("cluster_config.0.subcluster_spec") {
if err := updateDataprocSubclusters(d, meta); err != nil {
return err
}
}
d.Partial(false)
log.Printf("[DEBUG] Finished updating Data Proc Cluster %q", d.Id())
return resourceYandexDataprocClusterRead(d, meta)
}
func updateDataprocClusterParams(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
req, err := getDataprocClusterUpdateRequest(d)
if err != nil {
return err
}
if req == nil {
return nil
}
ctx, cancel := config.ContextWithTimeout(d.Timeout(schema.TimeoutUpdate))
defer cancel()
op, err := config.sdk.WrapOperation(config.sdk.Dataproc().Cluster().Update(ctx, req))
if err != nil {
return fmt.Errorf("error while requesting API to update Data Proc Cluster %q: %s", d.Id(), err)
}
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("error while updating Data Proc Cluster %q: %s", d.Id(), err)
}
for _, field := range req.UpdateMask.Paths {
d.SetPartial(field)
}
return nil
}
func getDataprocClusterUpdateRequest(d *schema.ResourceData) (*dataproc.UpdateClusterRequest, error) {
labels, err := expandLabels(d.Get("labels"))
if err != nil {
return nil, fmt.Errorf("error while expanding labels on Data Proc Cluster update: %s", err)
}
req := &dataproc.UpdateClusterRequest{
ClusterId: d.Id(),
Description: d.Get("description").(string),
Labels: labels,
Name: d.Get("name").(string),
ServiceAccountId: d.Get("service_account_id").(string),
Bucket: d.Get("bucket").(string),
}
var updatePaths []string
fieldNames := []string{"description", "labels", "name", "service_account_id", "bucket"}
for _, fieldName := range fieldNames {
if d.HasChange(fieldName) {
updatePaths = append(updatePaths, fieldName)
}
}
if len(updatePaths) == 0 {
return nil, nil
}
req.UpdateMask = &field_mask.FieldMask{Paths: updatePaths}
return req, nil
}
func updateDataprocSubclusters(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx, cancel := config.ContextWithTimeout(d.Timeout(schema.TimeoutRead))
defer cancel()
subclusters, err := listDataprocSubclusters(ctx, config, d.Id())
if err != nil {
return err
}
createReqs, updateReqs, deleteReqs, err := partitionDataprocSubclustersByAction(d, subclusters)
if err != nil {
return err
}
for _, deleteReq := range deleteReqs {
err := deleteDataprocSubcluster(deleteReq, config, d.Timeout(schema.TimeoutDelete))
if err != nil {
return err
}
}
for _, createReq := range createReqs {
err := createDataprocSubcluster(createReq, config, d.Timeout(schema.TimeoutCreate))
if err != nil {
return err
}
}
for _, updateReq := range updateReqs {
err := updateDataprocSubcluster(updateReq, config, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}
for _, field := range updateReq.UpdateMask.Paths {
d.SetPartial(field)
}
}
return nil
}
func partitionDataprocSubclustersByAction(d *schema.ResourceData, subclusters []*dataproc.Subcluster) ([]*dataproc.CreateSubclusterRequest,
[]*dataproc.UpdateSubclusterRequest, []*dataproc.DeleteSubclusterRequest, error) {
subclusterSpecs := d.Get("cluster_config.0.subcluster_spec").([]interface{})
updateReqs := make([]*dataproc.UpdateSubclusterRequest, 0, len(subclusters))
createReqs := make([]*dataproc.CreateSubclusterRequest, 0, len(subclusters))
clusterID := d.Id()
subclusterByID := make(map[string]*dataproc.Subcluster)
for _, subcluster := range subclusters {
subclusterByID[subcluster.Id] = subcluster
}
for i, subclusterSpec := range subclusterSpecs {
var id string
if val, ok := subclusterSpec.(map[string]interface{})["id"]; ok {
id = val.(string)
}
if _, exist := subclusterByID[id]; exist {
delete(subclusterByID, id)
path := fmt.Sprintf("cluster_config.0.subcluster_spec.%d", i)
req, err := getDataprocSubclusterUpdateRequest(d, path)
if err != nil {
return nil, nil, nil, err
}
if req != nil {
updateReqs = append(updateReqs, req)
}
} else {
req := getDataprocSubclusterCreateRequest(clusterID, subclusterSpec)
createReqs = append(createReqs, req)
}
}
deleteReqs := make([]*dataproc.DeleteSubclusterRequest, 0, len(subclusters))
for _, subcluster := range subclusterByID {
req := &dataproc.DeleteSubclusterRequest{
ClusterId: clusterID,
SubclusterId: subcluster.Id,
}
deleteReqs = append(deleteReqs, req)
}
return createReqs, updateReqs, deleteReqs, nil
}
func getDataprocSubclusterCreateRequest(clusterID string, element interface{}) *dataproc.CreateSubclusterRequest {
createSpec := expandDataprocSubclusterSpec(element)
return &dataproc.CreateSubclusterRequest{
ClusterId: clusterID,
Name: createSpec.Name,
Role: createSpec.Role,
Resources: createSpec.Resources,
SubnetId: createSpec.SubnetId,
HostsCount: createSpec.HostsCount,
}
}
func getDataprocSubclusterUpdateRequest(d *schema.ResourceData, path string) (*dataproc.UpdateSubclusterRequest, error) {
subclusterSpec := d.Get(path).(map[string]interface{})
resourcesSpec := subclusterSpec["resources"].([]interface{})[0]
req := &dataproc.UpdateSubclusterRequest{
ClusterId: d.Id(),
SubclusterId: subclusterSpec["id"].(string),
Resources: expandDataprocResources(resourcesSpec),
Name: subclusterSpec["name"].(string),
HostsCount: int64(subclusterSpec["hosts_count"].(int)),
}
constFields := []string{"role", "subnet_id"}
for _, fieldName := range constFields {
field := path + "." + fieldName
if d.HasChange(field) {
return nil, fmt.Errorf("error while trying to update Data Proc Subcluster %q:"+
" changing %q of existing subcluster is not supported", req.SubclusterId, fieldName)
}
}
var updatePaths []string
fieldNames := []string{"resources", "name", "hosts_count"}
for _, fieldName := range fieldNames {
field := path + "." + fieldName
if d.HasChange(field) {
updatePaths = append(updatePaths, fieldName)
}
}
if len(updatePaths) == 0 {
return nil, nil
}
req.UpdateMask = &field_mask.FieldMask{Paths: updatePaths}
return req, nil
}
func deleteDataprocSubcluster(deleteReq *dataproc.DeleteSubclusterRequest, config *Config, timeout time.Duration) error {
log.Printf("[DEBUG] Deleting Data Proc Subcluster %q", deleteReq.SubclusterId)
ctx, cancel := config.ContextWithTimeout(timeout)
defer cancel()
op, err := config.sdk.WrapOperation(config.sdk.Dataproc().Subcluster().Delete(ctx, deleteReq))
if err != nil {
return fmt.Errorf("error while requesting API to delete Data Proc Subcluster %q: %s", deleteReq.SubclusterId, err)
}
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("error while deleting Data Proc Subcluster %q: %s", deleteReq.SubclusterId, err)
}
log.Printf("[DEBUG] Deleted Data Proc Subcluster %q", deleteReq.SubclusterId)
return nil
}
func createDataprocSubcluster(createReq *dataproc.CreateSubclusterRequest, config *Config, timeout time.Duration) error {
log.Printf("[DEBUG] Creating Data Proc Subcluster %q", createReq.Name)
ctx, cancel := config.ContextWithTimeout(timeout)
defer cancel()
op, err := config.sdk.WrapOperation(config.sdk.Dataproc().Subcluster().Create(ctx, createReq))
if err != nil {
return fmt.Errorf("error while requesting API to create Data Proc Subcluster %q: %s", createReq.Name, err)
}
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("error while creating Data Proc Subcluster %q: %s", createReq.Name, err)
}
log.Printf("[DEBUG] Created Data Proc Subcluster %q", createReq.Name)
return nil
}
func updateDataprocSubcluster(updateReq *dataproc.UpdateSubclusterRequest, config *Config, timeout time.Duration) error {
log.Printf("[DEBUG] Updating Data Proc Subcluster %q", updateReq.SubclusterId)
ctx, cancel := config.ContextWithTimeout(timeout)
defer cancel()
op, err := config.sdk.WrapOperation(config.sdk.Dataproc().Subcluster().Update(ctx, updateReq))
if err != nil {
return fmt.Errorf("error while requesting API to update Data Proc Subcluster %q: %s", updateReq.SubclusterId, err)
}
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("error while updating Data Proc Subcluster %q: %s", updateReq.SubclusterId, err)
}
log.Printf("[DEBUG] Updated Data Proc Subcluster %q", updateReq.SubclusterId)
return nil
}
func dataprocServiceNames() []string {
names := make([]string, len(dataproc.HadoopConfig_Service_name)-1)
for idx, serviceName := range dataproc.HadoopConfig_Service_name {
if idx > 0 {
names[idx-1] = serviceName
}
}
return names
}