Skip to content

Commit

Permalink
[part2] Complated plan's node and config APIs (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyingjunz authored Jun 1, 2024
1 parent 56b1154 commit 645f826
Show file tree
Hide file tree
Showing 15 changed files with 963 additions and 50 deletions.
108 changes: 108 additions & 0 deletions api/server/router/plan/config_routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright 2021 The Pixiu Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package plan

import (
"github.com/gin-gonic/gin"

"github.com/caoyingjunz/pixiu/api/server/httputils"
"github.com/caoyingjunz/pixiu/pkg/types"
)

type planConfigMeta struct {
planMeta

ConfigId int64 `uri:"configId" binding:"required"`
}

func (t *planRouter) createPlanConfig(c *gin.Context) {
r := httputils.NewResponse()

var (
opt planMeta
req types.CreatePlanConfigRequest
err error
)
if err = httputils.ShouldBindAny(c, &req, &opt, nil); err != nil {
httputils.SetFailed(c, r, err)
return
}
if err = t.c.Plan().CreateConfig(c, opt.PlanId, &req); err != nil {
httputils.SetFailed(c, r, err)
return
}

httputils.SetSuccess(c, r)
}

func (t *planRouter) updatePlanConfig(c *gin.Context) {
r := httputils.NewResponse()

var (
opt planConfigMeta
req types.UpdatePlanConfigRequest
err error
)
if err = httputils.ShouldBindAny(c, &req, &opt, nil); err != nil {
httputils.SetFailed(c, r, err)
return
}
if err = t.c.Plan().UpdateConfig(c, opt.PlanId, opt.ConfigId, &req); err != nil {
httputils.SetFailed(c, r, err)
return
}

httputils.SetSuccess(c, r)
}

func (t *planRouter) deletePlanConfig(c *gin.Context) {
r := httputils.NewResponse()

var (
opt planConfigMeta
err error
)
if err = httputils.ShouldBindAny(c, nil, &opt, nil); err != nil {
httputils.SetFailed(c, r, err)
return
}
if err = t.c.Plan().DeleteConfig(c, opt.PlanId, opt.ConfigId); err != nil {
httputils.SetFailed(c, r, err)
return
}

httputils.SetSuccess(c, r)
}

func (t *planRouter) getPlanConfig(c *gin.Context) {
r := httputils.NewResponse()

var (
opt planConfigMeta
err error
)
if err = httputils.ShouldBindAny(c, nil, &opt, nil); err != nil {
httputils.SetFailed(c, r, err)
return
}
if r.Result, err = t.c.Plan().GetConfig(c, opt.PlanId, opt.ConfigId); err != nil {
httputils.SetFailed(c, r, err)
return
}

httputils.SetSuccess(c, r)
}
127 changes: 127 additions & 0 deletions api/server/router/plan/node_routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
Copyright 2021 The Pixiu Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package plan

import (
"github.com/gin-gonic/gin"

"github.com/caoyingjunz/pixiu/api/server/httputils"
"github.com/caoyingjunz/pixiu/pkg/types"
)

type planNodeMeta struct {
planMeta `json:",inline"`

NodeId int64 `uri:"nodeId" binding:"required"`
}

func (t *planRouter) createPlanNode(c *gin.Context) {
r := httputils.NewResponse()

var (
opt planMeta
req types.CreatePlanNodeRequest
err error
)
if err = httputils.ShouldBindAny(c, &req, &opt, nil); err != nil {
httputils.SetFailed(c, r, err)
return
}
if err = t.c.Plan().CreateNode(c, opt.PlanId, &req); err != nil {
httputils.SetFailed(c, r, err)
return
}

httputils.SetSuccess(c, r)
}

func (t *planRouter) updatePlanNode(c *gin.Context) {
r := httputils.NewResponse()

var (
opt planNodeMeta
req types.UpdatePlanNodeRequest
err error
)
if err = httputils.ShouldBindAny(c, &req, &opt, nil); err != nil {
httputils.SetFailed(c, r, err)
return
}
if err = t.c.Plan().UpdateNode(c, opt.PlanId, opt.NodeId, &req); err != nil {
httputils.SetFailed(c, r, err)
return
}

httputils.SetSuccess(c, r)
}

func (t *planRouter) deletePlanNode(c *gin.Context) {
r := httputils.NewResponse()

var (
opt planNodeMeta
err error
)
if err = httputils.ShouldBindAny(c, nil, &opt, nil); err != nil {
httputils.SetFailed(c, r, err)
return
}
if err = t.c.Plan().DeleteNode(c, opt.PlanId, opt.NodeId); err != nil {
httputils.SetFailed(c, r, err)
return
}

httputils.SetSuccess(c, r)
}

