-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add deploy plan API supported (#400)
- Loading branch information
1 parent
06068cd
commit 56b1154
Showing
10 changed files
with
445 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
Oops, something went wrong.