-
Notifications
You must be signed in to change notification settings - Fork 154
/
resource_huaweicloud_dli_sql_template.go
301 lines (262 loc) · 9.39 KB
/
resource_huaweicloud_dli_sql_template.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
// ---------------------------------------------------------------
// *** AUTO GENERATED CODE ***
// @Product DLI
// ---------------------------------------------------------------
package dli
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jmespath/go-jmespath"
"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 DLI POST /v1.0/{project_id}/sqls
// @API DLI GET /v1.0/{project_id}/sqls
// @API DLI PUT /v1.0/{project_id}/sqls/{id}
// @API DLI POST /v1.0/{project_id}/sqls-deletion
func ResourceSQLTemplate() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSQLTemplateCreate,
UpdateContext: resourceSQLTemplateUpdate,
ReadContext: resourceSQLTemplateRead,
DeleteContext: resourceSQLTemplateDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Description: `The name of the SQL template.`,
},
"sql": {
Type: schema.TypeString,
Required: true,
Description: `The statement of the SQL template.`,
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `The description of the SQL template.`,
},
"group": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `The group of the SQL template.`,
},
"owner": {
Type: schema.TypeString,
Computed: true,
Description: `The user ID of owner.`,
},
},
}
}
func resourceSQLTemplateCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
// createSQLTemplate: create a SQLTemplate.
var (
createSQLTemplateHttpUrl = "v1.0/{project_id}/sqls"
createSQLTemplateProduct = "dli"
)
createSQLTemplateClient, err := cfg.NewServiceClient(createSQLTemplateProduct, region)
if err != nil {
return diag.Errorf("error creating DLI Client: %s", err)
}
createSQLTemplatePath := createSQLTemplateClient.Endpoint + createSQLTemplateHttpUrl
createSQLTemplatePath = strings.ReplaceAll(createSQLTemplatePath, "{project_id}", createSQLTemplateClient.ProjectID)
createSQLTemplateOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
createSQLTemplateOpt.JSONBody = utils.RemoveNil(buildSQLTemplateBodyParams(d))
createSQLTemplateResp, err := createSQLTemplateClient.Request("POST", createSQLTemplatePath, &createSQLTemplateOpt)
if err != nil {
return diag.Errorf("error creating SQLTemplate: %s", err)
}
createSQLTemplateRespBody, err := utils.FlattenResponse(createSQLTemplateResp)
if err != nil {
return diag.FromErr(err)
}
if !utils.PathSearch("is_success", createSQLTemplateRespBody, true).(bool) {
return diag.Errorf("unable to create the SQL template: %s",
utils.PathSearch("message", createSQLTemplateRespBody, "Message Not Found"))
}
id, err := jmespath.Search("sql_id", createSQLTemplateRespBody)
if err != nil {
return diag.Errorf("error creating SQLTemplate: ID is not found in API response")
}
d.SetId(id.(string))
return resourceSQLTemplateRead(ctx, d, meta)
}
func buildSQLTemplateBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"sql_name": utils.ValueIngoreEmpty(d.Get("name")),
"sql": utils.ValueIngoreEmpty(d.Get("sql")),
"description": utils.ValueIngoreEmpty(d.Get("description")),
"group": utils.ValueIngoreEmpty(d.Get("group")),
}
return bodyParams
}
func resourceSQLTemplateRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
var mErr *multierror.Error
// getSQLTemplate: Query the SQLTemplate.
var (
getSQLTemplateHttpUrl = "v1.0/{project_id}/sqls"
getSQLTemplateProduct = "dli"
)
getSQLTemplateClient, err := cfg.NewServiceClient(getSQLTemplateProduct, region)
if err != nil {
return diag.Errorf("error creating DLI Client: %s", err)
}
getSQLTemplatePath := getSQLTemplateClient.Endpoint + getSQLTemplateHttpUrl
getSQLTemplatePath = strings.ReplaceAll(getSQLTemplatePath, "{project_id}", getSQLTemplateClient.ProjectID)
getSQLTemplatequeryParams := buildGetSQLTemplateQueryParams(d)
getSQLTemplatePath += getSQLTemplatequeryParams
getSQLTemplateOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
getSQLTemplateResp, err := getSQLTemplateClient.Request("GET", getSQLTemplatePath, &getSQLTemplateOpt)
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving SQLTemplate")
}
getSQLTemplateRespBody, err := utils.FlattenResponse(getSQLTemplateResp)
if err != nil {
return diag.FromErr(err)
}
if !utils.PathSearch("is_success", getSQLTemplateRespBody, true).(bool) {
return diag.Errorf("unable to query the SQL templates: %s",
utils.PathSearch("message", getSQLTemplateRespBody, "Message Not Found"))
}
jsonPath := fmt.Sprintf("sqls[?sql_id=='%s']|[0]", d.Id())
sqlTemplate := utils.PathSearch(jsonPath, getSQLTemplateRespBody, nil)
if sqlTemplate == nil {
return common.CheckDeletedDiag(d, golangsdk.ErrDefault404{}, "no data found")
}
mErr = multierror.Append(
mErr,
d.Set("region", region),
d.Set("name", utils.PathSearch("sql_name", sqlTemplate, nil)),
d.Set("sql", utils.PathSearch("sql", sqlTemplate, nil)),
d.Set("description", utils.PathSearch("description", sqlTemplate, nil)),
d.Set("group", utils.PathSearch("group", sqlTemplate, nil)),
d.Set("owner", utils.PathSearch("owner", sqlTemplate, nil)),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func buildGetSQLTemplateQueryParams(d *schema.ResourceData) string {
res := ""
if v, ok := d.GetOk("name"); ok {
res = fmt.Sprintf("%s&keyword=%v", res, v)
}
if res != "" {
res = "?" + res[1:]
}
return res
}
func resourceSQLTemplateUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
updateSQLTemplateChanges := []string{
"name",
"sql",
"description",
"group",
}
if d.HasChanges(updateSQLTemplateChanges...) {
// updateSQLTemplate: update SQLTemplate
var (
updateSQLTemplateHttpUrl = "v1.0/{project_id}/sqls/{id}"
updateSQLTemplateProduct = "dli"
)
updateSQLTemplateClient, err := cfg.NewServiceClient(updateSQLTemplateProduct, region)
if err != nil {
return diag.Errorf("error creating DLI Client: %s", err)
}
updateSQLTemplatePath := updateSQLTemplateClient.Endpoint + updateSQLTemplateHttpUrl
updateSQLTemplatePath = strings.ReplaceAll(updateSQLTemplatePath, "{project_id}", updateSQLTemplateClient.ProjectID)
updateSQLTemplatePath = strings.ReplaceAll(updateSQLTemplatePath, "{id}", d.Id())
updateSQLTemplateOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
updateSQLTemplateOpt.JSONBody = utils.RemoveNil(buildSQLTemplateBodyParams(d))
requestResp, err := updateSQLTemplateClient.Request("PUT", updateSQLTemplatePath, &updateSQLTemplateOpt)
if err != nil {
return diag.Errorf("error updating SQLTemplate: %s", err)
}
respBody, err := utils.FlattenResponse(requestResp)
if err != nil {
return diag.FromErr(err)
}
if !utils.PathSearch("is_success", respBody, true).(bool) {
return diag.Errorf("unable to update the SQL template: %s",
utils.PathSearch("message", respBody, "Message Not Found"))
}
}
return resourceSQLTemplateRead(ctx, d, meta)
}
func resourceSQLTemplateDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
var (
deleteSQLTemplateHttpUrl = "v1.0/{project_id}/sqls-deletion"
deleteSQLTemplateProduct = "dli"
)
deleteSQLTemplateClient, err := cfg.NewServiceClient(deleteSQLTemplateProduct, region)
if err != nil {
return diag.Errorf("error creating DLI Client: %s", err)
}
deleteSQLTemplatePath := deleteSQLTemplateClient.Endpoint + deleteSQLTemplateHttpUrl
deleteSQLTemplatePath = strings.ReplaceAll(deleteSQLTemplatePath, "{project_id}", deleteSQLTemplateClient.ProjectID)
deleteSQLTemplateOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
deleteSQLTemplateOpt.JSONBody = utils.RemoveNil(buildDeleteSQLTemplateBodyParams(d))
requestResp, err := deleteSQLTemplateClient.Request("POST", deleteSQLTemplatePath, &deleteSQLTemplateOpt)
if err != nil {
return diag.Errorf("error deleting SQLTemplate: %s", err)
}
respBody, err := utils.FlattenResponse(requestResp)
if err != nil {
return diag.FromErr(err)
}
if !utils.PathSearch("is_success", respBody, true).(bool) {
return diag.Errorf("unable to delete the SQL template: %s",
utils.PathSearch("message", respBody, "Message Not Found"))
}
return nil
}
func buildDeleteSQLTemplateBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"sql_ids": []string{d.Id()},
}
return bodyParams
}