package cloud import ( "context" "github.com/pkg/errors" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" "sigs.k8s.io/yaml" "strings" "time" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type CommitGeneralTaskLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewCommitGeneralTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommitGeneralTaskLogic { return &CommitGeneralTaskLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *CommitGeneralTaskLogic) CommitGeneralTask(req *types.GeneralTaskReq) error { var yamlStr []string for _, s := range req.ReqBody { j2, err := yaml.YAMLToJSON([]byte(s)) if err != nil { logx.Errorf("Failed to convert yaml to JSON, err: %v", err) return err } yamlStr = append(yamlStr, string(j2)) } result := strings.Join(yamlStr, ",") //TODO The namespace is fixed to ns-admin for the time being. Later, the namespace is obtained based on the user taskModel := models.Task{ Status: constants.Saved, Name: req.Name, CommitTime: time.Now(), NsID: "ns-admin", YamlString: "[" + result + "]", } // Save the task data to the database tx := l.svcCtx.DbEngin.Create(&taskModel) if tx.Error != nil { return tx.Error } var clusterIds []int64 l.svcCtx.DbEngin.Raw("SELECT id FROM `t_cluster` where adapter_id = ? and id in ?", req.AdapterId, req.ClusterIds).Scan(&clusterIds) if len(clusterIds) == 0 || clusterIds == nil { return errors.Errorf("the cluster does not match the drive resources. Check the data") } //保存数据到cloud表 return nil }