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
3 changes: 2 additions & 1 deletion cmd/admin/v2/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func AddCmds(cmd *cobra.Command, c *config.Config) {
adminCmd := &cobra.Command{
Use: "admin",
Short: "admin commands",
Long: "",
Long: "these commands utilize the admin api, which can only be accessed by metal-stack operators.",
SilenceUsage: true,
Hidden: true,
}
Expand All @@ -19,6 +19,7 @@ func AddCmds(cmd *cobra.Command, c *config.Config) {
adminCmd.AddCommand(newImageCmd(c))
adminCmd.AddCommand(newProjectCmd(c))
adminCmd.AddCommand(newSwitchCmd(c))
adminCmd.AddCommand(newTaskCmd(c))
adminCmd.AddCommand(newTenantCmd(c))
adminCmd.AddCommand(newTokenCmd(c))

Expand Down
125 changes: 125 additions & 0 deletions cmd/admin/v2/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package v2

import (
"fmt"

adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
"github.com/metal-stack/cli/cmd/config"
"github.com/metal-stack/cli/cmd/sorters"
"github.com/metal-stack/metal-lib/pkg/genericcli"
"github.com/metal-stack/metal-lib/pkg/genericcli/printers"
"github.com/metal-stack/metal-lib/pkg/pointer"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

type task struct {
c *config.Config
}

func newTaskCmd(c *config.Config) *cobra.Command {
w := &task{
c: c,
}

cmdsConfig := &genericcli.CmdsConfig[any, any, *adminv2.TaskInfo]{
BinaryName: config.BinaryName,
GenericCLI: genericcli.NewGenericCLI(w).WithFS(c.Fs),
Singular: "task",
Plural: "tasks",
Description: "get task insights",
DescribePrinter: func() printers.Printer { return c.DescribePrinter },
ListPrinter: func() printers.Printer { return c.ListPrinter },
Sorter: sorters.TaskSorter(),
DescribeCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().String("queue", "default", "the queue for which tasks should be described")
},
ListCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().String("queue", "", "the queue for which tasks should be listed")
},
DeleteCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().String("queue", "default", "the queue of the task which should be delete")
},
OnlyCmds: genericcli.OnlyCmds(genericcli.ListCmd, genericcli.DescribeCmd, genericcli.DeleteCmd),
}

queueCmd := &cobra.Command{
Use: "queues",
Short: "list all queues",
RunE: func(cmd *cobra.Command, args []string) error {
return w.queues()
},
}

return genericcli.NewCmds(cmdsConfig, queueCmd)
}

func (t *task) queues() error {
ctx, cancel := t.c.NewRequestContext()
defer cancel()

req := &adminv2.TaskServiceQueuesRequest{}

resp, err := t.c.Client.Adminv2().Task().Queues(ctx, req)
if err != nil {
return fmt.Errorf("failed to get task queues: %w", err)
}

return t.c.ListPrinter.Print(resp)
}

func (t *task) Get(id string) (*adminv2.TaskInfo, error) {
ctx, cancel := t.c.NewRequestContext()
defer cancel()

req := &adminv2.TaskServiceGetRequest{TaskId: id, Queue: viper.GetString("queue")}

resp, err := t.c.Client.Adminv2().Task().Get(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to get task: %w", err)
}

return resp.Task, nil
}

func (t *task) List() ([]*adminv2.TaskInfo, error) {
ctx, cancel := t.c.NewRequestContext()
defer cancel()

req := &adminv2.TaskServiceListRequest{
Queue: pointer.PointerOrNil(viper.GetString("queue")),
}

resp, err := t.c.Client.Adminv2().Task().List(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to list tasks: %w", err)
}

return resp.Tasks, nil
}

func (t *task) Delete(id string) (*adminv2.TaskInfo, error) {
ctx, cancel := t.c.NewRequestContext()
defer cancel()

req := &adminv2.TaskServiceDeleteRequest{TaskId: id, Queue: viper.GetString("queue")}

_, err := t.c.Client.Adminv2().Task().Delete(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to delete task: %w", err)
}

return nil, nil
}

func (t *task) Create(rq any) (*adminv2.TaskInfo, error) {
panic("unimplemented")
}

func (t *task) Convert(r *adminv2.TaskInfo) (string, any, any, error) {
panic("unimplemented")
}

func (t *task) Update(rq any) (*adminv2.TaskInfo, error) {
panic("unimplemented")
}
2 changes: 1 addition & 1 deletion cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (l *login) login() error {
}

tokenResp, err := mc.Apiv2().Token().Create(context.Background(), &apiv2.TokenServiceCreateRequest{
Description: "admin access issues by metal cli",
Description: "admin access issued by metal cli",
Expires: durationpb.New(3 * time.Hour),
AdminRole: new(apiv2.AdminRole((apiv2.AdminRole_value[viper.GetString("admin-role")]))),
})
Expand Down
35 changes: 35 additions & 0 deletions cmd/sorters/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package sorters

import (
adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
"github.com/metal-stack/metal-lib/pkg/multisort"
)

