Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/dao/mocks/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,12 @@ func (d *resourceDaoMock) FindByKindAndOwnerForUpdate(
) (api.ResourceList, error) {
return d.FindByKindAndOwner(ctx, kind, ownerID)
}

func (d *resourceDaoMock) GetByID(_ context.Context, id string) (*api.Resource, error) {
for _, r := range d.resources {
if r.ID == id {
return r, nil
}
}
return nil, gorm.ErrRecordNotFound
}
10 changes: 10 additions & 0 deletions pkg/dao/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ResourceDao interface {
FindByKind(ctx context.Context, kind string) (api.ResourceList, error)
FindByKindAndOwner(ctx context.Context, kind, ownerID string) (api.ResourceList, error)
FindByKindAndOwnerForUpdate(ctx context.Context, kind, ownerID string) (api.ResourceList, error)
GetByID(ctx context.Context, id string) (*api.Resource, error)
}

var _ ResourceDao = &sqlResourceDao{}
Expand Down Expand Up @@ -129,6 +130,15 @@ func (d *sqlResourceDao) FindByKindAndOwner(ctx context.Context, kind, ownerID s
return resources, nil
}

func (d *sqlResourceDao) GetByID(ctx context.Context, id string) (*api.Resource, error) {
g2 := d.sessionFactory.New(ctx)
var resource api.Resource
if err := g2.Preload("Conditions").Take(&resource, "id = ?", id).Error; err != nil {
return nil, err
}
return &resource, nil
}

func (d *sqlResourceDao) FindByKindAndOwnerForUpdate(
ctx context.Context, kind, ownerID string,
) (api.ResourceList, error) {
Expand Down
44 changes: 44 additions & 0 deletions pkg/handlers/resource_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,47 @@ func (h *ResourceHandler) DeleteByOwner(w http.ResponseWriter, r *http.Request)
}
handleSoftDelete(w, r, cfg)
}

func (h *ResourceHandler) ForceDelete(w http.ResponseWriter, r *http.Request) {
var req openapi.ForceDeleteRequest
cfg := &handlerConfig{
MarshalInto: &req,
Validate: []validate{
validateNotEmpty(&req, "Reason", "reason"),
validateMaxLength(&req, "Reason", "reason", maxReasonLength),
},
Action: func() (interface{}, *errors.ServiceError) {
id := mux.Vars(r)["id"]
if err := h.service.ForceDelete(r.Context(), h.descriptor.Kind, id, req.Reason); err != nil {
return nil, err
}
return nil, nil
},
}
handleForceDelete(w, r, cfg)
}

func (h *ResourceHandler) ForceDeleteByOwner(w http.ResponseWriter, r *http.Request) {
var req openapi.ForceDeleteRequest
cfg := &handlerConfig{
MarshalInto: &req,
Validate: []validate{
validateNotEmpty(&req, "Reason", "reason"),
validateMaxLength(&req, "Reason", "reason", maxReasonLength),
},
Action: func() (interface{}, *errors.ServiceError) {
vars := mux.Vars(r)
parentID, id := vars["parent_id"], vars["id"]

if _, err := h.service.GetByOwner(r.Context(), h.descriptor.Kind, id, parentID); err != nil {
return nil, err
}

if err := h.service.ForceDelete(r.Context(), h.descriptor.Kind, id, req.Reason); err != nil {
return nil, err
}
return nil, nil
},
}
handleForceDelete(w, r, cfg)
}
172 changes: 172 additions & 0 deletions pkg/handlers/resource_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,3 +650,175 @@ func TestResourceHandler_DeleteByOwner(t *testing.T) {
})
}
}

