Skip to content

Commit db71178

Browse files
tongyimingmikatong
andauthored
Fix/gaap sweeper (#1166)
* fix: add realserver sweeper * fix: add certificate sweeper * fix: add security rule sweeper * fix: add l4 listener sweeper * fix: add l7 listener sweeper Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 0738c40 commit db71178

File tree

5 files changed

+269
-0
lines changed

5 files changed

+269
-0
lines changed

tencentcloud/resource_tc_gaap_certificate_test.go

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

911
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1012
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1113
)
1214

15+
func init() {
16+
// go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_gaap_certificate
17+
resource.AddTestSweepers("tencentcloud_gaap_certificate", &resource.Sweeper{
18+
Name: "tencentcloud_gaap_certificate",
19+
F: func(r string) error {
20+
logId := getLogId(contextNil)
21+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
22+
sharedClient, err := sharedClientForRegion(r)
23+
if err != nil {
24+
return fmt.Errorf("getting tencentcloud client error: %s", err.Error())
25+
}
26+
client := sharedClient.(*TencentCloudClient)
27+
service := GaapService{client: client.apiV3Conn}
28+
29+
respCertificates, err := service.DescribeCertificates(ctx, nil, nil, nil)
30+
if err != nil {
31+
return err
32+
}
33+
for _, respCertificate := range respCertificates {
34+
instanceName := *respCertificate.CertificateName
35+
now := time.Now()
36+
createTime := time.Unix(int64(*respCertificate.CreateTime), 0)
37+
interval := now.Sub(createTime).Minutes()
38+
39+
if strings.HasPrefix(instanceName, keepResource) || strings.HasPrefix(instanceName, defaultResource) {
40+
continue
41+
}
42+
43+
if needProtect == 1 && int64(interval) < 30 {
44+
continue
45+
}
46+
47+
ee := service.DeleteCertificate(ctx, *respCertificate.CertificateId)
48+
if ee != nil {
49+
continue
50+
}
51+
}
52+
53+
return nil
54+
},
55+
})
56+
}
57+
1358
func TestAccTencentCloudGaapCertificate_basic(t *testing.T) {
1459
t.Parallel()
1560

tencentcloud/resource_tc_gaap_layer4_listener_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,88 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"strings"
78
"testing"
9+
"time"
810

911
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1012
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1113
sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
1214
)
1315

16+
func init() {
17+
// go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_gaap_layer4_listener
18+
resource.AddTestSweepers("tencentcloud_gaap_layer4_listener", &resource.Sweeper{
19+
Name: "tencentcloud_gaap_layer4_listener",
20+
F: func(r string) error {
21+
logId := getLogId(contextNil)
22+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
23+
sharedClient, err := sharedClientForRegion(r)
24+
if err != nil {
25+
return fmt.Errorf("getting tencentcloud client error: %s", err.Error())
26+
}
27+
client := sharedClient.(*TencentCloudClient)
28+
service := GaapService{client: client.apiV3Conn}
29+
proxyIds := []string{defaultGaapProxyId, defaultGaapProxyId2}
30+
for _, proxyId := range proxyIds {
31+
proxyIdTmp := proxyId
32+
tcpListeners, err := service.DescribeTCPListeners(ctx, &proxyIdTmp, nil, nil, nil)
33+
if err != nil {
34+
return err
35+
}
36+
for _, tcpListener := range tcpListeners {
37+
instanceName := *tcpListener.ListenerName
38+
39+
now := time.Now()
40+
createTime := time.Unix(int64(*tcpListener.CreateTime), 0)
41+
interval := now.Sub(createTime).Minutes()
42+
43+
if strings.HasPrefix(instanceName, keepResource) || strings.HasPrefix(instanceName, defaultResource) {
44+
continue
45+
}
46+
47+
if needProtect == 1 && int64(interval) < 30 {
48+
continue
49+
}
50+
51+
ee := service.DeleteLayer4Listener(ctx, *tcpListener.ListenerId, proxyId, *tcpListener.Protocol)
52+
if ee != nil {
53+
continue
54+
}
55+
56+
}
57+
udpListeners, err := service.DescribeUDPListeners(ctx, &proxyIdTmp, nil, nil, nil)
58+
if err != nil {
59+
return err
60+
}
61+
62+
for _, udpListener := range udpListeners {
63+
instanceName := *udpListener.ListenerName
64+
65+
now := time.Now()
66+
createTime := time.Unix(int64(*udpListener.CreateTime), 0)
67+
interval := now.Sub(createTime).Minutes()
68+
69+
if strings.HasPrefix(instanceName, keepResource) || strings.HasPrefix(instanceName, defaultResource) {
70+
continue
71+
}
72+
73+
if needProtect == 1 && int64(interval) < 30 {
74+
continue
75+
}
76+
77+
ee := service.DeleteLayer4Listener(ctx, *udpListener.ListenerId, proxyId, *udpListener.Protocol)
78+
if ee != nil {
79+
continue
80+
}
81+
82+
}
83+
}
84+
return nil
85+
},
86+
})
87+
}
88+
1489
func TestAccTencentCloudGaapLayer4Listener_basic(t *testing.T) {
1590
t.Parallel()
1691
id := new(string)

tencentcloud/resource_tc_gaap_layer7_listener_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,88 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"strings"
78
"testing"
9+
"time"
810

911
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1012
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1113
sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
1214
)
1315

