store: Return an error from GetBlueprintChange

If the blueprint doesn't exist, or the commit for the selected blueprint
doesn't exist it will return an error.

This also fixes the blueprints/undo/ route to return the correct error
to the caller.
This commit is contained in:
Brian C. Lane 2020-03-11 14:23:44 -07:00 committed by Tom Gundersen
parent b4710b52f1
commit 77fd2a0d8b
2 changed files with 24 additions and 8 deletions

View file

@ -354,15 +354,20 @@ func (s *Store) GetBlueprintCommitted(name string) *blueprint.Blueprint {
return &bp
}
func (s *Store) GetBlueprintChange(name string, commit string) *blueprint.Change {
// GetBlueprintChange returns a specific change to a blueprint
// If the blueprint or change do not exist then an error is returned
func (s *Store) GetBlueprintChange(name string, commit string) (*blueprint.Change, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if _, ok := s.BlueprintsChanges[name]; !ok {
return nil, errors.New("Unknown blueprint")
}
change, ok := s.BlueprintsChanges[name][commit]
if !ok {
return nil
return nil, errors.New("Unknown commit")
}
return &change
return &change, nil
}
// GetBlueprintChanges returns the list of changes, oldest first

View file

@ -1249,15 +1249,26 @@ func (api *API) blueprintUndoHandler(writer http.ResponseWriter, request *http.R
name := params.ByName("blueprint")
commit := params.ByName("commit")
bpChange := api.store.GetBlueprintChange(name, commit)
bpChange, err := api.store.GetBlueprintChange(name, commit)
if err != nil {
errors := responseError{
ID: "BlueprintsError",
Msg: err.Error(),
}
statusResponseError(writer, http.StatusBadRequest, errors)
return
}
bp := bpChange.Blueprint
commitMsg := name + ".toml reverted to commit " + commit
err := api.store.PushBlueprint(bp, commitMsg)
err = api.store.PushBlueprint(bp, commitMsg)
if err != nil {
statusResponseError(writer, http.StatusInternalServerError, responseError{
ID: "BlueprintError",
errors := responseError{
ID: "BlueprintsError",
Msg: err.Error(),
})
}
statusResponseError(writer, http.StatusBadRequest, errors)
return
}
statusResponseOK(writer)
}