From fd79f975330a1e2aec0e5e020d5eafe2065914cd Mon Sep 17 00:00:00 2001 From: k8s wrangler <6446939+k8swrangler@users.noreply.github.com> Date: Thu, 19 Oct 2023 17:12:09 -0500 Subject: [PATCH] fix: support add envs to db wo route call --- internal/environments/defaultEnvironments.go | 24 ++++++++++++++++++++ internal/router/api/v1/environments.go | 11 ++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/internal/environments/defaultEnvironments.go b/internal/environments/defaultEnvironments.go index 70842c8e..149dcef6 100644 --- a/internal/environments/defaultEnvironments.go +++ b/internal/environments/defaultEnvironments.go @@ -16,10 +16,21 @@ import ( "os" "time" + "github.com/kubefirst/kubefirst-api/internal/db" "github.com/kubefirst/kubefirst-api/pkg/types" log "github.com/sirupsen/logrus" + "go.mongodb.org/mongo-driver/bson/primitive" ) +func NewEnvironment(envDef types.Environment) (types.Environment, error) { + // Create new environment + envDef.CreationTimestamp = fmt.Sprintf("%v", primitive.NewDateTimeFromTime(time.Now().UTC())) + + newEnv, err := db.Client.InsertEnvironment(envDef) + + return newEnv, err +} + func CreateDefaultEnvironments( mgmtCluster types.Cluster) error { // Logging handler @@ -61,7 +72,20 @@ func CreateDefaultEnvironments( mgmtCluster types.Cluster) error { vcluster.Environment.Name = clusterName vcluster.DomainName = fmt.Sprintf("%s.%s", clusterName, mgmtCluster.DomainName) vcluster.Environment.Description = fmt.Sprintf("Default %s environment", clusterName) + switch clusterName { + case "development": + vcluster.Environment.Color = "green" + case "staging": + vcluster.Environment.Color = "yellow" + case "production": + vcluster.Environment.Color = "pink" + } + var err error + vcluster.Environment, err = NewEnvironment(vcluster.Environment) + if err != nil { + log.Errorf("error creating default environment in db for env %s", err) + } defaultClusters = append(defaultClusters, vcluster) } diff --git a/internal/router/api/v1/environments.go b/internal/router/api/v1/environments.go index 371ac404..3fde0d84 100644 --- a/internal/router/api/v1/environments.go +++ b/internal/router/api/v1/environments.go @@ -2,15 +2,14 @@ package api import ( "fmt" - "time" "net/http" "github.com/gin-gonic/gin" "github.com/kubefirst/kubefirst-api/internal/db" + environments "github.com/kubefirst/kubefirst-api/internal/environments" "github.com/kubefirst/kubefirst-api/internal/types" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" - "go.mongodb.org/mongo-driver/bson/primitive" ) func GetEnvironments(c *gin.Context) { @@ -26,6 +25,8 @@ func GetEnvironments(c *gin.Context) { c.JSON(http.StatusOK, environments) } + + func CreateEnvironment(c *gin.Context) { // Bind to variable as application/json, handle error @@ -38,9 +39,7 @@ func CreateEnvironment(c *gin.Context) { return } - environmentDefinition.CreationTimestamp = fmt.Sprintf("%v", primitive.NewDateTimeFromTime(time.Now().UTC())) - - newEnv, err := db.Client.InsertEnvironment(environmentDefinition) + newEnv, err := environments.NewEnvironment(environmentDefinition) if err != nil { c.JSON(http.StatusConflict, types.JSONFailureResponse{ @@ -75,4 +74,4 @@ func DeleteEnvironment(c *gin.Context) { Message: fmt.Sprintf("successfully deleted %v environment", envName), }) -} \ No newline at end of file +}