Skip to content

Commit

Permalink
Add deploy plan API supported (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyingjunz authored May 30, 2024
1 parent 06068cd commit 56b1154
Show file tree
Hide file tree
Showing 10 changed files with 445 additions and 1 deletion.
46 changes: 46 additions & 0 deletions api/server/router/plan/plan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
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/cmd/app/options"
"github.com/caoyingjunz/pixiu/pkg/controller"
)

type planRouter struct {
c controller.PixiuInterface
}

func NewRouter(o *options.Options) {
router := &planRouter{
c: o.Controller,
}
router.initRoutes(o.HttpEngine)
}

func (t *planRouter) initRoutes(ginEngine *gin.Engine) {
planRoute := ginEngine.Group("/pixiu/plans")
{
planRoute.POST("", t.createPlan)
planRoute.PUT("/:planId", t.updatePlan)
planRoute.DELETE("/:planId", t.deletePlan)
planRoute.GET("/:planId", t.getPlan)
planRoute.GET("", t.listPlans)
}
}
114 changes: 114 additions & 0 deletions api/server/router/plan/plan_routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
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 planMeta struct {
PlanId int64 `uri:"planId" binding:"required"`
}

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

var req types.CreatePlanRequest
if err := c.ShouldBindJSON(&req); err != nil {
httputils.SetFailed(c, r, err)
return
}
if err := t.c.Plan().Create(c, &req); err != nil {
httputils.SetFailed(c, r, err)
return
}

httputils.SetSuccess(c, r)
}

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

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

httputils.SetSuccess(c, r)
}

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

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

httputils.SetSuccess(c, r)
}

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

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

httputils.SetSuccess(c, r)
}

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

var err error
if r.Result, err = t.c.Plan().List(c); err != nil {
httputils.SetFailed(c, r, err)
return
}

httputils.SetSuccess(c, r)
}
3 changes: 2 additions & 1 deletion api/server/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/caoyingjunz/pixiu/api/server/middleware"
"github.com/caoyingjunz/pixiu/api/server/router/cluster"
"github.com/caoyingjunz/pixiu/api/server/router/plan"
"github.com/caoyingjunz/pixiu/api/server/router/proxy"
"github.com/caoyingjunz/pixiu/api/server/router/tenant"
"github.com/caoyingjunz/pixiu/api/server/router/user"
Expand All @@ -39,7 +40,7 @@ type RegisterFunc func(o *options.Options)

