From 773e75f0d6571ab6ad11afe431c3d1f8d2af7abc Mon Sep 17 00:00:00 2001 From: qiwang <1364512070@qq.com> Date: Fri, 12 Apr 2024 17:44:19 +0800 Subject: [PATCH] feat:submit vm model Former-commit-id: 4955139d43a57a3d3320758060574514b7806b39 --- api/client/types.go | 3 +- api/desc/core/pcm-core.api | 28 ++--- api/desc/participant/pcm-participant.api | 5 +- api/internal/logic/core/commitvmtasklogic.go | 56 +++++----- api/internal/logic/core/pulltaskinfologic.go | 7 ++ api/internal/logic/core/pushtaskinfologic.go | 6 ++ api/internal/types/types.go | 51 ++++----- pkg/models/taskvmmodel.go | 24 +++++ pkg/models/taskvmmodel_gen.go | 107 +++++++++++++++++++ 9 files changed, 206 insertions(+), 81 deletions(-) create mode 100644 pkg/models/taskvmmodel.go create mode 100644 pkg/models/taskvmmodel_gen.go diff --git a/api/client/types.go b/api/client/types.go index 0b8250f8..f2fca5a5 100644 --- a/api/client/types.go +++ b/api/client/types.go @@ -119,5 +119,6 @@ type VmInfo struct { BlockUuid string `json:"block_uuid,omitempty"` SourceType string `json:"source_type,omitempty"` DeleteOnTermination bool `json:"delete_on_termination,omitempty"` - State string `json:"state,omitempty"` + Status string `json:"Status,omitempty"` + StartTime string `json:"startTime,omitempty"` } diff --git a/api/desc/core/pcm-core.api b/api/desc/core/pcm-core.api index 8e844817..4e07843d 100644 --- a/api/desc/core/pcm-core.api +++ b/api/desc/core/pcm-core.api @@ -131,35 +131,19 @@ type ( NsID string `json:"nsID"` Replicas int64 `json:"replicas,optional"` MatchLabels map[string]string `json:"matchLabels,optional"` - servers []ServerCommit `json:"servers,optional"` - platform string `json:"platform,optional"` AdapterId string `json:"adapterId,optional"` ClusterType string `json:"clusterType,optional"` + //Virtual Machine Section + CreateMulServer []CreateMulDomainServer `json:"createMulServer,optional"` } - ServerCommit { - allCardRunTime string `json:"allCardRunTime"` - flavorRef string `json:"flavorRef,optional"` + CreateMulDomainServer { + Platform string `json:"platform,optional"` name string `json:"name,optional"` + min_count int64 `json:"min_count,optional"` imageRef string `json:"imageRef,optional"` - accessIPv4 string `json:"accessIPv4,optional"` - accessIPv6 string `json:"accessIPv6,optional"` - adminPass string `json:"adminPass,optional"` - availability_zone string `json:"availability_zone,optional"` - key_name string `json:"key_name,optional"` - hostname string `json:"hostname,optional"` - host string `json:"host,optional"` - networks []Networks `json:"networks,optional"` - } - Networks { - uuid string `json:"uuid,optional"` - port string `json:"port,optional"` - fixed_ip string `json:"fixed_ip,optional"` - tag string `json:"tag,optional"` - } - Block_device_mapping_v2Commit { + flavorRef string `json:"flavorRef,optional"` uuid string `json:"uuid,optional"` } - commitVmTaskResp { // VmTask []VmTask `json:"vmTask" copier:"VmTask"` TaskId int64 `json:"taskId"` diff --git a/api/desc/participant/pcm-participant.api b/api/desc/participant/pcm-participant.api index d9f5a7d1..7f02d840 100644 --- a/api/desc/participant/pcm-participant.api +++ b/api/desc/participant/pcm-participant.api @@ -105,7 +105,10 @@ type VmInfo struct { BlockUuid string `json:"block_uuid,omitempty"` SourceType string `json:"source_type,omitempty"` DeleteOnTermination bool `json:"delete_on_termination,omitempty"` - State string `json:"state,omitempty"` + Status string `json:"status,omitempty"` + MinCount string `json:"min_count,omitempty"` + Platform string `json:"platform,omitempty"` + Uuid string `json:"uuid,omitempty"` } type PushTaskInfoReq struct { diff --git a/api/internal/logic/core/commitvmtasklogic.go b/api/internal/logic/core/commitvmtasklogic.go index fd048234..3e11b7ac 100644 --- a/api/internal/logic/core/commitvmtasklogic.go +++ b/api/internal/logic/core/commitvmtasklogic.go @@ -2,13 +2,12 @@ package core import ( "context" - "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/mqs" + "fmt" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" - "gitlink.org.cn/JointCloud/pcm-coordinator/api/pkg/response" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" - tool "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils" + "math/rand" "time" "github.com/zeromicro/go-zero/core/logx" @@ -35,7 +34,6 @@ func (l *CommitVmTaskLogic) CommitVmTask(req *types.CommitVmTaskReq) (resp *type Status: constants.Saved, Name: req.Name, CommitTime: time.Now(), - NsID: req.NsID, } // Save task data to database tx := l.svcCtx.DbEngin.Create(&taskModel) @@ -43,28 +41,38 @@ func (l *CommitVmTaskLogic) CommitVmTask(req *types.CommitVmTaskReq) (resp *type return nil, tx.Error } - var clusterIds []int64 - l.svcCtx.DbEngin.Raw("SELECT id FROM `t_cluster` where adapter_id = ? and label = ?", req.AdapterId, req.ClusterType).Scan(&clusterIds) + for _, CreateMulServer := range req.CreateMulServer { + fmt.Println("", req.CreateMulServer) + var clusterIds []int64 + l.svcCtx.DbEngin.Raw("SELECT id FROM `t_cluster` where adapter_id = ? and label = ?", req.AdapterId, req.ClusterType).Scan(&clusterIds) - if len(clusterIds) == 0 || clusterIds == nil { - return nil, nil + if len(clusterIds) == 0 || clusterIds == nil { + return nil, nil + } + + vmInfo := models.TaskVm{ + TaskId: taskModel.Id, + ClusterId: clusterIds[rand.Intn(len(clusterIds))], + Name: taskModel.Name, + Status: "Saved", + StartTime: time.Now().String(), + MinCount: CreateMulServer.Min_count, + ImageRef: CreateMulServer.ImageRef, + FlavorRef: CreateMulServer.FlavorRef, + Uuid: CreateMulServer.Uuid, + Platform: CreateMulServer.Platform, + } + + tx = l.svcCtx.DbEngin.Create(&vmInfo) + if tx.Error != nil { + return nil, tx.Error + } + resp = &types.CommitVmTaskResp{ + Code: 200, + Msg: "success", + TaskId: taskModel.Id, + } } - vm := models.Vm{} - tool.Convert(req, &vm) - mqInfo := response.TaskInfo{ - TaskId: taskModel.Id, - TaskType: "vm", - MatchLabels: req.MatchLabels, - NsID: req.NsID, - } - //req.TaskId = taskModel.Id - mqs.InsQueue.Beta.Add(&mqInfo) - tx = l.svcCtx.DbEngin.Create(&mqInfo) - resp = &types.CommitVmTaskResp{ - Code: 200, - Msg: "success", - TaskId: taskModel.Id, - } return resp, nil } diff --git a/api/internal/logic/core/pulltaskinfologic.go b/api/internal/logic/core/pulltaskinfologic.go index bc152ddc..ef9b86d9 100644 --- a/api/internal/logic/core/pulltaskinfologic.go +++ b/api/internal/logic/core/pulltaskinfologic.go @@ -67,6 +67,13 @@ func (l *PullTaskInfoLogic) PullTaskInfo(req *clientCore.PullTaskInfoReq) (*clie return nil, err } utils.Convert(aiModelList, &resp.AiInfoList) + case 3: + var vmModelList []models.TaskVm + err := findModelList(req.AdapterId, l.svcCtx.DbEngin, &vmModelList) + if err != nil { + return nil, err + } + utils.Convert(vmModelList, &resp.VmInfoList) } return &resp, nil } diff --git a/api/internal/logic/core/pushtaskinfologic.go b/api/internal/logic/core/pushtaskinfologic.go index 2944711d..1c056a86 100644 --- a/api/internal/logic/core/pushtaskinfologic.go +++ b/api/internal/logic/core/pushtaskinfologic.go @@ -49,6 +49,12 @@ func (l *PushTaskInfoLogic) PushTaskInfo(req *clientCore.PushTaskInfoReq) (*clie aiInfo.Status, aiInfo.StartTime, aiInfo.ProjectId, aiInfo.JobId, req.AdapterId, aiInfo.TaskId, aiInfo.Name) syncTask(l.svcCtx.DbEngin, aiInfo.TaskId) } + case 3: + for _, vmInfo := range req.VmInfoList { + l.svcCtx.DbEngin.Exec("update task_vm set status = ?,start_time = ? where participant_id = ? and task_id = ? and name = ?", + vmInfo.Status, vmInfo.StartTime, req.AdapterId, vmInfo.TaskId, vmInfo.Name) + syncTask(l.svcCtx.DbEngin, vmInfo.TaskId) + } } return &resp, nil diff --git a/api/internal/types/types.go b/api/internal/types/types.go index d30c6334..2176fa79 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -112,40 +112,22 @@ type TaskYaml struct { } type CommitVmTaskReq struct { - Name string `json:"name"` - NsID string `json:"nsID"` - Replicas int64 `json:"replicas,optional"` - MatchLabels map[string]string `json:"matchLabels,optional"` - Servers []ServerCommit `json:"servers,optional"` - Platform string `json:"platform,optional"` - AdapterId string `json:"adapterId,optional"` - ClusterType string `json:"clusterType,optional"` + Name string `json:"name"` + NsID string `json:"nsID"` + Replicas int64 `json:"replicas,optional"` + MatchLabels map[string]string `json:"matchLabels,optional"` + AdapterId string `json:"adapterId,optional"` + ClusterType string `json:"clusterType,optional"` + CreateMulServer []CreateMulDomainServer `json:"createMulServer,optional"` } -type ServerCommit struct { - AllCardRunTime string `json:"allCardRunTime"` - FlavorRef string `json:"flavorRef,optional"` - Name string `json:"name,optional"` - ImageRef string `json:"imageRef,optional"` - AccessIPv4 string `json:"accessIPv4,optional"` - AccessIPv6 string `json:"accessIPv6,optional"` - AdminPass string `json:"adminPass,optional"` - Availability_zone string `json:"availability_zone,optional"` - Key_name string `json:"key_name,optional"` - Hostname string `json:"hostname,optional"` - Host string `json:"host,optional"` - Networks []Networks `json:"networks,optional"` -} - -type Networks struct { - Uuid string `json:"uuid,optional"` - Port string `json:"port,optional"` - Fixed_ip string `json:"fixed_ip,optional"` - Tag string `json:"tag,optional"` -} - -type Block_device_mapping_v2Commit struct { - Uuid string `json:"uuid,optional"` +type CreateMulDomainServer struct { + Platform string `json:"platform,optional"` + Name string `json:"name,optional"` + Min_count int64 `json:"min_count,optional"` + ImageRef string `json:"imageRef,optional"` + FlavorRef string `json:"flavorRef,optional"` + Uuid string `json:"uuid,optional"` } type CommitVmTaskResp struct { @@ -5429,7 +5411,10 @@ type VmInfo struct { BlockUuid string `json:"block_uuid,omitempty"` SourceType string `json:"source_type,omitempty"` DeleteOnTermination bool `json:"delete_on_termination,omitempty"` - State string `json:"state,omitempty"` + Status string `json:"status,omitempty"` + MinCount string `json:"min_count,omitempty"` + Platform string `json:"platform,omitempty"` + Uuid string `json:"uuid,omitempty"` } type PushTaskInfoReq struct { diff --git a/pkg/models/taskvmmodel.go b/pkg/models/taskvmmodel.go new file mode 100644 index 00000000..1440be12 --- /dev/null +++ b/pkg/models/taskvmmodel.go @@ -0,0 +1,24 @@ +package models + +import "github.com/zeromicro/go-zero/core/stores/sqlx" + +var _ TaskVmModel = (*customTaskVmModel)(nil) + +type ( + // TaskVmModel is an interface to be customized, add more methods here, + // and implement the added methods in customTaskVmModel. + TaskVmModel interface { + taskVmModel + } + + customTaskVmModel struct { + *defaultTaskVmModel + } +) + +// NewTaskVmModel returns a model for the database table. +func NewTaskVmModel(conn sqlx.SqlConn) TaskVmModel { + return &customTaskVmModel{ + defaultTaskVmModel: newTaskVmModel(conn), + } +} diff --git a/pkg/models/taskvmmodel_gen.go b/pkg/models/taskvmmodel_gen.go new file mode 100644 index 00000000..6a89501a --- /dev/null +++ b/pkg/models/taskvmmodel_gen.go @@ -0,0 +1,107 @@ +// Code generated by goctl. DO NOT EDIT. + +package models + +import ( + "context" + "database/sql" + "fmt" + "strings" + + "github.com/zeromicro/go-zero/core/stores/builder" + "github.com/zeromicro/go-zero/core/stores/sqlc" + "github.com/zeromicro/go-zero/core/stores/sqlx" + "github.com/zeromicro/go-zero/core/stringx" +) + +var ( + taskVmFieldNames = builder.RawFieldNames(&TaskVm{}) + taskVmRows = strings.Join(taskVmFieldNames, ",") + taskVmRowsExpectAutoSet = strings.Join(stringx.Remove(taskVmFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") + taskVmRowsWithPlaceHolder = strings.Join(stringx.Remove(taskVmFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" +) + +type ( + taskVmModel interface { + Insert(ctx context.Context, data *TaskVm) (sql.Result, error) + FindOne(ctx context.Context, id int64) (*TaskVm, error) + Update(ctx context.Context, data *TaskVm) error + Delete(ctx context.Context, id int64) error + } + + defaultTaskVmModel struct { + conn sqlx.SqlConn + table string + } + + TaskVm struct { + Id int64 `db:"id"` // id + ParticipantId int64 `db:"participant_id"` // p端id + TaskId int64 `db:"task_id"` // 任务id + Name string `db:"name"` // 虚拟机名称 + AdapterId int64 `db:"adapter_id"` // 执行任务的适配器id + ClusterId int64 `db:"cluster_id"` // 执行任务的集群id + FlavorRef string `db:"flavor_ref"` // 规格索引 + ImageRef string `db:"image_ref"` // 镜像索引 + Status string `db:"status"` // 状态 + Platform string `db:"platform"` // 平台 + Description string `db:"description"` // 描述 + AvailabilityZone string `db:"availability_zone"` + MinCount int64 `db:"min_count"` // 数量 + Uuid string `db:"uuid"` // 网络id + StartTime string `db:"start_time"` // 开始时间 + RunningTime string `db:"running_time"` // 运行时间 + Result string `db:"result"` // 运行结果 + DeletedAt string `db:"deleted_at"` // 删除时间 + } +) + +func newTaskVmModel(conn sqlx.SqlConn) *defaultTaskVmModel { + return &defaultTaskVmModel{ + conn: conn, + table: "`task_vm`", + } +} + +func (m *defaultTaskVmModel) withSession(session sqlx.Session) *defaultTaskVmModel { + return &defaultTaskVmModel{ + conn: sqlx.NewSqlConnFromSession(session), + table: "`task_vm`", + } +} + +func (m *defaultTaskVmModel) Delete(ctx context.Context, id int64) error { + query := fmt.Sprintf("delete from %s where `id` = ?", m.table) + _, err := m.conn.ExecCtx(ctx, query, id) + return err +} + +func (m *defaultTaskVmModel) FindOne(ctx context.Context, id int64) (*TaskVm, error) { + query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", taskVmRows, m.table) + var resp TaskVm + err := m.conn.QueryRowCtx(ctx, &resp, query, id) + switch err { + case nil: + return &resp, nil + case sqlc.ErrNotFound: + return nil, ErrNotFound + default: + return nil, err + } +} + +func (m *defaultTaskVmModel) Insert(ctx context.Context, data *TaskVm) (sql.Result, error) { + query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, taskVmRowsExpectAutoSet) + ret, err := m.conn.ExecCtx(ctx, query, data.ParticipantId, data.TaskId, data.Name, data.AdapterId, data.ClusterId, data.FlavorRef, data.ImageRef, data.Status, data.Platform, data.Description, data.AvailabilityZone, data.MinCount, data.Uuid, data.StartTime, data.RunningTime, data.Result, data.DeletedAt) + return ret, err +} + +func (m *defaultTaskVmModel) Update(ctx context.Context, data *TaskVm) error { + query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, taskVmRowsWithPlaceHolder) + _, err := m.conn.ExecCtx(ctx, query, data.ParticipantId, data.TaskId, data.Name, data.AdapterId, data.ClusterId, data.FlavorRef, data.ImageRef, data.Status, data.Platform, data.Description, data.AvailabilityZone, data.MinCount, data.Uuid, data.StartTime, data.RunningTime, data.Result, data.DeletedAt, data.Id) + return err +} + +func (m *defaultTaskVmModel) tableName() string { + return m.table +}