This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package diskapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/itworx/pulse/internal/auth"
|
||||
"github.com/itworx/pulse/internal/disk"
|
||||
"github.com/itworx/pulse/internal/problem"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
Provider disk.Provider
|
||||
MaxPageSize int
|
||||
}
|
||||
|
||||
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet || (r.URL.Path != "/api/v1/disks" && !strings.HasPrefix(r.URL.Path, "/api/v1/disks/")) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if _, ok := auth.PrincipalFromContext(r.Context()); !ok {
|
||||
problem.Write(w, r, http.StatusUnauthorized, "UNAUTHORIZED", "Authentication required", "Authentication is required to read disks.", nil)
|
||||
return
|
||||
}
|
||||
if err := r.Context().Err(); err != nil {
|
||||
return
|
||||
}
|
||||
snapshot, err := h.snapshot(r)
|
||||
if err != nil {
|
||||
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return
|
||||
}
|
||||
problem.Write(w, r, http.StatusServiceUnavailable, "DISKS_UNAVAILABLE", "Diskgegevens niet beschikbaar", "De diskgegevens konden niet worden gelezen.", nil)
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(r.URL.Path, "/api/v1/disks/") {
|
||||
id := strings.TrimPrefix(r.URL.Path, "/api/v1/disks/")
|
||||
item, ok := disk.DiskByID(snapshot, id)
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
writeJSON(w, struct {
|
||||
Source disk.Source `json:"source"`
|
||||
Disk disk.Disk `json:"disk"`
|
||||
}{snapshot.Source, item})
|
||||
return
|
||||
}
|
||||
limit := 50
|
||||
if value := r.URL.Query().Get("limit"); value != "" {
|
||||
parsed, parseErr := strconv.Atoi(value)
|
||||
if parseErr != nil {
|
||||
problem.Write(w, r, http.StatusBadRequest, "DISK_QUERY_INVALID", "Invalid disk query", "De disklimiet is ongeldig.", nil)
|
||||
return
|
||||
}
|
||||
limit = parsed
|
||||
}
|
||||
max := h.MaxPageSize
|
||||
if max == 0 {
|
||||
max = 100
|
||||
}
|
||||
if limit < 1 || limit > max {
|
||||
problem.Write(w, r, http.StatusBadRequest, "DISK_QUERY_INVALID", "Invalid disk query", "De disklimiet is ongeldig.", nil)
|
||||
return
|
||||
}
|
||||
if limit < len(snapshot.Disks) {
|
||||
snapshot.Disks = snapshot.Disks[:limit]
|
||||
}
|
||||
writeJSON(w, snapshot)
|
||||
}
|
||||
func (h Handler) snapshot(r *http.Request) (disk.Snapshot, error) {
|
||||
if h.Provider == nil {
|
||||
return disk.UnknownSnapshot(time.Now().UTC(), "disks", "unraid", "source_unavailable"), nil
|
||||
}
|
||||
return h.Provider.Snapshot(r.Context())
|
||||
}
|
||||
func writeJSON(w http.ResponseWriter, value any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Cache-Control", "private, max-age=5")
|
||||
_ = json.NewEncoder(w).Encode(value)
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package diskapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/itworx/pulse/internal/auth"
|
||||
"github.com/itworx/pulse/internal/disk"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type provider struct{ snapshot disk.Snapshot }
|
||||
|
||||
func (p provider) Snapshot(context.Context) (disk.Snapshot, error) { return p.snapshot, nil }
|
||||
func req(method, path string) *http.Request {
|
||||
r := httptest.NewRequest(method, path, nil)
|
||||
return r.WithContext(auth.WithPrincipal(r.Context(), auth.Principal{Subject: "viewer", Role: auth.RoleViewer}))
|
||||
}
|
||||
func TestHandlerRequiresAuthenticationAndUnknown(t *testing.T) {
|
||||
u := httptest.NewRecorder()
|
||||
Handler{}.ServeHTTP(u, httptest.NewRequest(http.MethodGet, "/api/v1/disks", nil))
|
||||
if u.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status=%d", u.Code)
|
||||
}
|
||||
r := httptest.NewRecorder()
|
||||
Handler{}.ServeHTTP(r, req(http.MethodGet, "/api/v1/disks"))
|
||||
if r.Code != http.StatusOK || !strings.Contains(r.Body.String(), `"state":"unknown"`) {
|
||||
t.Fatalf("status=%d body=%s", r.Code, r.Body.String())
|
||||
}
|
||||
}
|
||||
func TestHandlerListsAndDetailsReadOnly(t *testing.T) {
|
||||
snapshot := disk.UnknownSnapshot(time.Date(2026, 8, 1, 12, 0, 0, 0, time.UTC), "fixture-disks", "fixture", "test")
|
||||
snapshot.Disks = []disk.Disk{{ID: "disk-1", Name: "Disk 1", State: disk.StateOnline}}
|
||||
snapshot.Total = 1
|
||||
list := httptest.NewRecorder()
|
||||
Handler{Provider: provider{snapshot: snapshot}}.ServeHTTP(list, req(http.MethodGet, "/api/v1/disks?limit=1"))
|
||||
if list.Code != http.StatusOK || !strings.Contains(list.Body.String(), `"disk-1"`) {
|
||||
t.Fatalf("status=%d body=%s", list.Code, list.Body.String())
|
||||
}
|
||||
detail := httptest.NewRecorder()
|
||||
Handler{Provider: provider{snapshot: snapshot}}.ServeHTTP(detail, req(http.MethodGet, "/api/v1/disks/disk-1"))
|
||||
if detail.Code != http.StatusOK || !strings.Contains(detail.Body.String(), `"disk":{"id":"disk-1"`) {
|
||||
t.Fatalf("status=%d body=%s", detail.Code, detail.Body.String())
|
||||
}
|
||||
mutation := httptest.NewRecorder()
|
||||
Handler{}.ServeHTTP(mutation, req(http.MethodPost, "/api/v1/disks/disk-1/format"))
|
||||
if mutation.Code != http.StatusNotFound {
|
||||
t.Fatalf("status=%d", mutation.Code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user