Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/3612.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
datasource/tencentcloud_cfw_nat_fw_switches: deprecated `status`, use `enable` instead
```

```release-note:enhancement
resource/tencentcloud_cfw_nat_firewall_switch: optimiz read function code logic
```
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn v1.0.1010
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdwch v1.1.7
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs v1.0.627
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfw v1.0.1018
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfw v1.3.3
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/chdfs v1.0.600
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ciam v1.0.695
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka v1.0.1073
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.1107
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.1033
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.1148
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.50
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.3
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.1206
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cwp v1.0.762
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.1161
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs v1.0.627 h1:nChNdoC
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs v1.0.627/go.mod h1:lr3IyWgJk4rLWr0vVd8J1Tfs5O+wNcwSZ9ciMhGUrlI=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfw v1.0.1018 h1:t9pWssFNmbTerWeZAASeJyMxj7V+Xfb6VnPPMHDpkDU=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfw v1.0.1018/go.mod h1:/o55E2/cnFVNlRWkpqgn+UHN3sK6AAIYDZOsxPEvdRM=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfw v1.3.3 h1:FlCo8dk2pV0K/7CsSqSGgXThMhTcT1w4v5kaI6oKCoY=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfw v1.3.3/go.mod h1:ZiYxDOM/SBJzgN2MLpSIeO7VqJxjhQz4hvcwT1/EBBo=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/chdfs v1.0.600 h1:qSpp4rEgAzfXhi1rPxes+AJp2mwnsyRGPY/Km1FuYGs=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/chdfs v1.0.600/go.mod h1:xud1dQ7Rc23yC5kS00TYRrvZ/A+94EOkwquaI6xGVac=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ciam v1.0.695 h1:FGwsF1/PgY+M92bEC+0NH4tJkI8i0qjrLbZWVjLXOAY=
Expand Down Expand Up @@ -981,6 +983,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.49 h1:BQwUw
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.49/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.50 h1:wZGiUXmzr4L0S1coFhnjddkyNrO5ZTtfxBDrbeR+1d8=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.50/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.3 h1:r05ohLc0LVEpiEQeOJ5QwCiKk6XM9kjTca6+UAbNR/8=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.3/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/controlcenter v1.0.993 h1:WlPgXldQCxt7qi5Xrc6j6zTrsXWzN5BcOGs7Irq7fwQ=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/controlcenter v1.0.993/go.mod h1:Z9U8zNtyuyKhjS0698wqsrG/kLx1TQ5CEixXBwVe7xY=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.860 h1:F3esKBIT3HW9+7Gt8cVgf8X06VdGIczpgLBUECzSEzU=
Expand Down
30 changes: 18 additions & 12 deletions tencentcloud/services/cfw/data_source_tc_cfw_nat_fw_switches.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ func DataSourceTencentCloudCfwNatFwSwitches() *schema.Resource {
"status": {
Optional: true,
Type: schema.TypeInt,
Deprecated: "It has been deprecated from version 1.82.37. Please use `enable` instead.",
Description: "Switch status, 1 open; 0 close.",
},
"enable": {
Optional: true,
Type: schema.TypeInt,
Description: "Switch enable status, 1 open; 0 close.",
},
"data": {
Computed: true,
Type: schema.TypeList,
Expand Down Expand Up @@ -134,21 +140,23 @@ func dataSourceTencentCloudCfwNatFwSwitchesRead(d *schema.ResourceData, meta int
defer tccommon.InconsistentCheck(d, meta)()

var (
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service = CfwService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
data []*cfw.NatSwitchListData
natInsId string
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service = CfwService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
data []*cfw.NatSwitchListData
)

paramMap := make(map[string]interface{})
if v, ok := d.GetOk("nat_ins_id"); ok {
paramMap["NatInsId"] = helper.String(v.(string))
natInsId = v.(string)
paramMap["NatInsId"] = v.(string)
}

if v, _ := d.GetOkExists("status"); v != nil {
paramMap["Status"] = helper.IntInt64(v.(int))
if v, ok := d.GetOkExists("status"); ok {
paramMap["Status"] = v.(int)
}

if v, ok := d.GetOkExists("enable"); ok {
paramMap["Enable"] = v.(int)
}

err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
Expand All @@ -166,11 +174,9 @@ func dataSourceTencentCloudCfwNatFwSwitchesRead(d *schema.ResourceData, meta int
}

tmpList := make([]map[string]interface{}, 0, len(data))

if data != nil {
for _, natSwitchListData := range data {
natSwitchListDataMap := map[string]interface{}{}

if natSwitchListData.Id != nil {
natSwitchListDataMap["id"] = natSwitchListData.Id
}
Expand Down Expand Up @@ -245,7 +251,7 @@ func dataSourceTencentCloudCfwNatFwSwitchesRead(d *schema.ResourceData, meta int
_ = d.Set("data", tmpList)
}

d.SetId(natInsId)
d.SetId(helper.BuildToken())
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), tmpList); e != nil {
Expand Down
10 changes: 5 additions & 5 deletions tencentcloud/services/cfw/data_source_tc_cfw_nat_fw_switches.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
Use this data source to query detailed information of cfw nat_fw_switches
Use this data source to query detailed information of CFW nat fw switches

Example Usage

Query Nat instance'switch by instance id
Query Nat instance'switch by instance ID

```hcl
data "tencentcloud_cfw_nat_fw_switches" "example" {
nat_ins_id = "cfwnat-18d2ba18"
}
```

Or filter by switch status
Or filter by switch enable status

```hcl
data "tencentcloud_cfw_nat_fw_switches" "example" {
nat_ins_id = "cfwnat-18d2ba18"
status = 1
enable = 1
}
```
```
29 changes: 23 additions & 6 deletions tencentcloud/services/cfw/resource_tc_cfw_nat_firewall_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,20 @@ func resourceTencentCloudCfwNatFirewallSwitchCreate(d *schema.ResourceData, meta
defer tccommon.LogElapsed("resource.tencentcloud_cfw_nat_firewall_switch.create")()
defer tccommon.InconsistentCheck(d, meta)()

natInsId := d.Get("nat_ins_id").(string)
subnetId := d.Get("subnet_id").(string)
var (
natInsId string
subnetId string
)

d.SetId(strings.Join([]string{natInsId, subnetId}, tccommon.FILED_SP))
if v, ok := d.GetOk("nat_ins_id"); ok {
natInsId = v.(string)
}

if v, ok := d.GetOk("subnet_id"); ok {
subnetId = v.(string)
}

d.SetId(strings.Join([]string{natInsId, subnetId}, tccommon.FILED_SP))
return resourceTencentCloudCfwNatFirewallSwitchUpdate(d, meta)
}

