pcm-coordinator/api/internal/logic/apps/deleteappbyappnamelogic.go

79 lines
2.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}