Added CreateCronWorkflow function to server.

This commit is contained in:
Aleksandr Melnikov
2020-03-25 11:08:40 -07:00
parent 8719a1ee2a
commit f92964937e

View File

@@ -1,8 +1,11 @@
package server
import (
"github.com/onepanelio/core/api"
"context"
"github.com/onepanelio/core/api"
v1 "github.com/onepanelio/core/pkg"
"github.com/onepanelio/core/pkg/util/ptr"
"github.com/onepanelio/core/server/auth"
)
type CronWorkflowServer struct{}
@@ -11,8 +14,6 @@ func NewCronWorkflowServer() *CronWorkflowServer {
return &CronWorkflowServer{}
}
func (c CronWorkflowServer) CreateCronWorkflow(ctx context.Context, req *api.CreateWorkflowRequest) (*api.CronWorkflow, error) {
panic("implement me")
func apiCronWorkflow(cwf *v1.CronWorkflow) (cronWorkflow *api.CronWorkflow) {
cronWorkflow = &api.CronWorkflow{
Schedule: cwf.Schedule,
@@ -26,4 +27,36 @@ func apiCronWorkflow(cwf *v1.CronWorkflow) (cronWorkflow *api.CronWorkflow) {
}
return
}
func (c *CronWorkflowServer) CreateCronWorkflow(ctx context.Context, req *api.CreateWorkflowRequest) (*api.CronWorkflow, error) {
client := ctx.Value("kubeClient").(*v1.Client)
allowed, err := auth.IsAuthorized(client, req.Namespace, "create", "argoproj.io", "cronworkflows", "")
if err != nil || !allowed {
return nil, err
}
workflow := &v1.WorkflowExecution{
WorkflowTemplate: &v1.WorkflowTemplate{
UID: req.CronWorkflow.WorkflowExecution.WorkflowTemplate.Uid,
Version: req.CronWorkflow.WorkflowExecution.WorkflowTemplate.Version,
},
}
for _, param := range req.CronWorkflow.WorkflowExecution.Parameters {
workflow.Parameters = append(workflow.Parameters, v1.WorkflowExecutionParameter{
Name: param.Name,
Value: ptr.String(param.Value),
})
}
cronWorkflow := &v1.CronWorkflow{
WorkflowExecution: workflow,
}
cwf, err := client.CreateCronWorkflow(req.Namespace, cronWorkflow)
if err != nil {
if err != nil {
return nil, err
}
}
return apiCronWorkflow(cwf), nil
}