Expand All @@ -76,7 +85,7 @@ func resourceTencentCloudCfwNatFirewallSwitchRead(d *schema.ResourceData, meta i
natInsId := idSplit[0]
subnetId := idSplit[1]

natFirewallSwitch, err := service.DescribeCfwNatFirewallSwitchById(ctx, natInsId, subnetId)
natFirewallSwitch, err := service.DescribeCfwNatFirewallFwSwitchById(ctx, natInsId, subnetId)
if err != nil {
return err
}
Expand All @@ -87,6 +96,14 @@ func resourceTencentCloudCfwNatFirewallSwitchRead(d *schema.ResourceData, meta i
return nil
}

if natFirewallSwitch.NatInsId != nil {
_ = d.Set("nat_ins_id", natFirewallSwitch.NatInsId)
}

if natFirewallSwitch.SubnetId != nil {
_ = d.Set("subnet_id", natFirewallSwitch.SubnetId)
}

if natFirewallSwitch.Enable != nil {
_ = d.Set("enable", natFirewallSwitch.Enable)
}
Expand All @@ -109,6 +126,7 @@ func resourceTencentCloudCfwNatFirewallSwitchUpdate(d *schema.ResourceData, meta
if len(idSplit) != 2 {
return fmt.Errorf("id is broken,%s", idSplit)
}

natInsId := idSplit[0]
subnetId := idSplit[1]

Expand All @@ -117,7 +135,6 @@ func resourceTencentCloudCfwNatFirewallSwitchUpdate(d *schema.ResourceData, meta
}

request.SubnetIdList = common.StringPtrs([]string{subnetId})

err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCfwClient().ModifyNatFwSwitch(request)
if e != nil {
Expand All @@ -136,7 +153,7 @@ func resourceTencentCloudCfwNatFirewallSwitchUpdate(d *schema.ResourceData, meta

// wait
err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
switchDetail, e := service.DescribeCfwNatFirewallSwitchById(ctx, natInsId, subnetId)
switchDetail, e := service.DescribeCfwNatFirewallFwSwitchById(ctx, natInsId, subnetId)
if e != nil {
return tccommon.RetryError(e)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Provides a resource to create a cfw nat_firewall_switch
Provides a resource to create a CFW nat firewall switch

Example Usage

Expand Down Expand Up @@ -32,8 +32,8 @@ resource "tencentcloud_cfw_nat_firewall_switch" "example" {

Import

cfw nat_firewall_switch can be imported using the id, e.g.
CFW nat firewall switch can be imported using the natInsId#subnetId, e.g.

```
terraform import tencentcloud_cfw_nat_firewall_switch.example cfwnat-18d2ba18#subnet-ef7wyymr
```
```
95 changes: 76 additions & 19 deletions tencentcloud/services/cfw/service_tencentcloud_cfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,32 +598,54 @@ func (me *CfwService) DeleteCfwVpcPolicyById(ctx context.Context, uuid string) (
return
}

func (me *CfwService) DescribeCfwNatFirewallSwitchById(ctx context.Context, natInsId, subnetId string) (natFirewallSwitch *cfw.NatSwitchListData, errRet error) {
func (me *CfwService) DescribeCfwNatFirewallFwSwitchById(ctx context.Context, natInsId, subnetId string) (natFirewallSwitch *cfw.NatSwitchListData, errRet error) {
logId := tccommon.GetLogId(ctx)

request := cfw.NewDescribeNatSwitchListRequest()
request := cfw.NewDescribeNatFwSwitchRequest()
response := cfw.NewDescribeNatFwSwitchResponse()
request.Offset = common.Int64Ptr(0)
request.Limit = common.Int64Ptr(20)
request.NatInsId = &natInsId
searchParam := fmt.Sprintf(`{"SubnetId":"%s"}`, subnetId)
request.SearchValue = &searchParam
request.Filters = []*cfw.CommonFilter{
{
Name: common.StringPtr("NatInsId"),
OperatorType: common.Int64Ptr(1),
Values: common.StringPtrs([]string{natInsId}),
},
{
Name: common.StringPtr("SubnetId"),
OperatorType: common.Int64Ptr(1),
Values: common.StringPtrs([]string{subnetId}),
},
}

defer func() {
if errRet != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
}
}()

ratelimit.Check(request.GetAction())
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
ratelimit.Check(request.GetAction())
result, e := me.client.UseCfwClient().DescribeNatFwSwitch(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}

if result == nil || result.Response == nil || result.Response.Data == nil {
return resource.NonRetryableError(fmt.Errorf("Describe nat firewall switch failed, Response is nil."))
}

response = result
return nil
})

response, err := me.client.UseCfwClient().DescribeNatSwitchList(request)
if err != nil {
errRet = err
return
}

log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())

if len(response.Response.Data) < 1 {
return
}
Expand All @@ -634,8 +656,10 @@ func (me *CfwService) DescribeCfwNatFirewallSwitchById(ctx context.Context, natI

func (me *CfwService) DescribeCfwNatFwSwitchesByFilter(ctx context.Context, param map[string]interface{}) (natFwSwitches []*cfw.NatSwitchListData, errRet error) {
var (
logId = tccommon.GetLogId(ctx)
request = cfw.NewDescribeNatSwitchListRequest()
logId = tccommon.GetLogId(ctx)
request = cfw.NewDescribeNatFwSwitchRequest()
response = cfw.NewDescribeNatFwSwitchResponse()
filters []*cfw.CommonFilter
)

defer func() {
Expand All @@ -645,33 +669,66 @@ func (me *CfwService) DescribeCfwNatFwSwitchesByFilter(ctx context.Context, para
}()

for k, v := range param {
if k == "NatInsId" {
filters = append(filters, &cfw.CommonFilter{
Name: common.StringPtr("NatInsId"),
OperatorType: common.Int64Ptr(1),
Values: common.StringPtrs([]string{v.(string)}),
})
}

if k == "Status" {
request.Status = v.(*int64)
filters = append(filters, &cfw.CommonFilter{
Name: common.StringPtr("Status"),
OperatorType: common.Int64Ptr(1),
Values: common.StringPtrs([]string{helper.IntToStr(v.(int))}),
})
}

if k == "NatInsId" {
request.NatInsId = v.(*string)
if k == "Enable" {
filters = append(filters, &cfw.CommonFilter{
Name: common.StringPtr("Enable"),
OperatorType: common.Int64Ptr(1),
Values: common.StringPtrs([]string{helper.IntToStr(v.(int))}),
})
}
}

ratelimit.Check(request.GetAction())
if len(filters) > 0 {
request.Filters = filters
}

var (
offset int64 = 0
limit int64 = 20
)

for {
request.Offset = &offset
request.Limit = &limit
response, err := me.client.UseCfwClient().DescribeNatSwitchList(request)
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
ratelimit.Check(request.GetAction())
result, e := me.client.UseCfwClient().DescribeNatFwSwitch(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}

if result == nil || result.Response == nil || result.Response.Data == nil {
return resource.NonRetryableError(fmt.Errorf("Describe nat firewall switch failed, Response is nil."))
}

response = result
return nil
})

if err != nil {
errRet = err
return
}

log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())

if response == nil || len(response.Response.Data) < 1 {
if len(response.Response.Data) < 1 {
break
}

Expand Down
Loading
Loading