66 "log"
77 "strings"
88 "testing"
9-
10- vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
11- "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
9+ "time"
1210
1311 "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1412 "github.com/hashicorp/terraform-plugin-sdk/terraform"
@@ -35,10 +33,7 @@ func testSweepSecurityGroups(region string) error {
3533 client : client .apiV3Conn ,
3634 }
3735
38- sgs , err := service .DescribeSecurityGroups (ctx , nil , helper .String (NamePrefix ), nil , nil )
39- var sgIds []* string
40- var candidates []string
41- var referredSgs = make (map [string ][]* string , 0 )
36+ sgs , err := service .DescribeSecurityGroups (ctx , nil , nil , nil , nil )
4237
4338 if err != nil {
4439 return fmt .Errorf ("DescribeSecurityGroups error: %s" , err .Error ())
@@ -47,43 +42,25 @@ func testSweepSecurityGroups(region string) error {
4742 for _ , v := range sgs {
4843 name := * v .SecurityGroupName
4944 id := * v .SecurityGroupId
50- sgIds = append (sgIds , v .SecurityGroupId )
51- if ! strings .HasPrefix (name , NamePrefix ) {
52- continue
53- }
54- candidates = append (candidates , id )
55-
56- }
5745
58- refReq := vpc .NewDescribeSecurityGroupReferencesRequest ()
59- refReq .SecurityGroupIds = sgIds
46+ now := time .Now ()
47+ createTime := stringTotime (* v .CreatedTime )
48+ interval := now .Sub (createTime ).Minutes ()
6049
61- refRes , err := client .apiV3Conn .UseVpcClient ().DescribeSecurityGroupReferences (refReq )
62- if err != nil {
63- return fmt .Errorf ("DescribeSecurityGroupReferences error: %s" , err .Error ())
64- }
65- for _ , v := range refRes .Response .ReferredSecurityGroupSet {
66- if len (v .ReferredSecurityGroupIds ) > 0 {
67- referredSgs [* v .SecurityGroupId ] = v .ReferredSecurityGroupIds
50+ if strings .HasPrefix (name , keepResource ) || strings .HasPrefix (name , defaultResource ) {
51+ continue
6852 }
69- }
70-
71- res , err := service .DescribeSecurityGroupsAssociate (ctx , candidates )
72- if err != nil {
73- return fmt .Errorf ("DescribeSecurityGroupsAssociate error: %s" , err .Error ())
74- }
7553
76- for _ , v := range res {
77- id := * v .SecurityGroupId
78-
79- if * v .TotalCount > 0 || len (referredSgs [id ]) > 0 {
54+ // less than 30 minute, not delete
55+ if needProtect == 1 && int64 (interval ) < 30 {
8056 continue
8157 }
8258
8359 if err := service .DeleteSecurityGroup (ctx , id ); err != nil {
8460 log .Printf ("[ERROR] sweep security group %s error: %s" , id , err .Error ())
8561 }
8662 }
63+
8764 return nil
8865}
8966
0 commit comments