Skip to content
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
13 changes: 9 additions & 4 deletions pkg/restapi/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"net/http"
"strings"

"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
Expand Down Expand Up @@ -56,7 +57,7 @@ func (h backupHandler) locationsCtx(next http.Handler) http.Handler {

// Read locations from the request
if v := r.FormValue("locations"); v != "" {
for _, v := range r.Form["locations"] {
for v := range strings.SplitSeq(v, ",") {
var l backupspec.Location
if err := l.UnmarshalText([]byte(v)); err != nil {
respondBadRequest(w, r, err)
Comment thread
karol-kokoszka marked this conversation as resolved.
Expand Down Expand Up @@ -112,9 +113,11 @@ func (h backupHandler) mustLocationsFromCtx(r *http.Request) []backupspec.Locati
func (h backupHandler) listFilterCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
filter := backup.ListFilter{
Keyspace: r.Form["keyspace"],
SnapshotTag: r.FormValue("snapshot_tag"),
}
if v := r.FormValue("keyspace"); v != "" {
filter.Keyspace = strings.Split(r.FormValue("keyspace"), ",")
Comment thread
karol-kokoszka marked this conversation as resolved.
}

c := mustClusterFromCtx(r)
if v := r.FormValue("query_cluster_id"); v != "" {
Expand Down Expand Up @@ -184,8 +187,10 @@ func (h backupHandler) listFiles(w http.ResponseWriter, r *http.Request) {
}

func (h backupHandler) deleteSnapshot(w http.ResponseWriter, r *http.Request) {
snapshotTags := r.Form["snapshot_tags"]
if len(snapshotTags) == 0 {
var snapshotTags []string
if v := r.FormValue("snapshot_tags"); v != "" {
snapshotTags = strings.Split(v, ",")
} else {
Comment thread
karol-kokoszka marked this conversation as resolved.
respondBadRequest(w, r, errors.New("missing snapshot tags"))
return
}
Expand Down
57 changes: 53 additions & 4 deletions pkg/restapi/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"

"github.com/golang/mock/gomock"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/scylladb/scylla-manager/v3/pkg/service/backup"
"github.com/scylladb/scylla-manager/v3/pkg/util/timeutc"
"github.com/scylladb/scylla-manager/v3/pkg/util/uuid"
slices2 "github.com/scylladb/scylla-manager/v3/pkg/util2/slices"
)

//go:generate mockgen -destination mock_backupservice_test.go -mock_names BackupService=MockBackupService -package restapi github.com/scylladb/scylla-manager/v3/pkg/restapi BackupService
Expand All @@ -28,14 +30,16 @@ func listBackupFilesRequest(clusterID uuid.UUID) *http.Request {
return httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/cluster/%s/backups/files", clusterID.String()), nil)
}

func deleteSnapshotRequest(clusterID uuid.UUID) *http.Request {
return httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/api/v1/cluster/%s/backups", clusterID.String()), nil)
}

func withForm(r *http.Request, locations []backupspec.Location, filter backup.ListFilter, query string) *http.Request {
r.Form = url.Values{}
for _, l := range locations {
r.Form.Add("locations", l.String())
}
r.Form.Set("locations", strings.Join(slices2.MapToString(locations), ","))
r.Form.Add("cluster_id", filter.ClusterID.String())
r.Form.Add("query_cluster_id", query)
r.Form["keyspace"] = filter.Keyspace
r.Form.Set("keyspace", strings.Join(filter.Keyspace, ","))
r.Form.Add("snapshot_tag", filter.SnapshotTag)
a, _ := filter.MinDate.MarshalText()
r.Form.Add("min_date", string(a))
Expand Down Expand Up @@ -196,3 +200,48 @@ func TestBackupListFiles(t *testing.T) {
h.ServeHTTP(w, r)
assertJsonBody(t, w, golden)
}

func TestBackupDeleteSnapshot(t *testing.T) {
t.Parallel()

ctrl := gomock.NewController(t)
defer ctrl.Finish()
cm := restapi.NewMockClusterService(ctrl)
bm := restapi.NewMockBackupService(ctrl)

services := restapi.Services{
Cluster: cm,
Backup: bm,
}

h := restapi.New(services, log.Logger{})

const (
tag1 = "sm_20240101120000UTC"
tag2 = "sm_20240102120000UTC"
)

var (
cluster = givenCluster()

locations = []backupspec.Location{
{Provider: backupspec.S3, Path: "foo"},
{Provider: backupspec.S3, Path: "bar"},
}
snapshotTags = []string{tag1, tag2}
)

cm.EXPECT().GetCluster(gomock.Any(), cluster.ID.String()).Return(cluster, nil)
bm.EXPECT().DeleteSnapshot(gomock.Any(), cluster.ID, locations, snapshotTags).Return(nil)

r := deleteSnapshotRequest(cluster.ID)
r.Form = url.Values{}
r.Form.Set("locations", strings.Join(slices2.MapToString(locations), ","))
r.Form.Set("snapshot_tags", strings.Join(snapshotTags, ","))
Comment thread
karol-kokoszka marked this conversation as resolved.
w := httptest.NewRecorder()
h.ServeHTTP(w, r)

if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
}
}
Loading