Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add _internal/delete_all to delete all data #1833

Merged
merged 3 commits into from
Jan 5, 2025
Merged
Changes from 2 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
30 changes: 30 additions & 0 deletions fakestorage/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,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 @@ -361,6 +362,35 @@ func (s *Server) reseedServer(r *http.Request) jsonResponse {
return jsonResponse{data: fromBackendObjects(backendObjects)}
}

func (s *Server) deleteAllFiles(r *http.Request) jsonResponse {
storageRoot := s.options.StorageRoot
storageType := "filesystem"

if storageRoot != "" {
if err := os.RemoveAll(storageRoot); err != nil {
return jsonResponse{
status: http.StatusInternalServerError,
errorMessage: err.Error(),
}
}

if err := os.MkdirAll(filepath.Join(storageRoot), 0o700); err != nil {
return jsonResponse{
status: http.StatusInternalServerError,
errorMessage: err.Error(),
}
}
} else {
storageType = "memory"
s.backend, _ = backend.NewStorageMemory(nil)
}

return jsonResponse{
status: http.StatusOK,
data: map[string]string{"message": fmt.Sprintf("All files deleted successfully from %s", storageType)},
}
drehelis marked this conversation as resolved.
Show resolved Hide resolved
}

func generateObjectsFromFiles(folder string) ([]Object, []string) {
var objects []Object
var emptyBuckets []string
Expand Down