-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_mapreduce_clusters.go
670 lines (640 loc) · 21.3 KB
/
data_source_huaweicloud_mapreduce_clusters.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
// ---------------------------------------------------------------
// *** AUTO GENERATED CODE ***
// @Product MRS
// ---------------------------------------------------------------
package mrs
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API MRS GET /v1.1/{project_id}/cluster_infos
func DataSourceMrsClusters() *schema.Resource {
return &schema.Resource{
ReadContext: resourceMrsClustersRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: `The name of cluster.`,
},
"status": {
Type: schema.TypeString,
Optional: true,
Description: `The status of cluster.`,
},
"enterprise_project_id": {
Type: schema.TypeString,
Optional: true,
Description: `The enterprise project ID used to query clusters in a specified enterprise project.`,
},
"tags": {
Type: schema.TypeString,
Optional: true,
Description: `You can search for a cluster by its tags.`,
},
"clusters": {
Type: schema.TypeList,
Elem: mrsClustersClustersSchema(),
Computed: true,
Description: `The list of clusters.`,
},
},
}
}
func mrsClustersClustersSchema() *schema.Resource {
sc := schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: `Cluster ID.`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `Cluster name.`,
},
"master_node_num": {
Type: schema.TypeString,
Computed: true,
Description: `Number of Master nodes deployed in a cluster.`,
},
"core_node_num": {
Type: schema.TypeString,
Computed: true,
Description: `Number of Core nodes deployed in a cluster.`,
},
"total_node_num": {
Type: schema.TypeString,
Computed: true,
Description: `Total number of nodes deployed in a cluster.`,
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: `Cluster status.`,
},
"billing_type": {
Type: schema.TypeString,
Computed: true,
Description: `Cluster billing mode.`,
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
Description: `VPC ID.`,
},
"subnet_id": {
Type: schema.TypeString,
Computed: true,
Description: `Subnet ID.`,
},
"duration": {
Type: schema.TypeString,
Computed: true,
Description: `Cluster subscription duration.`,
},
"fee": {
Type: schema.TypeString,
Computed: true,
Description: `Cluster creation fee, which is automatically calculated.`,
},
"hadoop_version": {
Type: schema.TypeString,
Computed: true,
Description: `Hadoop version.`,
},
"master_node_size": {
Type: schema.TypeString,
Computed: true,
Description: `Instance specifications of a Master node.`,
},
"core_node_size": {
Type: schema.TypeString,
Computed: true,
Description: `Instance specifications of a Core node.`,
},
"component_list": {
Type: schema.TypeList,
Elem: mrsClustersClustersComponentListSchema(),
Computed: true,
Description: `Component list.`,
},
"external_ip": {
Type: schema.TypeString,
Computed: true,
Description: `External IP address.`,
},
"external_alternate_ip": {
Type: schema.TypeString,
Computed: true,
Description: `Backup external IP address.`,
},
"internal_ip": {
Type: schema.TypeString,
Computed: true,
Description: `Internal IP address.`,
},
"deployment_id": {
Type: schema.TypeString,
Computed: true,
Description: `Cluster deployment ID.`,
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: `Cluster description.`,
},
"order_id": {
Type: schema.TypeString,
Computed: true,
Description: `Cluster creation order ID.`,
},
"master_node_product_id": {
Type: schema.TypeString,
Computed: true,
Description: `Product ID of a Master node.`,
},
"master_node_spec_id": {
Type: schema.TypeString,
Computed: true,
Description: `Specification ID of a Master node.`,
},
"core_node_product_id": {
Type: schema.TypeString,
Computed: true,
Description: `Product ID of a Core node.`,
},
"core_node_spec_id": {
Type: schema.TypeString,
Computed: true,
Description: `Specification ID of a Core node.`,
},
"availability_zone": {
Type: schema.TypeString,
Computed: true,
Description: `The AZ.`,
},
"vnc": {
Type: schema.TypeString,
Computed: true,
Description: `URI for remotely logging in to an ECS.`,
},
"volume_size": {
Type: schema.TypeInt,
Computed: true,
Description: `Disk storage space.`,
},
"volume_type": {
Type: schema.TypeString,
Computed: true,
Description: `Disk type.`,
},
"enterprise_project_id": {
Type: schema.TypeString,
Computed: true,
Description: `Enterprise project ID.`,
},
"type": {
Type: schema.TypeInt,
Computed: true,
Description: `Cluster type.`,
},
"security_group_id": {
Type: schema.TypeString,
Computed: true,
Description: `Security group ID.`,
},
"slave_security_group_id": {
Type: schema.TypeString,
Computed: true,
Description: `Security group ID of a non-Master node.`,
},
"stage_desc": {
Type: schema.TypeString,
Computed: true,
Description: `Cluster progress description.`,
},
"safe_mode": {
Type: schema.TypeInt,
Computed: true,
Description: `Running mode of an MRS cluster.`,
},
"version": {
Type: schema.TypeString,
Computed: true,
Description: `Cluster version.`,
},
"node_public_cert_name": {
Type: schema.TypeString,
Computed: true,
Description: `Name of the key file.`,
},
"master_node_ip": {
Type: schema.TypeString,
Computed: true,
Description: `IP address of a Master node.`,
},
"private_ip_first": {
Type: schema.TypeString,
Computed: true,
Description: `Preferred private IP address.`,
},
"tags": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"log_collection": {
Type: schema.TypeInt,
Computed: true,
Description: `Whether to collect logs when cluster installation fails.`,
},
"task_node_groups": {
Type: schema.TypeList,
Elem: mrsClustersClustersNodeGroupSchema(),
Computed: true,
Description: `List of Task nodes.`,
},
"node_groups": {
Type: schema.TypeList,
Elem: mrsClustersClustersNodeGroupSchema(),
Computed: true,
Description: `List of Master, Core and Task nodes.`,
},
"master_data_volume_type": {
Type: schema.TypeString,
Computed: true,
Description: `Data disk storage type of the Master node.`,
},
"master_data_volume_size": {
Type: schema.TypeInt,
Computed: true,
Description: `Data disk storage space of the Master node`,
},
"master_data_volume_count": {
Type: schema.TypeInt,
Computed: true,
Description: `Number of data disks of the Master node`,
},
"core_data_volume_type": {
Type: schema.TypeString,
Computed: true,
Description: `Data disk storage type of the Core node.`,
},
"core_data_volume_size": {
Type: schema.TypeInt,
Computed: true,
Description: `Data disk storage space of the Core node.`,
},
"core_data_volume_count": {
Type: schema.TypeInt,
Computed: true,
Description: `Number of data disks of the Core node.`,
},
"period_type": {
Type: schema.TypeInt,
Computed: true,
Description: `Whether the subscription type is yearly or monthly.`,
},
"scale": {
Type: schema.TypeString,
Computed: true,
Description: `Status of node changes.`,
},
"eip_id": {
Type: schema.TypeString,
Computed: true,
Description: `Unique ID of the cluster EIP.`,
},
"eip_address": {
Type: schema.TypeString,
Computed: true,
Description: `IPv4 address of the cluster EIP.`,
},
"eipv6_address": {
Type: schema.TypeString,
Computed: true,
Description: `IPv6 address of the cluster EIP.`,
},
},
}
return &sc
}
func mrsClustersClustersComponentListSchema() *schema.Resource {
sc := schema.Resource{
Schema: map[string]*schema.Schema{
"component_id": {
Type: schema.TypeString,
Computed: true,
Description: `Component ID.`,
},
"component_name": {
Type: schema.TypeString,
Computed: true,
Description: `Component name.`,
},
"component_version": {
Type: schema.TypeString,
Computed: true,
Description: `Component version.`,
},
"component_desc": {
Type: schema.TypeString,
Computed: true,
Description: `Component description.`,
},
},
}
return &sc
}
func mrsClustersClustersNodeGroupSchema() *schema.Resource {
sc := schema.Resource{
Schema: map[string]*schema.Schema{
"group_name": {
Type: schema.TypeString,
Computed: true,
Description: `Node group name.`,
},
"node_num": {
Type: schema.TypeInt,
Computed: true,
Description: `Number of nodes in a node group.`,
},
"node_size": {
Type: schema.TypeString,
Computed: true,
Description: `Instance specifications of a node group.`,
},
"node_spec_id": {
Type: schema.TypeString,
Computed: true,
Description: `Instance specification ID of a node group.`,
},
"node_product_id": {
Type: schema.TypeString,
Computed: true,
Description: `Instance product ID of a node group.`,
},
"vm_product_id": {
Type: schema.TypeString,
Computed: true,
Description: `VM product ID of a node group.`,
},
"vm_spec_code": {
Type: schema.TypeString,
Computed: true,
Description: `VM specification code of a node group.`,
},
"root_volume_size": {
Type: schema.TypeInt,
Computed: true,
Description: `Root disk storage space of a node group.`,
},
"root_volume_type": {
Type: schema.TypeString,
Computed: true,
Description: `Root disk storage type of a node group.`,
},
"root_volume_product_id": {
Type: schema.TypeString,
Computed: true,
Description: `Root disk product ID of a node group.`,
},
"root_volume_resource_spec_code": {
Type: schema.TypeString,
Computed: true,
Description: `Root disk specification code of a node group.`,
},
"root_volume_resource_type": {
Type: schema.TypeString,
Computed: true,
Description: `System disk product type of a node group.`,
},
"data_volume_type": {
Type: schema.TypeString,
Computed: true,
Description: `Data disk storage type of a node group.`,
},
"data_volume_count": {
Type: schema.TypeInt,
Computed: true,
Description: `Number of data disks of a node group.`,
},
"data_volume_size": {
Type: schema.TypeInt,
Computed: true,
Description: `Data disk storage space of a node group.`,
},
"data_volume_product_id": {
Type: schema.TypeString,
Computed: true,
Description: `Data disk product ID of a node group.`,
},
"data_volume_resource_spec_code": {
Type: schema.TypeString,
Computed: true,
Description: `Data disk specification code of a node group.`,
},
"data_volume_resource_type": {
Type: schema.TypeString,
Computed: true,
Description: `Data disk product type of a node group.`,
},
},
}
return &sc
}
func resourceMrsClustersRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
var mErr *multierror.Error
// getClusters: Query clusters
var (
getClustersHttpUrl = "v1.1/{project_id}/cluster_infos"
getClustersProduct = "mrs"
)
getClustersClient, err := cfg.NewServiceClient(getClustersProduct, region)
if err != nil {
return diag.Errorf("error creating MRS client: %s", err)
}
getClustersPath := getClustersClient.Endpoint + getClustersHttpUrl
getClustersPath = strings.ReplaceAll(getClustersPath, "{project_id}", getClustersClient.ProjectID)
getClustersqueryParams := buildGetClustersQueryParams(d)
getClustersPath += getClustersqueryParams
getClustersOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
MoreHeaders: map[string]string{"Content-Type": "application/json"},
}
getClustersResp, err := getClustersClient.Request("GET", getClustersPath, &getClustersOpt)
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving MrsClusters")
}
getClustersRespBody, err := utils.FlattenResponse(getClustersResp)
if err != nil {
return diag.FromErr(err)
}
uuid, err := uuid.GenerateUUID()
if err != nil {
return diag.Errorf("unable to generate ID: %s", err)
}
d.SetId(uuid)
mErr = multierror.Append(
mErr,
d.Set("region", region),
d.Set("clusters", flattenGetClustersResponseBodyClusters(getClustersRespBody)),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func flattenGetClustersResponseBodyClusters(resp interface{}) []interface{} {
if resp == nil {
return nil
}
curJson := utils.PathSearch("clusters", resp, make([]interface{}, 0))
curArray := curJson.([]interface{})
rst := make([]interface{}, 0, len(curArray))
for _, v := range curArray {
rst = append(rst, map[string]interface{}{
"id": utils.PathSearch("clusterId", v, nil),
"name": utils.PathSearch("clusterName", v, nil),
"master_node_num": utils.PathSearch("masterNodeNum", v, nil),
"core_node_num": utils.PathSearch("coreNodeNum", v, nil),
"total_node_num": utils.PathSearch("totalNodeNum", v, nil),
"status": utils.PathSearch("clusterState", v, nil),
"billing_type": utils.PathSearch("billingType", v, nil),
"vpc_id": utils.PathSearch("vpcId", v, nil),
"subnet_id": utils.PathSearch("subnetId", v, nil),
"duration": utils.PathSearch("duration", v, nil),
"fee": utils.PathSearch("fee", v, nil),
"hadoop_version": utils.PathSearch("hadoopVersion", v, nil),
"master_node_size": utils.PathSearch("masterNodeSize", v, nil),
"core_node_size": utils.PathSearch("coreNodeSize", v, nil),
"component_list": flattenClustersComponentList(v),
"external_ip": utils.PathSearch("externalIp", v, nil),
"external_alternate_ip": utils.PathSearch("externalAlternateIp", v, nil),
"internal_ip": utils.PathSearch("internalIp", v, nil),
"deployment_id": utils.PathSearch("deploymentId", v, nil),
"description": utils.PathSearch("remark", v, nil),
"order_id": utils.PathSearch("orderId", v, nil),
"master_node_product_id": utils.PathSearch("masterNodeProductId", v, nil),
"master_node_spec_id": utils.PathSearch("masterNodeSpecId", v, nil),
"core_node_product_id": utils.PathSearch("coreNodeProductId", v, nil),
"core_node_spec_id": utils.PathSearch("coreNodeSpecId", v, nil),
"availability_zone": utils.PathSearch("availabilityZoneId", v, nil),
"vnc": utils.PathSearch("vnc", v, nil),
"volume_size": utils.PathSearch("volumeSize", v, nil),
"volume_type": utils.PathSearch("volumeType", v, nil),
"enterprise_project_id": utils.PathSearch("enterpriseProjectId", v, nil),
"type": utils.PathSearch("clusterType", v, nil),
"security_group_id": utils.PathSearch("securityGroupsId", v, nil),
"slave_security_group_id": utils.PathSearch("slaveSecurityGroupsId", v, nil),
"stage_desc": utils.PathSearch("stageDesc", v, nil),
"safe_mode": utils.PathSearch("safeMode", v, nil),
"version": utils.PathSearch("clusterVersion", v, nil),
"node_public_cert_name": utils.PathSearch("nodePublicCertName", v, nil),
"master_node_ip": utils.PathSearch("masterNodeIp", v, nil),
"private_ip_first": utils.PathSearch("privateIpFirst", v, nil),
"tags": flattenTags(utils.PathSearch("tags", v, "").(string)),
"log_collection": utils.PathSearch("logCollection", v, nil),
"task_node_groups": flattenClustersNodeGroups(v, "taskNodeGroups"),
"node_groups": flattenClustersNodeGroups(v, "nodeGroups"),
"master_data_volume_type": utils.PathSearch("masterDataVolumeType", v, nil),
"master_data_volume_size": utils.PathSearch("masterDataVolumeSize", v, nil),
"master_data_volume_count": utils.PathSearch("masterDataVolumeCount", v, nil),
"core_data_volume_type": utils.PathSearch("coreDataVolumeType", v, nil),
"core_data_volume_size": utils.PathSearch("coreDataVolumeSize", v, nil),
"core_data_volume_count": utils.PathSearch("coreDataVolumeCount", v, nil),
"period_type": utils.PathSearch("periodType", v, nil),
"scale": utils.PathSearch("scale", v, nil),
"eip_id": utils.PathSearch("eipId", v, nil),
"eip_address": utils.PathSearch("eipAddress", v, nil),
"eipv6_address": utils.PathSearch("eipv6Address", v, nil),
})
}
return rst
}
func flattenClustersComponentList(resp interface{}) []interface{} {
if resp == nil {
return nil
}
curJson := utils.PathSearch("componentList", resp, make([]interface{}, 0))
curArray := curJson.([]interface{})
rst := make([]interface{}, 0, len(curArray))
for _, v := range curArray {
rst = append(rst, map[string]interface{}{
"component_id": utils.PathSearch("componentId", v, nil),
"component_name": utils.PathSearch("componentName", v, nil),
"component_version": utils.PathSearch("componentVersion", v, nil),
"component_desc": utils.PathSearch("componentDesc", v, nil),
})
}
return rst
}
func flattenClustersNodeGroups(resp interface{}, key string) []interface{} {
if resp == nil {
return nil
}
curJson := utils.PathSearch(key, resp, make([]interface{}, 0))
curArray := curJson.([]interface{})
rst := make([]interface{}, 0, len(curArray))
for _, v := range curArray {
rst = append(rst, map[string]interface{}{
"group_name": utils.PathSearch("groupName", v, nil),
"node_num": utils.PathSearch("nodeNum", v, nil),
"node_size": utils.PathSearch("nodeSize", v, nil),
"node_spec_id": utils.PathSearch("nodeSpecId", v, nil),
"node_product_id": utils.PathSearch("nodeProductId", v, nil),
"vm_product_id": utils.PathSearch("vmProductId", v, nil),
"vm_spec_code": utils.PathSearch("vmSpecCode", v, nil),
"root_volume_size": utils.PathSearch("rootVolumeSize", v, nil),
"root_volume_type": utils.PathSearch("rootVolumeType", v, nil),
"root_volume_product_id": utils.PathSearch("rootVolumeProductId", v, nil),
"root_volume_resource_spec_code": utils.PathSearch("rootVolumeResourceSpecCode", v, nil),
"root_volume_resource_type": utils.PathSearch("rootVolumeResourceType", v, nil),
"data_volume_type": utils.PathSearch("dataVolumeType", v, nil),
"data_volume_count": utils.PathSearch("dataVolumeCount", v, nil),
"data_volume_size": utils.PathSearch("dataVolumeSize", v, nil),
"data_volume_product_id": utils.PathSearch("dataVolumeProductId", v, nil),
"data_volume_resource_spec_code": utils.PathSearch("dataVolumeResourceSpecCode", v, nil),
"data_volume_resource_type": utils.PathSearch("dataVolumeResourceType", v, nil),
})
}
return rst
}
func buildGetClustersQueryParams(d *schema.ResourceData) string {
res := ""
if v, ok := d.GetOk("name"); ok {
res = fmt.Sprintf("%s&clusterName=%v", res, v)
}
if v, ok := d.GetOk("status"); ok {
res = fmt.Sprintf("%s&clusterState=%v", res, v)
}
if v, ok := d.GetOk("enterprise_project_id"); ok {
res = fmt.Sprintf("%s&enterpriseProjectId=%v", res, v)
}
res = fmt.Sprintf("%s&pageSize=%v", res, 2147483646)
if v, ok := d.GetOk("tags"); ok {
res = fmt.Sprintf("%s&tags=%v", res, v)
}
if res != "" {
res = "?" + res[1:]
}
return res
}