Skip to content

feat(es): [115889723]Fix resource scanning issue #2602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tencentcloud/common/cloud_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func GetResourceCreatorAccountInfo(client *connectivity.TencentCloudClient, reso
response, err := client.UseClsClient().SearchLog(request)
if err != nil {
log.Printf("[CRITAL] search resource[%v] log data error: %v", r.Id, err.Error())
return resourceIdToSubAccountInfoMap
continue
}
if response == nil || response.Response == nil {
log.Printf("[CRITAL] search resource[%v] log data response is nil", r.Id)
return resourceIdToSubAccountInfoMap
continue
}
if len(response.Response.Results) == 0 {
log.Printf("[CRITAL] search resource[%v] log data response results is empty", r.Id)
return resourceIdToSubAccountInfoMap
continue
}

result := response.Response.Results[0]
Expand Down
23 changes: 21 additions & 2 deletions tencentcloud/common/resource_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
const (
KeepResource = "keep"
NonKeepResource = "non-keep"

SystemUserName = "system"
)

// TimeFormats add all possible time formats
Expand All @@ -25,7 +27,7 @@ var TimeFormats = []string{
type ResourceInstance struct {
Id string
Name string
CreatTime string
CreateTime string
DefaultKeep bool
}

Expand All @@ -47,7 +49,7 @@ func ProcessResources(client *connectivity.TencentCloudClient, resources []*Reso
isResourceKeep = KeepResource
}

creationDuration, err := DaysSinceCreation(r.CreatTime)
creationDuration, err := DaysSinceCreation(r.CreateTime)
if err != nil {
log.Printf("[CRITAL] compute resource creation duration error: %v", err.Error())
}
Expand All @@ -59,6 +61,11 @@ func ProcessResources(client *connectivity.TencentCloudClient, resources []*Reso
resourceName = creatorAccountInfo.ResourceName
principalId = creatorAccountInfo.PrincipalId
userName = creatorAccountInfo.UserName
} else {
parsedTime, _ := ParsedTime(r.CreateTime)
if IsDefaultSearchLogStartTimestampAfter(*parsedTime) {
userName = SystemUserName
}
}

data[i] = []string{
Expand Down Expand Up @@ -139,6 +146,9 @@ func DaysSinceCreation(createTime string) (string, error) {
if err != nil {
return "", err
}
if parsedTime == nil {
return "", nil
}

duration := time.Since(*parsedTime)
days := duration.Hours() / 24
Expand Down Expand Up @@ -174,3 +184,12 @@ func ParsedTime(createTime string) (*time.Time, error) {
}
return &parsedTime, nil
}

// IsDefaultSearchLogStartTimestampAfter check whether the resource creation time is after the default search log start time
func IsDefaultSearchLogStartTimestampAfter(parsedTime time.Time) bool {
// 将 DefaultSearchLogStartTimestamp 转换为 time.Time 类型
startTime := time.Unix(DefaultSearchLogStartTimestamp/1000, 0)

// 判断 parsedTime 是否在 startTime 之后
return parsedTime.After(startTime)
}
6 changes: 3 additions & 3 deletions tencentcloud/services/as/resource_tc_as_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func testSweepAsAttachment(r string) error {
})
}
resources = append(resources, &tccommon.ResourceInstance{
Id: *v.AutoScalingGroupId,
Name: *v.AutoScalingGroupName,
CreatTime: *v.CreatedTime,
Id: *v.AutoScalingGroupId,
Name: *v.AutoScalingGroupName,
CreateTime: *v.CreatedTime,
})
}
tccommon.ProcessScanCloudResources(client, resources, nonKeepResources, "CreateAutoScalingGroup")
Expand Down
22 changes: 20 additions & 2 deletions tencentcloud/services/as/resource_tc_as_scaling_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,30 @@ func init() {
if err != nil {
return fmt.Errorf("getting tencentcloud client error: %s", err.Error())
}
client := sharedClient.(tccommon.ProviderMeta)
asService := svcas.NewAsService(client.GetAPIV3Conn())
client := sharedClient.(tccommon.ProviderMeta).GetAPIV3Conn()
asService := svcas.NewAsService(client)
configs, err := asService.DescribeLaunchConfigurationByFilter(ctx, "", "")
if err != nil {
return err
}

// add scanning resources
var resources, nonKeepResources []*tccommon.ResourceInstance
for _, v := range configs {
if !tccommon.CheckResourcePersist(*v.LaunchConfigurationName, *v.CreatedTime) {
nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{
Id: *v.LaunchConfigurationId,
Name: *v.LaunchConfigurationName,
})
}
resources = append(resources, &tccommon.ResourceInstance{
Id: *v.LaunchConfigurationId,
Name: *v.LaunchConfigurationName,
CreateTime: *v.CreatedTime,
})
}
tccommon.ProcessScanCloudResources(client, resources, nonKeepResources, "CreateLaunchConfiguration")

