-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_dli_spark_templates.go
379 lines (361 loc) · 12 KB
/
data_source_huaweicloud_dli_spark_templates.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
// Generated by PMS #89
package dli
import (
"context"
"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/tidwall/gjson"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/filters"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
)
func DataSourceDliSparkTemplates() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDliSparkTemplatesRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `Specifies the region in which to query the resource.`,
},
"template_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the ID of the spark template to be queried.`,
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the name of the spark template to be queried.`,
},
"group": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the group name to which the spark templates belong.`,
},
"templates": {
Type: schema.TypeList,
Computed: true,
Description: `All templates that match the filter parameters.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of template.`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The name of template.`,
},
"group": {
Type: schema.TypeString,
Computed: true,
Description: `The group name to which the spark template belongs.`,
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: `The description of template.`,
},
"body": {
Type: schema.TypeList,
Computed: true,
Description: `The body of template.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The spark job name.`,
},
"dependent_packages": {
Type: schema.TypeList,
Computed: true,
Description: `The list of package resource objects.`,
Elem: temBodDepPacElem(),
},
"app_parameters": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The input parameters of the main class, that is application parameters.`,
},
"obs_bucket": {
Type: schema.TypeString,
Computed: true,
Description: `The OBS bucket for storing the spark jobs.`,
},
"auto_recovery": {
Type: schema.TypeBool,
Computed: true,
Description: `Indicates whether to enable the retry function.`,
},
"max_retry_times": {
Type: schema.TypeInt,
Computed: true,
Description: `The maximum number of retries.`,
},
"jars": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The name of the resource package of type jar upload to the DLI resource management system.`,
},
"configurations": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The configuration items of the DLI spark.`,
},
"executor_memory": {
Type: schema.TypeString,
Computed: true,
Description: `The executor memory of the spark application.`,
},
"queue_name": {
Type: schema.TypeString,
Computed: true,
Description: `The DLI queue name.`,
},
"main_class": {
Type: schema.TypeString,
Computed: true,
Description: `The spark main class of the template.`,
},
"python_files": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `Name of the package that is of the PyFile type.`,
},
"files": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The name of the resource package of type file upload to the DLI resource management system.`,
},
"modules": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The name of the dependent system resource module.`,
},
"driver_memory": {
Type: schema.TypeString,
Computed: true,
Description: `The driver memory of the spark application.`,
},
"executor_cores": {
Type: schema.TypeInt,
Computed: true,
Description: `The number of CPU cores of each executor in the spark application.`,
},
"app_name": {
Type: schema.TypeString,
Computed: true,
Description: `The name of the uploaded JAR or pyFile type package.`,
},
"specification": {
Type: schema.TypeString,
Computed: true,
Description: `The compute resource type. Currently, resource types A, B, and C are available.`,
},
"resources": {
Type: schema.TypeList,
Computed: true,
Description: `The list of resource objects.`,
Elem: temBodResElem(),
},
"driver_cores": {
Type: schema.TypeInt,
Computed: true,
Description: `The number of CPU cores of the spark application driver.`,
},
"num_executors": {
Type: schema.TypeInt,
Computed: true,
Description: `The number of executors in a spark application.`,
},
},
},
},
},
},
},
},
}
}
// temBodResElem
// The Elem of "templates.body.resources"
func temBodResElem() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The name of resource.`,
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: `The type of resource.`,
},
},
}
}
// temBodDepPacElem
// The Elem of "templates.body.dependent_packages"
func temBodDepPacElem() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The name of a user group.`,
},
"resources": {
Type: schema.TypeList,
Computed: true,
Description: `The resources of a user group.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The name of resource.`,
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: `The type of resource.`,
},
},
},
},
},
}
}
type SparkTemplatesDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}
func newSparkTemplatesDSWrapper(d *schema.ResourceData, meta interface{}) *SparkTemplatesDSWrapper {
return &SparkTemplatesDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}
func dataSourceDliSparkTemplatesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newSparkTemplatesDSWrapper(d, meta)
lisSpaJobTemRst, err := wrapper.ListSparkJobTemplates()
if err != nil {
return diag.FromErr(err)
}
id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)
err = wrapper.listSparkJobTemplatesToSchema(lisSpaJobTemRst)
if err != nil {
return diag.FromErr(err)
}
return nil
}
// @API DLI GET /v3/{project_id}/templates
func (w *SparkTemplatesDSWrapper) ListSparkJobTemplates() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "dli")
if err != nil {
return nil, err
}
uri := "/v3/{project_id}/templates"
params := map[string]any{
"type": "SPARK",
}
return httphelper.New(client).
Method("GET").
URI(uri).
Query(params).
PageSizePager("templates", "current-page", "page-size", 50).
Filter(
filters.New().From("templates").
Where("id", "=", w.Get("template_id")).
Where("name", "=", w.Get("name")).
Where("group", "=", w.Get("group")),
).
Request().
Result()
}
func (w *SparkTemplatesDSWrapper) listSparkJobTemplatesToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("templates", schemas.SliceToList(body.Get("templates"),
func(template gjson.Result) any {
return map[string]any{
"id": template.Get("id").Value(),
"name": template.Get("name").Value(),
"group": template.Get("group").Value(),
"description": template.Get("description").Value(),
"body": schemas.SliceToList(template.Get("body"),
func(body gjson.Result) any {
return map[string]any{
"name": body.Get("name").Value(),
"dependent_packages": w.setTemBodGro(body),
"app_parameters": schemas.SliceToStrList(body.Get("args")),
"obs_bucket": body.Get("obs_bucket").Value(),
"auto_recovery": body.Get("auto_recovery").Value(),
"max_retry_times": body.Get("max_retry_times").Value(),
"jars": schemas.SliceToStrList(body.Get("jars")),
"configurations": schemas.MapToStrMap(body.Get("conf")),
"executor_memory": body.Get("executorMemory").Value(),
"queue_name": body.Get("queue").Value(),
"main_class": body.Get("className").Value(),
"python_files": schemas.SliceToStrList(body.Get("pyFiles")),
"files": schemas.SliceToStrList(body.Get("files")),
"modules": schemas.SliceToStrList(body.Get("modules")),
"driver_memory": body.Get("driverMemory").Value(),
"executor_cores": body.Get("executorCores").Value(),
"app_name": body.Get("file").Value(),
"specification": body.Get("sc_type").Value(),
"resources": w.setTemBodRes(body),
"driver_cores": body.Get("driverCores").Value(),
"num_executors": body.Get("numExecutors").Value(),
}
},
),
}
},
)),
)
return mErr.ErrorOrNil()
}
func (*SparkTemplatesDSWrapper) setTemBodGro(body gjson.Result) any {
return schemas.SliceToList(body.Get("groups"), func(depPac gjson.Result) any {
return map[string]any{
"name": depPac.Get("name").Value(),
"resources": schemas.SliceToList(depPac.Get("resources"),
func(resource gjson.Result) any {
return map[string]any{
"name": resource.Get("name").Value(),
"type": resource.Get("type").Value(),
}
},
),
}
})
}
func (*SparkTemplatesDSWrapper) setTemBodRes(body gjson.Result) any {
return schemas.SliceToList(body.Get("resources"), func(resource gjson.Result) any {
return map[string]any{
"name": resource.Get("name").Value(),
"type": resource.Get("type").Value(),
}
})
}