Skip to content

Commit

Permalink
feat: Add _internal/delete_all to delete all data (#1833)
Browse files Browse the repository at this point in the history
* feat: Add  to delete all data

* fix: lint

* cr: move DeleteAllFiles to be part of the backend interface
  • Loading branch information
drehelis authored Jan 5, 2025
1 parent 35f10fe commit c9ce436
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions fakestorage/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ func (s *Server) buildMuxer() {
handler.Path("/_internal/config").Methods(http.MethodPut).HandlerFunc(jsonToHTTPHandler(s.updateServerConfig))
handler.MatcherFunc(s.publicHostMatcher).Path("/_internal/config").Methods(http.MethodPut).HandlerFunc(jsonToHTTPHandler(s.updateServerConfig))
handler.Path("/_internal/reseed").Methods(http.MethodPut, http.MethodPost).HandlerFunc(jsonToHTTPHandler(s.reseedServer))
handler.Path("/_internal/delete_all").Methods(http.MethodPost).HandlerFunc(jsonToHTTPHandler(s.deleteAllFiles))
// Internal - end

// XML API
Expand Down Expand Up @@ -367,6 +368,20 @@ func (s *Server) reseedServer(r *http.Request) jsonResponse {
return jsonResponse{data: fromBackendObjects(backendObjects)}
}

func (s *Server) deleteAllFiles(r *http.Request) jsonResponse {
if err := s.backend.DeleteAllFiles(); err != nil {
return jsonResponse{
status: http.StatusInternalServerError,
errorMessage: err.Error(),
}
}

return jsonResponse{
status: http.StatusOK,
data: map[string]string{"message": "All files deleted successfully"},
}
}

func generateObjectsFromFiles(folder string) ([]Object, []string) {
var objects []Object
var emptyBuckets []string
Expand Down
9 changes: 9 additions & 0 deletions internal/backend/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,12 @@ func (s *storageFS) ComposeObject(bucketName string, objectNames []string, desti

return result, nil
}

func (s *storageFS) DeleteAllFiles() error {
s.mtx.Lock()
defer s.mtx.Unlock()
if err := os.RemoveAll(s.rootDir); err != nil {
return err
}
return os.MkdirAll(s.rootDir, 0o700)
}
7 changes: 7 additions & 0 deletions internal/backend/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,10 @@ func (s *storageMemory) ComposeObject(bucketName string, objectNames []string, d

return result, nil
}

func (s *storageMemory) DeleteAllFiles() error {
s.mtx.Lock()
defer s.mtx.Unlock()
s.buckets = make(map[string]bucketInMemory)
return nil
}
1 change: 1 addition & 0 deletions internal/backend/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Storage interface {
PatchObject(bucketName, objectName string, attrsToUpdate ObjectAttrs) (StreamingObject, error)
UpdateObject(bucketName, objectName string, attrsToUpdate ObjectAttrs) (StreamingObject, error)
ComposeObject(bucketName string, objectNames []string, destinationName string, metadata map[string]string, contentType string) (StreamingObject, error)
DeleteAllFiles() error
}

type Error string
Expand Down

0 comments on commit c9ce436

Please sign in to comment.