Skip to content

Commit 5c9ff1d

Browse files
authored
feat(teo): [126551351] add new resource (#3476)
* add * add
1 parent 5786aba commit 5c9ff1d

File tree

15 files changed

+12280
-5005
lines changed

15 files changed

+12280
-5005
lines changed

.changelog/3476.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_teo_ddos_protection_config
3+
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ require (
9090
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdcpg v1.0.533
9191
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq v1.0.955
9292
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tem v1.0.578
93-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.0.1203
93+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.1.5
9494
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.1196
9595
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/trocket v1.1.0
9696
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse v1.0.857

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tem v1.0.578 h1:vBpQhUr
10511051
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tem v1.0.578/go.mod h1:UlojGQh/9wb7/uXPNi7PvMral1CNAskVDNgqJEV83l0=
10521052
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.0.1203 h1:oEpdN03XuHqqw1OaJ9DQVr8Npv56nq1RfNvB6zVanoc=
10531053
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.0.1203/go.mod h1:/tuR/74u27UEv+a36Y1x1Zdgf2WqixNq+/1wfC0QQQc=
1054+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.1.5 h1:ZLBuFek7n9ri1RNqrwG+2UbJJ/2U/tAQUkiNhamP82o=
1055+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.1.5/go.mod h1:7tgthqW14nJik2A5huXBBQFldo27RFzPfDOVz9MarvE=
10541056
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/thpc v1.0.998 h1:f4/n0dVKQTD06xJ84B5asHViNJHrZmGojdAWEPIsITM=
10551057
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/thpc v1.0.998/go.mod h1:fyi/HUwCwVe2NCCCjz8k/C5GwPu3QazCZO+OBJ3MhLk=
10561058
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.1196 h1:+Fz9UoC3II3H3+rz9UUOHeSp2ntGGmMLAnCjHTUYliU=

tencentcloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,7 @@ func Provider() *schema.Provider {
18951895
"tencentcloud_teo_content_identifier": teo.ResourceTencentCloudTeoContentIdentifier(),
18961896
"tencentcloud_teo_customize_error_page": teo.ResourceTencentCloudTeoCustomizeErrorPage(),
18971897
"tencentcloud_teo_origin_acl": teo.ResourceTencentCloudTeoOriginAcl(),
1898+
"tencentcloud_teo_ddos_protection_config": teo.ResourceTencentCloudTeoDdosProtectionConfig(),
18981899
"tencentcloud_tcm_mesh": tcm.ResourceTencentCloudTcmMesh(),
18991900
"tencentcloud_tcm_cluster_attachment": tcm.ResourceTencentCloudTcmClusterAttachment(),
19001901
"tencentcloud_tcm_prometheus_attachment": tcm.ResourceTencentCloudTcmPrometheusAttachment(),

tencentcloud/provider.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,7 @@ tencentcloud_teo_bind_security_template
15161516
tencentcloud_teo_content_identifier
15171517
tencentcloud_teo_customize_error_page
15181518
tencentcloud_teo_origin_acl
1519+
tencentcloud_teo_ddos_protection_config
15191520

15201521
TencentCloud ServiceMesh(TCM)
15211522
Data Source
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
package teo
2+
3+
import (
4+
"context"
5+
"log"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
teov20220901 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo/v20220901"
10+
11+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
12+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
13+
)
14+
15+
func ResourceTencentCloudTeoDdosProtectionConfig() *schema.Resource {
16+
return &schema.Resource{
17+
Create: resourceTencentCloudTeoDdosProtectionConfigCreate,
18+
Read: resourceTencentCloudTeoDdosProtectionConfigRead,
19+
Update: resourceTencentCloudTeoDdosProtectionConfigUpdate,
20+
Delete: resourceTencentCloudTeoDdosProtectionConfigDelete,
21+
Importer: &schema.ResourceImporter{
22+
State: schema.ImportStatePassthrough,
23+
},
24+
Schema: map[string]*schema.Schema{
25+
"zone_id": {
26+
Type: schema.TypeString,
27+
Required: true,
28+
ForceNew: true,
29+
Description: "Zone ID.",
30+
},
31+
32+
"ddos_protection": {
33+
Type: schema.TypeList,
34+
Required: true,
35+
MaxItems: 1,
36+
Description: "Specifies the exclusive Anti-DDoS configuration.",
37+
Elem: &schema.Resource{
38+
Schema: map[string]*schema.Schema{
39+
"protection_option": {
40+
Type: schema.TypeString,
41+
Required: true,
42+
Description: "Specifies the protection scope of standalone DDoS. valid values:.\n<li>protect_all_domains: specifies exclusive Anti-DDoS protection for all domain names in the site. newly added domain names automatically enable exclusive Anti-DDoS protection. when this parameter is specified, DomainDDoSProtections will not be processed.</li>.\n<li>protect_specified_domains: only applicable to specified domains. specific scope can be set via DomainDDoSProtection parameter.</li>.",
43+
},
44+
"domain_ddos_protections": {
45+
Type: schema.TypeSet,
46+
Optional: true,
47+
Computed: true,
48+
Description: "Anti-DDoS configuration of the domain. specifies the exclusive ddos protection settings for the domain in request parameters.\n<li>When ProtectionOption remains protect_specified_domains, the domain names not filled in keep their exclusive Anti-DDoS protection configuration unchanged, while explicitly specified domain names are updated according to the input parameters.</li>.\n<li>When ProtectionOption switches from protect_all_domains to protect_specified_domains: if DomainDDoSProtections is empty, disable exclusive DDoS protection for all domains under the site; if DomainDDoSProtections is not empty, disable or maintain exclusive DDoS protection for the domain names specified in the parameter, and disable exclusive DDoS protection for other unlisted domain names.</li>.",
49+
Elem: &schema.Resource{
50+
Schema: map[string]*schema.Schema{
51+
"domain": {
52+
Type: schema.TypeString,
53+
Required: true,
54+
Description: "Domain name.",
55+
},
56+
"switch": {
57+
Type: schema.TypeString,
58+
Required: true,
59+
Description: "Standalone DDoS switch of the domain. valid values:.\n<li>on: enabled;</li>.\n<li>off: closed.</li>.",
60+
},
61+
},
62+
},
63+
},
64+
"shared_cname_ddos_protections": {
65+
Type: schema.TypeList,
66+
Computed: true,
67+
Description: "Specifies the exclusive DDoS protection configuration of a shared CNAME. used as an output parameter.",
68+
Elem: &schema.Resource{
69+
Schema: map[string]*schema.Schema{
70+
"domain": {
71+
Type: schema.TypeString,
72+
Computed: true,
73+
Description: "Domain name.",
74+
},
75+
"switch": {
76+
Type: schema.TypeString,
77+
Computed: true,
78+
Description: "Standalone DDoS switch of the domain. valid values:.\n<li>on: enabled;</li>.\n<li>off: closed.</li>.",
79+
},
80+
},
81+
},
82+
},
83+
},
84+
},
85+
},
86+
},
87+
}
88+
}
89+
90+
func resourceTencentCloudTeoDdosProtectionConfigCreate(d *schema.ResourceData, meta interface{}) error {
91+
defer tccommon.LogElapsed("resource.tencentcloud_teo_ddos_protection_config.create")()
92+
defer tccommon.InconsistentCheck(d, meta)()
93+
94+
var zoneId string
95+
if v, ok := d.GetOk("zone_id"); ok {
96+
zoneId = v.(string)
97+
}
98+
99+
d.SetId(zoneId)
100+
return resourceTencentCloudTeoDdosProtectionConfigUpdate(d, meta)
101+
}
102+
103+
func resourceTencentCloudTeoDdosProtectionConfigRead(d *schema.ResourceData, meta interface{}) error {
104+
defer tccommon.LogElapsed("resource.tencentcloud_teo_ddos_protection_config.read")()
105+
defer tccommon.InconsistentCheck(d, meta)()
106+
107+
var (
108+
logId = tccommon.GetLogId(tccommon.ContextNil)
109+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
110+
service = TeoService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
111+
zoneId = d.Id()
112+
)
113+
114+
respData, err := service.DescribeTeoDdosProtectionConfigById(ctx, zoneId)
115+
if err != nil {
116+
return err
117+
}
118+
119+
if respData == nil {
120+
log.Printf("[WARN]%s resource `tencentcloud_teo_ddos_protection_config` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
121+
d.SetId("")
122+
return nil
123+
}
124+
125+
_ = d.Set("zone_id", zoneId)
126+
127+
dMap := make(map[string]interface{}, 0)
128+
if respData.ProtectionOption != nil {
129+
dMap["protection_option"] = respData.ProtectionOption
130+
}
131+
132+
if respData.DomainDDoSProtections != nil {
133+
domainDDoSProtectionsList := make([]map[string]interface{}, 0, len(respData.DomainDDoSProtections))
134+
for _, domainDDoSProtections := range respData.DomainDDoSProtections {
135+
domainDDoSProtectionsMap := map[string]interface{}{}
136+
if domainDDoSProtections.Domain != nil {
137+
domainDDoSProtectionsMap["domain"] = domainDDoSProtections.Domain
138+
}
139+
140+
if domainDDoSProtections.Switch != nil {
141+
domainDDoSProtectionsMap["switch"] = domainDDoSProtections.Switch
142+
}
143+
144+
domainDDoSProtectionsList = append(domainDDoSProtectionsList, domainDDoSProtectionsMap)
145+
}
146+
147+
dMap["domain_ddos_protections"] = domainDDoSProtectionsList
148+
}
149+
150+
if respData.SharedCNAMEDDoSProtections != nil {
151+
sharedCNAMEDDoSProtectionsList := make([]map[string]interface{}, 0, len(respData.SharedCNAMEDDoSProtections))
152+
for _, sharedCNAMEDDoSProtections := range respData.SharedCNAMEDDoSProtections {
153+
sharedCNAMEDDoSProtectionsMap := map[string]interface{}{}
154+
if sharedCNAMEDDoSProtections.Domain != nil {
155+
sharedCNAMEDDoSProtectionsMap["domain"] = sharedCNAMEDDoSProtections.Domain
156+
}
157+
158+
if sharedCNAMEDDoSProtections.Switch != nil {
159+
sharedCNAMEDDoSProtectionsMap["switch"] = sharedCNAMEDDoSProtections.Switch
160+
}
161+
162+
sharedCNAMEDDoSProtectionsList = append(sharedCNAMEDDoSProtectionsList, sharedCNAMEDDoSProtectionsMap)
163+
}
164+
165+
dMap["shared_cname_ddos_protections"] = sharedCNAMEDDoSProtectionsList
166+
}
167+
168+
_ = d.Set("ddos_protection", []interface{}{dMap})
169+
return nil
170+
}
171+
172+
func resourceTencentCloudTeoDdosProtectionConfigUpdate(d *schema.ResourceData, meta interface{}) error {
173+
defer tccommon.LogElapsed("resource.tencentcloud_teo_ddos_protection_config.update")()
174+
defer tccommon.InconsistentCheck(d, meta)()
175+
176+
var (
177+
logId = tccommon.GetLogId(tccommon.ContextNil)
178+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
179+
request = teov20220901.NewModifyDDoSProtectionRequest()
180+
zoneId = d.Id()
181+
)
182+
183+
if dDoSProtectionMap, ok := helper.InterfacesHeadMap(d, "ddos_protection"); ok {
184+
dDoSProtection := teov20220901.DDoSProtection{}
185+
if v, ok := dDoSProtectionMap["protection_option"].(string); ok && v != "" {
186+
dDoSProtection.ProtectionOption = helper.String(v)
187+
}
188+
189+
if v, ok := dDoSProtectionMap["domain_ddos_protections"]; ok {
190+
for _, item := range v.(*schema.Set).List() {
191+
domainDDoSProtectionsMap := item.(map[string]interface{})
192+
domainDDoSProtection := teov20220901.DomainDDoSProtection{}
193+
if v, ok := domainDDoSProtectionsMap["domain"].(string); ok && v != "" {
194+
domainDDoSProtection.Domain = helper.String(v)
195+
}
196+
197+
if v, ok := domainDDoSProtectionsMap["switch"].(string); ok && v != "" {
198+
domainDDoSProtection.Switch = helper.String(v)
199+
}
200+
201+
dDoSProtection.DomainDDoSProtections = append(dDoSProtection.DomainDDoSProtections, &domainDDoSProtection)
202+
}
203+
}
204+
205+
request.DDoSProtection = &dDoSProtection
206+
}
207+
208+
request.ZoneId = &zoneId
209+
reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
210+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().ModifyDDoSProtectionWithContext(ctx, request)
211+
if e != nil {
212+
return tccommon.RetryError(e)
213+
} else {
214+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
215+
}
216+
217+
return nil
218+
})
219+
220+
if reqErr != nil {
221+
log.Printf("[CRITAL]%s update teo ddos protection config failed, reason:%+v", logId, reqErr)
222+
return reqErr
223+
}
224+
225+
return resourceTencentCloudTeoDdosProtectionConfigRead(d, meta)
226+
}
227+
228+
func resourceTencentCloudTeoDdosProtectionConfigDelete(d *schema.ResourceData, meta interface{}) error {
229+
defer tccommon.LogElapsed("resource.tencentcloud_teo_ddos_protection_config.delete")()
230+
defer tccommon.InconsistentCheck(d, meta)()
231+
232+
return nil
233+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Provides a resource to create a TEO ddos protection config
2+
3+
~> **NOTE:** If `protection_option` is `protect_specified_domains`, then all domains need to be listed. For domains that do not need protection, just set the `switch` to `off`.
4+
5+
Example Usage
6+
7+
Protect all domains
8+
9+
```hcl
10+
resource "tencentcloud_teo_ddos_protection_config" "example" {
11+
zone_id = "zone-3edjdliiw3he"
12+
ddos_protection {
13+
protection_option = "protect_all_domains"
14+
}
15+
}
16+
```
17+
18+
Protect designated domains
19+
20+
```hcl
21+
resource "tencentcloud_teo_ddos_protection_config" "example" {
22+
zone_id = "zone-3edjdliiw3he"
23+
ddos_protection {
24+
protection_option = "protect_specified_domains"
25+
domain_ddos_protections {
26+
domain = "1.demo.com"
27+
switch = "on"
28+
}
29+
30+
domain_ddos_protections {
31+
domain = "2.demo.com"
32+
switch = "on"
33+
}
34+
35+
domain_ddos_protections {
36+
domain = "3.demo.com"
37+
switch = "off"
38+
}
39+
}
40+
}
41+
```
42+
43+
Import
44+
45+
TEO ddos protection config can be imported using the id, e.g.
46+
47+
```
48+
terraform import tencentcloud_teo_ddos_protection_config.example zone-3edjdliiw3he
49+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package teo_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
8+
tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
9+
)
10+
11+
func TestAccTencentCloudTeoDdosProtectionConfigResource_basic(t *testing.T) {
12+
t.Parallel()
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() {
15+
tcacctest.AccPreCheck(t)
16+
},
17+
Providers: tcacctest.AccProviders,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: testAccTeoDdosProtectionConfig,
21+
Check: resource.ComposeTestCheckFunc(
22+
resource.TestCheckResourceAttrSet("tencentcloud_teo_ddos_protection_config.example", "id"),
23+
resource.TestCheckResourceAttrSet("tencentcloud_teo_ddos_protection_config.example", "zone_id"),
24+
),
25+
},
26+
{
27+
Config: testAccTeoDdosProtectionConfigUpdate,
28+
Check: resource.ComposeTestCheckFunc(
29+
resource.TestCheckResourceAttrSet("tencentcloud_teo_ddos_protection_config.example", "id"),
30+
resource.TestCheckResourceAttrSet("tencentcloud_teo_ddos_protection_config.example", "zone_id"),
31+
),
32+
},
33+
{
34+
ResourceName: "tencentcloud_teo_ddos_protection_config.example",
35+
ImportState: true,
36+
ImportStateVerify: true,
37+
},
38+
},
39+
})
40+
}
41+
42+
const testAccTeoDdosProtectionConfig = `
43+
resource "tencentcloud_teo_ddos_protection_config" "example" {
44+
zone_id = "zone-3edjdliiw3he"
45+
ddos_protection {
46+
protection_option = "protect_all_domains"
47+
}
48+
}
49+
`
50+
51+
const testAccTeoDdosProtectionConfigUpdate = `
52+
resource "tencentcloud_teo_ddos_protection_config" "example" {
53+
zone_id = "zone-3edjdliiw3he"
54+
ddos_protection {
55+
protection_option = "protect_specified_domains"
56+
domain_ddos_protections {
57+
domain = "1.demo.com"
58+
switch = "on"
59+
}
60+
61+
domain_ddos_protections {
62+
domain = "2.demo.com"
63+
switch = "on"
64+
}
65+
66+
domain_ddos_protections {
67+
domain = "3.demo.com"
68+
switch = "off"
69+
}
70+
}
71+
}
72+
`

0 commit comments

Comments
 (0)