diff --git a/bootserver/controllers/client/client.go b/bootserver/controllers/client/client.go index 97a30b9..019a409 100644 --- a/bootserver/controllers/client/client.go +++ b/bootserver/controllers/client/client.go @@ -64,15 +64,47 @@ func (bc *BootController) setBootOption(w http.ResponseWriter, r *http.Request) return http.StatusAccepted, nil, nil } +func (bc *BootController) deleteClient(w http.ResponseWriter, r *http.Request) (int, []byte, error) { + dat, err := io.ReadAll(r.Body) + if err != nil { + return http.StatusInternalServerError, nil, fmt.Errorf("failed to read body: %w", err) + } + var payload setBootOptionPayload + if err := json.Unmarshal(dat, &payload); err != nil { + return http.StatusBadRequest, nil, fmt.Errorf("failed to parse body: %w", err) + } + + clientID, err := uuid.Parse(payload.ClientID) + if err != nil { + return http.StatusBadRequest, []byte("bad client ID"), fmt.Errorf("invalid format for client ID: %w", err) + } + if err := bc.clientService.DeleteClient(clientID); err != nil { + return http.StatusInternalServerError, []byte("failed to delete client"), fmt.Errorf("failed to delete client: %w", err) + } + + return http.StatusOK, nil, nil +} + func (bc *BootController) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodPut { + var returncode int + var content []byte + var err error + + switch r.Method { + case http.MethodPut: + returncode, content, err = bc.setBootOption(w, r) + if err != nil { + bc.l.Errorf("Error setting boot option for client: %s", err.Error()) + } + case http.MethodDelete: + returncode, content, err = bc.deleteClient(w, r) + if err != nil { + bc.l.Errorf("Error setting boot option for client: %s", err.Error()) + } + default: helpers.HandleResponse(w, r, http.StatusMethodNotAllowed, nil, bc.l) return } - returncode, content, err := bc.setBootOption(w, r) - if err != nil { - bc.l.Errorf("Error setting boot option for client: %s", err.Error()) - } helpers.HandleResponse(w, r, returncode, content, bc.l) } diff --git a/bootserver/services/services.go b/bootserver/services/services.go index 2293cc4..1669f22 100644 --- a/bootserver/services/services.go +++ b/bootserver/services/services.go @@ -156,6 +156,20 @@ func (chs *ClientHandlerService) GetClientSelectedBootOption(client uuid.UUID) ( } } +func (chs *ClientHandlerService) DeleteClient(client uuid.UUID) error { + var err error + + clients, err := chs.load() + if err != nil { + return fmt.Errorf("failed to load current config: %w", err) + } + delete(clients, client) + if err := chs.unload(clients); err != nil { + return fmt.Errorf("failed to save current config: %w", err) + } + return nil +} + func (chs *ClientHandlerService) SetClientBootOption(client uuid.UUID, option string) error { var err error