-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from kubefirst/list_instance_sizes
List instance sizes
- Loading branch information
Showing
11 changed files
with
481 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}) | ||
|
||
} |
Oops, something went wrong.