mirror of
https://github.com/kubevela/kubevela.git
synced 2026-05-07 01:46:54 +00:00
89 lines
2.5 KiB
Go
89 lines
2.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/oam-dev/kubevela/api/types"
|
|
"github.com/oam-dev/kubevela/pkg/utils/env"
|
|
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
"github.com/oam-dev/kubevela/pkg/server/apis"
|
|
"github.com/oam-dev/kubevela/pkg/server/util"
|
|
)
|
|
|
|
// environment related handlers
|
|
func CreateEnv(c *gin.Context) {
|
|
var environment apis.Environment
|
|
if err := c.ShouldBindJSON(&environment); err != nil {
|
|
util.HandleError(c, util.InvalidArgument, "the create environment request body is invalid")
|
|
return
|
|
}
|
|
ctrl.Log.Info("Get a create environment request", "env", environment)
|
|
name := environment.EnvName
|
|
namespace := environment.Namespace
|
|
if namespace == "" {
|
|
namespace = "default"
|
|
}
|
|
|
|
ctx := util.GetContext(c)
|
|
kubeClient := c.MustGet("KubeClient")
|
|
message, err := env.CreateEnv(ctx, kubeClient.(client.Client), name, &types.EnvMeta{
|
|
Name: name,
|
|
Current: environment.Current,
|
|
Namespace: namespace,
|
|
Email: environment.Email,
|
|
Domain: environment.Domain,
|
|
})
|
|
util.AssembleResponse(c, message, err)
|
|
}
|
|
|
|
func UpdateEnv(c *gin.Context) {
|
|
envName := c.Param("envName")
|
|
ctrl.Log.Info("Put a update environment request", "envName", envName)
|
|
var environmentBody apis.EnvironmentBody
|
|
if err := c.ShouldBindJSON(&environmentBody); err != nil {
|
|
util.HandleError(c, util.InvalidArgument, "the update environment request body is invalid")
|
|
return
|
|
}
|
|
ctx := util.GetContext(c)
|
|
kubeClient := c.MustGet("KubeClient")
|
|
message, err := env.UpdateEnv(ctx, kubeClient.(client.Client), envName, environmentBody.Namespace)
|
|
util.AssembleResponse(c, message, err)
|
|
}
|
|
|
|
func GetEnv(c *gin.Context) {
|
|
envName := c.Param("envName")
|
|
ctrl.Log.Info("Get a get environment request", "envName", envName)
|
|
envList, err := env.ListEnvs(envName)
|
|
|
|
environmentList := make([]apis.Environment, 0)
|
|
for _, envMeta := range envList {
|
|
environmentList = append(environmentList, apis.Environment{
|
|
EnvName: envMeta.Name,
|
|
Namespace: envMeta.Namespace,
|
|
Current: envMeta.Current,
|
|
})
|
|
}
|
|
util.AssembleResponse(c, environmentList, err)
|
|
}
|
|
|
|
func ListEnv(c *gin.Context) {
|
|
GetEnv(c)
|
|
}
|
|
|
|
func DeleteEnv(c *gin.Context) {
|
|
envName := c.Param("envName")
|
|
ctrl.Log.Info("Delete a delete environment request", "envName", envName)
|
|
msg, err := env.DeleteEnv(envName)
|
|
util.AssembleResponse(c, msg, err)
|
|
}
|
|
|
|
func SetEnv(c *gin.Context) {
|
|
envName := c.Param("envName")
|
|
ctrl.Log.Info("Patch a set environment request", "envName", envName)
|
|
msg, err := env.SetEnv(envName)
|
|
util.AssembleResponse(c, msg, err)
|
|
}
|