Skip to content

Commit

Permalink
enhance(workers): flags for new filters (#517)
Browse files Browse the repository at this point in the history
* enhance(workers): filters for list workers

* support time and duration
  • Loading branch information
ecrupper authored Jan 15, 2024
1 parent 1a00c3b commit d23ab1a
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 29 deletions.
17 changes: 15 additions & 2 deletions action/worker/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package worker

import (
"fmt"

"github.com/go-vela/cli/internal/output"

"github.com/go-vela/sdk-go/vela"
Expand All @@ -16,11 +18,22 @@ func (c *Config) Get(client *vela.Client) error {

logrus.Tracef("capturing workers")

filters := &vela.WorkerListOptions{
CheckedInAfter: c.CheckedInAfter,
}

if c.Active != nil {
filters.Active = fmt.Sprintf("%b", c.Active)
}

if c.CheckedInBefore > 0 {
filters.CheckedInBefore = c.CheckedInBefore
}

// send API call to capture a list of workers
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#WorkerService.GetAll
// TODO: add ability to pass in filter options
workers, _, err := client.Worker.GetAll(nil)
workers, _, err := client.Worker.GetAll(filters)
if err != nil {
return err
}
Expand Down
42 changes: 30 additions & 12 deletions action/worker/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func TestWorker_Config_Get(t *testing.T) {
// setup test server
s := httptest.NewServer(server.FakeHandler())

tBool := true
fBool := false

// create a vela client
client, err := vela.NewClient(s.URL, "vela", nil)
if err != nil {
Expand All @@ -29,43 +32,58 @@ func TestWorker_Config_Get(t *testing.T) {
{
failure: false,
config: &Config{
Action: "get",
Output: "",
Action: "get",
Output: "",
CheckedInBefore: 1,
CheckedInAfter: 0,
Active: &tBool,
},
},
{
failure: false,
config: &Config{
Action: "get",
Output: "dump",
Action: "get",
Output: "dump",
CheckedInBefore: 1,
Active: &fBool,
},
},
{
failure: false,
config: &Config{
Action: "get",
Output: "json",
Action: "get",
Output: "json",
CheckedInAfter: 0,
Active: &tBool,
},
},
{
failure: false,
config: &Config{
Action: "get",
Output: "spew",
Action: "get",
Output: "spew",
CheckedInBefore: 1,
CheckedInAfter: 0,
Active: &fBool,
},
},
{
failure: false,
config: &Config{
Action: "get",
Output: "wide",
Action: "get",
Output: "wide",
CheckedInBefore: 1,
CheckedInAfter: 0,
},
},
{
failure: false,
config: &Config{
Action: "get",
Output: "yaml",
Action: "get",
Output: "yaml",
CheckedInBefore: 1,
CheckedInAfter: 0,
Active: &tBool,
},
},
// TODO: mock doesn't have failure for listing workers
Expand Down
2 changes: 1 addition & 1 deletion action/worker/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (c *Config) Update(client *vela.Client) error {
w := &library.Worker{
Hostname: vela.String(c.Hostname),
Address: vela.String(c.Address),
Active: vela.Bool(c.Active),
Active: c.Active,
Routes: vela.Strings(c.Routes),
BuildLimit: vela.Int64(c.BuildLimit),
}
Expand Down
14 changes: 8 additions & 6 deletions action/worker/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func TestWorker_Config_Update(t *testing.T) {
t.Errorf("unable to create client: %v", err)
}

tBool := true

// setup tests
tests := []struct {
failure bool
Expand All @@ -34,7 +36,7 @@ func TestWorker_Config_Update(t *testing.T) {
Address: "myworker.example.com",
Routes: []string{"large", "small"},
BuildLimit: 1,
Active: true,
Active: &tBool,
Output: "",
},
},
Expand All @@ -46,7 +48,7 @@ func TestWorker_Config_Update(t *testing.T) {
Address: "myworker.example.com",
Routes: []string{"large", "small"},
BuildLimit: 1,
Active: true,
Active: &tBool,
Output: "dump",
},
},
Expand All @@ -58,7 +60,7 @@ func TestWorker_Config_Update(t *testing.T) {
Address: "myworker.example.com",
Routes: []string{"large", "small"},
BuildLimit: 1,
Active: true,
Active: &tBool,
Output: "json",
},
},
Expand All @@ -70,7 +72,7 @@ func TestWorker_Config_Update(t *testing.T) {
Address: "myworker.example.com",
Routes: []string{"large", "small"},
BuildLimit: 1,
Active: true,
Active: &tBool,
Output: "spew",
},
},
Expand All @@ -82,7 +84,7 @@ func TestWorker_Config_Update(t *testing.T) {
Address: "myworker.example.com",
Routes: []string{"large", "small"},
BuildLimit: 1,
Active: true,
Active: &tBool,
Output: "yaml",
},
},
Expand All @@ -94,7 +96,7 @@ func TestWorker_Config_Update(t *testing.T) {
Address: "myworker.example.com",
Routes: []string{"large", "small"},
BuildLimit: 1,
Active: true,
Active: &tBool,
Output: "",
},
},
Expand Down
8 changes: 5 additions & 3 deletions action/worker/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

func TestWorker_Config_Validate(t *testing.T) {
// setup tests
tBool := true

tests := []struct {
failure bool
config *Config
Expand Down Expand Up @@ -86,7 +88,7 @@ func TestWorker_Config_Validate(t *testing.T) {
Action: "update",
Hostname: "MyWorker",
Address: "myworker.example.com",
Active: true,
Active: &tBool,
BuildLimit: 1,
Routes: []string{"large", "small"},
Output: "",
Expand All @@ -98,7 +100,7 @@ func TestWorker_Config_Validate(t *testing.T) {
Action: "update",
Hostname: "",
Address: "myworker.example.com",
Active: true,
Active: &tBool,
BuildLimit: 1,
Routes: []string{"large", "small"},
Output: "",
Expand All @@ -110,7 +112,7 @@ func TestWorker_Config_Validate(t *testing.T) {
Action: "update",
Hostname: "",
Address: "::",
Active: true,
Active: &tBool,
BuildLimit: 1,
Routes: []string{"large", "small"},
Output: "",
Expand Down
4 changes: 3 additions & 1 deletion action/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ package worker
type Config struct {
Action string
Address string
CheckedInBefore int64
CheckedInAfter int64
Hostname string
Active bool
Active *bool
Routes []string
BuildLimit int64
RegistrationToken bool
Expand Down
77 changes: 75 additions & 2 deletions command/worker/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package worker

import (
"fmt"
"strconv"
"time"

"github.com/go-vela/cli/action"
"github.com/go-vela/cli/action/worker"
Expand All @@ -21,6 +23,29 @@ var CommandGet = &cli.Command{
Usage: "Display a list of workers",
Action: get,
Flags: []cli.Flag{
// Filter Flags

&cli.StringFlag{
EnvVars: []string{"VELA_ACTIVE", "WORKER_ACTIVE"},
Name: internal.FlagActive,
Aliases: []string{"a"},
Usage: "return workers based on active status",
},

// Time Flags

&cli.StringFlag{
EnvVars: []string{"VELA_CHECKED_IN_BEFORE", "CHECKED_IN_BEFORE"},
Name: internal.FlagBefore,
Aliases: []string{"bf", "until"},
Usage: "before time constraint",
},
&cli.StringFlag{
EnvVars: []string{"VELA_CHECKED_IN_AFTER", "CHECKED_IN_AFTER"},
Name: internal.FlagAfter,
Aliases: []string{"af", "since"},
Usage: "after time constraint",
},

// Output Flags

Expand Down Expand Up @@ -68,12 +93,34 @@ func get(c *cli.Context) error {
return err
}

var active bool

if len(c.String(internal.FlagActive)) > 0 {
active, err = strconv.ParseBool(c.String(internal.FlagActive))
if err != nil {
return err
}
}

before, err := parseUnix(c.String(internal.FlagBefore))
if err != nil {
return fmt.Errorf("unable to parse flag `before`: %w", err)
}

after, err := parseUnix(c.String(internal.FlagAfter))
if err != nil {
return fmt.Errorf("unable to parse flag `after`: %w", err)
}

// create the worker configuration
//
// https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config
w := &worker.Config{
Action: internal.ActionGet,
Output: c.String(internal.FlagOutput),
Action: internal.ActionGet,
Active: &active,
Output: c.String(internal.FlagOutput),
CheckedInBefore: before,
CheckedInAfter: after,
}

// validate worker configuration
Expand All @@ -89,3 +136,29 @@ func get(c *cli.Context) error {
// https://pkg.go.dev/github.com/go-vela/cli/action/worker?tab=doc#Config.Get
return w.Get(client)
}

// parseUnix is a helper function that converts a string of type Time, Duration, or Unix into an int64.
func parseUnix(input string) (int64, error) {
var result int64

if len(input) > 0 {
timestamp, err := time.Parse("2006-01-02T15:04:05", input)
if err != nil {
duration, err := time.ParseDuration(input)
if err != nil {
unix, err := strconv.ParseInt(input, 10, 64)
if err != nil {
return result, fmt.Errorf("invalid input for flag")
}

result = unix
} else {
result = time.Now().Add(-duration).Unix()
}
} else {
result = timestamp.Unix()
}
}

return result, nil
}
Loading

0 comments on commit d23ab1a

Please sign in to comment.