UpdateGroups handles PUT /hosts/:hostId/groups.
(w http.ResponseWriter, r *http.Request)
| 231 | |
| 232 | // UpdateGroups handles PUT /hosts/:hostId/groups. |
| 233 | func (h *HostsHandler) UpdateGroups(w http.ResponseWriter, r *http.Request) { |
| 234 | hostID := chi.URLParam(r, "hostId") |
| 235 | var req struct { |
| 236 | GroupIds []string `json:"groupIds"` |
| 237 | } |
| 238 | if err := decodeJSON(r, &req); err != nil { |
| 239 | Error(w, http.StatusBadRequest, "Invalid request body") |
| 240 | return |
| 241 | } |
| 242 | groupIds := req.GroupIds |
| 243 | if groupIds == nil { |
| 244 | groupIds = []string{} |
| 245 | } |
| 246 | |
| 247 | host, err := h.hosts.GetByID(r.Context(), hostID) |
| 248 | if err != nil || host == nil { |
| 249 | Error(w, http.StatusNotFound, "Host not found") |
| 250 | return |
| 251 | } |
| 252 | |
| 253 | for _, gid := range groupIds { |
| 254 | _, err = h.hostGroups.GetByID(r.Context(), gid) |
| 255 | if err != nil { |
| 256 | Error(w, http.StatusBadRequest, "One or more host groups not found") |
| 257 | return |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if err := h.hosts.SetHostGroups(r.Context(), hostID, groupIds); err != nil { |
| 262 | Error(w, http.StatusInternalServerError, "Failed to update host groups") |
| 263 | return |
| 264 | } |
| 265 | |
| 266 | host, _ = h.hosts.GetByID(r.Context(), hostID) |
| 267 | groups, _ := h.hosts.GetHostGroups(r.Context(), hostID) |
| 268 | JSON(w, http.StatusOK, map[string]interface{}{ |
| 269 | "message": "Host groups updated successfully", |
| 270 | "host": hostToResponse(host, groups), |
| 271 | }) |
| 272 | } |
| 273 | |
| 274 | // UpdateFriendlyName handles PATCH /hosts/:hostId/friendly-name. |
| 275 | func (h *HostsHandler) UpdateFriendlyName(w http.ResponseWriter, r *http.Request) { |
nothing calls this directly
no test coverage detected