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
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
15 changes: 15 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,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 @@ -467,3 +467,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 @@ -390,3 +390,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
Loading