增加删除app接口

Signed-off-by: devad <cossjie@foxmail.com>

Former-commit-id: f40f21e1420a89cd879d196ed8add71525e954e1
This commit is contained in:
devad 2024-01-02 20:03:25 +08:00
parent 277a1b3549
commit 13551239ff
15 changed files with 422 additions and 0 deletions

View File

@ -649,3 +649,16 @@ type (
data interface{} `json:"data"`
}
)
type (
DeleteAppReq {
Name string `form:"name"`
NsID string `form:"nsID"`
}
DeleteAppResp {
Code int `json:"code,omitempty"`
Msg string `json:"msg,omitempty"`
Data interface{} `json:"data,omitempty"`
}
)

View File

@ -546,4 +546,24 @@ service pcm {
@doc "获取应用详情"
@handler GetAppByAppName
get /apps/getAppByAppName/:appName (AppDetailReq) returns (AppTaskResp)
@doc "删除应用"
@handler DeleteAppByAppName
get /apps/deleteApp (DeleteAppReq) returns (DeleteAppResp)
@doc "更新应用"
@handler UpdateAppByAppName
get /apps/updateApp (DeleteAppReq) returns (AppTaskResp)
@doc "重启应用"
@handler RestartAppByAppName
get /apps/restartApp (DeleteAppReq) returns (AppTaskResp)
@doc "暂停应用"
@handler PauseAppByAppName
get /apps/pauseApp (DeleteAppReq) returns (AppTaskResp)
@doc "启动应用"
@handler StartAppByAppName
get /apps/startApp (DeleteAppReq) returns (AppTaskResp)
}

View File

@ -0,0 +1,28 @@
package apps
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/apps"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types"
)
func DeleteAppByAppNameHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.DeleteAppReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := apps.NewDeleteAppByAppNameLogic(r.Context(), svcCtx)
resp, err := l.DeleteAppByAppName(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

@ -0,0 +1,28 @@
package apps
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/apps"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types"
)
func PauseAppByAppNameHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.DeleteAppReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := apps.NewPauseAppByAppNameLogic(r.Context(), svcCtx)
resp, err := l.PauseAppByAppName(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

@ -0,0 +1,28 @@
package apps
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/apps"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types"
)
func RestartAppByAppNameHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.DeleteAppReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := apps.NewRestartAppByAppNameLogic(r.Context(), svcCtx)
resp, err := l.RestartAppByAppName(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

@ -0,0 +1,28 @@
package apps
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/apps"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types"
)
func StartAppByAppNameHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.DeleteAppReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := apps.NewStartAppByAppNameLogic(r.Context(), svcCtx)
resp, err := l.StartAppByAppName(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

@ -0,0 +1,28 @@
package apps
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/apps"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types"
)
func UpdateAppByAppNameHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.DeleteAppReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := apps.NewUpdateAppByAppNameLogic(r.Context(), svcCtx)
resp, err := l.UpdateAppByAppName(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

@ -659,6 +659,31 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/apps/getAppByAppName/:appName",
Handler: apps.GetAppByAppNameHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/apps/deleteApp",
Handler: apps.DeleteAppByAppNameHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/apps/updateApp",
Handler: apps.UpdateAppByAppNameHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/apps/restartApp",
Handler: apps.RestartAppByAppNameHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/apps/pauseApp",
Handler: apps.PauseAppByAppNameHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/apps/startApp",
Handler: apps.StartAppByAppNameHandler(serverCtx),
},
},
rest.WithPrefix("/pcm/v1"),
)

View File

