8000 bugfix by jietian-sts · Pull Request #30 · antgroup/CloudRec · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

bugfix #30

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

Closed
wants to merge 8 commits into from
Closed
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ build/
.vscode/
.cloudide
.git/**
/app/bootstrap/src/main/resources/config/application-test.properties

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.alipay.application.service.collector.domain.repo.CollectorTaskRepository;
import com.alipay.application.service.collector.enums.TaskStatus;
import com.alipay.application.service.common.Platform;
import com.alipay.application.service.resource.DelResourceService;
import com.alipay.application.service.resource.job.ClearJob;
import com.alipay.application.service.rule.job.AccountScanJob;
import com.alipay.application.service.system.utils.TokenUtil;
Expand Down Expand Up @@ -55,10 +56,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
Expand Down Expand Up @@ -102,6 +100,8 @@ public class AgentServiceImpl implements AgentService {
@Resource
private CloudResourceInstanceMapper cloudResourceInstanceMapper;
@Resource
private DelResourceService delResourceService;
@Resource
private CollectorRecordMapper collectorRecordMapper;
@Resource
private CollectorTaskRepository collectorTaskRepository;
Expand Down Expand Up @@ -388,9 +388,10 @@ public ApiResponse<List<AgentCloudAccountVO>> queryCloudAccountList(String persi
try {
return AgentCloudAccountVO.build(po, agentRegistryPO);
} catch (Exception e) {
throw new RuntimeException(e);
log.error("build AgentCloudAccountVO error,cloudAccountId:{}", po.getCloudAccountId(), e);
return null;
}
}).toList();
}).filter(Objects::nonNull).toList();

// 5. pre handler
accountStartCollectPreHandler(list, agentRegistryPO);
Expand All @@ -402,28 +403,32 @@ public ApiResponse<List<AgentCloudAccountVO>> queryCloudAccountList(String persi
void accountStartCollectPreHandler(List<CloudAccountPO> list, AgentRegistryPO agentRegistryPO) {
// Change the status of this batch of account accounts to running
list.forEach(cloudAccountPO -> {
cloudAccountPO.setCollectorStatus(Status.running.name());
cloudAccountPO.setLastScanTime(new Date());
cloudAccountMapper.updateByPrimaryKeySelective(cloudAccountPO);

// Bind the corresponding relationship between account and collector
AgentRegistryCloudAccountPO agentRegistryCloudAccountPO = agentRegistryCloudAccountMapper
.findOne(agentRegistryPO.getId(), cloudAccountPO.getCloudAccountId());
if (agentRegistryCloudAccountPO == null) {
agentRegistryCloudAccountPO = new AgentRegistryCloudAccountPO();
agentRegistryCloudAccountPO.setAgentRegistryId(agentRegistryPO.getId());
agentRegistryCloudAccountPO.setCloudAccountId(cloudAccountPO.getCloudAccountId());
agentRegistryCloudAccountPO.setRegistryValue(agentRegistryPO.getRegistryValue());
agentRegistryCloudAccountPO.setPlatform(agentRegistryPO.getPlatform());
try {
agentRegistryCloudAccountMapper.insertSelective(agentRegistryCloudAccountPO);
} catch (Exception e) {
log.error("Exceptions due to concurrent registrations");
try {
cloudAccountPO.setCollectorStatus(Status.running.name());
cloudAccountPO.setLastScanTime(new Date());
cloudAccountMapper.updateByPrimaryKeySelective(cloudAccountPO);

// Bind the corresponding relationship between account and collector
AgentRegistryCloudAccountPO agentRegistryCloudAccountPO = agentRegistryCloudAccountMapper
.findOne(agentRegistryPO.getId(), cloudAccountPO.getCloudAccountId());
if (agentRegistryCloudAccountPO == null) {
agentRegistryCloudAccountPO = new AgentRegistryCloudAccountPO();
agentRegistryCloudAccountPO.setAgentRegistryId(agentRegistryPO.getId());
agentRegistryCloudAccountPO.setCloudAccountId(cloudAccountPO.getCloudAccountId());
agentRegistryCloudAccountPO.setRegistryValue(agentRegistryPO.getRegistryValue());
agentRegistryCloudAccountPO.setPlatform(agentRegistryPO.getPlatform());
try {
agentRegistryCloudAccountMapper.insertSelective(agentRegistryCloudAccountPO);
} catch (Exception e) {
log.error("Exceptions due to concurrent registrations");
}
}
}

// Pre-delete asset data
cloudResourceInstanceMapper.preDeleteByCloudAccountId(cloudAccountPO.getCloudAccountId());
// Pre-delete asset data
delResourceService.preDeleteByCloudAccountId(cloudAccountPO.getCloudAccountId());
} catch (Exception e) {
log.error("accountStartCollectPreHandler error,cloudAccountId:{}", cloudAccountPO.getCloudAccountId(), e);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.alipay.application.service.resource;


/*
*@title DelResourceService
*@description
*@author suitianshuang
*@version 1.0
*@create 2025/6/26 22:17
*/
public interface DelResourceService {

/**
* 预删除资源,将资源的逻辑删除次数 + 1
*
* @param cloudAccountId
* @return
*/
int preDeleteByCloudAccountId(String cloudAccountId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.alipay.application.service.resource;


import com.alipay.dao.dto.IQueryResourceDTO;
import com.alipay.dao.mapper.CloudResourceInstanceMapper;
import com.alipay.dao.po.CloudResourceInstancePO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/*
*@title DelResourceServiceImpl
*@description
*@author suitianshuang
*@version 1.0
*@create 2025/6/26 22:19
*/
@Service
public class DelResourceServiceImpl implements DelResourceService {

@Resource
private CloudResourceInstanceMapper cloudResourceInstanceMapper;

/**
* 分批次预删除资源,将资源的逻辑删除次数 + 1,并记录删除时间
*
* @param cloudAccountId 云账号id
* @return 删除的资源数量
*/
@Override
public int preDeleteByCloudAccountId(String cloudAccountId) {
int totalUpdated = 0;
final int size = 1000;
Long scrollId = 0L;
while (true) {
IQueryResourceDTO request = IQueryResourceDTO.builder()
.cloudAccountId(cloudAccountId)
.scrollId(scrollId)
.size(size)
.build();
List<CloudResourceInstancePO> cloudResourceInstancePOS = cloudResourceInstanceMapper.findByCondWithScrollId(request);
if (CollectionUtils.isEmpty(cloudResourceInstancePOS)) {
break;
} else {
List<Long> idList = cloudResourceInstancePOS.stream().map(CloudResourceInstancePO::getId).toList();
int effectCount = cloudResourceInstanceMapper.preDeleteByIdList(idList, new Date());
totalUpdated += effectCount;
if (cloudResourceInstancePOS.size() < size) {
break;
}
scrollId = cloudResourceInstancePOS.get(cloudResourceInstancePOS.size() - 1).getId();
}
}
return totalUpdated;
}
}
3 changes: 3 additions & 0 deletions app/bootstrap/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ target/
*.iml
*.ipr

### Application Properties ###
src/main/resources/config/application-test.properties

### NetBeans ###
/nbproject/private/
/nbbuild/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.alipay.dao.po.CloudResourceInstancePO;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;

public interface CloudResourceInstanceMapper {
Expand Down Expand Up @@ -50,7 +51,7 @@ CloudResourceInstancePO findExampleLimit1(@Param("platform") String platform,
void deleteByCloudAccountId(String cloudAccountId);

// 预删除
int preDeleteByCloudAccountId(String cloudAccountId);
int preDeleteByIdList(@Param("idList") List<Long> idList, @Param("deleteAt") Date deleteAt);

// 正式删除
int commitDeleteByCloudAccountId(@Param("cloudAccountId") String cloudAccountId, @Param("delNum") int delNum);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,11 +437,14 @@
#{id}
</foreach>
</update>
<update id="preDeleteByCloudAccountId">
<update id="preDeleteByIdList">
UPDATE cloud_resource_instance_v1
SET deleted_at = NOW(),
del_num = del_num + 1
WHERE cloud_account_id = #{cloudAccountId}
SET deleted_at =#{deleteAt},
del_num = del_num + 1
WHERE id IN
<foreach collection="idList" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</update>
<delete id="commitDeleteByCloudAccountId">
DELETE
Expand Down
1 change: 1 addition & 0 deletions collector/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ cloudrec_collector_baidu
cloudrec_collector_gcp
cloudrec_collector_hws
cloudrec_collector_tencent
cloudrec_collector_ksyun

*.misc.xml
deploy/*.tar.gz
Expand Down
2 changes: 1 addition & 1 deletion collector/alicloud-private/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ go 1.23
replace github.com/core-sdk => ../core-sdk

require (
github.com/core-sdk v0.0.0-00010101000000-000000000000
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1140
github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible
github.com/core-sdk v0.0.0-00010101000000-000000000000
go.uber.org/zap v1.27.0
)

Expand Down
1 change: 1 addition & 0 deletions collector/alicloud/collector/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@ const (
DdosCoo = "DdosCoo"
Yundun = "Yundun"
APIG = "APIG"
ResourceCenter = "ResourceCenter"
)
44 changes: 44 additions & 0 deletions collector/alicloud/collector/resourcecenter/resouce_center_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package resourcecenter

import (
"github.com/cloudrec/alicloud/collector"
"github.com/core-sdk/constant"
"github.com/core-sdk/log"
"github.com/core-sdk/schema"
"os"
"testing"
)

var GetTestAccount = func() (res []schema.CloudAccount) {
testAccount := schema.CloudAccount{
CloudAccountId: "test-account",
CommonCloudAccountAuthParam: schema.CommonCloudAccountAuthParam{
AK: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
SK: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
},
}

res = append(res, testAccount)

return res
}

func TestCloudCenterResource(t *testing.T) {
p := schema.GetInstance(schema.PlatformConfig{
Name: string(constant.AlibabaCloud),
Resources: []schema.Resource{
GeCloudCenterResource(),
},

Service: &collector.Services{},
DefaultRegions: []string{
"cn-shanghai",
"ap-southeast-1",
},
DefaultCloudAccounts: GetTestAccount(),
})

if err := schema.RunExecutor(p); err != nil {
log.GetWLogger().Error(err.Error())
}
}
89 changes: 89 additions & 0 deletions collector/alicloud/collector/resourcecenter/resource_center.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package resourcecenter

import (
"context"
resourcecenter20221201 "github.com/alibabacloud-go/resourcecenter-20221201/client"
"github.com/alibabacloud-go/tea/tea"
"github.com/cloudrec/alicloud/collector"
"github.com/core-sdk/constant"
"github.com/core-sdk/log"
"github.com/core-sdk/schema"
"go.uber.org/zap"
)

func GeCloudCenterResource() schema.Resource {
return schema.Resource{
ResourceType: collector.ResourceCenter,
ResourceTypeName: collector.ResourceCenter,
ResourceGroupType: constant.GOVERNANCE,
Desc: `https://api.aliyun.com/product/ResourceCenter`,
ResourceDetailFunc: GetDetail,
RowField: schema.RowField{
ResourceId: "$.ResourceId",
ResourceName: "$.ResourceId",
},
Regions: []string{
"cn-shanghai",
"ap-southeast-1",
},
Dimension: schema.Regional,
}
}

func GetDetail(ctx context.Context, service schema.ServiceInterface, res chan<- any) error {
cli := service.(*collector.Services).ResourceCenter

var sql = "SELECT resource_type,COUNT(*) AS cnt FROM resources GROUP BY resource_type ORDER BY cnt DESC;"
request := &resourcecenter20221201.ExecuteSQLQueryRequest{
Expression: tea.String(sql),
MaxResults: tea.Int32(100),
}

var columns []*resourcecenter20221201.ExecuteSQLQueryResponseBodyColumns
var rows []interface{}
for {
resp, err := cli.ExecuteSQLQuery(request)
if err != nil {
log.CtxLogger(ctx).Warn("ExecuteSQLQuery error", zap.Error(err))
return err
}

rows = append(rows, resp.Body.Rows...)

if tea.StringValue(resp.Body.NextToken) == "" {
columns = resp.Body.Columns
break
}

request.NextToken = resp.Body.NextToken
}

res <- &Detail{
Columns: columns,
Rows: rows,
ResourceId: log.GetCloudAccountId(ctx) + "-" + tea.StringValue(cli.RegionId),
}

return nil
}

type Detail struct {
Columns []*resourcecenter20221201.ExecuteSQLQueryResponseBodyColumns
Rows []interface{}
ResourceId string
}
Loading
0