Correctly return the created UUID on enrollment
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Melora Hugues 2023-08-13 22:34:12 +02:00
parent 016dc7fe5b
commit 8f1897da97

View file

@ -14,6 +14,10 @@ import (
const EnrollRoute = "/enroll" const EnrollRoute = "/enroll"
type newClientPayload struct {
ID string `json:"ID"`
}
type EnrollController struct { type EnrollController struct {
clientService *services.ClientHandlerService clientService *services.ClientHandlerService
l *logrus.Logger l *logrus.Logger
@ -26,30 +30,39 @@ func NewEnrollController(l *logrus.Logger, service *services.ClientHandlerServic
} }
} }
func (ec *EnrollController) enrollMachine(w http.ResponseWriter, r *http.Request) (int, error) { func (ec *EnrollController) enrollMachine(w http.ResponseWriter, r *http.Request) (int, []byte, error) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
return http.StatusMethodNotAllowed, nil return http.StatusMethodNotAllowed, nil, nil
} }
dat, err := io.ReadAll(r.Body) dat, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to read body: %w", err) return http.StatusInternalServerError, nil, fmt.Errorf("failed to read body: %w", err)
} }
var client bootoption.Client var client bootoption.Client
if err := json.Unmarshal(dat, &client); err != nil { if err := json.Unmarshal(dat, &client); err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to parse body: %w", err) return http.StatusInternalServerError, nil, fmt.Errorf("failed to parse body: %w", err)
}
cltID, err := ec.clientService.AddClient(&client)
if err != nil {
return http.StatusInternalServerError, nil, fmt.Errorf("failed to create client %w", err)
}
payload, err := json.Marshal(newClientPayload{ID: cltID.String()})
if err != nil {
return http.StatusInternalServerError, nil, fmt.Errorf("failed to serialize payload: %w", err)
} }
ec.clientService.AddClient(&client)
ec.l.Infof("Added client") ec.l.Infof("Added client")
return http.StatusAccepted, nil return http.StatusOK, payload, nil
} }
func (ec *EnrollController) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (ec *EnrollController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
returncode, err := ec.enrollMachine(w, r) returncode, content, err := ec.enrollMachine(w, r)
if err != nil { if err != nil {
ec.l.Errorf("Error handling client enrollement: %s", err.Error()) ec.l.Errorf("Error handling client enrollement: %s", err.Error())
} }
helpers.HandleResponse(w, r, returncode, nil, ec.l) helpers.HandleResponse(w, r, returncode, content, ec.l)
} }