func (t *planRouter) getPlanNode(c *gin.Context) {
r := httputils.NewResponse()

var (
opt planNodeMeta
err error
)
if err = httputils.ShouldBindAny(c, nil, &opt, nil); err != nil {
httputils.SetFailed(c, r, err)
return
}
if r.Result, err = t.c.Plan().GetNode(c, opt.PlanId, opt.NodeId); err != nil {
httputils.SetFailed(c, r, err)
return
}

httputils.SetSuccess(c, r)
}

func (t *planRouter) listPlanNodes(c *gin.Context) {
r := httputils.NewResponse()

var (
opt planMeta
err error
)
if err = httputils.ShouldBindAny(c, nil, &opt, nil); err != nil {
httputils.SetFailed(c, r, err)
return
}
if r.Result, err = t.c.Plan().ListNodes(c, opt.PlanId); err != nil {
httputils.SetFailed(c, r, err)
return
}

httputils.SetSuccess(c, r)
}
13 changes: 13 additions & 0 deletions api/server/router/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,18 @@ func (t *planRouter) initRoutes(ginEngine *gin.Engine) {
planRoute.DELETE("/:planId", t.deletePlan)
planRoute.GET("/:planId", t.getPlan)
planRoute.GET("", t.listPlans)

// 部署计划的节点API
planRoute.POST("/:planId/nodes", t.createPlanNode)
planRoute.PUT("/:planId/nodes/:nodeId", t.updatePlanNode)
planRoute.DELETE("/:planId/nodes/:nodeId", t.deletePlanNode)
planRoute.GET("/:planId/nodes/:nodeId", t.getPlanNode)
planRoute.GET("/:planId/nodes", t.listPlanNodes)

// 部署计划的部署配置
planRoute.POST("/:planId/configs", t.createPlanConfig)
planRoute.PUT("/:planId/configs/:configId", t.updatePlanConfig)
planRoute.DELETE("/:planId/configs/:configId", t.deletePlanConfig)
planRoute.GET("/:planId/configs/:configId", t.getPlanConfig)
}
}
41 changes: 35 additions & 6 deletions pkg/controller/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,46 @@ type Interface interface {
Delete(ctx context.Context, pid int64) error
Get(ctx context.Context, pid int64) (*types.Plan, error)
List(ctx context.Context) ([]types.Plan, error)

CreateNode(ctx context.Context, pid int64, req *types.CreatePlanNodeRequest) error
UpdateNode(ctx context.Context, pid int64, nodeId int64, req *types.UpdatePlanNodeRequest) error
DeleteNode(ctx context.Context, pid int64, nodeId int64) error
GetNode(ctx context.Context, pid int64, nodeId int64) (*types.PlanNode, error)
ListNodes(ctx context.Context, pid int64) ([]types.PlanNode, error)

CreateConfig(ctx context.Context, pid int64, req *types.CreatePlanConfigRequest) error
UpdateConfig(ctx context.Context, pid int64, cfgId int64, req *types.UpdatePlanConfigRequest) error
DeleteConfig(ctx context.Context, pid int64, cfgId int64) error
GetConfig(ctx context.Context, pid int64, cfgId int64) (*types.PlanConfig, error)
}

type plan struct {
cc config.Config
factory db.ShareDaoFactory
}

// Create
// 1. 创建部署计划
// 2. 创建部署任务
// 3. 创建部署配置
func (p *plan) Create(ctx context.Context, req *types.CreatePlanRequest) error {

object := &model.Plan{
Name: req.Name,
}

if _, err := p.factory.Plan().Create(ctx, object); err != nil {
object, err := p.factory.Plan().Create(ctx, &model.Plan{
Name: req.Name,
Description: req.Description,
})
if err != nil {
klog.Errorf("failed to create plan %s: %v", req.Name, err)
return errors.ErrServerInternal
}

// 初始化部署计划关联的任务
if err = p.createPlanTask(ctx, object.Id, model.UnStartedPlanStep); err != nil {
_ = p.Delete(ctx, object.Id)
klog.Errorf("failed to create plan task: %v", err)
return err
}

// TODO: 创建部署配置
return nil
}

Expand All @@ -69,13 +91,20 @@ func (p *plan) Update(ctx context.Context, pid int64, req *types.UpdatePlanReque
return nil
}

// Delete
// TODO: 删除前校验
func (p *plan) Delete(ctx context.Context, pid int64) error {
_, err := p.factory.Plan().Delete(ctx, pid)
if err != nil {
klog.Errorf("failed to delete plan %d: %v", pid, err)
return errors.ErrServerInternal
}

// 删除部署计划后,同步删除任务,删除任务失败时,可直接忽略
err = p.deletePlanTask(ctx, pid)
if err != nil {
klog.Errorf("failed to delete plan(%d) task: %v", pid, err)
}
return nil
}

Expand Down
Loading

0 comments on commit 645f826

Please sign in to comment.