Skip to content

Commit

Permalink
Merge pull request #172 from kubefirst/list_instance_sizes
Browse files Browse the repository at this point in the history
List instance sizes
  • Loading branch information
D-B-Hawk authored Oct 4, 2023
2 parents b95b577 + de4e640 commit a3c0a7a
Show file tree
Hide file tree
Showing 11 changed files with 481 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/controller/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (clctrl *ClusterController) CreateCluster() error {
return fmt.Errorf(msg)
}

log.Info("created %s cloud resources", clctrl.CloudProvider)
log.Infof("created %s cloud resources", clctrl.CloudProvider)

telemetryShim.Transmit(clctrl.UseTelemetry, segmentClient, segment.MetricCloudTerraformApplyCompleted, "")

Expand Down
104 changes: 104 additions & 0 deletions internal/db/environments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package db

import (
"fmt"

pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)

// GetEnvironments
func (mdbcl *MongoDBClient) GetEnvironments() ([]pkgtypes.Environment, error) {
// Find
var result []pkgtypes.Environment
cursor, err := mdbcl.EnvironmentsCollection.Find(mdbcl.Context, bson.D{})
if err != nil {
return []pkgtypes.Environment{}, fmt.Errorf("error getting environments")
}

for cursor.Next(mdbcl.Context) {
//Create a value into which the single document can be decoded
var environment pkgtypes.Environment
err := cursor.Decode(&environment)
if err != nil {
return []pkgtypes.Environment{}, err
}
result = append(result, environment)

}
if err := cursor.Err(); err != nil {
return []pkgtypes.Environment{}, err
}

cursor.Close(mdbcl.Context)

return result, nil
}

// GetEnvironment
func (mdbcl *MongoDBClient) GetEnvironment(name string) (pkgtypes.Environment, error) {
// Find
filter := bson.D{{Key: "name", Value: name }}
var result pkgtypes.Environment
err := mdbcl.EnvironmentsCollection.FindOne(mdbcl.Context, filter).Decode(&result)
if err != nil {
if err == mongo.ErrNoDocuments {
return pkgtypes.Environment{}, fmt.Errorf("environment not found")
}
return pkgtypes.Environment{}, fmt.Errorf("error getting environment %s: %s", name, err)
}

return result, nil
}

// InsertEnvironment
func (mdbcl *MongoDBClient) InsertEnvironment(env pkgtypes.Environment) (pkgtypes.Environment ,error) {
filter := bson.D{{ Key: "name", Value: env.Name }}

result := pkgtypes.Environment {
ID: primitive.NewObjectID(),
Name: env.Name,
Color: env.Color,
Description: env.Description,
CreationTimestamp: env.CreationTimestamp,
}

err := mdbcl.EnvironmentsCollection.FindOne(mdbcl.Context, filter).Decode(&result)
if err != nil {
// This error means your query did not match any documents.
if err == mongo.ErrNoDocuments {
// Create if entry does not exist
insert, err := mdbcl.EnvironmentsCollection.InsertOne(mdbcl.Context, result)
if err != nil {
return pkgtypes.Environment{}, fmt.Errorf("error inserting environment %v: %s", env.Name, err)
}

log.Info(insert)
}
} else {
return pkgtypes.Environment{}, fmt.Errorf("environment %v already exists", env.Name)
}
return result, nil
}

func (mdbcl *MongoDBClient) DeleteEnvironment(envName string) error {
filter := bson.D{{Key: "name", Value: envName }}

findError := mdbcl.EnvironmentsCollection.FindOne(mdbcl.Context, filter).Err()

if findError != nil {
return fmt.Errorf("no environment by the name %v", envName)
}

_,err := mdbcl.EnvironmentsCollection.DeleteOne(mdbcl.Context, filter)
if err != nil {
return fmt.Errorf("error deleting environment %s: %s", envName, err)
}

log.Infof("%v environment deleted", envName)

return nil
}
2 changes: 2 additions & 0 deletions internal/db/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type MongoDBClient struct {
ClustersCollection *mongo.Collection
GitopsCatalogCollection *mongo.Collection
ServicesCollection *mongo.Collection
EnvironmentsCollection *mongo.Collection
}

var Client = Connect()
Expand Down Expand Up @@ -65,6 +66,7 @@ func Connect() *MongoDBClient {
ClustersCollection: client.Database("api").Collection("clusters"),
GitopsCatalogCollection: client.Database("api").Collection("gitops-catalog"),
ServicesCollection: client.Database("api").Collection("services"),
EnvironmentsCollection: client.Database("api").Collection("environments"),
}

return &cl
Expand Down
78 changes: 78 additions & 0 deletions internal/router/api/v1/environments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package api

import (
"fmt"
"time"

"net/http"

"github.com/gin-gonic/gin"
"github.com/kubefirst/kubefirst-api/internal/db"
"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) {
environments, err := db.Client.GetEnvironments()

if err != nil {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
Message: err.Error(),
})
return
}

c.JSON(http.StatusOK, environments)
}

func CreateEnvironment(c *gin.Context) {

// Bind to variable as application/json, handle error
var environmentDefinition pkgtypes.Environment
err := c.Bind(&environmentDefinition)
if err != nil {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
Message: err.Error(),
})
return
}

environmentDefinition.CreationTimestamp = fmt.Sprintf("%v", primitive.NewDateTimeFromTime(time.Now().UTC()))

newEnv, err := db.Client.InsertEnvironment(environmentDefinition)

if err != nil {
c.JSON(http.StatusConflict, types.JSONFailureResponse{
Message: err.Error(),
})
return
}

c.JSON(http.StatusCreated, newEnv)
}

func DeleteEnvironment(c *gin.Context) {
envName, param := c.Params.Get("environment_name")

if !param {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
Message: ":environment_name not provided",
})
return
}

err := db.Client.DeleteEnvironment(envName)

if err != nil {
c.JSON(http.StatusBadRequest, types.JSONFailureResponse{
Message: err.Error(),
})
return
}

c.JSON(http.StatusOK, types.JSONSuccessResponse{
Message: fmt.Sprintf("successfully deleted %v environment", envName),
})

}
Loading

0 comments on commit a3c0a7a

Please sign in to comment.