/
resource_ddoscoo_webconfig_ai_protect_config.go
351 lines (306 loc) · 10.4 KB
/
resource_ddoscoo_webconfig_ai_protect_config.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
package alicloud
import (
"context"
"time"
"fmt"
"github.com/cenkalti/backoff/v4"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
alicloudAntiddosClient "github.com/alibabacloud-go/ddoscoo-20200101/v2/client"
)
var (
_ resource.Resource = &ddoscooWebAIProtectConfigResource{}
_ resource.ResourceWithConfigure = &ddoscooWebAIProtectConfigResource{}
)
func NewDdosCooWebAIProtectConfigResource() resource.Resource {
return &ddoscooWebAIProtectConfigResource{}
}
type ddoscooWebAIProtectConfigResource struct {
client *alicloudAntiddosClient.Client
}
type ddoscooWebAIProtectConfigModel struct {
Enabled types.Bool `tfsdk:"enabled"`
Domain types.String `tfsdk:"domain"`
Mode types.String `tfsdk:"mode"`
Level types.String `tfsdk:"level"`
}
// Metadata returns the web ai protect mode configuration resource name.
func (r *ddoscooWebAIProtectConfigResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_ddoscoo_webconfig_ai_protect_config"
}
// Schema defines the schema for the web ai protect mode configuration resource.
func (r *ddoscooWebAIProtectConfigResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Modify a domain AI Protect Mode in Anti-DDoS website configuration.",
Attributes: map[string]schema.Attribute{
"enabled": schema.BoolAttribute{
Description: "Enable/Disable of ai protect mode status.",
Required: true,
},
"domain": schema.StringAttribute{
Description: "Domain name.",
Required: true,
},
"mode": schema.StringAttribute{
Description: "config to set AiMode.",
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.OneOf("warning", "protection"),
},
Default: stringdefault.StaticString("protection"),
},
"level": schema.StringAttribute{
Description: "config to set AiTemplate.",
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.OneOf("loose", "normal", "strict"),
},
Default: stringdefault.StaticString("normal"),
},
},
}
}
// Configure adds the provider configured client to the resource.
func (r *ddoscooWebAIProtectConfigResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
r.client = req.ProviderData.(alicloudClients).antiddosClient
}
// Create a modify web ai protect mode configuration.
func (r *ddoscooWebAIProtectConfigResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var plan *ddoscooWebAIProtectConfigModel
getStateDiags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(getStateDiags...)
if resp.Diagnostics.HasError() {
return
}
// Modify Web AI Protect Mode.
err := r.modifyAIProtectMode(plan)
if err != nil {
resp.Diagnostics.AddError(
"[API ERROR] Failed to modify Antiddos AI protection Mode.",
err.Error(),
)
return
}
// Set state items.
state := &ddoscooWebAIProtectConfigModel{
Enabled: plan.Enabled,
Domain: plan.Domain,
Mode: plan.Mode,
Level: plan.Level,
}
// Set state to fully populated data
setStateDiags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(setStateDiags...)
if resp.Diagnostics.HasError() {
return
}
}
// Read web ai protect configuration for domain.
func (r *ddoscooWebAIProtectConfigResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
var state *ddoscooWebAIProtectConfigModel
getStateDiags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(getStateDiags...)
if resp.Diagnostics.HasError() {
return
}
// Retry backoff function.
readWebAIProtectMode := func() error {
runtime := &util.RuntimeOptions{}
describeWebCcProtectSwitchRequest := &alicloudAntiddosClient.DescribeWebCcProtectSwitchRequest{
Domains: []*string{tea.String(state.Domain.ValueString())},
}
webCcProtectSwitch, err := r.client.DescribeWebCcProtectSwitchWithOptions(describeWebCcProtectSwitchRequest, runtime)
if err != nil {
if _t, ok := err.(*tea.SDKError); ok {
if isAbleToRetry(*_t.Code) {
return err
} else {
return backoff.Permanent(err)
}
} else {
return err
}
}
if len(webCcProtectSwitch.Body.ProtectSwitchList) > 0 {
//convert from aliyun antiddos web ai protect sdk AiRuleEnable keyword to readable variable (Enabled).
switch *webCcProtectSwitch.Body.ProtectSwitchList[0].AiRuleEnable {
case 0:
state.Enabled = types.BoolValue(false)
case 1:
state.Enabled = types.BoolValue(true)
}
//convert from aliyun antiddos web ai protect sdk AiMode keyword to readable variable (Mode).
switch *webCcProtectSwitch.Body.ProtectSwitchList[0].AiMode {
case "watch":
state.Mode = types.StringValue("warning")
case "defense":
state.Mode = types.StringValue("protection")
}
//convert from aliyun antiddos web ai protect sdk AiTemplate keyword to readable variable (Level).
switch *webCcProtectSwitch.Body.ProtectSwitchList[0].AiTemplate {
case "level30":
state.Level = types.StringValue("loose")
case "level60":
state.Level = types.StringValue("normal")
case "level90":
state.Level = types.StringValue("strict")
}
state.Domain = types.StringValue(*webCcProtectSwitch.Body.ProtectSwitchList[0].Domain)
// Set refreshed state
setStateDiags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(setStateDiags...)
if resp.Diagnostics.HasError() {
resp.Diagnostics.AddError(
"[API ERROR] Failed to Set ANtiddos AI Protection Website Configuration Mode to State",
err.Error(),
)
}
} else {
resp.State.RemoveResource(ctx)
}
return nil
}
// Retry backoff
reconnectBackoff := backoff.NewExponentialBackOff()
reconnectBackoff.MaxElapsedTime = 60 * time.Second
err := backoff.Retry(readWebAIProtectMode, reconnectBackoff)
if err != nil {
resp.Diagnostics.AddError(
"[API ERROR] Failed to Read Antiddos AI Protection Mode",
err.Error(),
)
return
}
}
// Update web ai protect configuration and sets the updated Terraform state on success.
func (r *ddoscooWebAIProtectConfigResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan *ddoscooWebAIProtectConfigModel
// Retrieve values from plan
getPlanDiags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(getPlanDiags...)
if resp.Diagnostics.HasError() {
return
}
// Modify Web AI Protect Mode
err := r.modifyAIProtectMode(plan)
if err != nil {
resp.Diagnostics.AddError(
"[API ERROR] Failed to Update modify Antiddos AI protection Mode.",
err.Error(),
)
return
}
// Set state items
state := &ddoscooWebAIProtectConfigModel{
Enabled: plan.Enabled,
Domain: plan.Domain,
Mode: plan.Mode,
Level: plan.Level,
}
// Set state to fully populated data
setStateDiags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(setStateDiags...)
if resp.Diagnostics.HasError() {
return
}
}
// SSL cert could not be unbinded, will always remain.
func (r *ddoscooWebAIProtectConfigResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Retrieve values from state
var state *ddoscooWebAIProtectConfigModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Function to modify AI Protection Mode for domain
func (r *ddoscooWebAIProtectConfigResource) modifyAIProtectMode(plan *ddoscooWebAIProtectConfigModel) error {
level := plan.Level.ValueString()
mode := plan.Mode.ValueString()
enabled := map[bool]int{false: 0, true: 1}[plan.Enabled.ValueBool()]
enableAIProtectConfig := func() error {
runtime := &util.RuntimeOptions{}
// enable/disable antiddos web ai protect configuration
modifyWebAIProtectSwitchRequest := &alicloudAntiddosClient.ModifyWebAIProtectSwitchRequest{
Config: tea.String(fmt.Sprintf("{\"AiRuleEnable\": %d}",enabled)),
Domain: tea.String(plan.Domain.ValueString()),
}
_, _err := r.client.ModifyWebAIProtectSwitchWithOptions(modifyWebAIProtectSwitchRequest, runtime)
if _err != nil {
if _t, ok := _err.(*tea.SDKError); ok {
if isAbleToRetry(*_t.Code) {
return _err
} else {
return backoff.Permanent(_err)
}
} else {
return _err
}
}
return nil
}
modifyAIProtectConfig := func() error {
runtime := &util.RuntimeOptions{}
//convert input (level) to aliyun antiddos web ai protect sdk AiTemplate needed keyword ("level30"/"level60"/"level90").
switch level {
case "loose":
level = "level30"
case "normal":
level = "level60"
case "strict":
level = "level90"
}
//convert input (mode) to aliyun antiddos web ai protect sdk AiMode needed keyword ("watch"/"defense").
switch mode {
case "warning":
mode = "watch"
case "protection":
mode = "defense"
}
// modify antiddos web ai protect mode configuration
modifyWebAIProtectModeRequest := &alicloudAntiddosClient.ModifyWebAIProtectModeRequest{
Domain: tea.String(plan.Domain.ValueString()),
Config: tea.String(fmt.Sprintf("{\"AiTemplate\":\"%s\",\"AiMode\":\"%s\"}", level, mode)),
}
_, _err := r.client.ModifyWebAIProtectModeWithOptions(modifyWebAIProtectModeRequest, runtime)
if _err != nil {
if _t, ok := _err.(*tea.SDKError); ok {
if isAbleToRetry(*_t.Code) {
return _err
} else {
return backoff.Permanent(_err)
}
} else {
return _err
}
}
return nil
}
// Retry backoff
reconnectBackoff := backoff.NewExponentialBackOff()
reconnectBackoff.MaxElapsedTime = 60 * time.Second
err := backoff.Retry(enableAIProtectConfig, reconnectBackoff)
if err != nil {
return err
}
reconnectBackoff.MaxElapsedTime = 60 * time.Second
err = backoff.Retry(modifyAIProtectConfig, reconnectBackoff)
if err != nil {
return err
}
return nil
}