init
This commit is contained in:
55
handler/handler.go
Normal file
55
handler/handler.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"main/db"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type Cluster struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Namespace string `json:"namespace"`
|
||||
ControlPlane string `json:"controlPlane"`
|
||||
PlatformVersion string `json:"platformversion`
|
||||
Cpu string `json:"cpu"`
|
||||
Memory string `json:"memory"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
UserID primitive.ObjectID `json:"userId"`
|
||||
}
|
||||
|
||||
func createClusterHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
vclusterCollection := db.Client.Database("vcluster").Collection("vclusters_details")
|
||||
|
||||
var cluster Cluster
|
||||
_ = json.NewDecoder(r.Body).Decode(&cluster)
|
||||
|
||||
if cluster.Name == "" || cluster.ControlPlane == "" || cluster.PlatformVersion == "" || cluster.Cpu == "" || cluster.Memory == "" {
|
||||
http.Error(w, "Invalid input", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var existsCluster Cluster
|
||||
_ = vclusterCollection.FindOne(context.TODO(), bson.M{"name": cluster.Name}).Decode(&existsCluster)
|
||||
|
||||
if existsCluster.Name == cluster.Name {
|
||||
http.Error(w, "Cluster name is duplicated", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
response := map[string]string{"message": "Cluster created"}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
|
||||
}
|
||||
|
||||
func RegsiterClusterRoute(r *mux.Router) {
|
||||
r.HandleFunc("/createcluster", createClusterHandler).Methods("POST")
|
||||
}
|
||||
Reference in New Issue
Block a user