Skip to content

Commit

Permalink
perf: 优化作业模板、执行方案资源的创建、更新的Web请求校验日志 TencentBlueKing#2101
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyu096 committed Jun 1, 2023
1 parent cab6da8 commit 5890798
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
package com.tencent.bk.job.manage.model.web.vo.task;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.tencent.bk.job.common.util.JobContextUtil;
import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.exception.InvalidParamException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
* @since 13/12/2019 17:31
Expand All @@ -40,21 +42,21 @@
@NoArgsConstructor
@ApiModel("云区域信息")
@JsonInclude(JsonInclude.Include.NON_NULL)
@Slf4j
public class CloudAreaInfoVO {
/**
* 云区域 ID:1是腾讯云 2是投后公司 3是私有云
* 云区域 ID
*/
@ApiModelProperty(value = "云区域 ID", required = true)
private Long id;

@ApiModelProperty("云区域名称")
private String name;

public boolean validate(boolean isCreate) {
if (id != null && id >= 0) {
return true;
public void validate(boolean isCreate) throws InvalidParamException {
if (id == null || id < 0) {
log.warn("Invalid cloud area info!");
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
JobContextUtil.addDebugMessage("Invalid cloud area info!");
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
package com.tencent.bk.job.manage.model.web.vo.task;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.tencent.bk.job.common.util.JobContextUtil;
import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.exception.InvalidParamException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
Expand All @@ -37,18 +39,22 @@
@Data
@ApiModel("目标节点信息")
@JsonInclude(JsonInclude.Include.NON_NULL)
@Slf4j
public class TargetNodeVO {
@ApiModelProperty(value = "节点 ID,对应拓扑树节点中的instanceId", required = true)
private Long id;

@ApiModelProperty(value = "节点类型 biz-业务 set-集群 module-模块 xxx-用户自定义节点类型,对应拓扑树节点中的objectId", required = true)
private String type;

public boolean validate(boolean isCreate) {
if (id != null && id > 0 && StringUtils.isNotBlank(type)) {
return true;
public void validate(boolean isCreate) throws InvalidParamException {
if (id == null || id <= 0) {
log.warn("Invalid target node id, id: {}", id);
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
if (StringUtils.isBlank(type)) {
log.warn("Invalid target node type");
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
JobContextUtil.addDebugMessage("Target node info does not have id or type");
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@

package com.tencent.bk.job.manage.model.web.vo.task;

import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.exception.InvalidParamException;
import com.tencent.bk.job.common.model.vo.TaskTargetVO;
import com.tencent.bk.job.common.util.FilePathValidateUtil;
import com.tencent.bk.job.common.util.JobContextUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.List;
Expand All @@ -39,6 +41,7 @@
*/
@Data
@ApiModel("步骤源文件信息")
@Slf4j
public class TaskFileSourceInfoVO {

@ApiModelProperty(value = "文件 ID 新建填 0")
Expand Down Expand Up @@ -68,47 +71,46 @@ public class TaskFileSourceInfoVO {
@ApiModelProperty(value = "文件源ID,来自文件源的文件需要传入")
private Integer fileSourceId;

public boolean validate(boolean isCreate) {
public void validate(boolean isCreate) throws InvalidParamException {
if (fileType == null || fileType <= 0 || fileType > 3) {
JobContextUtil.addDebugMessage("Invalid file type!");
return false;
log.warn("Invalid file type!");
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
switch (fileType) {
case 1:
if (CollectionUtils.isEmpty(fileLocation)) {
JobContextUtil.addDebugMessage("Empty file location!");
return false;
log.warn("Empty file location!");
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
for (String file : fileLocation) {
if (!FilePathValidateUtil.validateFileSystemAbsolutePath(file)) {
JobContextUtil.addDebugMessage("fileLocation is illegal!");
return false;
log.warn("fileLocation is illegal!");
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
}
host.validate(isCreate);
if (account == null || account <= 0) {
JobContextUtil.addDebugMessage("Invalid host account!");
return false;
log.warn("Invalid host account!");
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
break;
case 2:
try {
Long.valueOf(fileSize);
} catch (NumberFormatException e) {
JobContextUtil.addDebugMessage("Invalid file size!");
return false;
log.warn("Invalid file size!");
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
break;
case 3:
if (fileSourceId == null || fileSourceId <= 0) {
JobContextUtil.addDebugMessage("Invalid fileSourceId!");
return false;
log.warn("Invalid fileSourceId!");
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
break;
default:
JobContextUtil.addDebugMessage("Invalid file type!");
return false;
log.warn("Invalid file type!");
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ public void validate(boolean isCreate) throws InvalidParamException {
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
for (TaskFileSourceInfoVO taskFileSourceInfoVO : fileSourceList) {
if (!taskFileSourceInfoVO.validate(isCreate)) {
log.warn("Invalid file info!");
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
taskFileSourceInfoVO.validate(isCreate);
}
if (fileDestination == null) {
log.warn("Empty destination info!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
package com.tencent.bk.job.manage.model.web.vo.task;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.exception.InvalidParamException;
import com.tencent.bk.job.common.model.vo.HostInfoVO;
import com.tencent.bk.job.common.util.JobContextUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

Expand All @@ -41,6 +43,7 @@
@Data
@ApiModel("主机节点信息")
@JsonInclude(JsonInclude.Include.NON_NULL)
@Slf4j
public class TaskHostNodeVO {

@ApiModelProperty("机器 IP 列表")
Expand All @@ -52,23 +55,20 @@ public class TaskHostNodeVO {
@ApiModelProperty("动态分组 ID")
private List<String> dynamicGroupList;

public boolean validate(boolean isCreate) {
public void validate(boolean isCreate) throws InvalidParamException {
boolean allEmpty = true;
if (!CollectionUtils.isEmpty(topoNodeList)) {
allEmpty = false;
for (TargetNodeVO targetNodeVO : topoNodeList) {
if (!targetNodeVO.validate(isCreate)) {
JobContextUtil.addDebugMessage("Host node info validate failed!");
return false;
}
targetNodeVO.validate(isCreate);
}
}
if (!CollectionUtils.isEmpty(dynamicGroupList)) {
allEmpty = false;
for (String dynamicGroup : dynamicGroupList) {
if (!StringUtils.isNoneBlank(dynamicGroup)) {
JobContextUtil.addDebugMessage("Host dynamic group id is empty!");
return false;
log.warn("Host dynamic group id is empty!");
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
}
}
Expand All @@ -78,6 +78,9 @@ public boolean validate(boolean isCreate) {
hostInfoVO.validate(isCreate);
}
}
return !allEmpty;
if (allEmpty) {
log.warn("TaskHostNode is empty!");
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@

package com.tencent.bk.job.manage.model.web.vo.task;

import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.exception.InvalidParamException;
import com.tencent.bk.job.common.model.vo.TaskTargetVO;
import com.tencent.bk.job.common.util.JobContextUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
* @since 16/10/2019 10:50
*/
@Data
@ApiModel("全局变量信息")
@Slf4j
public class TaskVariableVO {

@ApiModelProperty(value = "变量 ID 新增时无需填写,删除时仅需填写 id 和 delete", required = true)
Expand Down Expand Up @@ -71,28 +75,27 @@ public class TaskVariableVO {
@ApiModelProperty(value = "删除 0-不删除 1-删除,仅在删除时填写")
private Integer delete;

public boolean validate(boolean isCreate) {
public void validate(boolean isCreate) throws InvalidParamException {
if (isCreate && !JobContextUtil.isAllowMigration()) {
if (id != null && id > 0) {
JobContextUtil.addDebugMessage("Create request has variable id!");
return false;
log.warn("Create request has variable id!");
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
}
if (StringUtils.isBlank(name)) {
JobContextUtil.addDebugMessage("Empty variable name!");
return false;
log.warn("Empty variable name!");
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
if (description == null) {
description = "";
}
if (type <= 0 || type > 6) {
JobContextUtil.addDebugMessage("Invalid variable type!");
return false;
log.warn("Invalid variable type!");
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
if (required == null || required > 1) {
JobContextUtil.addDebugMessage("Invalid require option!");
return false;
log.warn("Invalid require option!");
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM);
}
return true;
}
}

0 comments on commit 5890798

Please sign in to comment.