Skip to content

Commit

Permalink
feat(flag): add --label flag to TaskBase
Browse files Browse the repository at this point in the history
So that all tasks can use the --label flag.
  • Loading branch information
Michal-Leszczynski authored and karol-kokoszka committed Aug 9, 2024
1 parent f4dcb2f commit 80029fa
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkg/command/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func (w Wrapper) name(p *string) {
w.fs.StringVar(p, "name", "", usage["name"])
}

func (w Wrapper) label(p *Label) {
w.fs.Var(p, "label", usage["label"])
}

func (w Wrapper) cron(p *Cron) {
w.fs.VarP(p, "cron", "", usage["cron"])
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/command/flag/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type TaskBase struct {

enabled bool
name string
label Label
cron Cron
window []string
timezone Timezone
Expand Down Expand Up @@ -46,6 +47,7 @@ func (cmd *TaskBase) Init() {
w := Wrap(cmd.Flags())
w.enabled(&cmd.enabled)
w.name(&cmd.name)
w.label(&cmd.label)
w.cron(&cmd.cron)
w.window(&cmd.window)
w.timezone(&cmd.timezone)
Expand All @@ -66,6 +68,7 @@ func (cmd *TaskBase) CreateTask(taskType string) *managerclient.Task {
Type: taskType,
Enabled: cmd.enabled,
Name: cmd.name,
Labels: cmd.label.NewLabels(),
Schedule: &managerclient.Schedule{
Cron: cmd.cron.Value(),
Window: cmd.window,
Expand All @@ -90,6 +93,10 @@ func (cmd *TaskBase) UpdateTask(task *managerclient.Task) bool {
task.Name = cmd.name
ok = true
}
if cmd.Flag("label").Changed {
task.Labels = cmd.label.ApplyDiff(task.Labels)
ok = true
}
if cmd.Flag("cron").Changed {
task.Schedule.Cron = cmd.cron.Value()
ok = true
Expand Down
83 changes: 83 additions & 0 deletions pkg/command/repair/cmd_api_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (C) 2024 ScyllaDB

//go:build all || api_integration
// +build all api_integration

package repair

import (
"bytes"
"context"
"maps"
"os/exec"
"strings"
"testing"

"github.com/scylladb/scylla-manager/v3/pkg/managerclient"
"github.com/scylladb/scylla-manager/v3/pkg/util/uuid"
"github.com/scylladb/scylla-manager/v3/swagger/gen/scylla-manager/models"
)

const (
authToken = "token"
clusterIntroHost = "192.168.200.11"
)

func TestSctoolRepairLabelIntegrationAPITest(t *testing.T) {
client, err := managerclient.NewClient("http://localhost:5080/api/v1")
if err != nil {
t.Fatalf("Unable to create managerclient to consume managet HTTP API, err = {%v}", err)
}

clusterID, err := client.CreateCluster(context.Background(), &models.Cluster{
AuthToken: authToken,
Host: clusterIntroHost,
})
if err != nil {
t.Fatalf("Unable to create cluster for further listing, err = {%v}", err)
}

defer func() {
if err := client.DeleteCluster(context.Background(), clusterID); err != nil {
t.Logf("Failed to delete cluster, err = {%v}", err)
}
}()

cmd := exec.Command("./sctool.api-tests", "repair", "--cluster", clusterID, "--label", "k1=v1,k2=v2")
var stderr bytes.Buffer
cmd.Stderr = &stderr
cmd.Dir = "/scylla-manager"

rawRepairTaskID, err := cmd.Output()
if err != nil {
t.Fatalf("Unable to create task with sctool repair, err = {%v}, stderr = {%v}", err, stderr.String())
}

repairTaskID, _ := strings.CutSuffix(string(rawRepairTaskID), "\n")
rawTaskID, _ := strings.CutPrefix(repairTaskID, "repair/")
taskID, err := uuid.Parse(rawTaskID)
if err != nil {
t.Fatalf("Unable to parse created task ID, err = {%v}", err)
}

cmd = exec.Command("./sctool.api-tests", "repair", "update", "--cluster", clusterID, repairTaskID, "--label", "k1=v3")
cmd.Stderr = &stderr
cmd.Dir = "/scylla-manager"

_, err = cmd.Output()
if err != nil {
t.Fatalf("Unable to update task with sctool repair update, err = {%v}, stderr = {%v}", err, stderr.String())
}

task, err := client.GetTask(context.Background(), clusterID, "repair", taskID)
if err != nil {
t.Fatalf("Unable to get updated task wtih client, err = {%v}", err)
}

if !maps.Equal(task.Labels, map[string]string{
"k2": "v2",
"k1": "v3",
}) {
t.Fatalf("Labels mismatch {%v}", task.Labels)
}
}
1 change: 1 addition & 0 deletions pkg/managerclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ func (c *Client) UpdateTask(ctx context.Context, clusterID string, t *Task) erro
TaskFields: &models.TaskUpdate{
Enabled: t.Enabled,
Name: t.Name,
Labels: t.Labels,
Schedule: t.Schedule,
Tags: t.Tags,
Properties: t.Properties,
Expand Down
2 changes: 1 addition & 1 deletion pkg/managerclient/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ func makeTaskUpdate(t *Task) *models.TaskUpdate {
Type: t.Type,
Enabled: t.Enabled,
Name: t.Name,
Labels: t.Labels,
Schedule: t.Schedule,
Tags: t.Tags,
Properties: t.Properties,
}
}
Expand Down

0 comments on commit 80029fa

Please sign in to comment.