for _, config := range configs {
instanceName := *config.LaunchConfigurationName
now := time.Now()
Expand Down
21 changes: 19 additions & 2 deletions tencentcloud/services/as/resource_tc_as_scaling_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,31 @@ func testSweepAsScalingGroups(region string) error {
if err != nil {
return fmt.Errorf("getting tencentcloud client error: %s", err.Error())
}
client := sharedClient.(tccommon.ProviderMeta)
client := sharedClient.(tccommon.ProviderMeta).GetAPIV3Conn()

asService := svcas.NewAsService(client.GetAPIV3Conn())
asService := svcas.NewAsService(client)
scalingGroups, err := asService.DescribeAutoScalingGroupByFilter(ctx, "", "", "", nil)
if err != nil {
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,
CreateTime: *v.CreatedTime,
})
}
tccommon.ProcessScanCloudResources(client, resources, nonKeepResources, "CreateAutoScalingGroup")

for _, v := range scalingGroups {
scalingGroupId := *v.AutoScalingGroupId
scalingGroupName := *v.AutoScalingGroupName
Expand Down
6 changes: 3 additions & 3 deletions tencentcloud/services/cam/resource_tc_cam_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func init() {
})
}
resources = append(resources, &tccommon.ResourceInstance{
Id: strconv.FormatUint(*v.GroupId, 10),
Name: *v.GroupName,
CreatTime: *v.CreateTime,
Id: strconv.FormatUint(*v.GroupId, 10),
Name: *v.GroupName,
CreateTime: *v.CreateTime,
})
}
tccommon.ProcessScanCloudResources(client, resources, nonKeepResources, "CreateGroup")
Expand Down
18 changes: 18 additions & 0 deletions tencentcloud/services/cam/resource_tc_cam_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ func init() {
if err != nil {
return err
}

// add scanning resources
var resources, nonKeepResources []*tccommon.ResourceInstance
for _, v := range groups {
if !tccommon.CheckResourcePersist(*v.RoleName, *v.AddTime) {
nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{
Id: *v.RoleId,
Name: *v.RoleName,
})
}
resources = append(resources, &tccommon.ResourceInstance{
Id: *v.RoleId,
Name: *v.RoleName,
CreateTime: *v.AddTime,
})
}
tccommon.ProcessScanCloudResources(client, resources, nonKeepResources, "CreateRole")

for _, v := range groups {
name := *v.RoleName

Expand Down
18 changes: 18 additions & 0 deletions tencentcloud/services/cam/resource_tc_cam_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -36,6 +37,23 @@ func init() {
return err
}

// add scanning resources
var resources, nonKeepResources []*tccommon.ResourceInstance
for _, v := range users {
if !tccommon.CheckResourcePersist(*v.Name, *v.CreateTime) {
nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{
Id: strconv.FormatUint(*v.Uin, 10),
Name: *v.Name,
})
}
resources = append(resources, &tccommon.ResourceInstance{
Id: strconv.FormatUint(*v.Uin, 10),
Name: *v.Name,
CreateTime: *v.CreateTime,
})
}
tccommon.ProcessScanCloudResources(client, resources, nonKeepResources, "AddUser")

for _, v := range users {
if tcacctest.PersistResource.MatchString(*v.Name) {
continue
Expand Down
6 changes: 3 additions & 3 deletions tencentcloud/services/cbs/resource_tc_cbs_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func init() {
})
}
resources = append(resources, &tccommon.ResourceInstance{
Id: *v.DiskId,
Name: *v.DiskName,
CreatTime: *v.CreateTime,
Id: *v.DiskId,
Name: *v.DiskName,
CreateTime: *v.CreateTime,
})
}
tccommon.ProcessScanCloudResources(client, resources, nonKeepResources, "CreateDisks")
Expand Down
6 changes: 3 additions & 3 deletions tencentcloud/services/ccn/resource_tc_ccn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func testSweepCcnInstance(region string) error {
})
}
resources = append(resources, &tccommon.ResourceInstance{
Id: v.CcnId(),
Name: v.Name(),
CreatTime: v.CreateTime(),
Id: v.CcnId(),
Name: v.Name(),
CreateTime: v.CreateTime(),
})
}
tccommon.ProcessScanCloudResources(client, resources, nonKeepResources, "CreateCcn")
Expand Down
6 changes: 3 additions & 3 deletions tencentcloud/services/cdb/resource_tc_mysql_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func testSweepMySQLInstance(region string) error {
})
}
resources = append(resources, &tccommon.ResourceInstance{
Id: *v.InstanceId,
Name: *v.InstanceName,
CreatTime: *v.CreateTime,
Id: *v.InstanceId,
Name: *v.InstanceName,
CreateTime: *v.CreateTime,
})
}
tccommon.ProcessScanCloudResources(client, resources, nonKeepResources, "CreateDBInstance")
Expand Down
17 changes: 17 additions & 0 deletions tencentcloud/services/cfs/resource_tc_cfs_access_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ func init() {
return err
}

