From 1749361172e95c535f65fbc8bc349f7c0af927e9 Mon Sep 17 00:00:00 2001 From: jagger Date: Fri, 12 Jan 2024 15:30:08 +0800 Subject: [PATCH] fix: Interface apps/list returns duplicate data Closes #4 Signed-off-by: jagger Former-commit-id: 22456e59e1b5b8c9a14bed7b7e328357de6e7ac8 --- api/desc/core/pcm-core.api | 16 ++++-- .../handler/core/commitvmtaskhandler.go | 24 ++++++++ api/internal/handler/routes.go | 5 ++ api/internal/logic/apps/applistlogic.go | 26 +++++---- api/internal/logic/core/commitvmtasklogic.go | 30 ++++++++++ api/internal/types/types.go | 55 ++++++++++++++++--- 6 files changed, 132 insertions(+), 24 deletions(-) create mode 100644 api/internal/handler/core/commitvmtaskhandler.go create mode 100644 api/internal/logic/core/commitvmtasklogic.go diff --git a/api/desc/core/pcm-core.api b/api/desc/core/pcm-core.api index ebb7ded3..771ec7e0 100644 --- a/api/desc/core/pcm-core.api +++ b/api/desc/core/pcm-core.api @@ -233,10 +233,10 @@ type ( type ( commitVmTaskReq { - server Server `json:"server,optional"` + server ServerCommit `json:"server,optional"` platform string `json:"platform,optional"` } - Server { + ServerCommit { allCardRunTime string `json:"allCardRunTime"` flavorRef string `json:"flavorRef,optional"` name string `json:"name,optional"` @@ -256,7 +256,7 @@ type ( fixed_ip string `json:"fixed_ip,optional"` tag string `json:"tag,optional"` } - Block_device_mapping_v2 { + Block_device_mapping_v2Commit { uuid string `json:"uuid,optional"` } commitVmTaskResp { @@ -649,14 +649,18 @@ type ( Replica int32 `json:"replica"` } App { - Id int64 `json:"id,optional"` + Id int64 `json:"id,optional" db:"id"` Name string `json:"name,optional"` Status string `json:"status,optional"` - ParticipantId string `json:"participantId,optional"` - ParticipantName string `json:"participantName,optional"` CreateTime string `json:"createTime,optional"` MinReplicas string `json:"minReplicas,optional"` MaxReplicas string `json:"maxReplicas,optional"` + AppLocations []AppLocation `json:"appLocations,optional"` + } + AppLocation { + ParticipantId string `json:"participantId,optional" db:"participant_id"` + ParticipantName string `json:"participantName,optional" db:"participant_name"` + Kind string `json:"kind,optional" db:"kind"` } ) diff --git a/api/internal/handler/core/commitvmtaskhandler.go b/api/internal/handler/core/commitvmtaskhandler.go new file mode 100644 index 00000000..74971c31 --- /dev/null +++ b/api/internal/handler/core/commitvmtaskhandler.go @@ -0,0 +1,24 @@ +package core + +import ( + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/core" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/repository/result" + "net/http" +) + +func CommitVmTaskHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.CommitVmTaskReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + + l := core.NewCommitVmTaskLogic(r.Context(), svcCtx) + resp, err := l.CommitVmTask(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/routes.go b/api/internal/handler/routes.go index bc66b888..67994398 100644 --- a/api/internal/handler/routes.go +++ b/api/internal/handler/routes.go @@ -41,6 +41,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/core/commitHpcTask", Handler: core.CommitHpcTaskHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/core/commitVmTask", + Handler: core.CommitVmTaskHandler(serverCtx), + }, { Method: http.MethodDelete, Path: "/core/deleteTask/:id", diff --git a/api/internal/logic/apps/applistlogic.go b/api/internal/logic/apps/applistlogic.go index 1e9d9699..f4183bd3 100644 --- a/api/internal/logic/apps/applistlogic.go +++ b/api/internal/logic/apps/applistlogic.go @@ -5,7 +5,6 @@ import ( "gitlink.org.cn/jcce-pcm/pcm-participant-kubernetes/kubernetes" "gorm.io/datatypes" "gorm.io/gorm" - "strconv" "time" "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc" @@ -50,7 +49,7 @@ type Task struct { func (l *AppListLogic) AppList(req *types.AppListReq) (resp *types.AppListResp, err error) { var tasks []Task resp = &types.AppListResp{} - l.svcCtx.DbEngin.Raw("SELECT t.*,phy.name as p_name,phy.id as p_id FROM task t LEFT JOIN cloud c ON c.task_id = t.id join sc_participant_phy_info phy on c.participant_id = phy.id WHERE c.kind in ('Deployment', 'StatefulSet') AND t.`ns_id` = ? AND t.`deleted_at` IS NULL ORDER BY t.created_time Desc", req.NsID).Scan(&tasks) + l.svcCtx.DbEngin.Raw("select * from task t where t.`ns_id` = ? AND t.`deleted_at` IS NULL ORDER BY t.created_time Desc", req.NsID).Scan(&tasks) for _, task := range tasks { //调用p端接口查询应用状态 running、creating、waiting、error、pause data, err := l.svcCtx.K8sRpc.GetAppByAppName(context.Background(), &kubernetes.DeploymentDetailReq{ @@ -93,15 +92,22 @@ func (l *AppListLogic) AppList(req *types.AppListReq) (resp *types.AppListResp, } } } + var details []types.AppLocation + sql := + `select phy.id as participant_id, phy.name as participant_name, c.kind + from cloud c + join sc_participant_phy_info phy on c.participant_id = phy.id + WHERE c.kind in ('Deployment', 'StatefulSet') + and task_id = ?` + l.svcCtx.DbEngin.Raw(sql, task.Id).Scan(&details) resp.Apps = append(resp.Apps, types.App{ - Id: task.Id, - Name: task.Name, - Status: status, - CreateTime: task.CommitTime.Format("2006-01-02 15:04:05"), - ParticipantId: strconv.FormatInt(task.PId, 10), - ParticipantName: task.PName, - MinReplicas: minReplicas, - MaxReplicas: maxReplicas, + Id: task.Id, + Name: task.Name, + Status: status, + CreateTime: task.CommitTime.Format("2006-01-02 15:04:05"), + MinReplicas: minReplicas, + MaxReplicas: maxReplicas, + AppLocations: details, }) } return diff --git a/api/internal/logic/core/commitvmtasklogic.go b/api/internal/logic/core/commitvmtasklogic.go new file mode 100644 index 00000000..64d2b587 --- /dev/null +++ b/api/internal/logic/core/commitvmtasklogic.go @@ -0,0 +1,30 @@ +package core + +import ( + "context" + + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type CommitVmTaskLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewCommitVmTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommitVmTaskLogic { + return &CommitVmTaskLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *CommitVmTaskLogic) CommitVmTask(req *types.CommitVmTaskReq) (resp *types.CommitVmTaskResp, err error) { + // todo: add your logic here and delete this line + + return +} diff --git a/api/internal/types/types.go b/api/internal/types/types.go index 540e0edb..91420796 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -208,6 +208,40 @@ type CommitHpcTaskResp struct { TaskId int64 `json:"taskId"` } +type CommitVmTaskReq struct { + Server ServerCommit `json:"server,optional"` + Platform string `json:"platform,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 CommitVmTaskResp struct { +} + type ScheduleTaskByYamlResp struct { TaskId int64 `json:"taskId"` } @@ -581,14 +615,19 @@ type Replica struct { } type App struct { - Id int64 `json:"id,optional"` - Name string `json:"name,optional"` - Status string `json:"status,optional"` - ParticipantId string `json:"participantId,optional"` - ParticipantName string `json:"participantName,optional"` - CreateTime string `json:"createTime,optional"` - MinReplicas string `json:"minReplicas,optional"` - MaxReplicas string `json:"maxReplicas,optional"` + Id int64 `json:"id,optional" db:"id"` + Name string `json:"name,optional"` + Status string `json:"status,optional"` + CreateTime string `json:"createTime,optional"` + MinReplicas string `json:"minReplicas,optional"` + MaxReplicas string `json:"maxReplicas,optional"` + AppLocations []AppLocation `json:"appLocations,optional"` +} + +type AppLocation struct { + ParticipantId string `json:"participantId,optional" db:"participant_id"` + ParticipantName string `json:"participantName,optional" db:"participant_name"` + Kind string `json:"kind,optional" db:"kind"` } type AppDetailReq struct {