16+
func init() {
17+
// go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_gaap_layer7_listener
18+
resource.AddTestSweepers("tencentcloud_gaap_layer7_listener", &resource.Sweeper{
19+
Name: "tencentcloud_gaap_layer7_listener",
20+
F: func(r string) error {
21+
logId := getLogId(contextNil)
22+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
23+
sharedClient, err := sharedClientForRegion(r)
24+
if err != nil {
25+
return fmt.Errorf("getting tencentcloud client error: %s", err.Error())
26+
}
27+
client := sharedClient.(*TencentCloudClient)
28+
service := GaapService{client: client.apiV3Conn}
29+
proxyIds := []string{defaultGaapProxyId, defaultGaapProxyId2}
30+
for _, proxyId := range proxyIds {
31+
proxyIdTmp := proxyId
32+
httpListeners, err := service.DescribeHTTPListeners(ctx, &proxyIdTmp, nil, nil, nil)
33+
if err != nil {
34+
return err
35+
}
36+
for _, httpListener := range httpListeners {
37+
instanceName := *httpListener.ListenerName
38+
39+
now := time.Now()
40+
createTime := time.Unix(int64(*httpListener.CreateTime), 0)
41+
interval := now.Sub(createTime).Minutes()
42+
43+
if strings.HasPrefix(instanceName, keepResource) || strings.HasPrefix(instanceName, defaultResource) {
44+
continue
45+
}
46+
47+
if needProtect == 1 && int64(interval) < 30 {
48+
continue
49+
}
50+
51+
ee := service.DeleteLayer7Listener(ctx, *httpListener.ListenerId, proxyId, *httpListener.Protocol)
52+
if ee != nil {
53+
continue
54+
}
55+
56+
}
57+
httpsListeners, err := service.DescribeHTTPSListeners(ctx, &proxyIdTmp, nil, nil, nil)
58+
if err != nil {
59+
return err
60+
}
61+
62+
for _, httpsListener := range httpsListeners {
63+
instanceName := *httpsListener.ListenerName
64+
65+
now := time.Now()
66+
createTime := time.Unix(int64(*httpsListener.CreateTime), 0)
67+
interval := now.Sub(createTime).Minutes()
68+
69+
if strings.HasPrefix(instanceName, keepResource) || strings.HasPrefix(instanceName, defaultResource) {
70+
continue
71+
}
72+
73+
if needProtect == 1 && int64(interval) < 30 {
74+
continue
75+
}
76+
77+
ee := service.DeleteLayer7Listener(ctx, *httpsListener.ListenerId, proxyId, *httpsListener.Protocol)
78+
if ee != nil {
79+
continue
80+
}
81+
82+
}
83+
}
84+
return nil
85+
},
86+
})
87+
}
88+
1489
func TestAccTencentCloudGaapLayer7Listener_basic(t *testing.T) {
1590
t.Parallel()
1691
id := new(string)

tencentcloud/resource_tc_gaap_realserver_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,49 @@ import (
55
"errors"
66
"fmt"
77
"strconv"
8+
"strings"
89
"testing"
910

1011
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1112
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1213
)
1314

15+
func init() {
16+
// go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_gaap_realserver
17+
resource.AddTestSweepers("tencentcloud_gaap_realserver", &resource.Sweeper{
18+
Name: "tencentcloud_gaap_realserver",
19+
F: func(r string) error {
20+
logId := getLogId(contextNil)
21+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
22+
sharedClient, err := sharedClientForRegion(r)
23+
if err != nil {
24+
return fmt.Errorf("getting tencentcloud client error: %s", err.Error())
25+
}
26+
client := sharedClient.(*TencentCloudClient)
27+
service := GaapService{client: client.apiV3Conn}
28+
29+
realservers, err := service.DescribeRealservers(ctx, nil, nil, nil, -1)
30+
if err != nil {
31+
return err
32+
}
33+
for _, realserver := range realservers {
34+
instanceName := *realserver.RealServerName
35+
36+
if strings.HasPrefix(instanceName, keepResource) || strings.HasPrefix(instanceName, defaultResource) {
37+
continue
38+
}
39+
40+
ee := service.DeleteRealserver(ctx, *realserver.RealServerId)
41+
if ee != nil {
42+
continue
43+
}
44+
}
45+
46+
return nil
47+
},
48+
})
49+
}
50+
1451
func TestAccTencentCloudGaapRealserver_basic(t *testing.T) {
1552
t.Parallel()
1653
id := new(string)

tencentcloud/resource_tc_gaap_security_rule_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,49 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"strings"
78
"testing"
89

910
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1011
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1112
)
1213

14+
func init() {
15+
// go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_gaap_security_rule
16+
resource.AddTestSweepers("tencentcloud_gaap_security_rule", &resource.Sweeper{
17+
Name: "tencentcloud_gaap_security_rule",
18+
F: func(r string) error {
19+
logId := getLogId(contextNil)
20+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
21+
sharedClient, err := sharedClientForRegion(r)
22+
if err != nil {
23+
return fmt.Errorf("getting tencentcloud client error: %s", err.Error())
24+
}
25+
client := sharedClient.(*TencentCloudClient)
26+
service := GaapService{client: client.apiV3Conn}
27+
28+
securityRules, err := service.DescribeSecurityRules(ctx, defaultGaapSecurityPolicyId)
29+
if err != nil {
30+
return err
31+
}
32+
for _, securityRule := range securityRules {
33+
instanceName := *securityRule.AliasName
34+
35+
if strings.HasPrefix(instanceName, keepResource) || strings.HasPrefix(instanceName, defaultResource) {
36+
continue
37+
}
38+
39+
ee := service.DeleteSecurityRule(ctx, defaultGaapSecurityPolicyId, *securityRule.RuleId)
40+
if ee != nil {
41+
continue
42+
}
43+
}
44+
45+
return nil
46+
},
47+
})
48+
}
49+
1350
func TestAccTencentCloudGaapSecurityRule_basic(t *testing.T) {
1451
t.Parallel()
1552
id := new(string)

0 commit comments

Comments
 (0)