func TestResourceHandler_ForceDelete(t *testing.T) {
RegisterTestingT(t)

resourceID := "ch-123"

tests := []struct {
setupMock func(mock *services.MockResourceService)
name string
body string
expectedStatusCode int
}{
{
name: "Success 204 - resource force-deleted",
body: `{"reason": "Stuck in finalizing for 2 hours"}`,
setupMock: func(mock *services.MockResourceService) {
mock.EXPECT().
ForceDelete(gomock.Any(), "Channel", resourceID, "Stuck in finalizing for 2 hours").
Return(nil)
},
expectedStatusCode: http.StatusNoContent,
},
{
name: "Error 400 - malformed JSON",
body: `not json`,
setupMock: func(mock *services.MockResourceService) {
},
expectedStatusCode: http.StatusBadRequest,
},
{
name: "Error 400 - empty reason",
body: `{"reason": ""}`,
setupMock: func(mock *services.MockResourceService) {
},
expectedStatusCode: http.StatusBadRequest,
},
{
name: "Error 400 - reason exceeds max length",
body: `{"reason": "` + strings.Repeat("x", maxReasonLength+1) + `"}`,
setupMock: func(mock *services.MockResourceService) {
},
expectedStatusCode: http.StatusBadRequest,
},
{
name: "Error 404 - resource not found",
body: `{"reason": "some reason"}`,
setupMock: func(mock *services.MockResourceService) {
mock.EXPECT().
ForceDelete(gomock.Any(), "Channel", resourceID, "some reason").
Return(errors.NotFound("Channel with id='%s' not found", resourceID))
},
expectedStatusCode: http.StatusNotFound,
},
{
name: "Error 409 - resource not in Finalizing state",
body: `{"reason": "some reason"}`,
setupMock: func(mock *services.MockResourceService) {
mock.EXPECT().
ForceDelete(gomock.Any(), "Channel", resourceID, "some reason").
Return(errors.ConflictState("Channel '%s' is not in Finalizing state", resourceID))
},
expectedStatusCode: http.StatusConflict,
},
{
name: "Error 500 - service internal error",
body: `{"reason": "some reason"}`,
setupMock: func(mock *services.MockResourceService) {
mock.EXPECT().
ForceDelete(gomock.Any(), "Channel", resourceID, "some reason").
Return(errors.GeneralError("database connection lost"))
},
expectedStatusCode: http.StatusInternalServerError,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
RegisterTestingT(t)

ctrl := gomock.NewController(t)
defer ctrl.Finish()

handler, mockSvc := newTestResourceHandler(ctrl)
tt.setupMock(mockSvc)

reqURL := "/api/hyperfleet/v1/channels/" + resourceID + "/force-delete"
req := httptest.NewRequest(http.MethodPost, reqURL, strings.NewReader(tt.body))
req.Header.Set("Content-Type", "application/json")
req = mux.SetURLVars(req, map[string]string{"id": resourceID})

rr := httptest.NewRecorder()
handler.ForceDelete(rr, req)

Expect(rr.Code).To(Equal(tt.expectedStatusCode))

if tt.expectedStatusCode == http.StatusNoContent {
Expect(rr.Body.Len()).To(Equal(0))
}
})
}
}

func TestResourceHandler_ForceDeleteByOwner(t *testing.T) {
RegisterTestingT(t)

parentID := "ch-1"
versionID := "v-1"

tests := []struct {
setupMock func(mock *services.MockResourceService)
name string
body string
expectedStatusCode int
}{
{
name: "Success 204 - nested resource force-deleted",
body: `{"reason": "Stuck in finalizing"}`,
setupMock: func(mock *services.MockResourceService) {
mock.EXPECT().
GetByOwner(gomock.Any(), "Version", versionID, parentID).
Return(&api.Resource{Meta: api.Meta{ID: versionID}, Kind: "Version"}, nil)
mock.EXPECT().
ForceDelete(gomock.Any(), "Version", versionID, "Stuck in finalizing").
Return(nil)
},
expectedStatusCode: http.StatusNoContent,
},
{
name: "Error 404 - ownership mismatch",
body: `{"reason": "some reason"}`,
setupMock: func(mock *services.MockResourceService) {
mock.EXPECT().
GetByOwner(gomock.Any(), "Version", versionID, parentID).
Return(nil, errors.NotFound("Version with id='%s' not found for owner '%s'", versionID, parentID))
},
expectedStatusCode: http.StatusNotFound,
},
{
name: "Error 400 - empty reason",
body: `{"reason": ""}`,
setupMock: func(mock *services.MockResourceService) {
},
expectedStatusCode: http.StatusBadRequest,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
RegisterTestingT(t)

ctrl := gomock.NewController(t)
defer ctrl.Finish()

handler, mockSvc := newTestVersionHandler(ctrl)
tt.setupMock(mockSvc)

reqURL := "/api/hyperfleet/v1/channels/" + parentID + "/versions/" + versionID + "/force-delete"
req := httptest.NewRequest(http.MethodPost, reqURL, strings.NewReader(tt.body))
req.Header.Set("Content-Type", "application/json")
req = mux.SetURLVars(req, map[string]string{"parent_id": parentID, "id": versionID})

rr := httptest.NewRecorder()
handler.ForceDeleteByOwner(rr, req)

Expect(rr.Code).To(Equal(tt.expectedStatusCode))

if tt.expectedStatusCode == http.StatusNoContent {
Expect(rr.Body.Len()).To(Equal(0))
}
})
}
}
Loading