Files
vclusterapi/handler/handler.go
Ybehrooz e3bce3715b cluster
2025-07-13 20:23:02 +03:30

108 lines
3.2 KiB
Go

package handler
import (
"context"
"encoding/base64"
"encoding/json"
"main/argohandler"
"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"`
Cluster_config string `json:"clusterconfig"`
}
type Header struct {
Authorization string `bson:"token"`
}
func CreateClusterHandler(w http.ResponseWriter, r *http.Request) {
var cluster Cluster
_ = json.NewDecoder(r.Body).Decode(&cluster)
var header Header
header.Authorization = r.Header.Get("Authorization")
// vclusterCollection := db.Vclusters_details.FindOne(context.TODO(), bson.M{"name": Cluster.Name}).Decode(&existsCluster)
if cluster.Name == "" || cluster.ControlPlane == "" || cluster.PlatformVersion == "" || cluster.Cpu == "" || cluster.Memory == "" {
http.Error(w, "Invalid input", http.StatusBadRequest)
return
}
var existsCluster Cluster
_ = db.Vclusters_details.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
}
res, err := db.Vclusters_details.InsertOne(context.TODO(), cluster)
if err != nil {
http.Error(w, `{"message": "Could not create cluster"}`, http.StatusInternalServerError)
}
objectID := res.InsertedID.(primitive.ObjectID)
idStr := objectID.Hex()
argohandler.CreateApp(idStr, cluster.Name, cluster.ControlPlane, cluster.PlatformVersion, cluster.Cpu, cluster.Memory, "userid")
response := map[string]string{"message": "Cluster created"}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
func ListUserClusters(w http.ResponseWriter, r *http.Request) {
// var cluster Cluster
_, clusterList := argohandler.ListUserClusters("userid")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(clusterList)
}
func Connect(w http.ResponseWriter, r *http.Request) {
clusterName := r.URL.Query().Get("Name")
if clusterName == "" {
http.Error(w, "Missing 'Name' parameter", http.StatusBadRequest)
return
}
var existsCluster Cluster
_ = db.Vclusters_details.FindOne(context.TODO(), bson.M{"name": clusterName}).Decode(&existsCluster)
decoded, err := base64.StdEncoding.DecodeString(existsCluster.Cluster_config)
if err != nil {
http.Error(w, "Failed to decode cluster config", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/x-yaml")
w.Header().Set("Content-Disposition", `attachment; filename="`+clusterName+`.yaml"`)
w.Write(decoded)
}
// func RegsiterClusterRoute(r *mux.Router) {
// r.HandleFunc("/createcluster", createClusterHandler).Methods("POST", "OPTIONS")
// }