-
Notifications
You must be signed in to change notification settings - Fork 154
/
resource_huaweicloud_dli_global_variable.go
273 lines (238 loc) · 9.19 KB
/
resource_huaweicloud_dli_global_variable.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
// ---------------------------------------------------------------
// *** AUTO GENERATED CODE ***
// @Product DLI
// ---------------------------------------------------------------
package dli
import (
"context"
"encoding/json"
"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/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/chnsz/golangsdk"
"github.com/chnsz/golangsdk/pagination"
"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}/variables
// @API DLI GET /v1.0/{project_id}/variables
// @API DLI PUT /v1.0/{project_id}/variables/{var_name}
// @API DLI DELETE /v1.0/{project_id}/variables/{var_name}
func ResourceGlobalVariable() *schema.Resource {
return &schema.Resource{
CreateContext: resourceGlobalVariableCreate,
UpdateContext: resourceGlobalVariableUpdate,
ReadContext: resourceGlobalVariableRead,
DeleteContext: resourceGlobalVariableDelete,
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,
ForceNew: true,
Description: `The name of a Global variable.`,
ValidateFunc: validation.StringLenBetween(1, 128),
},
"value": {
Type: schema.TypeString,
Required: true,
Description: `The value of Global variable.`,
},
"is_sensitive": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
ForceNew: true,
Description: `Whether to set a variable as a sensitive variable. The default value is **false**.`,
},
},
}
}
func resourceGlobalVariableCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
// createGlobalVariable: create a Global variable.
var (
createGlobalVariableHttpUrl = "v1.0/{project_id}/variables"
createGlobalVariableProduct = "dli"
)
createGlobalVariableClient, err := cfg.NewServiceClient(createGlobalVariableProduct, region)
if err != nil {
return diag.Errorf("error creating DLI Client: %s", err)
}
createGlobalVariablePath := createGlobalVariableClient.Endpoint + createGlobalVariableHttpUrl
createGlobalVariablePath = strings.ReplaceAll(createGlobalVariablePath, "{project_id}", createGlobalVariableClient.ProjectID)
createGlobalVariableOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
createGlobalVariableOpt.JSONBody = utils.RemoveNil(buildCreateGlobalVariableBodyParams(d))
requestResp, err := createGlobalVariableClient.Request("POST", createGlobalVariablePath, &createGlobalVariableOpt)
if err != nil {
return diag.Errorf("error creating DLI global variable: %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 create the global variable: %s",
utils.PathSearch("message", respBody, "Message Not Found"))
}
d.SetId(d.Get("name").(string))
return resourceGlobalVariableRead(ctx, d, meta)
}
func buildCreateGlobalVariableBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"var_name": utils.ValueIngoreEmpty(d.Get("name")),
"var_value": utils.ValueIngoreEmpty(d.Get("value")),
"is_sensitive": utils.ValueIngoreEmpty(d.Get("is_sensitive")),
}
return bodyParams
}
func resourceGlobalVariableRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
var mErr *multierror.Error
// getGlobalVariable: Query the Global variable.
var (
getGlobalVariableHttpUrl = "v1.0/{project_id}/variables"
getGlobalVariableProduct = "dli"
)
getGlobalVariableClient, err := cfg.NewServiceClient(getGlobalVariableProduct, region)
if err != nil {
return diag.Errorf("error creating DLI Client: %s", err)
}
getGlobalVariablePath := getGlobalVariableClient.Endpoint + getGlobalVariableHttpUrl
getGlobalVariablePath = strings.ReplaceAll(getGlobalVariablePath, "{project_id}", getGlobalVariableClient.ProjectID)
getGlobalVariableResp, err := pagination.ListAllItems(
getGlobalVariableClient,
"offset",
getGlobalVariablePath,
&pagination.QueryOpts{MarkerField: ""})
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving DLI global variable")
}
getGlobalVariableRespJson, err := json.Marshal(getGlobalVariableResp)
if err != nil {
return diag.FromErr(err)
}
var getGlobalVariableRespBody interface{}
err = json.Unmarshal(getGlobalVariableRespJson, &getGlobalVariableRespBody)
if err != nil {
return diag.FromErr(err)
}
if !utils.PathSearch("is_success", getGlobalVariableRespBody, true).(bool) {
return diag.Errorf("unable to query the global variables: %s",
utils.PathSearch("message", getGlobalVariableRespBody, "Message Not Found"))
}
jsonPath := fmt.Sprintf("global_vars[?var_name=='%s']|[0]", d.Id())
globalVariable := utils.PathSearch(jsonPath, getGlobalVariableRespBody, nil)
if globalVariable == nil {
return common.CheckDeletedDiag(d, golangsdk.ErrDefault404{}, "no data found")
}
mErr = multierror.Append(
mErr,
d.Set("region", region),
d.Set("name", utils.PathSearch("var_name", globalVariable, nil)),
d.Set("value", utils.PathSearch("var_value", globalVariable, nil)),
d.Set("is_sensitive", utils.PathSearch("is_sensitive", globalVariable, nil)),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func resourceGlobalVariableUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
updateGlobalVariableChanges := []string{
"value",
}
if d.HasChanges(updateGlobalVariableChanges...) {
// updateGlobalVariable: update Global variable
var (
updateGlobalVariableHttpUrl = "v1.0/{project_id}/variables/{var_name}"
updateGlobalVariableProduct = "dli"
)
updateGlobalVariableClient, err := cfg.NewServiceClient(updateGlobalVariableProduct, region)
if err != nil {
return diag.Errorf("error creating DLI Client: %s", err)
}
updateGlobalVariablePath := updateGlobalVariableClient.Endpoint + updateGlobalVariableHttpUrl
updateGlobalVariablePath = strings.ReplaceAll(updateGlobalVariablePath, "{project_id}", updateGlobalVariableClient.ProjectID)
updateGlobalVariablePath = strings.ReplaceAll(updateGlobalVariablePath, "{var_name}", d.Id())
updateGlobalVariableOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
updateGlobalVariableOpt.JSONBody = utils.RemoveNil(buildUpdateGlobalVariableBodyParams(d))
requestResp, err := updateGlobalVariableClient.Request("PUT", updateGlobalVariablePath, &updateGlobalVariableOpt)
if err != nil {
return diag.Errorf("error updating DLI global variable: %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 global variable: %s",
utils.PathSearch("message", respBody, "Message Not Found"))
}
}
return resourceGlobalVariableRead(ctx, d, meta)
}
func buildUpdateGlobalVariableBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"var_value": utils.ValueIngoreEmpty(d.Get("value")),
}
return bodyParams
}
func resourceGlobalVariableDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
// deleteGlobalVariable: delete Global variable
var (
deleteGlobalVariableHttpUrl = "v1.0/{project_id}/variables/{var_name}"
deleteGlobalVariableProduct = "dli"
)
deleteGlobalVariableClient, err := cfg.NewServiceClient(deleteGlobalVariableProduct, region)
if err != nil {
return diag.Errorf("error creating DLI Client: %s", err)
}
deleteGlobalVariablePath := deleteGlobalVariableClient.Endpoint + deleteGlobalVariableHttpUrl
deleteGlobalVariablePath = strings.ReplaceAll(deleteGlobalVariablePath, "{project_id}", deleteGlobalVariableClient.ProjectID)
deleteGlobalVariablePath = strings.ReplaceAll(deleteGlobalVariablePath, "{var_name}", d.Id())
deleteGlobalVariableOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
requestResp, err := deleteGlobalVariableClient.Request("DELETE", deleteGlobalVariablePath, &deleteGlobalVariableOpt)
if err != nil {
return diag.Errorf("error deleting DLI global variable: %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 global variable: %s",
utils.PathSearch("message", respBody, "Message Not Found"))
}
return nil
}