Files
vclusterapi/handler/handler.go
behrooz razzaghi 3bbe28c5f4 init
2025-03-11 17:31:12 +03:30

56 lines
1.6 KiB
Go

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")
}