From b4710b52f1b54d3095100f3235b65a89719dee96 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Tue, 10 Mar 2020 17:01:04 -0700 Subject: [PATCH] store: Return an error from DeleteBlueprint and DeleteBlueprintFromWorkspace If an unknown blueprint or workspace is deleted it will now return an error. Also fixes the blueprints DELETE handlers to return the correct error to the client. Includes a new test. --- internal/store/store.go | 21 +++++++++++++++------ internal/weldr/api.go | 18 ++++++++++++++++-- internal/weldr/api_test.go | 1 + 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/internal/store/store.go b/internal/store/store.go index ce1ac32e3..b05e8ef60 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -431,18 +431,27 @@ func (s *Store) PushBlueprintToWorkspace(bp blueprint.Blueprint) error { }) } -func (s *Store) DeleteBlueprint(name string) { - // FIXME: handle or comment this possible error - _ = s.change(func() error { +// DeleteBlueprint will remove the named blueprint from the store +// if the blueprint does not exist it will return an error +// The workspace copy is deleted unconditionally, it will not return an error if it does not exist. +func (s *Store) DeleteBlueprint(name string) error { + return s.change(func() error { delete(s.Workspace, name) + if _, ok := s.Blueprints[name]; !ok { + return fmt.Errorf("Unknown blueprint: %s", name) + } delete(s.Blueprints, name) return nil }) } -func (s *Store) DeleteBlueprintFromWorkspace(name string) { - // FIXME: handle or comment this possible error - _ = s.change(func() error { +// DeleteBlueprintFromWorkspace deletes the workspace copy of a blueprint +// if the blueprint doesn't exist in the workspace it returns an error +func (s *Store) DeleteBlueprintFromWorkspace(name string) error { + return s.change(func() error { + if _, ok := s.Workspace[name]; !ok { + return fmt.Errorf("Unknown blueprint: %s", name) + } delete(s.Workspace, name) return nil }) diff --git a/internal/weldr/api.go b/internal/weldr/api.go index 07cf0e5af..175f31f66 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -1267,7 +1267,14 @@ func (api *API) blueprintDeleteHandler(writer http.ResponseWriter, request *http return } - api.store.DeleteBlueprint(params.ByName("blueprint")) + if err := api.store.DeleteBlueprint(params.ByName("blueprint")); err != nil { + errors := responseError{ + ID: "BlueprintsError", + Msg: err.Error(), + } + statusResponseError(writer, http.StatusBadRequest, errors) + return + } statusResponseOK(writer) } @@ -1276,7 +1283,14 @@ func (api *API) blueprintDeleteWorkspaceHandler(writer http.ResponseWriter, requ return } - api.store.DeleteBlueprintFromWorkspace(params.ByName("blueprint")) + if err := api.store.DeleteBlueprintFromWorkspace(params.ByName("blueprint")); err != nil { + errors := responseError{ + ID: "BlueprintsError", + Msg: err.Error(), + } + statusResponseError(writer, http.StatusBadRequest, errors) + return + } statusResponseOK(writer) } diff --git a/internal/weldr/api_test.go b/internal/weldr/api_test.go index 4e162a83d..dccb68daf 100644 --- a/internal/weldr/api_test.go +++ b/internal/weldr/api_test.go @@ -366,6 +366,7 @@ func TestBlueprintsDelete(t *testing.T) { ExpectedJSON string }{ {"DELETE", "/api/v0/blueprints/delete/test", ``, http.StatusOK, `{"status":true}`}, + {"DELETE", "/api/v0/blueprints/delete/test3-non", ``, http.StatusBadRequest, `{"status":false,"errors":[{"id":"BlueprintsError","msg":"Unknown blueprint: test3-non"}]}`}, } for _, c := range cases {