-
Notifications
You must be signed in to change notification settings - Fork 154
/
resource_huaweicloud_dns_custom_line.go
385 lines (338 loc) · 12.1 KB
/
resource_huaweicloud_dns_custom_line.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
// ---------------------------------------------------------------
// *** AUTO GENERATED CODE ***
// @Product DNS
// ---------------------------------------------------------------
package dns
import (
"context"
"fmt"
"regexp"
"strings"
"time"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"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 DNS GET /v2.1/customlines
// @API DNS POST /v2.1/customlines
// @API DNS DELETE /v2.1/customlines/{line_id}
// @API DNS PUT /v2.1/customlines/{line_id}
func ResourceDNSCustomLine() *schema.Resource {
return &schema.Resource{
CreateContext: resourceDNSCustomLineCreate,
UpdateContext: resourceDNSCustomLineUpdate,
ReadContext: resourceDNSCustomLineRead,
DeleteContext: resourceDNSCustomLineDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 80),
validation.StringMatch(regexp.MustCompile("^[\u4e00-\u9fa5A-Za-z]([\u4e00-\u9fa5\\w-.]*)?$"),
"Only chinese and english letters, digits, hyphens (-), underscores (_), and periods"+
" (.) are allowed"),
),
Description: `Specifies the custom line name.`,
},
"ip_segments": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
MinItems: 1,
MaxItems: 50,
Description: `Specifies the IP address range.`,
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringLenBetween(0, 255),
Description: `Specifies the custom line description. A maximum of 255 characters are allowed.`,
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: `The resource status.`,
},
},
}
}
func resourceDNSCustomLineCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
createDNSCustomLineClient, err := cfg.NewServiceClient("dns", region)
if err != nil {
return diag.Errorf("error creating DNS Client: %s", err)
}
// createDNSCustomLine: create DNS custom line.
if err := createDNSCustomLine(createDNSCustomLineClient, d); err != nil {
return diag.FromErr(err)
}
timeout := d.Timeout(schema.TimeoutCreate)
if err := waitForDNSCustomLineCreateOrUpdate(ctx, createDNSCustomLineClient, d, timeout); err != nil {
return diag.FromErr(err)
}
return resourceDNSCustomLineRead(ctx, d, meta)
}
func createDNSCustomLine(customLineClient *golangsdk.ServiceClient, d *schema.ResourceData) error {
var (
createDNSCustomLineHttpUrl = "v2.1/customlines"
)
createDNSCustomLinePath := customLineClient.Endpoint + createDNSCustomLineHttpUrl
createDNSCustomLineOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
202,
},
}
createDNSCustomLineOpt.JSONBody = utils.RemoveNil(buildCreateOrUpdateDNSCustomLineBodyParams(d))
createDNSCustomLineResp, err := customLineClient.Request("POST", createDNSCustomLinePath,
&createDNSCustomLineOpt)
if err != nil {
return fmt.Errorf("error creating DNS custom line: %s", err)
}
createDNSCustomLineRespBody, err := utils.FlattenResponse(createDNSCustomLineResp)
if err != nil {
return err
}
id, err := jmespath.Search("line_id", createDNSCustomLineRespBody)
if err != nil {
return fmt.Errorf("error creating DNS custom line: ID is not found in API response")
}
d.SetId(id.(string))
return nil
}
func waitForDNSCustomLineCreateOrUpdate(ctx context.Context, customLineClient *golangsdk.ServiceClient,
d *schema.ResourceData, timeout time.Duration) error {
stateConf := &resource.StateChangeConf{
Target: []string{"ACTIVE"},
Pending: []string{"PENDING"},
Refresh: dnsCustomLineStatusRefreshFunc(d, customLineClient),
Timeout: timeout,
Delay: 5 * time.Second,
PollInterval: 5 * time.Second,
}
_, err := stateConf.WaitForStateContext(ctx)
if err != nil {
return fmt.Errorf("error waiting for DNS custom line (%s) to be ACTIVE : %s", d.Id(), err)
}
return nil
}
func buildCreateOrUpdateDNSCustomLineBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"name": utils.ValueIngoreEmpty(d.Get("name")),
"description": utils.ValueIngoreEmpty(d.Get("description")),
"ip_segments": utils.ValueIngoreEmpty(d.Get("ip_segments")),
}
return bodyParams
}
func resourceDNSCustomLineRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
var mErr *multierror.Error
// getDNSCustomLine: Query DNS custom line
var (
getDNSCustomLineHttpUrl = "v2.1/customlines"
getDNSCustomLineProduct = "dns"
)
getDNSCustomLineClient, err := cfg.NewServiceClient(getDNSCustomLineProduct, region)
if err != nil {
return diag.Errorf("error creating DNS Client: %s", err)
}
getDNSCustomLinePath := getDNSCustomLineClient.Endpoint + getDNSCustomLineHttpUrl
getDNSCustomLinePath += buildGetDNSCustomLineQueryParams(d)
getDNSCustomLineOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
getDNSCustomLineResp, err := getDNSCustomLineClient.Request("GET", getDNSCustomLinePath,
&getDNSCustomLineOpt)
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving DNS custom line")
}
getDNSCustomLineRespBody, err := utils.FlattenResponse(getDNSCustomLineResp)
if err != nil {
return diag.FromErr(err)
}
customLineMap, err := flattenCustomLineResponseBody(getDNSCustomLineRespBody, d)
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving DNS custom line")
}
mErr = multierror.Append(
mErr,
d.Set("region", region),
d.Set("name", customLineMap["name"]),
d.Set("ip_segments", customLineMap["ip_segments"]),
d.Set("status", customLineMap["status"]),
d.Set("description", customLineMap["description"]),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func flattenCustomLineResponseBody(resp interface{}, d *schema.ResourceData) (map[string]interface{}, error) {
if resp == nil {
return nil, fmt.Errorf("the custom line response is empty")
}
curJson := utils.PathSearch("lines", resp, make([]interface{}, 0))
curArray := curJson.([]interface{})
for _, v := range curArray {
lineId := utils.PathSearch("line_id", v, "")
if d.Id() == lineId.(string) {
return map[string]interface{}{
"name": utils.PathSearch("name", v, nil),
"ip_segments": utils.PathSearch("ip_segments", v, nil),
"status": utils.PathSearch("status", v, nil),
"description": utils.PathSearch("description", v, nil),
}, nil
}
}
return nil, golangsdk.ErrDefault404{}
}
func buildGetDNSCustomLineQueryParams(d *schema.ResourceData) string {
return fmt.Sprintf("?line_id=%s", d.Id())
}
func resourceDNSCustomLineUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
updateDNSCustomLineClient, err := cfg.NewServiceClient("dns", region)
if err != nil {
return diag.Errorf("error creating DNS Client: %s", err)
}
updateDNSCustomLineChanges := []string{
"name",
"description",
"ip_segments",
}
if d.HasChanges(updateDNSCustomLineChanges...) {
// updateDNSCustomLine: Update DNS custom line
if err := updateDNSCustomLine(updateDNSCustomLineClient, d); err != nil {
return diag.FromErr(err)
}
timeout := d.Timeout(schema.TimeoutUpdate)
if err := waitForDNSCustomLineCreateOrUpdate(ctx, updateDNSCustomLineClient, d, timeout); err != nil {
return diag.FromErr(err)
}
}
return resourceDNSCustomLineRead(ctx, d, meta)
}
func updateDNSCustomLine(customLineClient *golangsdk.ServiceClient, d *schema.ResourceData) error {
var (
updateDNSCustomLineHttpUrl = "v2.1/customlines/{line_id}"
)
updateDNSCustomLinePath := customLineClient.Endpoint + updateDNSCustomLineHttpUrl
updateDNSCustomLinePath = strings.ReplaceAll(updateDNSCustomLinePath, "{line_id}", d.Id())
updateDNSCustomLineOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
202,
},
}
updateDNSCustomLineOpt.JSONBody = utils.RemoveNil(buildCreateOrUpdateDNSCustomLineBodyParams(d))
_, err := customLineClient.Request("PUT", updateDNSCustomLinePath,
&updateDNSCustomLineOpt)
if err != nil {
return fmt.Errorf("error updating DNS custom line: %s", err)
}
return nil
}
func resourceDNSCustomLineDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
// deleteDNSCustomLine: Delete DNS custom line
var (
deleteDNSCustomLineHttpUrl = "v2.1/customlines/{line_id}"
deleteDNSCustomLineProduct = "dns"
)
deleteDNSCustomLineClient, err := cfg.NewServiceClient(deleteDNSCustomLineProduct, region)
if err != nil {
return diag.Errorf("error creating DNS Client: %s", err)
}
deleteDNSCustomLinePath := deleteDNSCustomLineClient.Endpoint + deleteDNSCustomLineHttpUrl
deleteDNSCustomLinePath = strings.ReplaceAll(deleteDNSCustomLinePath, "{line_id}", d.Id())
deleteDNSCustomLineOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
202,
},
}
_, err = deleteDNSCustomLineClient.Request("DELETE", deleteDNSCustomLinePath, &deleteDNSCustomLineOpt)
if err != nil {
return diag.Errorf("error deleting DNS custom line: %s", err)
}
if err := waitForDNSCustomLineDeleted(ctx, deleteDNSCustomLineClient, d); err != nil {
return diag.FromErr(err)
}
return nil
}
func waitForDNSCustomLineDeleted(ctx context.Context, customLineClient *golangsdk.ServiceClient,
d *schema.ResourceData) error {
stateConf := &resource.StateChangeConf{
Target: []string{"DELETED"},
Pending: []string{"ACTIVE", "PENDING", "ERROR"},
Refresh: dnsCustomLineStatusRefreshFunc(d, customLineClient),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 5 * time.Second,
PollInterval: 5 * time.Second,
}
_, err := stateConf.WaitForStateContext(ctx)
if err != nil {
return fmt.Errorf("error waiting for DNS custom line (%s) to be DELETED: %s", d.Id(), err)
}
return nil
}
func dnsCustomLineStatusRefreshFunc(d *schema.ResourceData, client *golangsdk.ServiceClient) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
var (
getDNSCustomLineHttpUrl = "v2.1/customlines"
)
getDNSCustomLinePath := client.Endpoint + getDNSCustomLineHttpUrl
getDNSCustomLinePath += buildGetDNSCustomLineQueryParams(d)
getDNSCustomLineOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
getDNSCustomLineResp, err := client.Request("GET", getDNSCustomLinePath, &getDNSCustomLineOpt)
if err != nil {
return nil, "", err
}
getDNSCustomLineRespBody, err := utils.FlattenResponse(getDNSCustomLineResp)
if err != nil {
return nil, "", err
}
customLineMap, err := flattenCustomLineResponseBody(getDNSCustomLineRespBody, d)
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
// When the error code is 404, the value of respBody is nil, and a non-null value is returned to avoid continuing the loop check.
return "Resource Not Found", "DELETED", nil
}
return customLineMap, "", err
}
status := customLineMap["status"]
return getDNSCustomLineRespBody, parseStatus(status.(string)), nil
}
}