Skip to content

Commit 7316a5a

Browse files
author
hellertang
authored
fix vpn test (#911)
1 parent cc12438 commit 7316a5a

6 files changed

+115
-11
lines changed

tencentcloud/data_source_tc_vpn_connections_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ func TestAccTencentCloudVpnConnectionsDataSource(t *testing.T) {
3030
const testAccTencentCloudVpnConnectionsDataSourceConfig_basic = `
3131
resource "tencentcloud_vpn_customer_gateway" "cgw" {
3232
name = "terraform_test"
33-
public_ip_address = "3.3.3.3"
34-
33+
public_ip_address = "3.2.3.3"
3534
}
3635
3736
# Create VPC and Subnet

tencentcloud/data_source_tc_vpn_gateway_routes_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ resource "tencentcloud_vpn_gateway_route" "route1" {
4141
vpn_gateway_id = data.tencentcloud_vpn_gateways.foo.gateway_list.0.id
4242
destination_cidr_block = "10.0.0.0/16"
4343
instance_type = "VPNCONN"
44-
instance_id = data.tencentcloud_vpn_connection.conns.connection_list.0.id
44+
instance_id = data.tencentcloud_vpn_connections.conns.connection_list.0.id
4545
priority = "100"
4646
status = "ENABLE"
4747
}

tencentcloud/resource_tc_vpn_connection_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func testAccCheckVpnConnectionExists(n string) resource.TestCheckFunc {
167167
const testAccVpnConnectionConfig = `
168168
resource "tencentcloud_vpn_customer_gateway" "cgw" {
169169
name = "terraform_test"
170-
public_ip_address = "3.3.3.3"
170+
public_ip_address = "1.3.3.3"
171171
172172
}
173173
@@ -219,8 +219,7 @@ resource "tencentcloud_vpn_connection" "connection" {
219219
const testAccVpnConnectionConfigUpdate = `
220220
resource "tencentcloud_vpn_customer_gateway" "cgw" {
221221
name = "terraform_test"
222-
public_ip_address = "3.3.3.3"
223-
222+
public_ip_address = "1.3.3.3"
224223
}
225224
226225
# Create VPC and Subnet

tencentcloud/resource_tc_vpn_customer_gateway_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tencentcloud
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"testing"
@@ -11,6 +12,47 @@ import (
1112
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
1213
)
1314

15+
func init() {
16+
resource.AddTestSweepers("tencentcloud_vpn_customer_gateway", &resource.Sweeper{
17+
Name: "tencentcloud_vpn_customer_gateway",
18+
F: testSweepVpnCustomerGateway,
19+
})
20+
}
21+
22+
func testSweepVpnCustomerGateway(region string) error {
23+
logId := getLogId(contextNil)
24+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
25+
26+
sharedClient, err := sharedClientForRegion(region)
27+
if err != nil {
28+
return fmt.Errorf("getting tencentcloud client error: %s", err.Error())
29+
}
30+
client := sharedClient.(*TencentCloudClient)
31+
32+
vpcService := VpcService{
33+
client: client.apiV3Conn,
34+
}
35+
36+
instances, err := vpcService.DescribeCustomerGatewayByFilter(ctx, nil)
37+
if err != nil {
38+
return fmt.Errorf("get instance list error: %s", err.Error())
39+
}
40+
41+
for _, v := range instances {
42+
customerGwId := *v.CustomerGatewayId
43+
44+
//if !strings.HasPrefix(instanceName, defaultInsName) {
45+
// continue
46+
//}
47+
48+
if err = vpcService.DeleteCustomerGateway(ctx, customerGwId); err != nil {
49+
log.Printf("[ERROR] sweep instance %s error: %s", customerGwId, err.Error())
50+
}
51+
}
52+
53+
return nil
54+
}
55+
1456
func TestAccTencentCloudVpnCustomerGateway_basic(t *testing.T) {
1557
t.Parallel()
1658
resource.Test(t, resource.TestCase{

tencentcloud/resource_tc_vpn_gateway_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,15 @@ func testSweepVpnGateway(region string) error {
3939
}
4040

4141
for _, v := range instances {
42-
vpnGwId := *v.VpnGatewayId
42+
//vpnGwName := *v.VpnGatewayName
4343

44-
//if !strings.HasPrefix(instanceName, defaultInsName) {
44+
//if !strings.HasPrefix(vpnGwName, defaultInsName) {
4545
// continue
4646
//}
4747

48-
instanceId := *v.VpnGatewayId
49-
48+
vpnGwId := *v.VpnGatewayId
5049
if err = vpcService.DeleteVpnGateway(ctx, vpnGwId); err != nil {
51-
log.Printf("[ERROR] sweep instance %s error: %s", instanceId, err.Error())
50+
log.Printf("[ERROR] sweep instance %s error: %s", vpnGwId, err.Error())
5251
}
5352
}
5453

tencentcloud/service_tencentcloud_vpc.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3602,6 +3602,71 @@ func (me *VpcService) DeleteVpnGateway(ctx context.Context, vpnGatewayId string)
36023602
return
36033603
}
36043604

3605+
func (me *VpcService) DescribeCustomerGatewayByFilter(ctx context.Context, filters map[string]string) (instances []*vpc.CustomerGateway, errRet error) {
3606+
var (
3607+
logId = getLogId(ctx)
3608+
request = vpc.NewDescribeCustomerGatewaysRequest()
3609+
)
3610+
request.Filters = make([]*vpc.Filter, 0, len(filters))
3611+
for k, v := range filters {
3612+
filter := vpc.Filter{
3613+
Name: helper.String(k),
3614+
Values: []*string{helper.String(v)},
3615+
}
3616+
request.Filters = append(request.Filters, &filter)
3617+
}
3618+
3619+
var offset uint64 = 0
3620+
var pageSize uint64 = 100
3621+
instances = make([]*vpc.CustomerGateway, 0)
3622+
3623+
for {
3624+
request.Offset = &offset
3625+
request.Limit = &pageSize
3626+
ratelimit.Check(request.GetAction())
3627+
response, err := me.client.UseVpcClient().DescribeCustomerGateways(request)
3628+
if err != nil {
3629+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
3630+
logId, request.GetAction(), request.ToJsonString(), err.Error())
3631+
errRet = err
3632+
return
3633+
}
3634+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
3635+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
3636+
3637+
if response == nil || len(response.Response.CustomerGatewaySet) < 1 {
3638+
break
3639+
}
3640+
instances = append(instances, response.Response.CustomerGatewaySet...)
3641+
if len(response.Response.CustomerGatewaySet) < int(pageSize) {
3642+
break
3643+
}
3644+
offset += pageSize
3645+
}
3646+
return
3647+
}
3648+
3649+
func (me *VpcService) DeleteCustomerGateway(ctx context.Context, customerGatewayId string) (errRet error) {
3650+
logId := getLogId(ctx)
3651+
request := vpc.NewDeleteCustomerGatewayRequest()
3652+
defer func() {
3653+
if errRet != nil {
3654+
log.Printf("[CRITAL]%s api[%s] fail,reason[%s]", logId, request.GetAction(), errRet.Error())
3655+
}
3656+
}()
3657+
request.CustomerGatewayId = &customerGatewayId
3658+
3659+
errRet = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
3660+
ratelimit.Check(request.GetAction())
3661+
_, errRet = me.client.UseVpcClient().DeleteCustomerGateway(request)
3662+
if errRet != nil {
3663+
return retryError(errRet, InternalError)
3664+
}
3665+
return nil
3666+
})
3667+
return
3668+
}
3669+
36053670
func (me *VpcService) CreateAddressTemplate(ctx context.Context, name string, addresses []interface{}) (templateId string, errRet error) {
36063671
logId := getLogId(ctx)
36073672
request := vpc.NewCreateAddressTemplateRequest()

0 commit comments

Comments
 (0)