diff --git a/tencentcloud/data_source_tc_vpn_connections_test.go b/tencentcloud/data_source_tc_vpn_connections_test.go index f4bf58ede4..6d7ce93152 100644 --- a/tencentcloud/data_source_tc_vpn_connections_test.go +++ b/tencentcloud/data_source_tc_vpn_connections_test.go @@ -30,8 +30,7 @@ func TestAccTencentCloudVpnConnectionsDataSource(t *testing.T) { const testAccTencentCloudVpnConnectionsDataSourceConfig_basic = ` resource "tencentcloud_vpn_customer_gateway" "cgw" { name = "terraform_test" - public_ip_address = "3.3.3.3" - + public_ip_address = "3.2.3.3" } # Create VPC and Subnet diff --git a/tencentcloud/data_source_tc_vpn_gateway_routes_test.go b/tencentcloud/data_source_tc_vpn_gateway_routes_test.go index 3b1f893177..d44f628296 100644 --- a/tencentcloud/data_source_tc_vpn_gateway_routes_test.go +++ b/tencentcloud/data_source_tc_vpn_gateway_routes_test.go @@ -41,7 +41,7 @@ resource "tencentcloud_vpn_gateway_route" "route1" { vpn_gateway_id = data.tencentcloud_vpn_gateways.foo.gateway_list.0.id destination_cidr_block = "10.0.0.0/16" instance_type = "VPNCONN" - instance_id = data.tencentcloud_vpn_connection.conns.connection_list.0.id + instance_id = data.tencentcloud_vpn_connections.conns.connection_list.0.id priority = "100" status = "ENABLE" } diff --git a/tencentcloud/resource_tc_vpn_connection_test.go b/tencentcloud/resource_tc_vpn_connection_test.go index 2d5926bf39..209281e3c8 100644 --- a/tencentcloud/resource_tc_vpn_connection_test.go +++ b/tencentcloud/resource_tc_vpn_connection_test.go @@ -167,7 +167,7 @@ func testAccCheckVpnConnectionExists(n string) resource.TestCheckFunc { const testAccVpnConnectionConfig = ` resource "tencentcloud_vpn_customer_gateway" "cgw" { name = "terraform_test" - public_ip_address = "3.3.3.3" + public_ip_address = "1.3.3.3" } @@ -219,8 +219,7 @@ resource "tencentcloud_vpn_connection" "connection" { const testAccVpnConnectionConfigUpdate = ` resource "tencentcloud_vpn_customer_gateway" "cgw" { name = "terraform_test" - public_ip_address = "3.3.3.3" - + public_ip_address = "1.3.3.3" } # Create VPC and Subnet diff --git a/tencentcloud/resource_tc_vpn_customer_gateway_test.go b/tencentcloud/resource_tc_vpn_customer_gateway_test.go index 94bf5d47e1..9f159221c3 100644 --- a/tencentcloud/resource_tc_vpn_customer_gateway_test.go +++ b/tencentcloud/resource_tc_vpn_customer_gateway_test.go @@ -1,6 +1,7 @@ package tencentcloud import ( + "context" "fmt" "log" "testing" @@ -11,6 +12,47 @@ import ( vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" ) +func init() { + resource.AddTestSweepers("tencentcloud_vpn_customer_gateway", &resource.Sweeper{ + Name: "tencentcloud_vpn_customer_gateway", + F: testSweepVpnCustomerGateway, + }) +} + +func testSweepVpnCustomerGateway(region string) error { + logId := getLogId(contextNil) + ctx := context.WithValue(context.TODO(), logIdKey, logId) + + sharedClient, err := sharedClientForRegion(region) + if err != nil { + return fmt.Errorf("getting tencentcloud client error: %s", err.Error()) + } + client := sharedClient.(*TencentCloudClient) + + vpcService := VpcService{ + client: client.apiV3Conn, + } + + instances, err := vpcService.DescribeCustomerGatewayByFilter(ctx, nil) + if err != nil { + return fmt.Errorf("get instance list error: %s", err.Error()) + } + + for _, v := range instances { + customerGwId := *v.CustomerGatewayId + + //if !strings.HasPrefix(instanceName, defaultInsName) { + // continue + //} + + if err = vpcService.DeleteCustomerGateway(ctx, customerGwId); err != nil { + log.Printf("[ERROR] sweep instance %s error: %s", customerGwId, err.Error()) + } + } + + return nil +} + func TestAccTencentCloudVpnCustomerGateway_basic(t *testing.T) { t.Parallel() resource.Test(t, resource.TestCase{ diff --git a/tencentcloud/resource_tc_vpn_gateway_test.go b/tencentcloud/resource_tc_vpn_gateway_test.go index ffa6ecd0cd..a5b64738f1 100644 --- a/tencentcloud/resource_tc_vpn_gateway_test.go +++ b/tencentcloud/resource_tc_vpn_gateway_test.go @@ -39,16 +39,15 @@ func testSweepVpnGateway(region string) error { } for _, v := range instances { - vpnGwId := *v.VpnGatewayId + //vpnGwName := *v.VpnGatewayName - //if !strings.HasPrefix(instanceName, defaultInsName) { + //if !strings.HasPrefix(vpnGwName, defaultInsName) { // continue //} - instanceId := *v.VpnGatewayId - + vpnGwId := *v.VpnGatewayId if err = vpcService.DeleteVpnGateway(ctx, vpnGwId); err != nil { - log.Printf("[ERROR] sweep instance %s error: %s", instanceId, err.Error()) + log.Printf("[ERROR] sweep instance %s error: %s", vpnGwId, err.Error()) } } diff --git a/tencentcloud/service_tencentcloud_vpc.go b/tencentcloud/service_tencentcloud_vpc.go index 7d5e126258..de0442ed9e 100644 --- a/tencentcloud/service_tencentcloud_vpc.go +++ b/tencentcloud/service_tencentcloud_vpc.go @@ -3602,6 +3602,71 @@ func (me *VpcService) DeleteVpnGateway(ctx context.Context, vpnGatewayId string) return } +func (me *VpcService) DescribeCustomerGatewayByFilter(ctx context.Context, filters map[string]string) (instances []*vpc.CustomerGateway, errRet error) { + var ( + logId = getLogId(ctx) + request = vpc.NewDescribeCustomerGatewaysRequest() + ) + request.Filters = make([]*vpc.Filter, 0, len(filters)) + for k, v := range filters { + filter := vpc.Filter{ + Name: helper.String(k), + Values: []*string{helper.String(v)}, + } + request.Filters = append(request.Filters, &filter) + } + + var offset uint64 = 0 + var pageSize uint64 = 100 + instances = make([]*vpc.CustomerGateway, 0) + + for { + request.Offset = &offset + request.Limit = &pageSize + ratelimit.Check(request.GetAction()) + response, err := me.client.UseVpcClient().DescribeCustomerGateways(request) + if err != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", + logId, request.GetAction(), request.ToJsonString(), err.Error()) + 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.CustomerGatewaySet) < 1 { + break + } + instances = append(instances, response.Response.CustomerGatewaySet...) + if len(response.Response.CustomerGatewaySet) < int(pageSize) { + break + } + offset += pageSize + } + return +} + +func (me *VpcService) DeleteCustomerGateway(ctx context.Context, customerGatewayId string) (errRet error) { + logId := getLogId(ctx) + request := vpc.NewDeleteCustomerGatewayRequest() + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail,reason[%s]", logId, request.GetAction(), errRet.Error()) + } + }() + request.CustomerGatewayId = &customerGatewayId + + errRet = resource.Retry(writeRetryTimeout, func() *resource.RetryError { + ratelimit.Check(request.GetAction()) + _, errRet = me.client.UseVpcClient().DeleteCustomerGateway(request) + if errRet != nil { + return retryError(errRet, InternalError) + } + return nil + }) + return +} + func (me *VpcService) CreateAddressTemplate(ctx context.Context, name string, addresses []interface{}) (templateId string, errRet error) { logId := getLogId(ctx) request := vpc.NewCreateAddressTemplateRequest()