Skip to content

Commit 9fba0c9

Browse files
authored
feat/cfw (#2207)
* feat/cfw * feat/cfw * feat/cfw
1 parent 1937ba6 commit 9fba0c9

File tree

6 files changed

+97
-16
lines changed

6 files changed

+97
-16
lines changed

.changelog/2207.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_cfw_edge_policy: Update code logic
3+
```
4+
5+
```release-note:enhancement
6+
resource/tencentcloud_cfw_nat_policy: Update code logic
7+
```

tencentcloud/extension_cfw.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,3 +505,13 @@ var POLICY_RULE_ACTION = []string{
505505
POLICY_RULE_ACTION_DROP,
506506
POLICY_RULE_ACTION_LOG,
507507
}
508+
509+
type SourceContentJson struct {
510+
Key string `json:"Key"`
511+
Value string `json:"Value"`
512+
}
513+
514+
type TargetContentJson struct {
515+
Key string `json:"Key"`
516+
Value string `json:"Value"`
517+
}

tencentcloud/resource_tc_cfw_edge_policy.go

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ resource "tencentcloud_cfw_edge_policy" "example" {
1919
}
2020
```
2121
22+
If target_type is tag
23+
24+
```hcl
25+
resource "tencentcloud_cfw_edge_policy" "example" {
26+
source_content = "0.0.0.0/0"
27+
source_type = "net"
28+
target_content = jsonencode({"Key":"test","Value":"dddd"})
29+
target_type = "tag"
30+
protocol = "TCP"
31+
rule_action = "drop"
32+
port = "-1/-1"
33+
direction = 1
34+
enable = "true"
35+
description = "policy description."
36+
scope = "all"
37+
}
38+
```
39+
2240
Import
2341
2442
cfw edge_policy can be imported using the id, e.g.
@@ -31,9 +49,11 @@ package tencentcloud
3149

3250
import (
3351
"context"
52+
"encoding/json"
3453
"fmt"
3554
"log"
3655
"strconv"
56+
"strings"
3757

3858
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
3959
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -74,7 +94,7 @@ func resourceTencentCloudCfwEdgePolicy() *schema.Resource {
7494
"protocol": {
7595
Type: schema.TypeString,
7696
Required: true,
77-
Description: "Protocol, optional values: TCP UDP ICMP ANY HTTP HTTPS HTTP/HTTPS SMTP SMTPS SMTP/SMTPS FTP DNS.",
97+
Description: "Protocol. If Direction=1 && Scope=serial, optional values: TCP UDP ICMP ANY HTTP HTTPS HTTP/HTTPS SMTP SMTPS SMTP/SMTPS FTP DNS; If Direction=1 && Scope!=serial, optional values: TCP; If Direction=0 && Scope=serial, optional values: TCP UDP ICMP ANY HTTP HTTPS HTTP/HTTPS SMTP SMTPS SMTP/SMTPS FTP DNS; If Direction=0 && Scope!=serial, optional values: TCP HTTP/HTTPS TLS/SSL.",
7898
},
7999
"rule_action": {
80100
Type: schema.TypeString,
@@ -160,7 +180,7 @@ func resourceTencentCloudCfwEdgePolicyCreate(d *schema.ResourceData, meta interf
160180
createRuleItem.Port = helper.String(v.(string))
161181
}
162182

163-
if v, ok := d.GetOk("direction"); ok {
183+
if v, ok := d.GetOkExists("direction"); ok {
164184
createRuleItem.Direction = helper.IntUint64(v.(int))
165185
}
166186

@@ -207,10 +227,12 @@ func resourceTencentCloudCfwEdgePolicyRead(d *schema.ResourceData, meta interfac
207227
defer inconsistentCheck(d, meta)()
208228

209229
var (
210-
logId = getLogId(contextNil)
211-
ctx = context.WithValue(context.TODO(), logIdKey, logId)
212-
service = CfwService{client: meta.(*TencentCloudClient).apiV3Conn}
213-
ruleUuid = d.Id()
230+
logId = getLogId(contextNil)
231+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
232+
service = CfwService{client: meta.(*TencentCloudClient).apiV3Conn}
233+
ruleUuid = d.Id()
234+
sourceType string
235+
targetType string
214236
)
215237

216238
edgePolicy, err := service.DescribeCfwEdgePolicyById(ctx, ruleUuid)
@@ -224,20 +246,44 @@ func resourceTencentCloudCfwEdgePolicyRead(d *schema.ResourceData, meta interfac
224246
return nil
225247
}
226248

227-
if edgePolicy.SourceContent != nil {
228-
_ = d.Set("source_content", edgePolicy.SourceContent)
229-
}
230-
231249
if edgePolicy.SourceType != nil {
232250
_ = d.Set("source_type", edgePolicy.SourceType)
251+
sourceType = *edgePolicy.SourceType
233252
}
234253

235-
if edgePolicy.TargetContent != nil {
236-
_ = d.Set("target_content", edgePolicy.TargetContent)
254+
if edgePolicy.SourceContent != nil {
255+
if sourceType == "tag" {
256+
params := strings.Split(*edgePolicy.SourceContent, "|")
257+
key := params[0]
258+
value := params[1]
259+
var obj SourceContentJson
260+
obj.Key = key
261+
obj.Value = value
262+
tmpStr, _ := json.Marshal(obj)
263+
_ = d.Set("source_content", string(tmpStr))
264+
} else {
265+
_ = d.Set("source_content", edgePolicy.SourceContent)
266+
}
237267
}
238268

239269
if edgePolicy.TargetType != nil {
240270
_ = d.Set("target_type", edgePolicy.TargetType)
271+
targetType = *edgePolicy.TargetType
272+
}
273+
274+
if edgePolicy.TargetContent != nil {
275+
if targetType == "tag" {
276+
params := strings.Split(*edgePolicy.TargetContent, "|")
277+
key := params[0]
278+
value := params[1]
279+
var obj TargetContentJson
280+
obj.Key = key
281+
obj.Value = value
282+
tmpStr, _ := json.Marshal(obj)
283+
_ = d.Set("target_content", string(tmpStr))
284+
} else {
285+
_ = d.Set("target_content", edgePolicy.TargetContent)
286+
}
241287
}
242288

243289
if edgePolicy.Protocol != nil {

tencentcloud/resource_tc_cfw_nat_policy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func resourceTencentCloudCfwNatPolicy() *schema.Resource {
7373
"protocol": {
7474
Type: schema.TypeString,
7575
Required: true,
76-
Description: "Protocol, optional values: TCP UDP ICMP ANY HTTP HTTPS HTTP/HTTPS SMTP SMTPS SMTP/SMTPS FTP DNS.",
76+
Description: "Protocol. If Direction=1, optional values: TCP, UDP, ANY; If Direction=0, optional values: TCP, UDP, ICMP, ANY, HTTP, HTTPS, HTTP/HTTPS, SMTP, SMTPS, SMTP/SMTPS, FTP, and DNS.",
7777
},
7878
"rule_action": {
7979
Type: schema.TypeString,
@@ -152,7 +152,7 @@ func resourceTencentCloudCfwNatPolicyCreate(d *schema.ResourceData, meta interfa
152152
createNatRuleItem.Port = helper.String(v.(string))
153153
}
154154

155-
if v, ok := d.GetOk("direction"); ok {
155+
if v, ok := d.GetOkExists("direction"); ok {
156156
createNatRuleItem.Direction = helper.IntUint64(v.(int))
157157
}
158158

website/docs/r/cfw_edge_policy.html.markdown

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,31 @@ resource "tencentcloud_cfw_edge_policy" "example" {
2929
}
3030
```
3131

32+
### If target_type is tag
33+
34+
```hcl
35+
resource "tencentcloud_cfw_edge_policy" "example" {
36+
source_content = "0.0.0.0/0"
37+
source_type = "net"
38+
target_content = jsonencode({ "Key" : "test", "Value" : "dddd" })
39+
target_type = "tag"
40+
protocol = "TCP"
41+
rule_action = "drop"
42+
port = "-1/-1"
43+
direction = 1
44+
enable = "true"
45+
description = "policy description."
46+
scope = "all"
47+
}
48+
```
49+
3250
## Argument Reference
3351

3452
The following arguments are supported:
3553

3654
* `direction` - (Required, Int) Rule direction: 1, inbound; 0, outbound.
3755
* `port` - (Required, String) The port for the access control policy. Value: -1/-1: All ports 80: Port 80.
38-
* `protocol` - (Required, String) Protocol, optional values: TCP UDP ICMP ANY HTTP HTTPS HTTP/HTTPS SMTP SMTPS SMTP/SMTPS FTP DNS.
56+
* `protocol` - (Required, String) Protocol. If Direction=1 && Scope=serial, optional values: TCP UDP ICMP ANY HTTP HTTPS HTTP/HTTPS SMTP SMTPS SMTP/SMTPS FTP DNS; If Direction=1 && Scope!=serial, optional values: TCP; If Direction=0 && Scope=serial, optional values: TCP UDP ICMP ANY HTTP HTTPS HTTP/HTTPS SMTP SMTPS SMTP/SMTPS FTP DNS; If Direction=0 && Scope!=serial, optional values: TCP HTTP/HTTPS TLS/SSL.
3957
* `rule_action` - (Required, String) How the traffic set in the access control policy passes through the cloud firewall. Values: accept: allow; drop: reject; log: observe.
4058
* `source_content` - (Required, String) Access source example: net:IP/CIDR(192.168.0.2).
4159
* `source_type` - (Required, String) Access source type: for inbound rules, the type can be net, location, vendor, template; for outbound rules, it can be net, instance, tag, template, group.

website/docs/r/cfw_nat_policy.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ The following arguments are supported:
3434

3535
* `direction` - (Required, Int) Rule direction: 1, inbound; 0, outbound.
3636
* `port` - (Required, String) The port for the access control policy. Value: -1/-1: All ports 80: Port 80.
37-
* `protocol` - (Required, String) Protocol, optional values: TCP UDP ICMP ANY HTTP HTTPS HTTP/HTTPS SMTP SMTPS SMTP/SMTPS FTP DNS.
37+
* `protocol` - (Required, String) Protocol. If Direction=1, optional values: TCP, UDP, ANY; If Direction=0, optional values: TCP, UDP, ICMP, ANY, HTTP, HTTPS, HTTP/HTTPS, SMTP, SMTPS, SMTP/SMTPS, FTP, and DNS.
3838
* `rule_action` - (Required, String) How the traffic set in the access control policy passes through the cloud firewall. Values: accept: allow; drop: reject; log: observe.
3939
* `source_content` - (Required, String) Access source example: net:IP/CIDR(192.168.0.2).
4040
* `source_type` - (Required, String) Access source type: for inbound rules, the type can be net, location, vendor, template; for outbound rules, it can be net, instance, tag, template, group.

0 commit comments

Comments
 (0)