func InstallRouters(o *options.Options) {
fs := []RegisterFunc{
middleware.InstallMiddlewares, cluster.NewRouter, proxy.NewRouter, tenant.NewRouter, user.NewRouter,
middleware.InstallMiddlewares, cluster.NewRouter, proxy.NewRouter, tenant.NewRouter, user.NewRouter, plan.NewRouter,
}

install(o, fs...)
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controller
import (
"github.com/caoyingjunz/pixiu/cmd/app/config"
"github.com/caoyingjunz/pixiu/pkg/controller/cluster"
"github.com/caoyingjunz/pixiu/pkg/controller/plan"
"github.com/caoyingjunz/pixiu/pkg/controller/tenant"
"github.com/caoyingjunz/pixiu/pkg/controller/user"
"github.com/caoyingjunz/pixiu/pkg/db"
Expand All @@ -28,6 +29,7 @@ type PixiuInterface interface {
cluster.ClusterGetter
tenant.TenantGetter
user.UserGetter
plan.PlanGetter
}

type pixiu struct {
Expand All @@ -38,6 +40,7 @@ type pixiu struct {
func (p *pixiu) Cluster() cluster.Interface { return cluster.NewCluster(p.cc, p.factory) }
func (p *pixiu) Tenant() tenant.Interface { return tenant.NewTenant(p.cc, p.factory) }
func (p *pixiu) User() user.Interface { return user.NewUser(p.cc, p.factory) }
func (p *pixiu) Plan() plan.Interface { return plan.NewPlan(p.cc, p.factory) }

func New(cfg config.Config, f db.ShareDaoFactory) PixiuInterface {
return &pixiu{
Expand Down
126 changes: 126 additions & 0 deletions pkg/controller/plan/plan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
Copyright 2021 The Pixiu Authors.
Licensed under the Apache License, Version 2.0 (phe "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 (
"context"

"k8s.io/klog/v2"

"github.com/caoyingjunz/pixiu/api/server/errors"
"github.com/caoyingjunz/pixiu/cmd/app/config"
"github.com/caoyingjunz/pixiu/pkg/db"
"github.com/caoyingjunz/pixiu/pkg/db/model"
"github.com/caoyingjunz/pixiu/pkg/types"
)

type PlanGetter interface {
Plan() Interface
}

type Interface interface {
Create(ctx context.Context, req *types.CreatePlanRequest) error
Update(ctx context.Context, pid int64, req *types.UpdatePlanRequest) error
Delete(ctx context.Context, pid int64) error
Get(ctx context.Context, pid int64) (*types.Plan, error)
List(ctx context.Context) ([]types.Plan, error)
}

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

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 {
klog.Errorf("failed to create plan %s: %v", req.Name, err)
return errors.ErrServerInternal
}

return nil
}

func (p *plan) Update(ctx context.Context, pid int64, req *types.UpdatePlanRequest) error {
updates := make(map[string]interface{})

if err := p.factory.Plan().Update(ctx, pid, req.ResourceVersion, updates); err != nil {
klog.Errorf("failed to update plan %d: %v", pid, err)
return errors.ErrServerInternal
}
return nil
}

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
}

return nil
}

func (p *plan) Get(ctx context.Context, pid int64) (*types.Plan, error) {
object, err := p.factory.Plan().Get(ctx, pid)
if err != nil {
klog.Errorf("failed to get plan %d: %v", pid, err)
return nil, errors.ErrServerInternal
}

return p.model2Type(object), nil
}

func (p *plan) List(ctx context.Context) ([]types.Plan, error) {
objects, err := p.factory.Plan().List(ctx)
if err != nil {
klog.Errorf("failed to get plans: %v", err)
return nil, errors.ErrServerInternal
}

var ps []types.Plan
for _, object := range objects {
ps = append(ps, *p.model2Type(&object))
}
return ps, nil
}

func (p *plan) model2Type(o *model.Plan) *types.Plan {
return &types.Plan{
PixiuMeta: types.PixiuMeta{
Id: o.Id,
ResourceVersion: o.ResourceVersion,
},
TimeMeta: types.TimeMeta{
GmtCreate: o.GmtCreate,
GmtModified: o.GmtModified,
},
Name: o.Name,
Description: o.Description,
}
}

func NewPlan(cfg config.Config, f db.ShareDaoFactory) *plan {
return &plan{
cc: cfg,
factory: f,
}
}
2 changes: 2 additions & 0 deletions pkg/db/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type ShareDaoFactory interface {
Cluster() ClusterInterface
Tenant() TenantInterface
User() UserInterface
Plan() PlanInterface
}

type shareDaoFactory struct {
Expand All @@ -33,6 +34,7 @@ type shareDaoFactory struct {
func (f *shareDaoFactory) Cluster() ClusterInterface { return newCluster(f.db) }
func (f *shareDaoFactory) Tenant() TenantInterface { return newTenant(f.db) }
func (f *shareDaoFactory) User() UserInterface { return newUser(f.db) }
func (f *shareDaoFactory) Plan() PlanInterface { return newPlan(f.db) }

func NewDaoFactory(db *gorm.DB, migrate bool) (ShareDaoFactory, error) {
if migrate {
Expand Down
34 changes: 34 additions & 0 deletions pkg/db/model/plan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2024 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 model

import "github.com/caoyingjunz/pixiu/pkg/db/model/pixiu"

func init() {
register(&Plan{})
}

type Plan struct {
pixiu.Model

Name string `gorm:"index:idx_name,unique" json:"name"`
Description string `gorm:"type:text" json:"description"`
}

func (plan *Plan) TableName() string {
return "plans"
}
Loading

0 comments on commit 56b1154

Please sign in to comment.