@ -0,0 +1,78 @@
package apps
import (
"context"
"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/utils"
"gitlink.org.cn/jcce-pcm/pcm-participant-kubernetes/kubernetes"
"gorm.io/gorm"
"github.com/zeromicro/go-zero/core/logx"
)
type DeleteAppByAppNameLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewDeleteAppByAppNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteAppByAppNameLogic {
return &DeleteAppByAppNameLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
type Cloud struct {
Id int64 `db:"id"` // id
TaskId int64 `db:"task_id"` // 任务id
ParticipantId int64 `db:"participant_id"` // 集群静态信息id
ApiVersion string `db:"api_version"` //api版本
Name string `db:"name"` // 名称
Namespace string `db:"namespace"` // 命名空间
Kind string `db:"kind"` // 种类
Status string `db:"status"` // 状态
StartTime string `db:"start_time"` // 开始时间
RunningTime int64 `db:"running_time"` // 运行时长
DeletedAt gorm.DeletedAt `gorm:"index"`
YamlString string `db:"yaml_string"`
Result string `db:"result"` // 运行结果
NsID string `db:"ns_id"`
Replica int32 `db:"replica"`
}
func (l *DeleteAppByAppNameLogic) DeleteAppByAppName(req *types.DeleteAppReq) (resp *types.DeleteAppResp, err error) {
resp = &types.DeleteAppResp{}
var task = &Task{}
//查询应用的yamlString
l.svcCtx.DbEngin.Raw(`select * from task where ns_id= ? and name= ? `, req.NsID, req.Name).Scan(&task)
if task.Id == 0 {
resp.Code = 500
resp.Msg = "App not fount"
return resp, err
}
var clouds []Cloud
l.svcCtx.DbEngin.Raw(`select * from cloud where task_id= ? `, task.Id).Scan(&clouds)
for _, cloud := range clouds {
toYaml := utils.StringToYaml(cloud.YamlString)
//调用p端接口查询应用详情
_, err = l.svcCtx.K8sRpc.DeleteYaml(context.Background(), &kubernetes.ApplyReq{
YamlString: *toYaml,
})
}
if err != nil {
logx.Errorf("调用p端接口删除应用失败err:%v", err)
resp.Code = 500
resp.Msg = err.Error()
return resp, nil
}
//删除task跟cloud表数据
l.svcCtx.DbEngin.Delete(&Task{}, task.Id)
l.svcCtx.DbEngin.Delete(&Cloud{}, task.Id).Where("task_id = ?", task.Id)
resp.Code = 200
resp.Msg = "succeed"
return
}

View File

@ -0,0 +1,30 @@
package apps
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 PauseAppByAppNameLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewPauseAppByAppNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PauseAppByAppNameLogic {
return &PauseAppByAppNameLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *PauseAppByAppNameLogic) PauseAppByAppName(req *types.DeleteAppReq) (resp *types.AppTaskResp, err error) {
// todo: add your logic here and delete this line
return
}

View File

@ -0,0 +1,30 @@
package apps
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 RestartAppByAppNameLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewRestartAppByAppNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RestartAppByAppNameLogic {
return &RestartAppByAppNameLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *RestartAppByAppNameLogic) RestartAppByAppName(req *types.DeleteAppReq) (resp *types.AppTaskResp, err error) {
// todo: add your logic here and delete this line
return
}

View File

@ -0,0 +1,30 @@
package apps
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 StartAppByAppNameLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewStartAppByAppNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StartAppByAppNameLogic {
return &StartAppByAppNameLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *StartAppByAppNameLogic) StartAppByAppName(req *types.DeleteAppReq) (resp *types.AppTaskResp, err error) {
// todo: add your logic here and delete this line
return
}

View File

@ -0,0 +1,30 @@
package apps
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 UpdateAppByAppNameLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUpdateAppByAppNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAppByAppNameLogic {
return &UpdateAppByAppNameLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UpdateAppByAppNameLogic) UpdateAppByAppName(req *types.DeleteAppReq) (resp *types.AppTaskResp, err error) {
// todo: add your logic here and delete this line
return
}

View File

@ -612,6 +612,17 @@ type AppTaskResp struct {
Data interface{} `json:"data"`
}
type DeleteAppReq struct {
Name string `form:"name"`
NsID string `form:"nsID"`
}
type DeleteAppResp struct {
Code int `json:"code,omitempty"`
Msg string `json:"msg,omitempty"`
Data interface{} `json:"data,omitempty"`
}
type Job struct {
SlurmVersion string `json:"slurmVersion"`
Name string `json:"name"`

View File

@ -115,3 +115,18 @@ func ConvertStructToMap(in interface{}) map[string]string {
}
return out
}
func StringToYaml(in string) *string {
// 将Go字符串转换为字节数组
byteArray := []byte(in)
// 解析YAML数据
var yamlData map[string]interface{}
err := yaml.Unmarshal(byteArray, &yamlData)
// 将解析后的数据转换为YAML格式
yamlString, err := yaml.Marshal(yamlData)
if err != nil {
fmt.Println(err)
}
resp := string(yamlString)
return &resp
}