diff --git a/tencentcloud/common/file_common.go b/tencentcloud/common/file_common.go new file mode 100644 index 0000000000..da8d6f5f6f --- /dev/null +++ b/tencentcloud/common/file_common.go @@ -0,0 +1,90 @@ +package common + +import ( + "encoding/csv" + "log" + "os" + "path/filepath" + "time" +) + +const ( + SweeperResourceScanDir = "../../../tmp/resource_scan/" + SweeperNonKeepResourceScanDir = "../../../tmp/non_keep_resource_scan/" +) + +var ResourceScanHeader = []string{"资源类型", "资源名称", "实例ID", "实例名称", "分类", "创建时长(天)"} +var NonKeepResourceScanHeader = []string{"ResourceType", "ResourceName", "InstanceId", "InstanceName"} + +// WriteCsvFileData write data to csv file +func WriteCsvFileData(dirPath string, header []string, data [][]string) error { + log.Printf("[INFO] write csv file data[%v] to path[%v] start", len(data), dirPath) + + count := 0 + defer func() { + log.Printf("[INFO] write csv file data to path[%v] success count[%v]", dirPath, count) + }() + + if len(data) == 0 { + return nil + } + + err := os.MkdirAll(dirPath, 0755) + if err != nil { + log.Printf("[CRITAL] create directory %s error: %v", dirPath, err.Error()) + return err + } + + currentDate := time.Now().Format("20060102") + filePath := filepath.Join(dirPath, currentDate+".csv") + + _, err = os.Stat(filePath) + if os.IsNotExist(err) { + err = GenerateCsvFile(filePath, header) + if err != nil { + log.Printf("[CRITAL] generate csv file error: %v", err.Error()) + return err + } + } + + file, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, 0644) + if err != nil { + log.Printf("[CRITAL] open csv file error: %v", err.Error()) + return err + } + defer file.Close() + + writer := csv.NewWriter(file) + + for _, row := range data { + err = writer.Write(row) + if err != nil { + log.Printf("[CRITAL] write data[%v] to csv file error: %v", row, err.Error()) + return err + } + count++ + } + writer.Flush() + + return nil +} + +// GenerateCsvFile generate when csv file does not exist +func GenerateCsvFile(filePath string, header []string) error { + file, err := os.Create(filePath) + if err != nil { + log.Printf("[CRITAL] create csv file error: %v", err.Error()) + return err + } + defer file.Close() + + writer := csv.NewWriter(file) + err = writer.Write(header) + if err != nil { + log.Printf("[CRITAL] write header to csv file error: %v", err.Error()) + return err + } + writer.Flush() + + return nil +} diff --git a/tencentcloud/common/resource_scan.go b/tencentcloud/common/resource_scan.go new file mode 100644 index 0000000000..dfed120c3f --- /dev/null +++ b/tencentcloud/common/resource_scan.go @@ -0,0 +1,148 @@ +package common + +import ( + "fmt" + "log" + "regexp" + "strconv" + "time" +) + +const ( + KeepResource = "keep" + NonKeepResource = "non-keep" +) + +// TimeFormats add all possible time formats +var TimeFormats = []string{ + time.RFC3339, //ISO8601 UTC time + "2006-01-02 15:04:05", + // add other time formats here +} + +type ResourceInstance struct { + Id string + Name string + CreatTime string + DefaultKeep bool +} + +func ProcessScanCloudResources(resources, nonKeepResources []*ResourceInstance, resourceType, resourceName string) { + ProcessResources(resources, resourceType, resourceName) + + ProcessNonKeepResources(nonKeepResources, resourceType, resourceName) +} + +// ProcessResources Process all scanned cloud resources +func ProcessResources(resources []*ResourceInstance, resourceType, resourceName string) { + data := make([][]string, len(resources)) + for i, r := range resources { + isResourceKeep := CheckResourceNameKeep(r.Name) + // some resources default to keep + if r.DefaultKeep { + isResourceKeep = KeepResource + } + + creationDuration, err := DaysSinceCreation(r.CreatTime) + if err != nil { + log.Printf("[CRITAL] compute resource creation duration error: %v", err.Error()) + } + + data[i] = []string{ + resourceType, + resourceName, + r.Id, + r.Name, + isResourceKeep, + creationDuration, + } + } + err := WriteCsvFileData(SweeperResourceScanDir, ResourceScanHeader, data) + if err != nil { + log.Printf("[CRITAL] write csv file data error: %v", err.Error()) + } +} + +// ProcessNonKeepResources Processing scanned non-keep cloud resources +func ProcessNonKeepResources(nonKeepResources []*ResourceInstance, resourceType, resourceName string) { + data := make([][]string, len(nonKeepResources)) + for i, r := range nonKeepResources { + data[i] = []string{ + resourceType, + resourceName, + r.Id, + r.Name, + } + } + err := WriteCsvFileData(SweeperNonKeepResourceScanDir, NonKeepResourceScanHeader, data) + if err != nil { + log.Printf("[CRITAL] write csv file data error: %v", err.Error()) + } +} + +// CheckResourceNameKeep check whether to keep resource name +func CheckResourceNameKeep(name string) string { + flag := CheckResourcePersist(name, "") + if flag { + return KeepResource + } + return NonKeepResource +} + +// CheckResourcePersist check whether to persist resource +func CheckResourcePersist(name, createTime string) bool { + if name == "" && createTime == "" { + return false + } + parsedTime, _ := ParsedTime(createTime) + + createdWithin30Minutes := false + if parsedTime != nil { + createdWithin30Minutes = parsedTime.Add(time.Minute * 30).After(time.Now()) + } + + flag := regexp.MustCompile("^(keep|Default)").MatchString(name) + return flag || createdWithin30Minutes +} + +// DaysSinceCreation compute resource creation duration +func DaysSinceCreation(createTime string) (string, error) { + parsedTime, err := ParsedTime(createTime) + if err != nil { + return "", err + } + + duration := time.Since(*parsedTime) + days := duration.Hours() / 24 + + return fmt.Sprintf("%.2f", days), nil +} + +// ParsedTime parse time +func ParsedTime(createTime string) (*time.Time, error) { + if createTime == "" { + return nil, nil + } + + var parsedTime time.Time + var err error + + timestamp, err := strconv.ParseInt(createTime, 10, 64) + if err == nil { + parsedTime = time.Unix(timestamp, 0) + } else { + // try parsing input strings using different time formats + for _, format := range TimeFormats { + parsedTime, err = time.Parse(format, createTime) + if err == nil { + break + } + } + } + + if err != nil { + log.Printf("[CRITAL] unable to parse create time[%s]", createTime) + return nil, fmt.Errorf("unable to parse create time: %v", err.Error()) + } + return &parsedTime, nil +} diff --git a/tencentcloud/services/as/resource_tc_as_attachment_test.go b/tencentcloud/services/as/resource_tc_as_attachment_test.go index e8d95576c1..535a3b4abd 100644 --- a/tencentcloud/services/as/resource_tc_as_attachment_test.go +++ b/tencentcloud/services/as/resource_tc_as_attachment_test.go @@ -35,6 +35,23 @@ func testSweepAsAttachment(r string) error { return fmt.Errorf("list scaling group error: %s", err.Error()) } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range scalingGroups { + if !tccommon.CheckResourcePersist(*v.AutoScalingGroupName, *v.CreatedTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.AutoScalingGroupId, + Name: *v.AutoScalingGroupName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.AutoScalingGroupId, + Name: *v.AutoScalingGroupName, + CreatTime: *v.CreatedTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "as", "attachment") + for _, v := range scalingGroups { scalingGroupId := *v.AutoScalingGroupId scalingGroupName := *v.AutoScalingGroupName diff --git a/tencentcloud/services/cam/resource_tc_cam_group_test.go b/tencentcloud/services/cam/resource_tc_cam_group_test.go index becdd2574d..45076c91f5 100644 --- a/tencentcloud/services/cam/resource_tc_cam_group_test.go +++ b/tencentcloud/services/cam/resource_tc_cam_group_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "strconv" "testing" tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest" @@ -33,6 +34,24 @@ func init() { if err != nil { return err } + + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range groups { + if !tccommon.CheckResourcePersist(*v.GroupName, *v.CreateTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: strconv.FormatUint(*v.GroupId, 10), + Name: *v.GroupName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: strconv.FormatUint(*v.GroupId, 10), + Name: *v.GroupName, + CreatTime: *v.CreateTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "cam", "group") + for _, v := range groups { name := *v.GroupName diff --git a/tencentcloud/services/cbs/resource_tc_cbs_storage_test.go b/tencentcloud/services/cbs/resource_tc_cbs_storage_test.go index 76b2b7977d..de6bae3230 100644 --- a/tencentcloud/services/cbs/resource_tc_cbs_storage_test.go +++ b/tencentcloud/services/cbs/resource_tc_cbs_storage_test.go @@ -27,11 +27,27 @@ func init() { service := localcbs.NewCbsService(client) disks, err := service.DescribeDisksByFilter(ctx, nil) - if err != nil { return err } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range disks { + if !tccommon.CheckResourcePersist(*v.DiskName, *v.CreateTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.DiskId, + Name: *v.DiskName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.DiskId, + Name: *v.DiskName, + CreatTime: *v.CreateTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "cbs", "storage") + for i := range disks { disk := disks[i] id := *disk.DiskId diff --git a/tencentcloud/services/ccn/resource_tc_ccn_test.go b/tencentcloud/services/ccn/resource_tc_ccn_test.go index 8b970e5cdc..62ec4aa002 100644 --- a/tencentcloud/services/ccn/resource_tc_ccn_test.go +++ b/tencentcloud/services/ccn/resource_tc_ccn_test.go @@ -41,6 +41,23 @@ func testSweepCcnInstance(region string) error { return fmt.Errorf("get instance list error: %s", err.Error()) } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range instances { + if !tccommon.CheckResourcePersist(v.Name(), v.CreateTime()) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: v.CcnId(), + Name: v.Name(), + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: v.CcnId(), + Name: v.Name(), + CreatTime: v.CreateTime(), + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "ccn", "") + for _, v := range instances { instanceId := v.CcnId() instanceName := v.Name() diff --git a/tencentcloud/services/cdb/resource_tc_mysql_instance_test.go b/tencentcloud/services/cdb/resource_tc_mysql_instance_test.go index 08192e1454..b8d7dbde76 100644 --- a/tencentcloud/services/cdb/resource_tc_mysql_instance_test.go +++ b/tencentcloud/services/cdb/resource_tc_mysql_instance_test.go @@ -47,12 +47,30 @@ func testSweepMySQLInstance(region string) error { if err != nil { return err } + items := response.Response.Items - if len(response.Response.Items) == 0 { + if len(items) == 0 { return nil } - for _, v := range response.Response.Items { + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range items { + if !tccommon.CheckResourcePersist(*v.InstanceId, *v.CreateTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.InstanceId, + Name: *v.InstanceName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.InstanceId, + Name: *v.InstanceName, + CreatTime: *v.CreateTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "msql", "instance") + + for _, v := range items { id := *v.InstanceId name := *v.InstanceName if tcacctest.IsResourcePersist(name, nil) { diff --git a/tencentcloud/services/clb/resource_tc_clb_instance_test.go b/tencentcloud/services/clb/resource_tc_clb_instance_test.go index 844633e28f..3b5d48f9be 100644 --- a/tencentcloud/services/clb/resource_tc_clb_instance_test.go +++ b/tencentcloud/services/clb/resource_tc_clb_instance_test.go @@ -47,6 +47,23 @@ func testSweepClbInstance(region string) error { return err } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range res { + if !tccommon.CheckResourcePersist(*v.LoadBalancerName, *v.CreateTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.LoadBalancerId, + Name: *v.LoadBalancerName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.LoadBalancerId, + Name: *v.LoadBalancerName, + CreatTime: *v.CreateTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "clb", "instance") + if len(res) > 0 { for _, v := range res { id := *v.LoadBalancerId diff --git a/tencentcloud/services/clb/service_tencentcloud_clb.go b/tencentcloud/services/clb/service_tencentcloud_clb.go index 53c66d6c6a..c62f9c2ed3 100644 --- a/tencentcloud/services/clb/service_tencentcloud_clb.go +++ b/tencentcloud/services/clb/service_tencentcloud_clb.go @@ -1232,13 +1232,12 @@ func (me *ClbService) DescribeTargetGroups(ctx context.Context, targetGroupId st if targetGroupId != "" { request.TargetGroupIds = []*string{&targetGroupId} } - request.Filters = make([]*clb.Filter, 0, len(filters)) for k, v := range filters { - filter := clb.Filter{ + tmpFilter := clb.Filter{ Name: helper.String(k), Values: []*string{helper.String(v)}, } - request.Filters = append(request.Filters, &filter) + request.Filters = append(request.Filters, &tmpFilter) } var offset uint64 = 0 diff --git a/tencentcloud/services/cls/resource_tc_cls_topic_test.go b/tencentcloud/services/cls/resource_tc_cls_topic_test.go index 282e70153b..8b7f8320e3 100644 --- a/tencentcloud/services/cls/resource_tc_cls_topic_test.go +++ b/tencentcloud/services/cls/resource_tc_cls_topic_test.go @@ -40,6 +40,23 @@ func testSweepClsTopic(region string) error { return fmt.Errorf("get instance list error: %s", err.Error()) } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range instances { + if !tccommon.CheckResourcePersist(*v.TopicName, *v.CreateTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.TopicId, + Name: *v.TopicName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.TopicId, + Name: *v.TopicName, + CreatTime: *v.CreateTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "cls", "topic") + for _, v := range instances { instanceId := v.TopicId instanceName := v.TopicName diff --git a/tencentcloud/services/cos/resource_tc_cos_bucket_test.go b/tencentcloud/services/cos/resource_tc_cos_bucket_test.go index b14d648f42..3376fa632e 100644 --- a/tencentcloud/services/cos/resource_tc_cos_bucket_test.go +++ b/tencentcloud/services/cos/resource_tc_cos_bucket_test.go @@ -39,6 +39,21 @@ func testSweepCosBuckets(region string) error { return fmt.Errorf("list buckets error: %s", err.Error()) } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range buckets { + if !tccommon.CheckResourcePersist(*v.Name, v.CreationDate.Format("2006-01-02 15:04:05")) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Name: *v.Name, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Name: *v.Name, + CreatTime: v.CreationDate.Format("2006-01-02 15:04:05"), + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "cos", "bucket") + //prefix := regexp.MustCompile("^(tf|test)-") for _, v := range buckets { diff --git a/tencentcloud/services/cvm/resource_tc_instance_test.go b/tencentcloud/services/cvm/resource_tc_instance_test.go index a39053e744..d57e47073a 100644 --- a/tencentcloud/services/cvm/resource_tc_instance_test.go +++ b/tencentcloud/services/cvm/resource_tc_instance_test.go @@ -40,6 +40,23 @@ func testSweepCvmInstance(region string) error { return fmt.Errorf("get instance list error: %s", err.Error()) } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range instances { + if !tccommon.CheckResourcePersist(*v.InstanceName, *v.CreatedTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.InstanceId, + Name: *v.InstanceName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.InstanceId, + Name: *v.InstanceName, + CreatTime: *v.CreatedTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "cvm", "instance") + for _, v := range instances { instanceId := *v.InstanceId //instanceName := *v.InstanceName diff --git a/tencentcloud/services/scf/resource_tc_scf_function_test.go b/tencentcloud/services/scf/resource_tc_scf_function_test.go index 6ef152784d..bf20a781a1 100644 --- a/tencentcloud/services/scf/resource_tc_scf_function_test.go +++ b/tencentcloud/services/scf/resource_tc_scf_function_test.go @@ -44,6 +44,24 @@ func init() { if err != nil { continue } + + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range funs { + if !tccommon.CheckResourcePersist(*v.FunctionName, *v.AddTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.FunctionId, + Name: *v.FunctionName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.FunctionId, + Name: *v.FunctionName, + CreatTime: *v.AddTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "scf", "") + for _, fun := range funs { createTime := tccommon.StringToTime(*fun.AddTime) now := time.Now() diff --git a/tencentcloud/services/sqlserver/resource_tc_sqlserver_account_test.go b/tencentcloud/services/sqlserver/resource_tc_sqlserver_account_test.go index db193dc281..f759ecacb9 100644 --- a/tencentcloud/services/sqlserver/resource_tc_sqlserver_account_test.go +++ b/tencentcloud/services/sqlserver/resource_tc_sqlserver_account_test.go @@ -46,6 +46,21 @@ func init() { accounts, _ := service.DescribeSqlserverAccounts(ctx, instanceId) + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range accounts { + if !tccommon.CheckResourcePersist(*v.Name, *v.CreateTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Name: *v.Name, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Name: *v.Name, + CreatTime: *v.CreateTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "sqlserver", "account") + for i := range accounts { account := accounts[i] name := *account.Name diff --git a/tencentcloud/services/sqlserver/resource_tc_sqlserver_db_test.go b/tencentcloud/services/sqlserver/resource_tc_sqlserver_db_test.go index 3ae817944f..dc8a0a4bc9 100644 --- a/tencentcloud/services/sqlserver/resource_tc_sqlserver_db_test.go +++ b/tencentcloud/services/sqlserver/resource_tc_sqlserver_db_test.go @@ -51,11 +51,25 @@ func init() { } dbs, err := service.DescribeDBsOfInstance(ctx, insId) - if err != nil { return err } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range dbs { + if !tccommon.CheckResourcePersist(*v.Name, *v.CreateTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Name: *v.Name, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Name: *v.Name, + CreatTime: *v.CreateTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "sqlserver", "db") + for i := range dbs { db := dbs[i] if !strings.HasPrefix(*db.Name, "test") { diff --git a/tencentcloud/services/sqlserver/resource_tc_sqlserver_instance_test.go b/tencentcloud/services/sqlserver/resource_tc_sqlserver_instance_test.go index fc8998b13c..515178ef9c 100644 --- a/tencentcloud/services/sqlserver/resource_tc_sqlserver_instance_test.go +++ b/tencentcloud/services/sqlserver/resource_tc_sqlserver_instance_test.go @@ -36,6 +36,23 @@ func init() { return err } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range instances { + if !tccommon.CheckResourcePersist(*v.Name, *v.CreateTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.InstanceId, + Name: *v.Name, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.InstanceId, + Name: *v.Name, + CreatTime: *v.CreateTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "sqlserver", "instance") + err = batchDeleteSQLServerInstances(ctx, service, instances) if err != nil { diff --git a/tencentcloud/services/tcr/resource_tc_tcr_customized_domain_test.go b/tencentcloud/services/tcr/resource_tc_tcr_customized_domain_test.go index 9a69e64d1f..de2f521d6d 100644 --- a/tencentcloud/services/tcr/resource_tc_tcr_customized_domain_test.go +++ b/tencentcloud/services/tcr/resource_tc_tcr_customized_domain_test.go @@ -35,6 +35,22 @@ func testSweepTcrCustomizedDomain(r string) error { return nil } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range domains { + if !tccommon.CheckResourcePersist(*v.DomainName, "") { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.RegistryId, + Name: *v.DomainName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.RegistryId, + Name: *v.DomainName, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "tcr", "customized_domain") + for _, v := range domains { delName := *v.DomainName diff --git a/tencentcloud/services/tcr/resource_tc_tcr_immutable_tag_rule_test.go b/tencentcloud/services/tcr/resource_tc_tcr_immutable_tag_rule_test.go index 2a07120bc2..03caac6ef3 100644 --- a/tencentcloud/services/tcr/resource_tc_tcr_immutable_tag_rule_test.go +++ b/tencentcloud/services/tcr/resource_tc_tcr_immutable_tag_rule_test.go @@ -45,6 +45,20 @@ func testSweepTcrImmutableTagRule(r string) error { return err } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range rules { + if !tccommon.CheckResourcePersist("", "") { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: helper.Int64ToStr(*v.RuleId), + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: helper.Int64ToStr(*v.RuleId), + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "tcr", "immutable_tag_rule") + for _, rule := range rules { ruleId := helper.Int64ToStr(*rule.RuleId) diff --git a/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go b/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go index 4cddb0dbe5..88fd398e96 100644 --- a/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go +++ b/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go @@ -36,6 +36,23 @@ func init() { return err } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range instances { + if !tccommon.CheckResourcePersist(*v.RegistryName, *v.CreatedAt) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.RegistryId, + Name: *v.RegistryName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.RegistryId, + Name: *v.RegistryName, + CreatTime: *v.CreatedAt, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "tcr", "instance") + for i := range instances { ins := instances[i] id := *ins.RegistryId diff --git a/tencentcloud/services/tcr/resource_tc_tcr_namespace_test.go b/tencentcloud/services/tcr/resource_tc_tcr_namespace_test.go index 75d502dbe8..7821b08399 100644 --- a/tencentcloud/services/tcr/resource_tc_tcr_namespace_test.go +++ b/tencentcloud/services/tcr/resource_tc_tcr_namespace_test.go @@ -3,6 +3,7 @@ package tcr_test import ( "context" "fmt" + "strconv" "strings" "testing" @@ -52,6 +53,23 @@ func init() { return err } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range namespaces { + if !tccommon.CheckResourcePersist(*v.Name, *v.CreationTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: strconv.FormatInt(*v.NamespaceId, 10), + Name: *v.Name, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: strconv.FormatInt(*v.NamespaceId, 10), + Name: *v.Name, + CreatTime: *v.CreationTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "tcr", "namespace") + for i := range namespaces { n := namespaces[i] if tcacctest.IsResourcePersist(*n.Name, nil) { diff --git a/tencentcloud/services/tcr/resource_tc_tcr_repository_test.go b/tencentcloud/services/tcr/resource_tc_tcr_repository_test.go index 20aafbcd96..f83c3cca2f 100644 --- a/tencentcloud/services/tcr/resource_tc_tcr_repository_test.go +++ b/tencentcloud/services/tcr/resource_tc_tcr_repository_test.go @@ -54,6 +54,27 @@ func testSweepTCRRepository(r string) error { return err } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range repos { + names := strings.Split(*v.Name, "/") + if len(names) != 2 { + continue + } + repoName := names[1] + + if !tccommon.CheckResourcePersist(repoName, *v.CreationTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Name: repoName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Name: repoName, + CreatTime: *v.CreationTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "tcr", "repository") + for i := range repos { n := repos[i] names := strings.Split(*n.Name, "/") diff --git a/tencentcloud/services/tcr/resource_tc_tcr_token_test.go b/tencentcloud/services/tcr/resource_tc_tcr_token_test.go index 892990d775..70688d5f4e 100644 --- a/tencentcloud/services/tcr/resource_tc_tcr_token_test.go +++ b/tencentcloud/services/tcr/resource_tc_tcr_token_test.go @@ -54,6 +54,21 @@ func init() { return err } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range tokens { + if !tccommon.CheckResourcePersist("", *v.CreatedAt) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.Id, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.Id, + CreatTime: *v.CreatedAt, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "tcr", "token") + for i := range tokens { token := tokens[i] id := *token.Id diff --git a/tencentcloud/services/tke/resource_tc_kubernetes_cluster_test.go b/tencentcloud/services/tke/resource_tc_kubernetes_cluster_test.go index 17fa059bfb..9897272f30 100644 --- a/tencentcloud/services/tke/resource_tc_kubernetes_cluster_test.go +++ b/tencentcloud/services/tke/resource_tc_kubernetes_cluster_test.go @@ -37,6 +37,23 @@ func init() { return err } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range clusters { + if !tccommon.CheckResourcePersist(v.ClusterName, v.CreatedTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: v.ClusterId, + Name: v.ClusterName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: v.ClusterId, + Name: v.ClusterName, + CreatTime: v.CreatedTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "tke", "cluster") + for _, v := range clusters { id := v.ClusterId name := v.ClusterName diff --git a/tencentcloud/services/tke/resource_tc_kubernetes_node_pool_test.go b/tencentcloud/services/tke/resource_tc_kubernetes_node_pool_test.go index 655ceff7af..0c5ad3af5a 100644 --- a/tencentcloud/services/tke/resource_tc_kubernetes_node_pool_test.go +++ b/tencentcloud/services/tke/resource_tc_kubernetes_node_pool_test.go @@ -64,6 +64,23 @@ func testNodePoolSweep(region string) error { if len(nodePools) == 0 { return nil } + + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range nodePools { + if !tccommon.CheckResourcePersist(*v.Name, "") { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.NodePoolId, + Name: *v.Name, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.NodePoolId, + Name: *v.Name, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "tke", "node_pool") + for i := range nodePools { poolId := *nodePools[i].NodePoolId poolName := nodePools[i].Name diff --git a/tencentcloud/services/tke/resource_tc_kubernetes_scale_worker_test.go b/tencentcloud/services/tke/resource_tc_kubernetes_scale_worker_test.go index da0a8a18db..91c413ee78 100644 --- a/tencentcloud/services/tke/resource_tc_kubernetes_scale_worker_test.go +++ b/tencentcloud/services/tke/resource_tc_kubernetes_scale_worker_test.go @@ -50,6 +50,9 @@ func init() { return err } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + cvmService := svccvm.NewCvmService(client) instanceIds := make([]string, 0) for i := range workers { @@ -62,6 +65,18 @@ func init() { continue } + if !tccommon.CheckResourcePersist(*instance.InstanceName, worker.CreatedTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: worker.InstanceId, + Name: *instance.InstanceName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: worker.InstanceId, + Name: *instance.InstanceName, + CreatTime: worker.CreatedTime, + }) + created, err := time.Parse(tccommon.TENCENTCLOUD_COMMON_TIME_LAYOUT, worker.CreatedTime) if err != nil { created = time.Time{} @@ -72,6 +87,8 @@ func init() { instanceIds = append(instanceIds, worker.InstanceId) } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "tke", "scale_worker") + err = service.DeleteClusterInstances(ctx, clusterId, instanceIds) if err != nil { return err diff --git a/tencentcloud/services/vpc/resource_tc_eni_test.go b/tencentcloud/services/vpc/resource_tc_eni_test.go index 32a7ab24ef..ee0b4a9b54 100644 --- a/tencentcloud/services/vpc/resource_tc_eni_test.go +++ b/tencentcloud/services/vpc/resource_tc_eni_test.go @@ -41,6 +41,23 @@ func testSweepEniInstance(region string) error { return fmt.Errorf("get instance list error: %s", err.Error()) } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range instances { + if !tccommon.CheckResourcePersist(*v.NetworkInterfaceName, *v.CreatedTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.NetworkInterfaceId, + Name: *v.NetworkInterfaceName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.NetworkInterfaceId, + Name: *v.NetworkInterfaceName, + CreatTime: *v.CreatedTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "vpc", "eni") + for _, v := range instances { instanceId := *v.NetworkInterfaceId instanceName := v.NetworkInterfaceName diff --git a/tencentcloud/services/vpc/resource_tc_ha_vip_test.go b/tencentcloud/services/vpc/resource_tc_ha_vip_test.go index 09b37f1cf9..88b6c9ad8d 100644 --- a/tencentcloud/services/vpc/resource_tc_ha_vip_test.go +++ b/tencentcloud/services/vpc/resource_tc_ha_vip_test.go @@ -43,6 +43,23 @@ func testSweepHaVipInstance(region string) error { return fmt.Errorf("get instance list error: %s", err.Error()) } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range instances { + if !tccommon.CheckResourcePersist(*v.HaVipName, *v.CreatedTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.HaVipId, + Name: *v.HaVipName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.HaVipId, + Name: *v.HaVipName, + CreatTime: *v.CreatedTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "vpc", "ha_vip") + for _, v := range instances { instanceId := *v.HaVipId instanceName := *v.HaVipName diff --git a/tencentcloud/services/vpc/resource_tc_nat_gateway_test.go b/tencentcloud/services/vpc/resource_tc_nat_gateway_test.go index 748086f7c0..74d513b204 100644 --- a/tencentcloud/services/vpc/resource_tc_nat_gateway_test.go +++ b/tencentcloud/services/vpc/resource_tc_nat_gateway_test.go @@ -41,6 +41,23 @@ func testSweepNatInstance(region string) error { return fmt.Errorf("get instance list error: %s", err.Error()) } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range instances { + if !tccommon.CheckResourcePersist(*v.NatGatewayName, *v.CreatedTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.NatGatewayId, + Name: *v.NatGatewayName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.NatGatewayId, + Name: *v.NatGatewayName, + CreatTime: *v.CreatedTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "vpc", "nat_gateway") + for _, v := range instances { instanceId := *v.NatGatewayId instanceName := v.NatGatewayName diff --git a/tencentcloud/services/vpc/resource_tc_security_group_test.go b/tencentcloud/services/vpc/resource_tc_security_group_test.go index f3a2810339..9f252b1c02 100644 --- a/tencentcloud/services/vpc/resource_tc_security_group_test.go +++ b/tencentcloud/services/vpc/resource_tc_security_group_test.go @@ -35,11 +35,27 @@ func testSweepSecurityGroups(region string) error { service := svcvpc.NewVpcService(client.GetAPIV3Conn()) sgs, err := service.DescribeSecurityGroups(ctx, nil, nil, nil, nil) - if err != nil { return fmt.Errorf("DescribeSecurityGroups error: %s", err.Error()) } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range sgs { + if !tccommon.CheckResourcePersist(*v.SecurityGroupName, *v.CreatedTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.SecurityGroupId, + Name: *v.SecurityGroupName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.SecurityGroupId, + Name: *v.SecurityGroupName, + CreatTime: *v.CreatedTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "vpc", "security_group") + for _, v := range sgs { name := *v.SecurityGroupName id := *v.SecurityGroupId diff --git a/tencentcloud/services/vpc/resource_tc_subnet_test.go b/tencentcloud/services/vpc/resource_tc_subnet_test.go index 80c19ea27e..39ccd638b0 100644 --- a/tencentcloud/services/vpc/resource_tc_subnet_test.go +++ b/tencentcloud/services/vpc/resource_tc_subnet_test.go @@ -41,6 +41,23 @@ func testSweepSubnet(region string) error { return fmt.Errorf("get instance list error: %s", err.Error()) } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range instances { + if !tccommon.CheckResourcePersist(v.Name(), v.CreateTime()) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: v.SubnetId(), + Name: v.Name(), + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: v.SubnetId(), + Name: v.Name(), + CreatTime: v.CreateTime(), + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "vpc", "subnet") + for _, v := range instances { instanceId := v.SubnetId() diff --git a/tencentcloud/services/vpc/resource_tc_vpc_test.go b/tencentcloud/services/vpc/resource_tc_vpc_test.go index 44289321c9..768333a6eb 100644 --- a/tencentcloud/services/vpc/resource_tc_vpc_test.go +++ b/tencentcloud/services/vpc/resource_tc_vpc_test.go @@ -40,6 +40,23 @@ func testSweepVpcInstance(region string) error { return fmt.Errorf("get instance list error: %s", err.Error()) } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range instances { + if !tccommon.CheckResourcePersist(v.Name(), v.CreateTime()) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: v.VpcId(), + Name: v.Name(), + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: v.VpcId(), + Name: v.Name(), + CreatTime: v.CreateTime(), + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "vpc", "") + for _, v := range instances { instanceId := v.VpcId() instanceName := v.Name() diff --git a/tencentcloud/services/vpn/resource_tc_vpn_gateway_test.go b/tencentcloud/services/vpn/resource_tc_vpn_gateway_test.go index d8482f485b..8156208bf4 100644 --- a/tencentcloud/services/vpn/resource_tc_vpn_gateway_test.go +++ b/tencentcloud/services/vpn/resource_tc_vpn_gateway_test.go @@ -42,6 +42,23 @@ func testSweepVpnGateway(region string) error { return fmt.Errorf("get instance list error: %s", err.Error()) } + // add scanning resources + var resources, nonKeepResources []*tccommon.ResourceInstance + for _, v := range instances { + if !tccommon.CheckResourcePersist(*v.VpnGatewayName, *v.CreatedTime) { + nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{ + Id: *v.VpnGatewayId, + Name: *v.VpnGatewayName, + }) + } + resources = append(resources, &tccommon.ResourceInstance{ + Id: *v.VpnGatewayId, + Name: *v.VpnGatewayName, + CreatTime: *v.CreatedTime, + }) + } + tccommon.ProcessScanCloudResources(resources, nonKeepResources, "vpn", "gateway") + for _, v := range instances { vpnGwId := *v.VpnGatewayId