alpha repo init
This commit is contained in:
190
internal/httpapp/vtl_handlers.go
Normal file
190
internal/httpapp/vtl_handlers.go
Normal file
@@ -0,0 +1,190 @@
|
||||
package httpapp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"gitea.avt.data-center.id/othman.suseno/atlas/internal/errors"
|
||||
)
|
||||
|
||||
// VTL API Handlers
|
||||
|
||||
// handleGetVTLStatus returns the overall VTL system status
|
||||
func (a *App) handleGetVTLStatus(w http.ResponseWriter, r *http.Request) {
|
||||
status, err := a.vtlService.GetStatus()
|
||||
if err != nil {
|
||||
log.Printf("get VTL status error: %v", err)
|
||||
writeError(w, errors.ErrInternal(fmt.Sprintf("failed to get VTL status: %v", err)))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, status)
|
||||
}
|
||||
|
||||
// handleListVTLDrives returns all virtual tape drives
|
||||
func (a *App) handleListVTLDrives(w http.ResponseWriter, r *http.Request) {
|
||||
drives, err := a.vtlService.ListDrives()
|
||||
if err != nil {
|
||||
log.Printf("list VTL drives error: %v", err)
|
||||
writeError(w, errors.ErrInternal(fmt.Sprintf("failed to list VTL drives: %v", err)))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, drives)
|
||||
}
|
||||
|
||||
// handleListVTLTapes returns all virtual tapes
|
||||
func (a *App) handleListVTLTapes(w http.ResponseWriter, r *http.Request) {
|
||||
tapes, err := a.vtlService.ListTapes()
|
||||
if err != nil {
|
||||
log.Printf("list VTL tapes error: %v", err)
|
||||
writeError(w, errors.ErrInternal(fmt.Sprintf("failed to list VTL tapes: %v", err)))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, tapes)
|
||||
}
|
||||
|
||||
// handleCreateVTLTape creates a new virtual tape
|
||||
func (a *App) handleCreateVTLTape(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Barcode string `json:"barcode"`
|
||||
Type string `json:"type"` // e.g., "LTO-5", "LTO-6"
|
||||
Size uint64 `json:"size"` // Size in bytes (0 = default)
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, errors.ErrValidation(fmt.Sprintf("invalid request body: %v", err)))
|
||||
return
|
||||
}
|
||||
|
||||
if req.Barcode == "" {
|
||||
writeError(w, errors.ErrValidation("barcode is required"))
|
||||
return
|
||||
}
|
||||
|
||||
if req.Type == "" {
|
||||
req.Type = "LTO-5" // Default type
|
||||
}
|
||||
|
||||
if err := a.vtlService.CreateTape(req.Barcode, req.Type, req.Size); err != nil {
|
||||
log.Printf("create VTL tape error: %v", err)
|
||||
writeError(w, errors.ErrInternal(fmt.Sprintf("failed to create VTL tape: %v", err)))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusCreated, map[string]string{
|
||||
"message": "Virtual tape created successfully",
|
||||
"barcode": req.Barcode,
|
||||
})
|
||||
}
|
||||
|
||||
// handleDeleteVTLTape deletes a virtual tape
|
||||
func (a *App) handleDeleteVTLTape(w http.ResponseWriter, r *http.Request) {
|
||||
barcode := pathParam(r, "barcode")
|
||||
if barcode == "" {
|
||||
writeError(w, errors.ErrValidation("barcode is required"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := a.vtlService.DeleteTape(barcode); err != nil {
|
||||
log.Printf("delete VTL tape error: %v", err)
|
||||
writeError(w, errors.ErrInternal(fmt.Sprintf("failed to delete VTL tape: %v", err)))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]string{
|
||||
"message": "Virtual tape deleted successfully",
|
||||
"barcode": barcode,
|
||||
})
|
||||
}
|
||||
|
||||
// handleVTLServiceControl controls the mhvtl service (start/stop/restart)
|
||||
func (a *App) handleVTLServiceControl(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Action string `json:"action"` // "start", "stop", "restart"
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, errors.ErrValidation(fmt.Sprintf("invalid request body: %v", err)))
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
switch req.Action {
|
||||
case "start":
|
||||
err = a.vtlService.StartService()
|
||||
case "stop":
|
||||
err = a.vtlService.StopService()
|
||||
case "restart":
|
||||
err = a.vtlService.RestartService()
|
||||
default:
|
||||
writeError(w, errors.ErrValidation("invalid action: must be 'start', 'stop', or 'restart'"))
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Printf("VTL service control error: %v", err)
|
||||
writeError(w, errors.ErrInternal(fmt.Sprintf("failed to %s VTL service: %v", req.Action, err)))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]string{
|
||||
"message": "VTL service " + req.Action + "ed successfully",
|
||||
"action": req.Action,
|
||||
})
|
||||
}
|
||||
|
||||
// handleGetVTLDrive returns a specific drive by ID
|
||||
func (a *App) handleGetVTLDrive(w http.ResponseWriter, r *http.Request) {
|
||||
driveIDStr := pathParam(r, "id")
|
||||
driveID, err := strconv.Atoi(driveIDStr)
|
||||
if err != nil {
|
||||
writeError(w, errors.ErrValidation(fmt.Sprintf("invalid drive ID: %v", err)))
|
||||
return
|
||||
}
|
||||
|
||||
drives, err := a.vtlService.ListDrives()
|
||||
if err != nil {
|
||||
log.Printf("list VTL drives error: %v", err)
|
||||
writeError(w, errors.ErrInternal(fmt.Sprintf("failed to list VTL drives: %v", err)))
|
||||
return
|
||||
}
|
||||
|
||||
for _, drive := range drives {
|
||||
if drive.ID == driveID {
|
||||
writeJSON(w, http.StatusOK, drive)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
writeError(w, errors.ErrNotFound("drive not found"))
|
||||
}
|
||||
|
||||
// handleGetVTLTape returns a specific tape by barcode
|
||||
func (a *App) handleGetVTLTape(w http.ResponseWriter, r *http.Request) {
|
||||
barcode := pathParam(r, "barcode")
|
||||
if barcode == "" {
|
||||
writeError(w, errors.ErrValidation("barcode is required"))
|
||||
return
|
||||
}
|
||||
|
||||
tapes, err := a.vtlService.ListTapes()
|
||||
if err != nil {
|
||||
log.Printf("list VTL tapes error: %v", err)
|
||||
writeError(w, errors.ErrInternal(fmt.Sprintf("failed to list VTL tapes: %v", err)))
|
||||
return
|
||||
}
|
||||
|
||||
for _, tape := range tapes {
|
||||
if tape.Barcode == barcode {
|
||||
writeJSON(w, http.StatusOK, tape)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
writeError(w, errors.ErrNotFound("tape not found"))
|
||||
}
|
||||
Reference in New Issue
Block a user