Skip to content

Commit 0be6cf0

Browse files
tongyimingmikatong
andauthored
add antiddos resource (#2342)
* add antiddos resource * add changelog --------- Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 012dea5 commit 0be6cf0

19 files changed

+2190
-0
lines changed

.changelog/2342.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
```release-note:new-resource
2+
tencentcloud_antiddos_ddos_geo_ip_block_config
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_antiddos_ddos_speed_limit_config
7+
```
8+
9+
```release-note:new-resource
10+
tencentcloud_antiddos_default_alarm_threshold
11+
```
12+
13+
```release-note:new-resource
14+
tencentcloud_antiddos_scheduling_domain_user_name
15+
```
16+
17+
```release-note:new-resource
18+
tencentcloud_antiddos_ip_alarm_threshold_config
19+
```
20+

tencentcloud/provider.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ Anti-DDoS(DayuV2)
7878
tencentcloud_dayu_cc_policy_v2
7979
tencentcloud_dayu_ddos_ip_attachment_v2
8080
tencentcloud_antiddos_ddos_black_white_ip
81+
tencentcloud_antiddos_ddos_geo_ip_block_config
82+
tencentcloud_antiddos_ddos_speed_limit_config
83+
tencentcloud_antiddos_default_alarm_threshold
84+
tencentcloud_antiddos_scheduling_domain_user_name
85+
tencentcloud_antiddos_ip_alarm_threshold_config
8186
8287
Anti-DDoS(Dayu)
8388
Data Source
@@ -3664,6 +3669,11 @@ func Provider() *schema.Provider {
36643669
"tencentcloud_cynosdb_security_group": resourceTencentCloudCynosdbSecurityGroup(),
36653670
"tencentcloud_dayu_ddos_ip_attachment_v2": resourceTencentCloudDayuDDosIpAttachmentV2(),
36663671
"tencentcloud_antiddos_ddos_black_white_ip": resourceTencentCloudAntiddosDdosBlackWhiteIp(),
3672+
"tencentcloud_antiddos_ddos_geo_ip_block_config": resourceTencentCloudAntiddosDdosGeoIpBlockConfig(),
3673+
"tencentcloud_antiddos_ddos_speed_limit_config": resourceTencentCloudAntiddosDdosSpeedLimitConfig(),
3674+
"tencentcloud_antiddos_default_alarm_threshold": resourceTencentCloudAntiddosDefaultAlarmThreshold(),
3675+
"tencentcloud_antiddos_scheduling_domain_user_name": resourceTencentCloudAntiddosSchedulingDomainUserName(),
3676+
"tencentcloud_antiddos_ip_alarm_threshold_config": resourceTencentCloudAntiddosIpAlarmThresholdConfig(),
36673677
"tencentcloud_tsf_microservice": resourceTencentCloudTsfMicroservice(),
36683678
"tencentcloud_tsf_application_config": resourceTencentCloudTsfApplicationConfig(),
36693679
"tencentcloud_cvm_launch_template": resourceTencentCloudCvmLaunchTemplate(),
Lines changed: 341 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,341 @@
1+
/*
2+
Provides a resource to create a antiddos ddos_geo_ip_block_config
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_antiddos_ddos_geo_ip_block_config" "ddos_geo_ip_block_config" {
8+
instance_id = "bgp-xxxxxx"
9+
ddos_geo_ip_block_config {
10+
region_type = "customized"
11+
action = "drop"
12+
area_list = [100002]
13+
}
14+
}
15+
```
16+
17+
Import
18+
19+
antiddos ddos_geo_ip_block_config can be imported using the id, e.g.
20+
21+
```
22+
terraform import tencentcloud_antiddos_ddos_geo_ip_block_config.ddos_geo_ip_block_config ${instanceId}#${configId}
23+
```
24+
*/
25+
package tencentcloud
26+
27+
import (
28+
"context"
29+
"fmt"
30+
"log"
31+
"reflect"
32+
"sort"
33+
"strings"
34+
35+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
36+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
37+
antiddos "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/antiddos/v20200309"
38+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
39+
)
40+
41+
func resourceTencentCloudAntiddosDdosGeoIpBlockConfig() *schema.Resource {
42+
return &schema.Resource{
43+
Create: resourceTencentCloudAntiddosDdosGeoIpBlockConfigCreate,
44+
Read: resourceTencentCloudAntiddosDdosGeoIpBlockConfigRead,
45+
Update: resourceTencentCloudAntiddosDdosGeoIpBlockConfigUpdate,
46+
Delete: resourceTencentCloudAntiddosDdosGeoIpBlockConfigDelete,
47+
Importer: &schema.ResourceImporter{
48+
State: schema.ImportStatePassthrough,
49+
},
50+
Schema: map[string]*schema.Schema{
51+
"instance_id": {
52+
Required: true,
53+
Type: schema.TypeString,
54+
Description: "InstanceId.",
55+
},
56+
57+
"ddos_geo_ip_block_config": {
58+
Required: true,
59+
Type: schema.TypeList,
60+
MaxItems: 1,
61+
Description: "DDoS region blocking configuration, configuration ID cannot be empty when filling in parameters.",
62+
Elem: &schema.Resource{
63+
Schema: map[string]*schema.Schema{
64+
"region_type": {
65+
Type: schema.TypeString,
66+
Required: true,
67+
Description: "Region type, value [oversea (overseas) China (domestic) customized (custom region)].",
68+
},
69+
"action": {
70+
Type: schema.TypeString,
71+
Required: true,
72+
Description: "Blocking action, value [drop (intercept) trans (release)].",
73+
},
74+
"area_list": {
75+
Type: schema.TypeSet,
76+
Elem: &schema.Schema{
77+
Type: schema.TypeInt,
78+
},
79+
Optional: true,
80+
Description: "When RegionType is customized, an AreaList must be filled in, with a maximum of 128 entries;.",
81+
},
82+
},
83+
},
84+
},
85+
},
86+
}
87+
}
88+
89+
func resourceTencentCloudAntiddosDdosGeoIpBlockConfigCreate(d *schema.ResourceData, meta interface{}) error {
90+
defer logElapsed("resource.tencentcloud_antiddos_ddos_geo_ip_block_config.create")()
91+
defer inconsistentCheck(d, meta)()
92+
93+
logId := getLogId(contextNil)
94+
95+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
96+
97+
var (
98+
request = antiddos.NewCreateDDoSGeoIPBlockConfigRequest()
99+
instanceId string
100+
regionType string
101+
action string
102+
)
103+
if v, ok := d.GetOk("instance_id"); ok {
104+
instanceId = v.(string)
105+
request.InstanceId = helper.String(instanceId)
106+
}
107+
108+
areaList := make([]int64, 0)
109+
if dMap, ok := helper.InterfacesHeadMap(d, "ddos_geo_ip_block_config"); ok {
110+
dDoSGeoIPBlockConfig := antiddos.DDoSGeoIPBlockConfig{}
111+
if v, ok := dMap["region_type"]; ok {
112+
regionType = v.(string)
113+
dDoSGeoIPBlockConfig.RegionType = helper.String(regionType)
114+
}
115+
if v, ok := dMap["action"]; ok {
116+
action = v.(string)
117+
dDoSGeoIPBlockConfig.Action = helper.String(action)
118+
}
119+
120+
if v, ok := dMap["area_list"]; ok {
121+
areaListSet := v.(*schema.Set).List()
122+
for i := range areaListSet {
123+
area := areaListSet[i].(int)
124+
areaList = append(areaList, int64(area))
125+
dDoSGeoIPBlockConfig.AreaList = append(dDoSGeoIPBlockConfig.AreaList, helper.IntInt64(area))
126+
}
127+
}
128+
request.DDoSGeoIPBlockConfig = &dDoSGeoIPBlockConfig
129+
}
130+
131+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
132+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseAntiddosClient().CreateDDoSGeoIPBlockConfig(request)
133+
if e != nil {
134+
return retryError(e)
135+
} else {
136+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
137+
}
138+
return nil
139+
})
140+
if err != nil {
141+
log.Printf("[CRITAL]%s create antiddos ddosGeoIpBlockConfig failed, reason:%+v", logId, err)
142+
return err
143+
}
144+
145+
service := AntiddosService{client: meta.(*TencentCloudClient).apiV3Conn}
146+
147+
configList, err := service.DescribeAntiddosDdosGeoIpBlockConfigById(ctx, instanceId)
148+
if err != nil {
149+
return err
150+
}
151+
152+
var targetConfig *antiddos.DDoSGeoIPBlockConfig
153+
for _, item := range configList {
154+
if *item.GeoIPBlockConfig.Action != action {
155+
continue
156+
}
157+
if *item.GeoIPBlockConfig.RegionType != regionType {
158+
continue
159+
}
160+
sort.Slice(areaList, func(i, j int) bool {
161+
return areaList[i] < areaList[j]
162+
})
163+
tmpAreaList := make([]int64, 0)
164+
for _, v := range item.GeoIPBlockConfig.AreaList {
165+
area := *v
166+
tmpAreaList = append(tmpAreaList, area)
167+
}
168+
sort.Slice(tmpAreaList, func(i, j int) bool {
169+
return tmpAreaList[i] < tmpAreaList[j]
170+
})
171+
if !reflect.DeepEqual(areaList, tmpAreaList) {
172+
continue
173+
}
174+
targetConfig = item.GeoIPBlockConfig
175+
}
176+
177+
if targetConfig == nil {
178+
return fmt.Errorf("can not find geo ip block config")
179+
}
180+
d.SetId(instanceId + FILED_SP + *targetConfig.Id)
181+
182+
return resourceTencentCloudAntiddosDdosGeoIpBlockConfigRead(d, meta)
183+
}
184+
185+
func resourceTencentCloudAntiddosDdosGeoIpBlockConfigRead(d *schema.ResourceData, meta interface{}) error {
186+
defer logElapsed("resource.tencentcloud_antiddos_ddos_geo_ip_block_config.read")()
187+
defer inconsistentCheck(d, meta)()
188+
189+
logId := getLogId(contextNil)
190+
191+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
192+
193+
service := AntiddosService{client: meta.(*TencentCloudClient).apiV3Conn}
194+
195+
idSplit := strings.Split(d.Id(), FILED_SP)
196+
if len(idSplit) != 2 {
197+
return fmt.Errorf("id is broken,%s", idSplit)
198+
}
199+
configList, err := service.DescribeAntiddosDdosGeoIpBlockConfigById(ctx, idSplit[0])
200+
if err != nil {
201+
return err
202+
}
203+
204+
if len(configList) == 0 {
205+
d.SetId("")
206+
log.Printf("[WARN]%s resource `AntiddosDdosGeoIpBlockConfig` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
207+
return nil
208+
}
209+
210+
var targetConfig *antiddos.DDoSGeoIPBlockConfig
211+
for _, item := range configList {
212+
if *item.GeoIPBlockConfig.Id == idSplit[1] {
213+
targetConfig = item.GeoIPBlockConfig
214+
break
215+
}
216+
}
217+
218+
if targetConfig != nil {
219+
_ = d.Set("instance_id", idSplit[0])
220+
221+
if targetConfig != nil {
222+
ddoSGeoIPBlockConfigMap := map[string]interface{}{}
223+
224+
if targetConfig.RegionType != nil {
225+
ddoSGeoIPBlockConfigMap["region_type"] = targetConfig.RegionType
226+
}
227+
228+
if targetConfig.Action != nil {
229+
ddoSGeoIPBlockConfigMap["action"] = targetConfig.Action
230+
}
231+
232+
if targetConfig.AreaList != nil {
233+
ddoSGeoIPBlockConfigMap["area_list"] = targetConfig.AreaList
234+
}
235+
236+
_ = d.Set("ddos_geo_ip_block_config", []interface{}{ddoSGeoIPBlockConfigMap})
237+
}
238+
}
239+
240+
return nil
241+
}
242+
243+
func resourceTencentCloudAntiddosDdosGeoIpBlockConfigUpdate(d *schema.ResourceData, meta interface{}) error {
244+
defer logElapsed("resource.tencentcloud_antiddos_ddos_geo_ip_block_config.update")()
245+
defer inconsistentCheck(d, meta)()
246+
247+
logId := getLogId(contextNil)
248+
249+
request := antiddos.NewModifyDDoSGeoIPBlockConfigRequest()
250+
251+
idSplit := strings.Split(d.Id(), FILED_SP)
252+
if len(idSplit) != 2 {
253+
return fmt.Errorf("id is broken,%s", idSplit)
254+
}
255+
256+
request.InstanceId = helper.String(idSplit[0])
257+
258+
immutableArgs := []string{"instance_id"}
259+
260+
for _, v := range immutableArgs {
261+
if d.HasChange(v) {
262+
return fmt.Errorf("argument `%s` cannot be changed", v)
263+
}
264+
}
265+
266+
if d.HasChange("ddos_geo_ip_block_config") {
267+
if dMap, ok := helper.InterfacesHeadMap(d, "ddos_geo_ip_block_config"); ok {
268+
dDoSGeoIPBlockConfig := antiddos.DDoSGeoIPBlockConfig{}
269+
if v, ok := dMap["region_type"]; ok {
270+
dDoSGeoIPBlockConfig.RegionType = helper.String(v.(string))
271+
}
272+
if v, ok := dMap["action"]; ok {
273+
dDoSGeoIPBlockConfig.Action = helper.String(v.(string))
274+
}
275+
dDoSGeoIPBlockConfig.Id = helper.String(idSplit[1])
276+
if v, ok := dMap["area_list"]; ok {
277+
areaListSet := v.(*schema.Set).List()
278+
for i := range areaListSet {
279+
areaList := areaListSet[i].(int)
280+
dDoSGeoIPBlockConfig.AreaList = append(dDoSGeoIPBlockConfig.AreaList, helper.IntInt64(areaList))
281+
}
282+
}
283+
request.DDoSGeoIPBlockConfig = &dDoSGeoIPBlockConfig
284+
}
285+
286+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
287+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseAntiddosClient().ModifyDDoSGeoIPBlockConfig(request)
288+
if e != nil {
289+
return retryError(e)
290+
} else {
291+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
292+
}
293+
return nil
294+
})
295+
if err != nil {
296+
log.Printf("[CRITAL]%s update antiddos ddosGeoIpBlockConfig failed, reason:%+v", logId, err)
297+
return err
298+
}
299+
}
300+
301+
return resourceTencentCloudAntiddosDdosGeoIpBlockConfigRead(d, meta)
302+
}
303+
304+
func resourceTencentCloudAntiddosDdosGeoIpBlockConfigDelete(d *schema.ResourceData, meta interface{}) error {
305+
defer logElapsed("resource.tencentcloud_antiddos_ddos_geo_ip_block_config.delete")()
306+
defer inconsistentCheck(d, meta)()
307+
308+
logId := getLogId(contextNil)
309+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
310+
311+
service := AntiddosService{client: meta.(*TencentCloudClient).apiV3Conn}
312+
idSplit := strings.Split(d.Id(), FILED_SP)
313+
if len(idSplit) != 2 {
314+
return fmt.Errorf("id is broken,%s", idSplit)
315+
}
316+
317+
configList, err := service.DescribeAntiddosDdosGeoIpBlockConfigById(ctx, idSplit[0])
318+
if err != nil {
319+
return err
320+
}
321+
322+
if len(configList) == 0 {
323+
d.SetId("")
324+
log.Printf("[WARN]%s resource `AntiddosDdosGeoIpBlockConfig` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
325+
return nil
326+
}
327+
328+
var targetConfig *antiddos.DDoSGeoIPBlockConfig
329+
for _, item := range configList {
330+
if *item.GeoIPBlockConfig.Id == idSplit[1] {
331+
targetConfig = item.GeoIPBlockConfig
332+
break
333+
}
334+
}
335+
336+
if err := service.DeleteAntiddosDdosGeoIpBlockConfigById(ctx, idSplit[0], targetConfig); err != nil {
337+
return err
338+
}
339+
340+
return nil
341+
}

0 commit comments

Comments
 (0)