Skip to content

Commit c1f7e71

Browse files
author
hellertang
authored
fix ssl tests (#987)
1 parent 9df21be commit c1f7e71

File tree

3 files changed

+82
-8
lines changed

3 files changed

+82
-8
lines changed

tencentcloud/resource_tc_ssl_certificate_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"log"
8+
"strings"
79
"testing"
10+
"time"
811

912
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
1013

@@ -15,6 +18,58 @@ import (
1518
ssl "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ssl/v20191205"
1619
)
1720

21+
func init() {
22+
resource.AddTestSweepers("tencentcloud_ssl_certificate", &resource.Sweeper{
23+
Name: "tencentcloud_ssl_certificate",
24+
F: testSweepSslCertificate,
25+
})
26+
}
27+
28+
func testSweepSslCertificate(region string) error {
29+
logId := getLogId(contextNil)
30+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
31+
32+
sharedClient, err := sharedClientForRegion(region)
33+
if err != nil {
34+
return fmt.Errorf("getting tencentcloud client error: %s", err.Error())
35+
}
36+
client := sharedClient.(*TencentCloudClient)
37+
38+
sslService := SSLService{
39+
client: client.apiV3Conn,
40+
}
41+
describeRequest := ssl.NewDescribeCertificatesRequest()
42+
instances, err := sslService.DescribeCertificates(ctx, describeRequest)
43+
if err != nil {
44+
return fmt.Errorf("get instance list error: %s", err.Error())
45+
}
46+
47+
for _, v := range instances {
48+
49+
instanceId := *v.CertificateId
50+
instanceName := *v.Alias
51+
now := time.Now()
52+
createTime := stringTotime(*v.CertBeginTime)
53+
interval := now.Sub(createTime).Minutes()
54+
55+
if strings.HasPrefix(instanceName, keepResource) || strings.HasPrefix(instanceName, defaultResource) {
56+
continue
57+
}
58+
59+
if needProtect == 1 && int64(interval) < 30 {
60+
continue
61+
}
62+
63+
request := ssl.NewDeleteCertificateRequest()
64+
request.CertificateId = helper.String(instanceId)
65+
if _, err = sslService.DeleteCertificate(ctx, request); err != nil {
66+
log.Printf("[ERROR] sweep instance %s error: %s", instanceId, err.Error())
67+
}
68+
}
69+
70+
return nil
71+
}
72+
1873
func TestAccTencentCloudSslCertificate_basic(t *testing.T) {
1974
t.Parallel()
2075
resource.Test(t, resource.TestCase{

tencentcloud/resource_tc_vpn_customer_gateway_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"strings"
78
"testing"
9+
"time"
810

911
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1012
"github.com/hashicorp/terraform-plugin-sdk/terraform"
@@ -40,10 +42,17 @@ func testSweepVpnCustomerGateway(region string) error {
4042

4143
for _, v := range instances {
4244
customerGwId := *v.CustomerGatewayId
45+
customerName := *v.CustomerGatewayName
4346

44-
//if !strings.HasPrefix(instanceName, defaultInsName) {
45-
// continue
46-
//}
47+
now := time.Now()
48+
createTime := stringTotime(*v.CreatedTime)
49+
interval := now.Sub(createTime).Minutes()
50+
if strings.HasPrefix(customerName, keepResource) || strings.HasPrefix(customerName, defaultResource) {
51+
continue
52+
}
53+
if needProtect == 1 && int64(interval) < 30 {
54+
continue
55+
}
4756

4857
if err = vpcService.DeleteCustomerGateway(ctx, customerGwId); err != nil {
4958
log.Printf("[ERROR] sweep instance %s error: %s", customerGwId, err.Error())

tencentcloud/resource_tc_vpn_gateway_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"strings"
78
"testing"
9+
"time"
810

911
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1012
"github.com/hashicorp/terraform-plugin-sdk/terraform"
@@ -39,13 +41,21 @@ func testSweepVpnGateway(region string) error {
3941
}
4042

4143
for _, v := range instances {
42-
//vpnGwName := *v.VpnGatewayName
43-
44-
//if !strings.HasPrefix(vpnGwName, defaultInsName) {
45-
// continue
46-
//}
4744

4845
vpnGwId := *v.VpnGatewayId
46+
vpnName := *v.VpnGatewayName
47+
now := time.Now()
48+
createTime := stringTotime(*v.CreatedTime)
49+
interval := now.Sub(createTime).Minutes()
50+
51+
if strings.HasPrefix(vpnName, keepResource) || strings.HasPrefix(vpnName, defaultResource) {
52+
continue
53+
}
54+
55+
if needProtect == 1 && int64(interval) < 30 {
56+
continue
57+
}
58+
4959
if err = vpcService.DeleteVpnGateway(ctx, vpnGwId); err != nil {
5060
log.Printf("[ERROR] sweep instance %s error: %s", vpnGwId, err.Error())
5161
}

0 commit comments

Comments
 (0)