@@ -958,23 +958,61 @@ func (a *App) handleCreateISCSITarget(w http.ResponseWriter, r *http.Request) {
|
||||
func (a *App) handleGetISCSITarget(w http.ResponseWriter, r *http.Request) {
|
||||
id := pathParam(r, "/api/v1/iscsi/targets/")
|
||||
if id == "" {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "target id required"})
|
||||
writeError(w, errors.ErrBadRequest("target id required"))
|
||||
return
|
||||
}
|
||||
|
||||
target, err := a.iscsiStore.Get(id)
|
||||
if err != nil {
|
||||
if err == storage.ErrISCSITargetNotFound {
|
||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": err.Error()})
|
||||
writeError(w, errors.ErrNotFound("iSCSI target"))
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
writeError(w, errors.ErrInternal("failed to get iSCSI target").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, target)
|
||||
}
|
||||
|
||||
func (a *App) handleGetISCSIConnectionInstructions(w http.ResponseWriter, r *http.Request) {
|
||||
id := pathParam(r, "/api/v1/iscsi/targets/")
|
||||
if id == "" {
|
||||
writeError(w, errors.ErrBadRequest("target id required"))
|
||||
return
|
||||
}
|
||||
|
||||
target, err := a.iscsiStore.Get(id)
|
||||
if err != nil {
|
||||
if err == storage.ErrISCSITargetNotFound {
|
||||
writeError(w, errors.ErrNotFound("iSCSI target"))
|
||||
return
|
||||
}
|
||||
writeError(w, errors.ErrInternal("failed to get iSCSI target").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// Get portal IP (with fallback)
|
||||
portalIP, err := a.iscsiService.GetPortalIP()
|
||||
if err != nil {
|
||||
log.Printf("get portal IP error: %v", err)
|
||||
portalIP = "127.0.0.1" // Fallback
|
||||
}
|
||||
|
||||
// Get portal port from query parameter or use default
|
||||
portalPort := 3260
|
||||
if portStr := r.URL.Query().Get("port"); portStr != "" {
|
||||
if port, err := strconv.Atoi(portStr); err == nil && port > 0 && port < 65536 {
|
||||
portalPort = port
|
||||
}
|
||||
}
|
||||
|
||||
// Generate connection instructions
|
||||
instructions := a.iscsiService.GetConnectionInstructions(*target, portalIP, portalPort)
|
||||
|
||||
writeJSON(w, http.StatusOK, instructions)
|
||||
}
|
||||
|
||||
func (a *App) handleUpdateISCSITarget(w http.ResponseWriter, r *http.Request) {
|
||||
id := pathParam(r, "/api/v1/iscsi/targets/")
|
||||
if id == "" {
|
||||
|
||||
@@ -193,12 +193,27 @@ func (a *App) handleBackupOps(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (a *App) handleISCSITargetOps(w http.ResponseWriter, r *http.Request) {
|
||||
id := pathParam(r, "/api/v1/iscsi/targets/")
|
||||
if id == "" {
|
||||
writeError(w, errors.ErrBadRequest("target id required"))
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasSuffix(r.URL.Path, "/connection") {
|
||||
if r.Method == http.MethodGet {
|
||||
a.handleGetISCSIConnectionInstructions(w, r)
|
||||
} else {
|
||||
writeError(w, errors.NewAPIError(errors.ErrCodeBadRequest, "method not allowed", http.StatusMethodNotAllowed))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasSuffix(r.URL.Path, "/luns") {
|
||||
if r.Method == http.MethodPost {
|
||||
a.handleAddLUN(w, r)
|
||||
return
|
||||
}
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
writeError(w, errors.NewAPIError(errors.ErrCodeBadRequest, "method not allowed", http.StatusMethodNotAllowed))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -207,7 +222,7 @@ func (a *App) handleISCSITargetOps(w http.ResponseWriter, r *http.Request) {
|
||||
a.handleRemoveLUN(w, r)
|
||||
return
|
||||
}
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
writeError(w, errors.NewAPIError(errors.ErrCodeBadRequest, "method not allowed", http.StatusMethodNotAllowed))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user