|
| 1 | +/* |
| 2 | +Copyright 2026 Cozystack contributors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package rest |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "net/http" |
| 22 | + |
| 23 | + "github.com/cockroachdb/errors" |
| 24 | + |
| 25 | + apiv1 "github.com/cozystack/blockstor/pkg/api/v1" |
| 26 | + "github.com/cozystack/blockstor/pkg/store" |
| 27 | +) |
| 28 | + |
| 29 | +// registerNodes wires the /v1/nodes endpoints on mux. It is split out of |
| 30 | +// Server.Start so each resource group lives in its own file. |
| 31 | +func (s *Server) registerNodes(mux *http.ServeMux) { |
| 32 | + mux.HandleFunc("GET /v1/nodes", s.requireStore(s.handleNodesList)) |
| 33 | + mux.HandleFunc("GET /v1/nodes/{node}", s.requireStore(s.handleNodeGet)) |
| 34 | + mux.HandleFunc("POST /v1/nodes", s.requireStore(s.handleNodeCreate)) |
| 35 | + mux.HandleFunc("PUT /v1/nodes/{node}", s.requireStore(s.handleNodeUpdate)) |
| 36 | + mux.HandleFunc("DELETE /v1/nodes/{node}", s.requireStore(s.handleNodeDelete)) |
| 37 | +} |
| 38 | + |
| 39 | +// requireStore guards endpoints that need persistence; it returns 503 if the |
| 40 | +// Store is nil. We can serve /v1/controller/version without a store, so this |
| 41 | +// gate is per-handler rather than global. |
| 42 | +func (s *Server) requireStore(next http.HandlerFunc) http.HandlerFunc { |
| 43 | + return func(w http.ResponseWriter, r *http.Request) { |
| 44 | + if s.Store == nil { |
| 45 | + writeError(w, http.StatusServiceUnavailable, "store not configured") |
| 46 | + |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + next(w, r) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func (s *Server) handleNodesList(w http.ResponseWriter, r *http.Request) { |
| 55 | + nodes, err := s.Store.Nodes().List(r.Context()) |
| 56 | + if err != nil { |
| 57 | + writeError(w, http.StatusInternalServerError, err.Error()) |
| 58 | + |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + writeJSON(w, http.StatusOK, nodes) |
| 63 | +} |
| 64 | + |
| 65 | +func (s *Server) handleNodeGet(w http.ResponseWriter, r *http.Request) { |
| 66 | + name := r.PathValue("node") |
| 67 | + |
| 68 | + n, err := s.Store.Nodes().Get(r.Context(), name) |
| 69 | + if err != nil { |
| 70 | + writeStoreError(w, err) |
| 71 | + |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + writeJSON(w, http.StatusOK, n) |
| 76 | +} |
| 77 | + |
| 78 | +func (s *Server) handleNodeCreate(w http.ResponseWriter, r *http.Request) { |
| 79 | + var n apiv1.Node |
| 80 | + |
| 81 | + dec := json.NewDecoder(r.Body) |
| 82 | + dec.DisallowUnknownFields() |
| 83 | + |
| 84 | + err := dec.Decode(&n) |
| 85 | + if err != nil { |
| 86 | + writeError(w, http.StatusBadRequest, err.Error()) |
| 87 | + |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + if n.Name == "" { |
| 92 | + writeError(w, http.StatusBadRequest, "node name is required") |
| 93 | + |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + err = s.Store.Nodes().Create(r.Context(), &n) |
| 98 | + if err != nil { |
| 99 | + writeStoreError(w, err) |
| 100 | + |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + writeJSON(w, http.StatusCreated, n) |
| 105 | +} |
| 106 | + |
| 107 | +func (s *Server) handleNodeUpdate(w http.ResponseWriter, r *http.Request) { |
| 108 | + name := r.PathValue("node") |
| 109 | + |
| 110 | + var n apiv1.Node |
| 111 | + |
| 112 | + dec := json.NewDecoder(r.Body) |
| 113 | + dec.DisallowUnknownFields() |
| 114 | + |
| 115 | + err := dec.Decode(&n) |
| 116 | + if err != nil { |
| 117 | + writeError(w, http.StatusBadRequest, err.Error()) |
| 118 | + |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + // The path name wins over any body name, so callers can omit it. |
| 123 | + n.Name = name |
| 124 | + |
| 125 | + err = s.Store.Nodes().Update(r.Context(), &n) |
| 126 | + if err != nil { |
| 127 | + writeStoreError(w, err) |
| 128 | + |
| 129 | + return |
| 130 | + } |
| 131 | + |
| 132 | + writeJSON(w, http.StatusOK, n) |
| 133 | +} |
| 134 | + |
| 135 | +func (s *Server) handleNodeDelete(w http.ResponseWriter, r *http.Request) { |
| 136 | + name := r.PathValue("node") |
| 137 | + |
| 138 | + err := s.Store.Nodes().Delete(r.Context(), name) |
| 139 | + if err != nil { |
| 140 | + writeStoreError(w, err) |
| 141 | + |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + w.WriteHeader(http.StatusNoContent) |
| 146 | +} |
| 147 | + |
| 148 | +// writeStoreError maps store sentinel errors to HTTP statuses so handlers |
| 149 | +// don't repeat the same switch. |
| 150 | +func writeStoreError(w http.ResponseWriter, err error) { |
| 151 | + switch { |
| 152 | + case errors.Is(err, store.ErrNotFound): |
| 153 | + writeError(w, http.StatusNotFound, err.Error()) |
| 154 | + case errors.Is(err, store.ErrAlreadyExists): |
| 155 | + writeError(w, http.StatusConflict, err.Error()) |
| 156 | + default: |
| 157 | + writeError(w, http.StatusInternalServerError, err.Error()) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// writeError sends a minimal JSON error body. Once we wire ApiCallRc in |
| 162 | +// Phase 2 this becomes the real LINSTOR error envelope. |
| 163 | +func writeError(w http.ResponseWriter, status int, msg string) { |
| 164 | + writeJSON(w, status, map[string]string{"error": msg}) |
| 165 | +} |
0 commit comments