// add scanning resources
var resources, nonKeepResources []*tccommon.ResourceInstance
for _, v := range groups {
if !tccommon.CheckResourcePersist(*v.Name, *v.CDate) {
nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{
Id: *v.PGroupId,
Name: *v.Name,
})
}
resources = append(resources, &tccommon.ResourceInstance{
Id: *v.PGroupId,
Name: *v.Name,
CreateTime: *v.CDate,
})
}
tccommon.ProcessScanCloudResources(client, resources, nonKeepResources, "CreateCfsPGroup")

for i := range groups {
id := *groups[i].PGroupId
name := *groups[i].Name
Expand Down
18 changes: 18 additions & 0 deletions tencentcloud/services/cfs/resource_tc_cfs_file_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ func init() {
if err != nil {
return err
}

// add scanning resources
var resources, nonKeepResources []*tccommon.ResourceInstance
for _, v := range fsList {
if !tccommon.CheckResourcePersist(*v.FileSystemId, *v.CreationTime) {
nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{
Id: *v.FileSystemId,
Name: *v.FsName,
})
}
resources = append(resources, &tccommon.ResourceInstance{
Id: *v.FileSystemId,
Name: *v.FsName,
CreateTime: *v.CreationTime,
})
}
tccommon.ProcessScanCloudResources(client, resources, nonKeepResources, "CreateCfsFileSystem")

for i := range fsList {
item := fsList[i]
id := *item.FileSystemId
Expand Down
6 changes: 3 additions & 3 deletions tencentcloud/services/clb/resource_tc_clb_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func testSweepClbInstance(region string) error {
})
}
resources = append(resources, &tccommon.ResourceInstance{
Id: *v.LoadBalancerId,
Name: *v.LoadBalancerName,
CreatTime: *v.CreateTime,
Id: *v.LoadBalancerId,
Name: *v.LoadBalancerName,
CreateTime: *v.CreateTime,
})
}
tccommon.ProcessScanCloudResources(client, resources, nonKeepResources, "CreateLoadBalancer")
Expand Down
17 changes: 17 additions & 0 deletions tencentcloud/services/clb/resource_tc_clb_target_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ func init() {
return err
}

// add scanning resources
var resources, nonKeepResources []*tccommon.ResourceInstance
for _, v := range tgs {
if !tccommon.CheckResourcePersist(*v.TargetGroupName, *v.CreatedTime) {
nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{
Id: *v.TargetGroupId,
Name: *v.TargetGroupName,
})
}
resources = append(resources, &tccommon.ResourceInstance{
Id: *v.TargetGroupId,
Name: *v.TargetGroupName,
CreateTime: *v.CreatedTime,
})
}
tccommon.ProcessScanCloudResources(client, resources, nonKeepResources, "CreateTargetGroup")

for i := range tgs {
tg := tgs[i]
created := tccommon.ParseTimeFromCommonLayout(tg.CreatedTime)
Expand Down
21 changes: 19 additions & 2 deletions tencentcloud/services/cls/resource_tc_cls_logset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,32 @@ func testSweepClsLogset(region string) error {
if err != nil {
return fmt.Errorf("getting tencentcloud client error: %s", err.Error())
}
client := sharedClient.(tccommon.ProviderMeta)
client := sharedClient.(tccommon.ProviderMeta).GetAPIV3Conn()

clsService := localcls.NewClsService(client.GetAPIV3Conn())
clsService := localcls.NewClsService(client)

instances, err := clsService.DescribeClsLogsetByFilter(ctx, nil)
if err != nil {
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.LogsetName, *v.CreateTime) {
nonKeepResources = append(nonKeepResources, &tccommon.ResourceInstance{
Id: *v.LogsetId,
Name: *v.LogsetName,
})
}
resources = append(resources, &tccommon.ResourceInstance{
Id: *v.LogsetId,
Name: *v.LogsetName,
CreateTime: *v.CreateTime,
})
}
tccommon.ProcessScanCloudResources(client, resources, nonKeepResources, "CreateLogset")

for _, v := range instances {
instanceId := v.LogsetId
instanceName := v.LogsetName
Expand Down
Loading
Loading