Allow deleting clients
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Melora Hugues 2023-09-16 13:52:12 +02:00
parent 0a58ceefbc
commit 0318d5caf4
2 changed files with 51 additions and 5 deletions

View file

@ -64,15 +64,47 @@ func (bc *BootController) setBootOption(w http.ResponseWriter, r *http.Request)
return http.StatusAccepted, nil, nil 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) { 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) helpers.HandleResponse(w, r, http.StatusMethodNotAllowed, nil, bc.l)
return 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) helpers.HandleResponse(w, r, returncode, content, bc.l)
} }

View file

@ -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 { func (chs *ClientHandlerService) SetClientBootOption(client uuid.UUID, option string) error {
var err error var err error