func TaskSorter() *multisort.Sorter[*adminv2.TaskInfo] {
return multisort.New(multisort.FieldMap[*adminv2.TaskInfo]{
"id": func(a, b *adminv2.TaskInfo, descending bool) multisort.CompareResult {
return multisort.Compare(a.Id, b.Id, descending)
},
"queue": func(a, b *adminv2.TaskInfo, descending bool) multisort.CompareResult {
return multisort.Compare(a.Queue, b.Queue, descending)
},
"type": func(a, b *adminv2.TaskInfo, descending bool) multisort.CompareResult {
return multisort.Compare(a.Type, b.Type, descending)
},
"state": func(a, b *adminv2.TaskInfo, descending bool) multisort.CompareResult {
return multisort.Compare(adminv2.TaskState_name[int32(a.State)], adminv2.TaskState_name[int32(b.State)], descending)
},
"retried": func(a, b *adminv2.TaskInfo, descending bool) multisort.CompareResult {
return multisort.Compare(a.Retried, b.Retried, descending)
},
"completed-at": func(a, b *adminv2.TaskInfo, descending bool) multisort.CompareResult {
return multisort.Compare(a.CompletedAt.AsTime().UnixMilli(), b.CompletedAt.AsTime().UnixMilli(), descending)
},
"last-failed-at": func(a, b *adminv2.TaskInfo, descending bool) multisort.CompareResult {
return multisort.Compare(a.LastFailedAt.AsTime().UnixMilli(), b.LastFailedAt.AsTime().UnixMilli(), descending)
},
"deadline-at": func(a, b *adminv2.TaskInfo, descending bool) multisort.CompareResult {
return multisort.Compare(a.Deadline.AsTime().UnixMilli(), b.Deadline.AsTime().UnixMilli(), descending)
},
}, multisort.Keys{{ID: "id"}})
}
7 changes: 7 additions & 0 deletions cmd/tableprinters/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ func (t *TablePrinter) ToHeaderAndRows(data any, wide bool) ([]string, [][]strin
case []*apiv2.ProjectMember:
return t.ProjectMemberTable(d, wide)

case *adminv2.TaskInfo:
return t.TaskTable(pointer.WrapInSlice(d), wide)
case []*adminv2.TaskInfo:
return t.TaskTable(d, wide)
case *adminv2.TaskServiceQueuesResponse:
return t.TaskQueueTable(d, wide)

case *apiv2.Token:
return t.TokenTable(pointer.WrapInSlice(d), wide)
case []*apiv2.Token:
Expand Down
74 changes: 74 additions & 0 deletions cmd/tableprinters/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package tableprinters

import (
"time"

"github.com/google/uuid"
"github.com/metal-stack/api/go/enum"
adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
"github.com/metal-stack/metal-lib/pkg/genericcli"
)

func (t *TablePrinter) TaskTable(data []*adminv2.TaskInfo, wide bool) ([]string, [][]string, error) {
var (
rows [][]string
)

header := []string{"ID", "Queue", "When", "Type", "State"}

if wide {
header = []string{"ID", "Queue", "When", "Type", "State", "Issued At", "Payload", "Result"}
}

for _, task := range data {
var (
id = task.Id
queue = task.Queue
typeString = task.Type
payload = genericcli.TruncateEnd(string(task.Payload), 40)
result = genericcli.TruncateEnd(string(task.Result), 40)
)

state, err := enum.GetStringValue(task.State)
if err != nil {
state = new("unknown")
}

parsed, err := uuid.Parse(id)
if err != nil {
return nil, nil, err
}

var (
sec, nano = parsed.Time().UnixTime()
issuedAt = time.Unix(sec, nano).UTC()
when = humanizeDuration(time.Since(issuedAt)) + " ago"
)

if wide {
rows = append(rows, []string{id, queue, when, typeString, *state, issuedAt.String(), payload, result})
} else {
rows = append(rows, []string{id, queue, when, typeString, *state})
}
}

t.t.DisableAutoWrap(false)

return header, rows, nil
}

func (t *TablePrinter) TaskQueueTable(data *adminv2.TaskServiceQueuesResponse, _ bool) ([]string, [][]string, error) {
var (
rows [][]string
)

header := []string{"Queue"}

for _, queue := range data.Queues {
rows = append(rows, []string{queue})
}

t.t.DisableAutoWrap(false)

return header, rows, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/dustin/go-humanize v1.0.1
github.com/fatih/color v1.18.0
github.com/google/go-cmp v0.7.0
github.com/google/uuid v1.6.0
github.com/metal-stack/api v0.0.61
github.com/metal-stack/metal-lib v0.24.0
github.com/metal-stack/v v1.0.3
Expand Down Expand Up @@ -41,7 +42,6 @@ require (
github.com/goccy/go-yaml v1.19.2 // indirect
github.com/golang-jwt/jwt/v5 v5.3.1 // indirect
github.com/google/cel-go v0.28.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.18.5 // indirect
github.com/klauspost/connect-compress/v2 v2.1.1 // indirect
Expand Down
4 changes: 1 addition & 3 deletions pkg/helpers/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func ToPhase(phase string) *apiv2.AuditPhase {
return new(apiv2.AuditPhase(p))
}

func TryPrettifyBody(trace *apiv2.AuditTrace) *apiv2.AuditTrace {
func TryPrettifyBody(trace *apiv2.AuditTrace) {
if trace.Body != nil {
trimmed := strings.Trim(*trace.Body, `"`)
body := map[string]any{}
Expand All @@ -44,6 +44,4 @@ func TryPrettifyBody(trace *apiv2.AuditTrace) *apiv2.AuditTrace {
}
}
}

return trace
}
Loading
Loading