Skip to content

Commit

Permalink
Return destroyed dataset list on destroy operation
Browse files Browse the repository at this point in the history
Signed-off-by: Raamsri Kumar <[email protected]>
  • Loading branch information
raamsri committed Jan 27, 2025
1 parent 97344f8 commit 4f7bcda
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 21 deletions.
5 changes: 3 additions & 2 deletions pkg/zfs/api/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,13 @@ func (h *DatasetHandler) destroyDataset(c *gin.Context) {
return
}

if err := h.manager.Destroy(c.Request.Context(), req); err != nil {
result, err := h.manager.Destroy(c.Request.Context(), req)
if err != nil {
APIError(c, err)
return
}

c.Status(http.StatusNoContent)
c.JSON(http.StatusOK, gin.H{"result": result})
}

func (h *DatasetHandler) getProperty(c *gin.Context) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/zfs/api/dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,14 @@ func TestDatasetAPI(t *testing.T) {
Force: true,
}
body, _ := json.Marshal(destroyReq)
req := httptest.NewRequest(http.MethodDelete, dsURI, bytes.NewBuffer(body))
req := httptest.NewRequest(http.MethodPost, dsURI+"/delete", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)

if w.Code != http.StatusNoContent {
if w.Code != http.StatusOK {
t.Errorf("destroy dataset returned wrong status: got %v want %v",
w.Code, http.StatusNoContent)
w.Code, http.StatusOK)
t.Errorf("error: %v", w.Body.String())
}
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/zfs/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (h *DatasetHandler) RegisterRoutes(router *gin.RouterGroup) {
// Dataset operations
dataset.POST("/list", h.listDatasets)

dataset.DELETE("",
dataset.POST("/delete",
ValidateZFSEntityName(common.TypeZFSEntityMask),
h.destroyDataset)

Expand Down
40 changes: 28 additions & 12 deletions pkg/zfs/dataset/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,16 @@ func (m *Manager) List(ctx context.Context, cfg ListConfig) (ListResult, error)
}

// Destroy removes a dataset
func (m *Manager) Destroy(ctx context.Context, dc DestroyConfig) error {
args := []string{"destroy"}
func (m *Manager) Destroy(ctx context.Context, dc DestroyConfig) (DestroyResult, error) {
args := []string{"destroy", "-p", "-v"}
result := DestroyResult{
Destroyed: make([]string, 0),
}

if dc.RecursiveDestroyChildren {
args = append(args, "-r")
} else if dc.RecursiveDestroyDependents {
}
if dc.RecursiveDestroyDependents {
args = append(args, "-R")
}
if dc.Force {
Expand All @@ -127,12 +131,6 @@ func (m *Manager) Destroy(ctx context.Context, dc DestroyConfig) error {
if dc.DryRun {
args = append(args, "-n")
}
if dc.Parsable {
args = append(args, "-p")
}
if dc.Verbose {
args = append(args, "-v")
}

args = append(args, dc.Name)

Expand All @@ -141,13 +139,31 @@ func (m *Manager) Destroy(ctx context.Context, dc DestroyConfig) error {
out, err := m.executor.Execute(ctx, opts, "zfs destroy", args...)
if err != nil {
if len(out) > 0 {
return errors.Wrap(err, errors.ZFSDatasetDestroy).
return result, errors.Wrap(err, errors.ZFSDatasetDestroy).
WithMetadata("output", string(out))
}
return errors.Wrap(err, errors.ZFSDatasetDestroy)
return result, errors.Wrap(err, errors.ZFSDatasetDestroy)
}

return nil
// Parse output lines
lines := strings.Split(string(out), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Extract dataset name from "destroy dataset" line
// Using Contains() instead of HasPrefix() and handling multiple spaces
if strings.Contains(line, "destroy") {
// Split on whitespace and take the last part which should be the dataset name
parts := strings.Fields(line)
if len(parts) >= 2 {
result.Destroyed = append(result.Destroyed, parts[len(parts)-1])
}
}
}

return result, nil
}

// GetProperty gets a dataset property
Expand Down
2 changes: 1 addition & 1 deletion pkg/zfs/dataset/dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func TestDatasetOperations(t *testing.T) {
})

t.Run("Destroy", func(t *testing.T) {
err := datasetMgr.Destroy(context.Background(), DestroyConfig{
_, err := datasetMgr.Destroy(context.Background(), DestroyConfig{
NameConfig: NameConfig{
Name: poolName + "/fs1_renamed",
},
Expand Down
7 changes: 5 additions & 2 deletions pkg/zfs/dataset/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ type DestroyConfig struct {
RecursiveDestroyChildren bool `json:"recursive_destroy_children"` // Recursively destroy all children
Force bool `json:"force"`
DryRun bool `json:"dry_run"`
Parsable bool `json:"parsable"` // -P Print machine-parsable verbose information about the created dataset
Verbose bool `json:"verbose"`
}

// DestroyResult represents the output of a destroy operation
type DestroyResult struct {
Destroyed []string `json:"destroyed"` // List of datasets that would be/were destroyed
}

// FilesystemConfig for filesystem-specific creation
Expand Down

0 comments on commit 4f7bcda

Please sign in to comment.