diff --git a/api/desc/core/pcm-core.api b/api/desc/core/pcm-core.api index 771ec7e0..5d3a2c6f 100644 --- a/api/desc/core/pcm-core.api +++ b/api/desc/core/pcm-core.api @@ -704,4 +704,96 @@ type ( Msg string `json:"msg,omitempty"` Data interface{} `json:"data,omitempty"` } -) \ No newline at end of file +) + +type ( + AdapterReq { + Id string `json:"id,optional" db:"id"` + Name string `json:"name,optional"` + Type string `json:"type,optional"` + Nickname string `json:"nickname,optional"` + Version string `json:"version,optional"` + Server string `json:"server,optional"` + } + AdapterDelReq { + Id string `form:"id,optional" db:"id"` + } + AdapterInfo { + Id string `json:"id,omitempty" db:"id"` + Name string `json:"name,omitempty" db:"name"` + Type string `json:"type,omitempty" db:"type"` + Nickname string `json:"nickname,omitempty" db:"nickname"` + Version string `json:"version,omitempty" db:"version"` + Server string `json:"server,omitempty" db:"server"` + CreateTime string `json:"createTime,omitempty" db:"create_time" gorm:"autoCreateTime"` + } + AdapterResp { + Code int `json:"code,omitempty"` + Msg string `json:"msg,omitempty"` + Data AdapterInfo `json:"data,omitempty"` + } + AdapterListResp { + Code int `json:"code,omitempty"` + Msg string `json:"msg,omitempty"` + Data []AdapterInfo `json:"data,omitempty"` + } +) + +type ClusterReq { + Id string `json:"id,optional"` + AdapterId string `json:"adapterId,optional"` + Name string `json:"name,optional"` + Nickname string `json:"nickname,optional"` + Description string `json:"description,optional"` + Server string `json:"server,optional"` + MonitorServer string `json:"monitorServer,optional"` + Username string `json:"username,optional"` + Password string `json:"password,optional"` + Token string `json:"token,optional"` + Ak string `json:"ak,optional"` + Sk string `json:"sk,optional"` + Region string `json:"region,optional"` + ProjectId string `json:"projectId,optional"` + Version string `json:"version,optional"` + Label string `json:"label,optional"` + OwnerId string `json:"ownerId,omitempty,optional"` + AuthType string `json:"authType,optional"` +} + +type ClusterDelReq { + Id string `form:"id,optional"` +} + +type ClusterInfo { + Id string `json:"id,omitempty" db:"id"` + AdapterId string `json:"adapterId,omitempty" db:"adapter_id"` + Name string `json:"name,omitempty" db:"name"` + Nickname string `json:"nickname,omitempty" db:"nickname"` + Description string `json:"description,omitempty" db:"description"` + Server string `json:"server,omitempty" db:"server"` + MonitorServer string `json:"monitorServer,omitempty" db:"monitor_server"` + Username string `json:"username,omitempty" db:"username"` + Password string `json:"password,omitempty" db:"password"` + Token string `json:"token,omitempty" db:"token"` + Ak string `json:"ak,omitempty" db:"ak"` + Sk string `json:"sk,omitempty" db:"sk"` + Region string `json:"region,omitempty" db:"region"` + ProjectId string `json:"projectId,omitempty" db:"project_id"` + Version string `json:"version,omitempty" db:"version"` + Label string `json:"label,omitempty" db:"label"` + OwnerId string `json:"ownerId,omitempty" db:"owner_id"` + AuthType string `json:"authType,omitempty" db:"auth_type"` + CreateTime string `json:"createTime,omitempty" db:"created_time" gorm:"autoCreateTime"` +} + +type ClusterResp { + Code int `json:"code,omitempty"` + Msg string `json:"msg,omitempty"` + Data ClusterInfo `json:"data,omitempty"` +} + +type ClusterListResp { + Code int `json:"code,omitempty"` + Msg string `json:"msg,omitempty"` + Data []ClusterInfo `json:"data,omitempty"` +} \ No newline at end of file diff --git a/api/desc/pcm.api b/api/desc/pcm.api index 2e1b3817..1f264fe9 100644 --- a/api/desc/pcm.api +++ b/api/desc/pcm.api @@ -570,4 +570,42 @@ service pcm { @doc "启动应用" @handler StartAppByAppName put /apps/startApp (DeleteAppReq) returns (AppResp) +} + +// 接口 +@server( + prefix: pcm/v1 + group : adapters +) + +service pcm { + @handler AdaptersListHandler + get /adapter/list (AdapterReq) returns (AdapterListResp) + + @handler CreateAdapterHandler + post /adapter/create (AdapterReq) returns (AdapterResp) + + @handler UpdateAdapterHandler + put /adapter/update (AdapterReq) returns (AdapterResp) + + @handler DeleteAdapterHandler + delete /adapter/delete (AdapterDelReq) returns (AdapterResp) + + @handler GetAdapterHandler + get /adapter/get (AdapterDelReq) returns (AdapterResp) + + @handler ClusterListHandler + get /adapter/cluster/list (ClusterReq) returns (ClusterListResp) + + @handler CreateClusterHandler + post /adapter/cluster/create (ClusterReq) returns (ClusterResp) + + @handler UpdateClusterHandler + put /adapter/cluster/update (ClusterReq) returns (ClusterResp) + + @handler DeleteClusterHandler + delete /adapter/cluster/delete (ClusterDelReq) returns (ClusterResp) + + @handler GetClusterHandler + get /adapter/cluster/get (ClusterDelReq) returns (ClusterResp) } \ No newline at end of file diff --git a/api/internal/handler/adapters/adapterslisthandler.go b/api/internal/handler/adapters/adapterslisthandler.go new file mode 100644 index 00000000..b2b17e5e --- /dev/null +++ b/api/internal/handler/adapters/adapterslisthandler.go @@ -0,0 +1,24 @@ +package adapters + +import ( + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/adapters" + "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 AdaptersListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.AdapterReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + + l := adapters.NewAdaptersListLogic(r.Context(), svcCtx) + resp, err := l.AdaptersList(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/adapters/clusterlisthandler.go b/api/internal/handler/adapters/clusterlisthandler.go new file mode 100644 index 00000000..c2612fa8 --- /dev/null +++ b/api/internal/handler/adapters/clusterlisthandler.go @@ -0,0 +1,24 @@ +package adapters + +import ( + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/adapters" + "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 ClusterListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.ClusterReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + + l := adapters.NewClusterListLogic(r.Context(), svcCtx) + resp, err := l.ClusterList(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/adapters/createadapterhandler.go b/api/internal/handler/adapters/createadapterhandler.go new file mode 100644 index 00000000..e99d001e --- /dev/null +++ b/api/internal/handler/adapters/createadapterhandler.go @@ -0,0 +1,24 @@ +package adapters + +import ( + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/adapters" + "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 CreateAdapterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.AdapterReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + + l := adapters.NewCreateAdapterLogic(r.Context(), svcCtx) + resp, err := l.CreateAdapter(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/adapters/createclusterhandler.go b/api/internal/handler/adapters/createclusterhandler.go new file mode 100644 index 00000000..7938a705 --- /dev/null +++ b/api/internal/handler/adapters/createclusterhandler.go @@ -0,0 +1,24 @@ +package adapters + +import ( + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/adapters" + "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 CreateClusterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.ClusterReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + + l := adapters.NewCreateClusterLogic(r.Context(), svcCtx) + resp, err := l.CreateCluster(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/adapters/deleteadapterhandler.go b/api/internal/handler/adapters/deleteadapterhandler.go new file mode 100644 index 00000000..958ef721 --- /dev/null +++ b/api/internal/handler/adapters/deleteadapterhandler.go @@ -0,0 +1,24 @@ +package adapters + +import ( + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/adapters" + "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 DeleteAdapterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.AdapterDelReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + + l := adapters.NewDeleteAdapterLogic(r.Context(), svcCtx) + resp, err := l.DeleteAdapter(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/adapters/deleteclusterhandler.go b/api/internal/handler/adapters/deleteclusterhandler.go new file mode 100644 index 00000000..643d822e --- /dev/null +++ b/api/internal/handler/adapters/deleteclusterhandler.go @@ -0,0 +1,24 @@ +package adapters + +import ( + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/adapters" + "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 DeleteClusterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.ClusterDelReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + + l := adapters.NewDeleteClusterLogic(r.Context(), svcCtx) + resp, err := l.DeleteCluster(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/adapters/getadapterhandler.go b/api/internal/handler/adapters/getadapterhandler.go new file mode 100644 index 00000000..9e8274f0 --- /dev/null +++ b/api/internal/handler/adapters/getadapterhandler.go @@ -0,0 +1,24 @@ +package adapters + +import ( + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/adapters" + "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 GetAdapterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.AdapterDelReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + + l := adapters.NewGetAdapterLogic(r.Context(), svcCtx) + resp, err := l.GetAdapter(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/adapters/getclusterhandler.go b/api/internal/handler/adapters/getclusterhandler.go new file mode 100644 index 00000000..c1f271e4 --- /dev/null +++ b/api/internal/handler/adapters/getclusterhandler.go @@ -0,0 +1,24 @@ +package adapters + +import ( + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/adapters" + "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 GetClusterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.ClusterDelReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + + l := adapters.NewGetClusterLogic(r.Context(), svcCtx) + resp, err := l.GetCluster(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/adapters/updateadapterhandler.go b/api/internal/handler/adapters/updateadapterhandler.go new file mode 100644 index 00000000..9e991b8a --- /dev/null +++ b/api/internal/handler/adapters/updateadapterhandler.go @@ -0,0 +1,24 @@ +package adapters + +import ( + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/adapters" + "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 UpdateAdapterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.AdapterReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + + l := adapters.NewUpdateAdapterLogic(r.Context(), svcCtx) + resp, err := l.UpdateAdapter(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/adapters/updateclusterhandler.go b/api/internal/handler/adapters/updateclusterhandler.go new file mode 100644 index 00000000..90320e23 --- /dev/null +++ b/api/internal/handler/adapters/updateclusterhandler.go @@ -0,0 +1,24 @@ +package adapters + +import ( + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/adapters" + "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 UpdateClusterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.ClusterReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + + l := adapters.NewUpdateClusterLogic(r.Context(), svcCtx) + resp, err := l.UpdateCluster(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/routes.go b/api/internal/handler/routes.go index 67994398..bdda439d 100644 --- a/api/internal/handler/routes.go +++ b/api/internal/handler/routes.go @@ -4,6 +4,7 @@ package handler import ( "net/http" + adapters "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/handler/adapters" ai "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/handler/ai" apps "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/handler/apps" cloud "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/handler/cloud" @@ -692,4 +693,60 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { }, rest.WithPrefix("/pcm/v1"), ) + + server.AddRoutes( + []rest.Route{ + { + Method: http.MethodGet, + Path: "/adapter/list", + Handler: adapters.AdaptersListHandler(serverCtx), + }, + { + Method: http.MethodPost, + Path: "/adapter/create", + Handler: adapters.CreateAdapterHandler(serverCtx), + }, + { + Method: http.MethodPut, + Path: "/adapter/update", + Handler: adapters.UpdateAdapterHandler(serverCtx), + }, + { + Method: http.MethodDelete, + Path: "/adapter/delete", + Handler: adapters.DeleteAdapterHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/adapter/get", + Handler: adapters.GetAdapterHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/adapter/cluster/list", + Handler: adapters.ClusterListHandler(serverCtx), + }, + { + Method: http.MethodPost, + Path: "/adapter/cluster/create", + Handler: adapters.CreateClusterHandler(serverCtx), + }, + { + Method: http.MethodPut, + Path: "/adapter/cluster/update", + Handler: adapters.UpdateClusterHandler(serverCtx), + }, + { + Method: http.MethodDelete, + Path: "/adapter/cluster/delete", + Handler: adapters.DeleteClusterHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/adapter/cluster/get", + Handler: adapters.GetClusterHandler(serverCtx), + }, + }, + rest.WithPrefix("/pcm/v1"), + ) } diff --git a/api/internal/logic/adapters/adapterslistlogic.go b/api/internal/logic/adapters/adapterslistlogic.go new file mode 100644 index 00000000..6a11b7c3 --- /dev/null +++ b/api/internal/logic/adapters/adapterslistlogic.go @@ -0,0 +1,34 @@ +package adapters + +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 AdaptersListLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewAdaptersListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdaptersListLogic { + return &AdaptersListLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *AdaptersListLogic) AdaptersList(req *types.AdapterReq) (resp *types.AdapterListResp, err error) { + resp = &types.AdapterListResp{} + tx := l.svcCtx.DbEngin.Raw("select * from t_adapter where `deleted_at` IS NULL ORDER BY create_time Desc").Scan(&resp.Data) + if tx.Error != nil { + logx.Errorf(tx.Error.Error()) + return nil, tx.Error + } + return resp, nil +} diff --git a/api/internal/logic/adapters/clusterlistlogic.go b/api/internal/logic/adapters/clusterlistlogic.go new file mode 100644 index 00000000..b97c087d --- /dev/null +++ b/api/internal/logic/adapters/clusterlistlogic.go @@ -0,0 +1,34 @@ +package adapters + +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 ClusterListLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewClusterListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ClusterListLogic { + return &ClusterListLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *ClusterListLogic) ClusterList(req *types.ClusterReq) (resp *types.ClusterListResp, err error) { + resp = &types.ClusterListResp{} + tx := l.svcCtx.DbEngin.Raw("select * from t_cluster where `deleted_at` IS NULL ORDER BY create_time Desc").Scan(&resp.Data) + if tx.Error != nil { + logx.Errorf(tx.Error.Error()) + return nil, tx.Error + } + return resp, nil +} diff --git a/api/internal/logic/adapters/createadapterlogic.go b/api/internal/logic/adapters/createadapterlogic.go new file mode 100644 index 00000000..38b12cec --- /dev/null +++ b/api/internal/logic/adapters/createadapterlogic.go @@ -0,0 +1,35 @@ +package adapters + +import ( + "context" + "fmt" + "github.com/zeromicro/go-zero/core/logx" + "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" + "time" +) + +type CreateAdapterLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewCreateAdapterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateAdapterLogic { + return &CreateAdapterLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *CreateAdapterLogic) CreateAdapter(req *types.AdapterReq) (resp *types.AdapterResp, err error) { + adapter := types.AdapterInfo{} + utils.Convert(req, &adapter) + adapter.Id = utils.GenSnowflakeIDStr() + adapter.CreateTime = time.Now().Format("2006-01-02 15:04:05") + tx := l.svcCtx.DbEngin.Table("t_adapter").Create(&adapter) + fmt.Print(tx.Error) + return +} diff --git a/api/internal/logic/adapters/createclusterlogic.go b/api/internal/logic/adapters/createclusterlogic.go new file mode 100644 index 00000000..60f84a57 --- /dev/null +++ b/api/internal/logic/adapters/createclusterlogic.go @@ -0,0 +1,41 @@ +package adapters + +import ( + "context" + "errors" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils" + "time" + + "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 CreateClusterLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewCreateClusterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateClusterLogic { + return &CreateClusterLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *CreateClusterLogic) CreateCluster(req *types.ClusterReq) (resp *types.ClusterResp, err error) { + cluster := types.ClusterInfo{} + utils.Convert(req, &cluster) + cluster.Id = utils.GenSnowflakeIDStr() + cluster.CreateTime = time.Now().Format("2006-01-02 15:04:05") + cluster.OwnerId = "0" + tx := l.svcCtx.DbEngin.Table("t_cluster").Create(&cluster) + if tx.Error != nil { + logx.Errorf(tx.Error.Error()) + return nil, errors.New("cluster create failed") + } + return +} diff --git a/api/internal/logic/adapters/deleteadapterlogic.go b/api/internal/logic/adapters/deleteadapterlogic.go new file mode 100644 index 00000000..f3030bce --- /dev/null +++ b/api/internal/logic/adapters/deleteadapterlogic.go @@ -0,0 +1,44 @@ +package adapters + +import ( + "context" + "github.com/pkg/errors" + + "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 DeleteAdapterLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewDeleteAdapterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteAdapterLogic { + return &DeleteAdapterLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *DeleteAdapterLogic) DeleteAdapter(req *types.AdapterDelReq) (resp *types.AdapterResp, err error) { + var sId int64 + l.svcCtx.DbEngin.Table("t_adapter").Raw("select a.id from t_cluster c left join t_adapter a on c.adapter_id=a.id where a.id = ? ", req.Id).Scan(&sId) + if sId != 0 { + return nil, errors.New("Delete failed,The adapter is associated with a cluster") + } + db := l.svcCtx.DbEngin.Table("t_adapter").Where("id = ?", req.Id).First(&types.AdapterInfo{}) + if db.Error != nil { + logx.Errorf("err %v", db.Error.Error()) + return nil, errors.New("Adapter does not exist") + } + tx := l.svcCtx.DbEngin.Table("t_adapter").Delete(types.AdapterInfo{}, req.Id) + if tx.Error != nil { + logx.Errorf("err %v", db.Error.Error()) + return nil, errors.New("Delete adapter failed") + } + return +} diff --git a/api/internal/logic/adapters/deleteclusterlogic.go b/api/internal/logic/adapters/deleteclusterlogic.go new file mode 100644 index 00000000..f2b75209 --- /dev/null +++ b/api/internal/logic/adapters/deleteclusterlogic.go @@ -0,0 +1,39 @@ +package adapters + +import ( + "context" + "github.com/pkg/errors" + + "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 DeleteClusterLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewDeleteClusterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteClusterLogic { + return &DeleteClusterLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *DeleteClusterLogic) DeleteCluster(req *types.ClusterDelReq) (resp *types.ClusterResp, err error) { + db := l.svcCtx.DbEngin.Table("t_cluster").Where("id = ?", req.Id).First(&types.ClusterInfo{}) + if db.Error != nil { + logx.Errorf("err %v", db.Error.Error()) + return nil, errors.New("Cluster does not exist") + } + tx := l.svcCtx.DbEngin.Table("t_cluster").Delete(types.AdapterInfo{}, req.Id) + if tx.Error != nil { + logx.Errorf("err %v", db.Error.Error()) + return nil, errors.New("Delete Cluster failed") + } + return +} diff --git a/api/internal/logic/adapters/getadapterlogic.go b/api/internal/logic/adapters/getadapterlogic.go new file mode 100644 index 00000000..06aef43d --- /dev/null +++ b/api/internal/logic/adapters/getadapterlogic.go @@ -0,0 +1,35 @@ +package adapters + +import ( + "context" + "github.com/pkg/errors" + + "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 GetAdapterLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetAdapterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAdapterLogic { + return &GetAdapterLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetAdapterLogic) GetAdapter(req *types.AdapterDelReq) (resp *types.AdapterResp, err error) { + resp = &types.AdapterResp{} + db := l.svcCtx.DbEngin.Table("t_adapter").Where("id = ?", req.Id).First(&resp.Data) + if db.Error != nil { + logx.Errorf("err %v", db.Error.Error()) + return nil, errors.New("Adapter does not exist") + } + return +} diff --git a/api/internal/logic/adapters/getclusterlogic.go b/api/internal/logic/adapters/getclusterlogic.go new file mode 100644 index 00000000..bf554ed0 --- /dev/null +++ b/api/internal/logic/adapters/getclusterlogic.go @@ -0,0 +1,36 @@ +package adapters + +import ( + "context" + "github.com/pkg/errors" + + "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 GetClusterLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetClusterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetClusterLogic { + return &GetClusterLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetClusterLogic) GetCluster(req *types.ClusterDelReq) (resp *types.ClusterResp, err error) { + resp = &types.ClusterResp{} + db := l.svcCtx.DbEngin.Table("t_cluster").Where("id = ?", req.Id).First(&resp.Data) + if db.Error != nil { + logx.Errorf("err %v", db.Error.Error()) + return nil, errors.New("Adapter does not exist") + } + + return +} diff --git a/api/internal/logic/adapters/updateadapterlogic.go b/api/internal/logic/adapters/updateadapterlogic.go new file mode 100644 index 00000000..511a8be7 --- /dev/null +++ b/api/internal/logic/adapters/updateadapterlogic.go @@ -0,0 +1,30 @@ +package adapters + +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 UpdateAdapterLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUpdateAdapterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAdapterLogic { + return &UpdateAdapterLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *UpdateAdapterLogic) UpdateAdapter(req *types.AdapterReq) (resp *types.AdapterResp, err error) { + // todo: add your logic here and delete this line + + return +} diff --git a/api/internal/logic/adapters/updateclusterlogic.go b/api/internal/logic/adapters/updateclusterlogic.go new file mode 100644 index 00000000..2bab3ce2 --- /dev/null +++ b/api/internal/logic/adapters/updateclusterlogic.go @@ -0,0 +1,30 @@ +package adapters + +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 UpdateClusterLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUpdateClusterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateClusterLogic { + return &UpdateClusterLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *UpdateClusterLogic) UpdateCluster(req *types.ClusterReq) (resp *types.ClusterResp, 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 91420796..ad9e8536 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -668,6 +668,100 @@ type AppResp struct { Data interface{} `json:"data,omitempty"` } +type AdapterReq struct { + Id string `json:"id,optional" db:"id"` + Name string `json:"name,optional"` + Type string `json:"type,optional"` + Nickname string `json:"nickname,optional"` + Version string `json:"version,optional"` + Server string `json:"server,optional"` +} + +type AdapterDelReq struct { + Id string `form:"id,optional" db:"id"` +} + +type AdapterInfo struct { + Id string `json:"id,omitempty" db:"id"` + Name string `json:"name,omitempty" db:"name"` + Type string `json:"type,omitempty" db:"type"` + Nickname string `json:"nickname,omitempty" db:"nickname"` + Version string `json:"version,omitempty" db:"version"` + Server string `json:"server,omitempty" db:"server"` + CreateTime string `json:"createTime,omitempty" db:"create_time" gorm:"autoCreateTime"` +} + +type AdapterResp struct { + Code int `json:"code,omitempty"` + Msg string `json:"msg,omitempty"` + Data AdapterInfo `json:"data,omitempty"` +} + +type AdapterListResp struct { + Code int `json:"code,omitempty"` + Msg string `json:"msg,omitempty"` + Data []AdapterInfo `json:"data,omitempty"` +} + +type ClusterReq struct { + Id string `json:"id,optional"` + AdapterId string `json:"adapterId,optional"` + Name string `json:"name,optional"` + Nickname string `json:"nickname,optional"` + Description string `json:"description,optional"` + Server string `json:"server,optional"` + MonitorServer string `json:"monitorServer,optional"` + Username string `json:"username,optional"` + Password string `json:"password,optional"` + Token string `json:"token,optional"` + Ak string `json:"ak,optional"` + Sk string `json:"sk,optional"` + Region string `json:"region,optional"` + ProjectId string `json:"projectId,optional"` + Version string `json:"version,optional"` + Label string `json:"label,optional"` + OwnerId string `json:"ownerId,omitempty,optional"` + AuthType string `json:"authType,optional"` +} + +type ClusterDelReq struct { + Id string `form:"id,optional"` +} + +type ClusterInfo struct { + Id string `json:"id,omitempty" db:"id"` + AdapterId string `json:"adapterId,omitempty" db:"adapter_id"` + Name string `json:"name,omitempty" db:"name"` + Nickname string `json:"nickname,omitempty" db:"nickname"` + Description string `json:"description,omitempty" db:"description"` + Server string `json:"server,omitempty" db:"server"` + MonitorServer string `json:"monitorServer,omitempty" db:"monitor_server"` + Username string `json:"username,omitempty" db:"username"` + Password string `json:"password,omitempty" db:"password"` + Token string `json:"token,omitempty" db:"token"` + Ak string `json:"ak,omitempty" db:"ak"` + Sk string `json:"sk,omitempty" db:"sk"` + Region string `json:"region,omitempty" db:"region"` + ProjectId string `json:"projectId,omitempty" db:"project_id"` + Version string `json:"version,omitempty" db:"version"` + Label string `json:"label,omitempty" db:"label"` + OwnerId string `json:"ownerId,omitempty" db:"owner_id"` + AuthType string `json:"authType,omitempty" db:"auth_type"` + CreateTime string `json:"createTime,omitempty" db:"created_time" gorm:"autoCreateTime"` +} + +type ClusterResp struct { + Code int `json:"code,omitempty"` + Msg string `json:"msg,omitempty"` + Data ClusterInfo `json:"data,omitempty"` +} + +type ClusterListResp struct { + Code int `json:"code,omitempty"` + Msg string `json:"msg,omitempty"` + Data []ClusterInfo `json:"data,omitempty"` +} + type Job struct { SlurmVersion string `json:"slurmVersion"` Name string `json:"name"` diff --git a/pkg/utils/snowflake.go b/pkg/utils/snowflake.go index fb727d43..9e677f3c 100644 --- a/pkg/utils/snowflake.go +++ b/pkg/utils/snowflake.go @@ -36,3 +36,8 @@ func InitSnowflake(machineID int64) (err error) { func GenSnowflakeID() int64 { return node.Generate().Int64() } + +// machineId 工作id +func GenSnowflakeIDStr() string { + return node.Generate().String() +}