Skip to content

Commit

Permalink
fix: support add envs to db wo route call
Browse files Browse the repository at this point in the history
  • Loading branch information
claywd committed Oct 20, 2023
1 parent 2d5dc51 commit 7be95b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
24 changes: 24 additions & 0 deletions internal/environments/defaultEnvironments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
11 changes: 5 additions & 6 deletions internal/router/api/v1/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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{
Expand Down Expand Up @@ -117,4 +116,4 @@ func UpdateEnvironment(c *gin.Context) {
Message: fmt.Sprintf("successfully updated environment with id: %v", envId),
})

}
}

0 comments on commit 7be95b7

Please sign in to comment.