Skip to content

Commit

Permalink
Merge pull request #2850 from actiontech/partially_audit_plan_params
Browse files Browse the repository at this point in the history
partially audit plan params
  • Loading branch information
BugsGuru authored Jan 3, 2025
2 parents 6cef63f + 9b8a9ea commit 8d070e7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
13 changes: 13 additions & 0 deletions sqle/api/controller/v1/audit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,19 @@ func checkAndGenerateAuditPlanParams(auditPlanType, instanceType string, paramsR
return meta.Params(), nil
}

func UpdateAuditPlanParams(auditParams params.Params, updateParamsReq []AuditPlanParamReqV1) (params.Params, error) {
// 如果params不为空,将paramsReq中的值替换掉params的值
for _, p := range updateParamsReq {
// set and valid param.
err := auditParams.SetParamValue(p.Key, p.Value)
if err != nil {
return nil, fmt.Errorf("set param error: %s", err)
}
}

return auditParams, nil
}

// @Summary 添加扫描任务
// @Description create audit plan
// @Id createAuditPlanV1
Expand Down
38 changes: 25 additions & 13 deletions sqle/api/controller/v1/instance_audit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ func UpdateInstanceAuditPlan(c echo.Context) error {
return controller.JSONBaseErrorReq(c, err)
}
inst := dms.GetInstancesByIdWithoutError(fmt.Sprintf("%d", dbAuditPlans.InstanceID))
for _, auditPlan := range req.AuditPlans {
if auditPlan.RuleTemplateName != "" {
exist, err := s.IsRuleTemplateExist(auditPlan.RuleTemplateName, []string{projectUid, model.ProjectIdForGlobalRuleTemplate})
for _, auditPlanReq := range req.AuditPlans {
if auditPlanReq.RuleTemplateName != "" {
exist, err := s.IsRuleTemplateExist(auditPlanReq.RuleTemplateName, []string{projectUid, model.ProjectIdForGlobalRuleTemplate})
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}
Expand All @@ -320,34 +320,46 @@ func UpdateInstanceAuditPlan(c echo.Context) error {
}
}
// check rule template name
ruleTemplateName, err := autoSelectRuleTemplate(c.Request().Context(), auditPlan.RuleTemplateName, inst.Name, dbAuditPlans.DBType, projectUid)
ruleTemplateName, err := autoSelectRuleTemplate(c.Request().Context(), auditPlanReq.RuleTemplateName, inst.Name, dbAuditPlans.DBType, projectUid)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}

// check params
if auditPlan.Type == "" {
auditPlan.Type = auditplan.TypeDefault
if auditPlanReq.Type == "" {
auditPlanReq.Type = auditplan.TypeDefault
}
ps, err := checkAndGenerateAuditPlanParams(auditPlan.Type, dbAuditPlans.DBType, auditPlan.Params)
if err != nil {
return controller.JSONBaseErrorReq(c, errors.New(errors.DataConflict, err))

var ps params.Params
// update old audit plan params or generate new audit plan params
if auditPlan, ok := dbAuditPlansMap[auditPlanReq.Type]; !ok {
ps, err = checkAndGenerateAuditPlanParams(auditPlanReq.Type, inst.DbType, auditPlanReq.Params)
if err != nil {
return controller.JSONBaseErrorReq(c, errors.New(errors.DataConflict, err))
}
} else {
ps, err = UpdateAuditPlanParams(auditPlan.Params, auditPlanReq.Params)
if err != nil {
return controller.JSONBaseErrorReq(c, errors.New(errors.DataConflict, err))
}
}
hpc, err := checkAndGenerateHighPriorityParams(auditPlan.Type, dbAuditPlans.DBType, auditPlan.HighPriorityConditions)

hpc, err := checkAndGenerateHighPriorityParams(auditPlanReq.Type, inst.DbType, auditPlanReq.HighPriorityConditions)
if err != nil {
return controller.JSONBaseErrorReq(c, errors.New(errors.DataConflict, err))
}

res := &model.AuditPlanV2{
Type: auditPlan.Type,
Type: auditPlanReq.Type,
RuleTemplateName: ruleTemplateName,
Params: ps,
HighPriorityParams: hpc,
NeedMarkHighPrioritySQL: auditPlan.NeedMarkHighPrioritySQL,
NeedMarkHighPrioritySQL: auditPlanReq.NeedMarkHighPrioritySQL,
InstanceAuditPlanID: dbAuditPlans.ID,
}

// if the data exists in the database, update the data; if it does not exist, insert the data.
if dbAuditPlan, ok := dbAuditPlansMap[auditPlan.Type]; ok {
if dbAuditPlan, ok := dbAuditPlansMap[auditPlanReq.Type]; ok {
dbAuditPlan.RuleTemplateName = res.RuleTemplateName
dbAuditPlan.Params = res.Params
dbAuditPlan.HighPriorityParams = res.HighPriorityParams
Expand Down

0 comments on commit 8d070e7

Please sign in to comment.