Skip to content

Commit

Permalink
Merge pull request #1110 from Tencent/3.5.x
Browse files Browse the repository at this point in the history
Merge: 3.5.x->master
  • Loading branch information
jsonwan authored Jul 7, 2022
2 parents dda2793 + beab7bf commit b8c6bcf
Show file tree
Hide file tree
Showing 12 changed files with 303 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,40 @@
import org.apache.commons.lang3.StringUtils;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.lang.NonNull;

import java.util.Map;

@Slf4j
public abstract class BaseIamCallbackService {

public CallbackBaseResponseDTO getFailResp(Long code, String message) {
private CallbackBaseResponseDTO getFailResp(Long code, String message) {
CallbackBaseResponseDTO respDTO = new CallbackBaseResponseDTO();
respDTO.setCode(code);
respDTO.setMessage(message);
return respDTO;
}

public CallbackBaseResponseDTO getNotFoundResp(String message) {
private CallbackBaseResponseDTO getNotFoundResp(String message) {
return getFailResp(CommonResponseCode.NOT_FOUND, message);
}

private String genNotExistMessage(String id) {
return String.format("cannot find resource by id %s, may be deleted", id);
}

protected void logNotExistId(@NonNull Object id) {
log.warn(genNotExistMessage(id.toString()));
}

protected void logBuildInstanceFailure(Object rawInstance, Exception e) {
FormattingTuple msg = MessageFormatter.format("Fail to buildInstance for {}", rawInstance);
log.warn(msg.getMessage(), e);
}

public CallbackBaseResponseDTO getNotFoundRespById(String id) {
String msg = String.format("cannot find resource by id %s, may be deleted", id);
log.warn(msg);
return getNotFoundResp(msg);
logNotExistId(id);
return getNotFoundResp(genNotExistMessage(id));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,37 @@ protected SearchInstanceResponseDTO searchInstanceResp(CallbackRequestDTO callba
return instanceResponse;
}

private InstanceInfoDTO buildInstance(String id) {
// 拓扑路径构建
List<PathInfoDTO> path = new ArrayList<>();
PathInfoDTO rootNode = new PathInfoDTO();
rootNode.setType(ResourceTypeId.DASHBOARD_VIEW);
rootNode.setId(AnalysisConsts.GLOBAL_DASHBOARD_VIEW_ID);
path.add(rootNode);
// 实例组装
InstanceInfoDTO instanceInfo = new InstanceInfoDTO();
instanceInfo.setId(id);
instanceInfo.setDisplayName(AnalysisConsts.GLOBAL_DASHBOARD_VIEW_NAME);
instanceInfo.setPath(path);
return instanceInfo;
}

@Override
protected CallbackBaseResponseDTO fetchInstanceResp(
CallbackRequestDTO callbackRequest
) {
IamSearchCondition searchCondition = IamSearchCondition.fromReq(callbackRequest);
List<Object> instanceAttributeInfoList = new ArrayList<>();
for (String instanceId : searchCondition.getIdList()) {
for (String id : searchCondition.getIdList()) {
if (!AnalysisConsts.GLOBAL_DASHBOARD_VIEW_ID.equals(id)) {
logNotExistId(id);
continue;
}
try {
if (!AnalysisConsts.GLOBAL_DASHBOARD_VIEW_ID.equals(instanceId)) {
return getNotFoundRespById(instanceId);
}
// 拓扑路径构建
List<PathInfoDTO> path = new ArrayList<>();
PathInfoDTO rootNode = new PathInfoDTO();
rootNode.setType(ResourceTypeId.DASHBOARD_VIEW);
rootNode.setId(AnalysisConsts.GLOBAL_DASHBOARD_VIEW_ID);
path.add(rootNode);
// 实例组装
InstanceInfoDTO instanceInfo = new InstanceInfoDTO();
instanceInfo.setId(instanceId);
instanceInfo.setDisplayName(AnalysisConsts.GLOBAL_DASHBOARD_VIEW_NAME);
instanceInfo.setPath(path);
InstanceInfoDTO instanceInfo = buildInstance(id);
instanceAttributeInfoList.add(instanceInfo);
} catch (NumberFormatException e) {
log.error("Parse object id failed!|{}", instanceId, e);
} catch (Exception e) {
logBuildInstanceFailure(id, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*/
@RequestMapping("/iam/api/v1/resources/cron")
@IamCallbackAPI
public interface IamCallbackController {
public interface IamCronCallbackResource {

/**
* 权限中心回调
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.tencent.bk.job.common.model.PageData;
import com.tencent.bk.job.common.model.dto.ResourceScope;
import com.tencent.bk.job.common.service.AppScopeMappingService;
import com.tencent.bk.job.crontab.api.iam.IamCallbackController;
import com.tencent.bk.job.crontab.api.iam.IamCronCallbackResource;
import com.tencent.bk.job.crontab.model.dto.CronJobInfoDTO;
import com.tencent.bk.job.crontab.service.CronJobService;
import com.tencent.bk.sdk.iam.dto.PathInfoDTO;
Expand All @@ -55,14 +55,14 @@

@Slf4j
@RestController
public class IamCallbackControllerImpl extends BaseIamCallbackService implements IamCallbackController {
public class IamCronCallbackResourceImpl extends BaseIamCallbackService implements IamCronCallbackResource {

private final CronJobService cronJobService;
private final AppScopeMappingService appScopeMappingService;

@Autowired
public IamCallbackControllerImpl(CronJobService cronJobService,
AppScopeMappingService appScopeMappingService) {
public IamCronCallbackResourceImpl(CronJobService cronJobService,
AppScopeMappingService appScopeMappingService) {
this.cronJobService = cronJobService;
this.appScopeMappingService = appScopeMappingService;
}
Expand Down Expand Up @@ -115,6 +115,24 @@ protected ListInstanceResponseDTO listInstanceResp(
return IamRespUtil.getListInstanceRespFromPageData(cronJobInfoPageData, this::convert);
}

private InstanceInfoDTO buildInstance(CronJobInfoDTO cronJobInfoDTO, Map<Long, ResourceScope> appIdScopeMap) {
Long appId = cronJobInfoDTO.getAppId();
// 拓扑路径构建
List<PathInfoDTO> path = new ArrayList<>();
PathInfoDTO rootNode = getPathNodeByAppId(appId, appIdScopeMap);
PathInfoDTO cronJobNode = new PathInfoDTO();
cronJobNode.setType(ResourceTypeId.CRON);
cronJobNode.setId(cronJobInfoDTO.getId().toString());
rootNode.setChild(cronJobNode);
path.add(rootNode);
// 实例组装
InstanceInfoDTO instanceInfo = new InstanceInfoDTO();
instanceInfo.setId(cronJobInfoDTO.getId().toString());
instanceInfo.setDisplayName(cronJobInfoDTO.getName());
instanceInfo.setPath(path);
return instanceInfo;
}

@Override
protected CallbackBaseResponseDTO fetchInstanceResp(
CallbackRequestDTO callbackRequest
Expand All @@ -138,23 +156,15 @@ protected CallbackBaseResponseDTO fetchInstanceResp(
for (Long id : cronJobIdList) {
CronJobInfoDTO cronJobInfoDTO = cronJobInfoMap.get(id);
if (cronJobInfoDTO == null) {
return getNotFoundRespById(id.toString());
logNotExistId(id);
continue;
}
try {
InstanceInfoDTO instanceInfo = buildInstance(cronJobInfoDTO, appIdScopeMap);
instanceAttributeInfoList.add(instanceInfo);
} catch (Exception e) {
logBuildInstanceFailure(cronJobInfoDTO, e);
}
Long appId = cronJobInfoDTO.getAppId();
// 拓扑路径构建
List<PathInfoDTO> path = new ArrayList<>();
PathInfoDTO rootNode = getPathNodeByAppId(appId, appIdScopeMap);
PathInfoDTO cronJobNode = new PathInfoDTO();
cronJobNode.setType(ResourceTypeId.CRON);
cronJobNode.setId(cronJobInfoDTO.getId().toString());
rootNode.setChild(cronJobNode);
path.add(rootNode);
// 实例组装
InstanceInfoDTO instanceInfo = new InstanceInfoDTO();
instanceInfo.setId(id.toString());
instanceInfo.setDisplayName(cronJobInfoDTO.getName());
instanceInfo.setPath(path);
instanceAttributeInfoList.add(instanceInfo);
}

FetchInstanceInfoResponseDTO fetchInstanceInfoResponse = new FetchInstanceInfoResponseDTO();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,25 @@ protected SearchInstanceResponseDTO searchInstanceResp(CallbackRequestDTO callba
return IamRespUtil.getSearchInstanceRespFromPageData(fileSourceDTOPageData, this::convert);
}

private InstanceInfoDTO buildInstance(FileSourceBasicInfoDTO fileSourceBasicInfoDTO,
Map<Long, ResourceScope> appIdScopeMap) {
Long appId = fileSourceBasicInfoDTO.getAppId();
// 拓扑路径构建
List<PathInfoDTO> path = new ArrayList<>();
PathInfoDTO rootNode = getPathNodeByAppId(appId, appIdScopeMap);
PathInfoDTO fileSourceNode = new PathInfoDTO();
fileSourceNode.setType(ResourceTypeId.FILE_SOURCE);
fileSourceNode.setId(fileSourceBasicInfoDTO.getId().toString());
rootNode.setChild(fileSourceNode);
path.add(rootNode);
// 实例组装
InstanceInfoDTO instanceInfo = new InstanceInfoDTO();
instanceInfo.setId(fileSourceBasicInfoDTO.getId().toString());
instanceInfo.setDisplayName(fileSourceBasicInfoDTO.getAlias());
instanceInfo.setPath(path);
return instanceInfo;
}

@Override
protected CallbackBaseResponseDTO fetchInstanceResp(
CallbackRequestDTO callbackRequest
Expand Down Expand Up @@ -195,29 +214,17 @@ protected CallbackBaseResponseDTO fetchInstanceResp(
// Job app --> CMDB biz/businessSet转换
Map<Long, ResourceScope> appIdScopeMap = appScopeMappingService.getScopeByAppIds(appIdSet);
for (Integer id : fileSourceIdList) {
// 文件源详情查询实现
FileSourceBasicInfoDTO fileSourceBasicInfoDTO = fileSourceBasicInfoDTOMap.get(id);
if (fileSourceBasicInfoDTO == null) {
logNotExistId(id);
continue;
}
try {
// 文件源详情查询实现
FileSourceBasicInfoDTO fileSourceBasicInfoDTO = fileSourceBasicInfoDTOMap.get(id);
if (fileSourceBasicInfoDTO == null) {
return getNotFoundRespById(id.toString());
}
Long appId = fileSourceBasicInfoDTO.getAppId();
// 拓扑路径构建
List<PathInfoDTO> path = new ArrayList<>();
PathInfoDTO rootNode = getPathNodeByAppId(appId, appIdScopeMap);
PathInfoDTO fileSourceNode = new PathInfoDTO();
fileSourceNode.setType(ResourceTypeId.FILE_SOURCE);
fileSourceNode.setId(fileSourceBasicInfoDTO.getId().toString());
rootNode.setChild(fileSourceNode);
path.add(rootNode);
// 实例组装
InstanceInfoDTO instanceInfo = new InstanceInfoDTO();
instanceInfo.setId(id.toString());
instanceInfo.setDisplayName(fileSourceBasicInfoDTO.getAlias());
instanceInfo.setPath(path);
InstanceInfoDTO instanceInfo = buildInstance(fileSourceBasicInfoDTO, appIdScopeMap);
instanceAttributeInfoList.add(instanceInfo);
} catch (NumberFormatException e) {
log.error("Parse object id failed!|{}", id, e);
} catch (Exception e) {
logBuildInstanceFailure(fileSourceBasicInfoDTO, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,26 @@ protected SearchInstanceResponseDTO searchInstanceResp(CallbackRequestDTO callba
return IamRespUtil.getSearchInstanceRespFromPageData(accountDTOPageData, this::convert);
}

private InstanceInfoDTO buildInstance(String instanceId,
AccountDisplayDTO accountDTO,
Map<Long, ResourceScope> appIdScopeMap) {
Long appId = accountDTO.getAppId();
PathInfoDTO rootNode = getPathNodeByAppId(appId, appIdScopeMap);
// 拓扑路径构建
List<PathInfoDTO> path = new ArrayList<>();
PathInfoDTO accountNode = new PathInfoDTO();
accountNode.setType(ResourceTypeId.ACCOUNT);
accountNode.setId(accountDTO.getId().toString());
rootNode.setChild(accountNode);
path.add(rootNode);
// 实例组装
InstanceInfoDTO instanceInfo = new InstanceInfoDTO();
instanceInfo.setId(instanceId);
instanceInfo.setDisplayName(accountDTO.getAlias());
instanceInfo.setPath(path);
return instanceInfo;
}

@Override
protected CallbackBaseResponseDTO fetchInstanceResp(
CallbackRequestDTO callbackRequest
Expand All @@ -133,29 +153,17 @@ protected CallbackBaseResponseDTO fetchInstanceResp(
accountInfoMap.values().forEach(accountDisplayDTO -> appIdSet.add(accountDisplayDTO.getAppId()));
Map<Long, ResourceScope> appIdScopeMap = applicationService.getScopeByAppIds(appIdSet);
for (String instanceId : searchCondition.getIdList()) {
long id = Long.parseLong(instanceId);
AccountDisplayDTO accountDTO = accountInfoMap.get(id);
if (accountDTO == null) {
logNotExistId(id);
continue;
}
try {
long id = Long.parseLong(instanceId);
AccountDisplayDTO accountDTO = accountInfoMap.get(id);
if (accountDTO == null) {
return getNotFoundRespById(instanceId);
}
Long appId = accountDTO.getAppId();
PathInfoDTO rootNode = getPathNodeByAppId(appId, appIdScopeMap);
// 拓扑路径构建
List<PathInfoDTO> path = new ArrayList<>();
PathInfoDTO accountNode = new PathInfoDTO();
accountNode.setType(ResourceTypeId.ACCOUNT);
accountNode.setId(accountDTO.getId().toString());
rootNode.setChild(accountNode);
path.add(rootNode);
// 实例组装
InstanceInfoDTO instanceInfo = new InstanceInfoDTO();
instanceInfo.setId(instanceId);
instanceInfo.setDisplayName(accountDTO.getAlias());
instanceInfo.setPath(path);
InstanceInfoDTO instanceInfo = buildInstance(instanceId, accountDTO, appIdScopeMap);
instanceAttributeInfoList.add(instanceInfo);
} catch (NumberFormatException e) {
log.error("Parse object id failed!|{}", instanceId, e);
} catch (Exception e) {
logBuildInstanceFailure(accountDTO, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ protected ListInstanceResponseDTO listInstanceResp(CallbackRequestDTO callbackRe
return IamRespUtil.getListInstanceRespFromPageData(tagDTOPageData, this::convert);
}

private InstanceInfoDTO buildInstance(TagDTO tagDTO,
Map<Long, ResourceScope> appIdScopeMap) {
Long appId = tagDTO.getAppId();
// 拓扑路径构建
List<PathInfoDTO> path = new ArrayList<>();
PathInfoDTO rootNode = getPathNodeByAppId(appId, appIdScopeMap);
PathInfoDTO tagNode = new PathInfoDTO();
tagNode.setType(ResourceTypeId.TAG);
tagNode.setId(tagDTO.getId().toString());
rootNode.setChild(tagNode);
path.add(rootNode);
// 实例组装
InstanceInfoDTO instanceInfo = new InstanceInfoDTO();
instanceInfo.setId(tagDTO.getId().toString());
instanceInfo.setDisplayName(tagDTO.getName());
instanceInfo.setPath(path);
return instanceInfo;
}

@Override
protected CallbackBaseResponseDTO fetchInstanceResp(CallbackRequestDTO callbackRequest) {
IamSearchCondition searchCondition = IamSearchCondition.fromReq(callbackRequest);
Expand All @@ -132,30 +151,18 @@ protected CallbackBaseResponseDTO fetchInstanceResp(CallbackRequestDTO callbackR
}
// Job app --> CMDB biz/businessSet转换
Map<Long, ResourceScope> appIdScopeMap = applicationService.getScopeByAppIds(appIdSet);
for (String instanceId : searchCondition.getIdList()) {
for (String id : searchCondition.getIdList()) {
Long tagId = Long.parseLong(id);
TagDTO tagDTO = tagDTOMap.get(tagId);
if (tagDTO == null) {
logNotExistId(id);
continue;
}
try {
Long tagId = Long.parseLong(instanceId);
TagDTO tagDTO = tagDTOMap.get(tagId);
if (tagDTO == null) {
return getNotFoundRespById(instanceId);
}
Long appId = tagDTO.getAppId();
// 拓扑路径构建
List<PathInfoDTO> path = new ArrayList<>();
PathInfoDTO rootNode = getPathNodeByAppId(appId, appIdScopeMap);
PathInfoDTO tagNode = new PathInfoDTO();
tagNode.setType(ResourceTypeId.TAG);
tagNode.setId(tagDTO.getId().toString());
rootNode.setChild(tagNode);
path.add(rootNode);
// 实例组装
InstanceInfoDTO instanceInfo = new InstanceInfoDTO();
instanceInfo.setId(instanceId);
instanceInfo.setDisplayName(tagDTO.getName());
instanceInfo.setPath(path);
InstanceInfoDTO instanceInfo = buildInstance(tagDTO, appIdScopeMap);
instanceAttributeInfoList.add(instanceInfo);
} catch (NumberFormatException e) {
log.error("Parse object id failed!|{}", instanceId, e);
} catch (Exception e) {
logBuildInstanceFailure(tagDTO, e);
}
}

Expand Down
Loading

0 comments on commit b8c6bcf

Please sign in to comment.