multi dataset
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.ma-al.com/goc_marek/zfs/internal/server/templates/pages"
|
||||
@@ -518,6 +520,118 @@ func (s *Server) handleAdminGetStats(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
// handleAdminGetDatasets returns all datasets, optionally filtered by client
|
||||
func (s *Server) handleAdminGetDatasets(w http.ResponseWriter, r *http.Request) {
|
||||
admin, err := s.authenticateAdmin(r)
|
||||
if err != nil || admin == nil {
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
clientID := r.URL.Query().Get("client_id")
|
||||
|
||||
var datasets []*DatasetConfig
|
||||
if clientID != "" {
|
||||
datasets, _ = s.db.GetDatasetsByClient(clientID)
|
||||
} else {
|
||||
datasets, _ = s.db.GetAllDatasets()
|
||||
}
|
||||
|
||||
// Get snapshot counts for each dataset
|
||||
type DatasetResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
ClientID string `json:"client_id"`
|
||||
DatasetName string `json:"dataset_name"`
|
||||
StorageType string `json:"storage_type"`
|
||||
Enabled bool `json:"enabled"`
|
||||
SnapshotCount int `json:"snapshot_count"`
|
||||
}
|
||||
|
||||
response := make([]DatasetResponse, len(datasets))
|
||||
for i, d := range datasets {
|
||||
snapshotCount, _ := s.db.GetSnapshotCountByDataset(d.ClientID, d.DatasetName)
|
||||
response[i] = DatasetResponse{
|
||||
ID: d.ID,
|
||||
ClientID: d.ClientID,
|
||||
DatasetName: d.DatasetName,
|
||||
StorageType: d.StorageType,
|
||||
Enabled: d.Enabled,
|
||||
SnapshotCount: snapshotCount,
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
// handleAdminUpdateDeleteDataset handles PUT and DELETE for a specific dataset
|
||||
func (s *Server) handleAdminUpdateDeleteDataset(w http.ResponseWriter, r *http.Request) {
|
||||
admin, err := s.authenticateAdmin(r)
|
||||
if err != nil || admin == nil {
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Extract dataset ID from URL
|
||||
parts := strings.Split(r.URL.Path, "/")
|
||||
if len(parts) < 4 {
|
||||
http.Error(w, "Invalid URL", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
datasetID, err := strconv.ParseInt(parts[len(parts)-1], 10, 64)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid dataset ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Get dataset from database
|
||||
dataset, err := s.db.GetDatasetByID(datasetID)
|
||||
if err != nil || dataset == nil {
|
||||
http.Error(w, "Dataset not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method == http.MethodDelete {
|
||||
// Delete dataset
|
||||
if err := s.db.DeleteDataset(datasetID); err != nil {
|
||||
http.Error(w, "Failed to delete dataset", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "Dataset deleted successfully",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method == http.MethodPut {
|
||||
// Update dataset
|
||||
var req struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
dataset.Enabled = req.Enabled
|
||||
if err := s.db.SaveDataset(dataset); err != nil {
|
||||
http.Error(w, "Failed to update dataset", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "Dataset updated successfully",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
|
||||
// Admin management handlers
|
||||
|
||||
// handleAdminGetAdmins returns all admins
|
||||
|
||||
Reference in New Issue
Block a user