Skip to content

Commit

Permalink
backport deployment filter on tasks cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
tjvman committed Sep 1, 2016
1 parent d9bbeea commit c563bd9
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 54 deletions.
4 changes: 4 additions & 0 deletions cmd/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func (f Factory) New(args []string) (Cmd, error) {
opts.Deployment = cmd.BoshOpts.DeploymentOpt
}

if opts, ok := command.(*TasksOpts); ok {
opts.Deployment = cmd.BoshOpts.DeploymentOpt
}

if len(extraArgs) > 0 {
errMsg := "Command '%T' does not support extra arguments: %s"
return fmt.Errorf(errMsg, command, strings.Join(extraArgs, ", "))
Expand Down
10 changes: 10 additions & 0 deletions cmd/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ var _ = Describe("Factory", func() {
})
})

Describe("tasks command", func() {
It("is passed the deployment flag", func() {
cmd, err := factory.New([]string{"tasks", "--deployment", "deployment"})
Expect(err).ToNot(HaveOccurred())

opts := cmd.Opts.(*TasksOpts)
Expect(opts.Deployment).To(Equal("deployment"))
})
})

Describe("help options", func() {
It("has a help flag", func() {
_, err := factory.New([]string{"--help"})
Expand Down
6 changes: 3 additions & 3 deletions cmd/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ type TaskArgs struct {
}

type TasksOpts struct {
Recent *int `long:"recent" short:"r" description:"Number of tasks to show" optional:"true" optional-value:"30"`

All bool `long:"all" short:"a" description:"Include all task types (ssh, logs, vms, etc)"`
Recent *int `long:"recent" short:"r" description:"Number of tasks to show" optional:"true" optional-value:"30"`
All bool `long:"all" short:"a" description:"Include all task types (ssh, logs, vms, etc)"`
Deployment string

cmd
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (c TaskCmd) Run(opts TaskOpts) error {
var err error

if opts.Args.ID == 0 {
tasks, err := c.director.CurrentTasks(opts.All)
tasks, err := c.director.CurrentTasks(boshdir.TasksFilter{All: opts.All})
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ var _ = Describe("TaskCmd", func() {
It("filters tasks based on 'all' option", func() {
err := act()
Expect(err).ToNot(HaveOccurred())
Expect(director.CurrentTasksArgsForCall(0)).To(Equal(false))
Expect(director.CurrentTasksArgsForCall(0)).To(Equal(boshdir.TasksFilter{}))

opts.All = true

err = act()
Expect(err).ToNot(HaveOccurred())
Expect(director.CurrentTasksArgsForCall(1)).To(Equal(true))
Expect(director.CurrentTasksArgsForCall(1)).To(Equal(boshdir.TasksFilter{All: true}))
})

It("returns error if there are no current tasks", func() {
Expand Down
9 changes: 7 additions & 2 deletions cmd/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ func NewTasksCmd(ui boshui.UI, director boshdir.Director) TasksCmd {
}

func (c TasksCmd) Run(opts TasksOpts) error {
filter := boshdir.TasksFilter{
All: opts.All,
Deployment: opts.Deployment,
}

if opts.Recent != nil {
return c.printTable(c.director.RecentTasks(*opts.Recent, opts.All))
return c.printTable(c.director.RecentTasks(*opts.Recent, filter))
}
return c.printTable(c.director.CurrentTasks(opts.All))
return c.printTable(c.director.CurrentTasks(filter))
}

func (c TasksCmd) printTable(tasks []boshdir.Task, err error) error {
Expand Down
24 changes: 16 additions & 8 deletions cmd/tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,22 @@ var _ = Describe("TasksCmd", func() {
}))
})

It("filters tasks based on 'all' option", func() {
It("filters tasks based options", func() {
director.CurrentTasksReturns(nil, nil)

err := act()
Expect(err).ToNot(HaveOccurred())
Expect(director.CurrentTasksArgsForCall(0)).To(Equal(false))
Expect(director.CurrentTasksArgsForCall(0)).To(Equal(boshdir.TasksFilter{}))

opts.All = true
opts.Deployment = "deployment"

err = act()
Expect(err).ToNot(HaveOccurred())
Expect(director.CurrentTasksArgsForCall(1)).To(Equal(true))
Expect(director.CurrentTasksArgsForCall(1)).To(Equal(boshdir.TasksFilter{
All: true,
Deployment: "deployment",
}))
})

It("returns error if tasks cannot be retrieved", func() {
Expand Down Expand Up @@ -216,18 +220,22 @@ var _ = Describe("TasksCmd", func() {
}))
})

It("filters tasks based on 'all' option", func() {
It("filters tasks based on options", func() {
director.RecentTasksReturns(nil, nil)

Expect(act()).ToNot(HaveOccurred())
_, includeAll := director.RecentTasksArgsForCall(0)
Expect(includeAll).To(Equal(false))
_, filter := director.RecentTasksArgsForCall(0)
Expect(filter).To(Equal(boshdir.TasksFilter{}))

opts.All = true
opts.Deployment = "deployment"

Expect(act()).ToNot(HaveOccurred())
_, includeAll = director.RecentTasksArgsForCall(1)
Expect(includeAll).To(Equal(true))
_, filter = director.RecentTasksArgsForCall(1)
Expect(filter).To(Equal(boshdir.TasksFilter{
All: true,
Deployment: "deployment",
}))
})

It("requests specific number of tasks", func() {
Expand Down
36 changes: 18 additions & 18 deletions director/fakes/fake_director.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ type FakeDirector struct {
result1 []director.Lock
result2 error
}
CurrentTasksStub func(includeAll bool) ([]director.Task, error)
CurrentTasksStub func(director.TasksFilter) ([]director.Task, error)
currentTasksMutex sync.RWMutex
currentTasksArgsForCall []struct {
includeAll bool
arg1 director.TasksFilter
}
currentTasksReturns struct {
result1 []director.Task
result2 error
}
RecentTasksStub func(limit int, includeAll bool) ([]director.Task, error)
RecentTasksStub func(int, director.TasksFilter) ([]director.Task, error)
recentTasksMutex sync.RWMutex
recentTasksArgsForCall []struct {
limit int
includeAll bool
arg1 int
arg2 director.TasksFilter
}
recentTasksReturns struct {
result1 []director.Task
Expand Down Expand Up @@ -342,14 +342,14 @@ func (fake *FakeDirector) LocksReturns(result1 []director.Lock, result2 error) {
}{result1, result2}
}

func (fake *FakeDirector) CurrentTasks(includeAll bool) ([]director.Task, error) {
func (fake *FakeDirector) CurrentTasks(arg1 director.TasksFilter) ([]director.Task, error) {
fake.currentTasksMutex.Lock()
fake.currentTasksArgsForCall = append(fake.currentTasksArgsForCall, struct {
includeAll bool
}{includeAll})
arg1 director.TasksFilter
}{arg1})
fake.currentTasksMutex.Unlock()
if fake.CurrentTasksStub != nil {
return fake.CurrentTasksStub(includeAll)
return fake.CurrentTasksStub(arg1)
} else {
return fake.currentTasksReturns.result1, fake.currentTasksReturns.result2
}
Expand All @@ -361,10 +361,10 @@ func (fake *FakeDirector) CurrentTasksCallCount() int {
return len(fake.currentTasksArgsForCall)
}

func (fake *FakeDirector) CurrentTasksArgsForCall(i int) bool {
func (fake *FakeDirector) CurrentTasksArgsForCall(i int) director.TasksFilter {
fake.currentTasksMutex.RLock()
defer fake.currentTasksMutex.RUnlock()
return fake.currentTasksArgsForCall[i].includeAll
return fake.currentTasksArgsForCall[i].arg1
}

func (fake *FakeDirector) CurrentTasksReturns(result1 []director.Task, result2 error) {
Expand All @@ -375,15 +375,15 @@ func (fake *FakeDirector) CurrentTasksReturns(result1 []director.Task, result2 e
}{result1, result2}
}

func (fake *FakeDirector) RecentTasks(limit int, includeAll bool) ([]director.Task, error) {
func (fake *FakeDirector) RecentTasks(arg1 int, arg2 director.TasksFilter) ([]director.Task, error) {
fake.recentTasksMutex.Lock()
fake.recentTasksArgsForCall = append(fake.recentTasksArgsForCall, struct {
limit int
includeAll bool
}{limit, includeAll})
arg1 int
arg2 director.TasksFilter
}{arg1, arg2})
fake.recentTasksMutex.Unlock()
if fake.RecentTasksStub != nil {
return fake.RecentTasksStub(limit, includeAll)
return fake.RecentTasksStub(arg1, arg2)
} else {
return fake.recentTasksReturns.result1, fake.recentTasksReturns.result2
}
Expand All @@ -395,10 +395,10 @@ func (fake *FakeDirector) RecentTasksCallCount() int {
return len(fake.recentTasksArgsForCall)
}

func (fake *FakeDirector) RecentTasksArgsForCall(i int) (int, bool) {
func (fake *FakeDirector) RecentTasksArgsForCall(i int) (int, director.TasksFilter) {
fake.recentTasksMutex.RLock()
defer fake.recentTasksMutex.RUnlock()
return fake.recentTasksArgsForCall[i].limit, fake.recentTasksArgsForCall[i].includeAll
return fake.recentTasksArgsForCall[i].arg1, fake.recentTasksArgsForCall[i].arg2
}

func (fake *FakeDirector) RecentTasksReturns(result1 []director.Task, result2 error) {
Expand Down
9 changes: 7 additions & 2 deletions director/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type Director interface {

Locks() ([]Lock, error)

CurrentTasks(includeAll bool) ([]Task, error)
RecentTasks(limit int, includeAll bool) ([]Task, error)
CurrentTasks(TasksFilter) ([]Task, error)
RecentTasks(int, TasksFilter) ([]Task, error)
FindTask(int) (Task, error)

Events(EventsFilter) ([]Event, error)
Expand Down Expand Up @@ -157,6 +157,11 @@ type Stemcell interface {
Delete(force bool) error
}

type TasksFilter struct {
All bool
Deployment string
}

type Task interface {
ID() int
StartedAt() time.Time
Expand Down
37 changes: 27 additions & 10 deletions director/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package director

import (
"fmt"
gourl "net/url"
"time"

bosherr "github.com/cloudfoundry/bosh-utils/errors"
Expand Down Expand Up @@ -72,10 +73,10 @@ func NewTaskFromResp(client Client, r TaskResp) TaskImpl {
}
}

func (d DirectorImpl) CurrentTasks(includeAll bool) ([]Task, error) {
func (d DirectorImpl) CurrentTasks(filter TasksFilter) ([]Task, error) {
tasks := []Task{}

taskResps, err := d.client.CurrentTasks(includeAll)
taskResps, err := d.client.CurrentTasks(filter)
if err != nil {
return tasks, err
}
Expand All @@ -87,10 +88,10 @@ func (d DirectorImpl) CurrentTasks(includeAll bool) ([]Task, error) {
return tasks, nil
}

func (d DirectorImpl) RecentTasks(limit int, includeAll bool) ([]Task, error) {
func (d DirectorImpl) RecentTasks(limit int, filter TasksFilter) ([]Task, error) {
tasks := []Task{}

taskResps, err := d.client.RecentTasks(limit, includeAll)
taskResps, err := d.client.RecentTasks(limit, filter)
if err != nil {
return tasks, err
}
Expand Down Expand Up @@ -131,11 +132,19 @@ func (t TaskImpl) RawOutput(taskReporter TaskReporter) error {
return t.client.TaskOutput(t.id, "raw", taskReporter)
}

func (c Client) CurrentTasks(includeAll bool) ([]TaskResp, error) {
func (c Client) CurrentTasks(filter TasksFilter) ([]TaskResp, error) {
var tasks []TaskResp

path := "/tasks?state=processing,cancelling,queued&verbose=" +
c.taskVerbosity(includeAll)
query := gourl.Values{}

query.Add("state", "processing,cancelling,queued")
query.Add("verbose", c.taskVerbosity(filter.All))

if len(filter.Deployment) > 0 {
query.Add("deployment", filter.Deployment)
}

path := fmt.Sprintf("/tasks?%s", query.Encode())

err := c.clientRequest.Get(path, &tasks)
if err != nil {
Expand All @@ -145,11 +154,19 @@ func (c Client) CurrentTasks(includeAll bool) ([]TaskResp, error) {
return tasks, nil
}

func (c Client) RecentTasks(limit int, includeAll bool) ([]TaskResp, error) {
func (c Client) RecentTasks(limit int, filter TasksFilter) ([]TaskResp, error) {
var tasks []TaskResp

path := fmt.Sprintf("/tasks?limit=%d&verbose=%s",
limit, c.taskVerbosity(includeAll))
query := gourl.Values{}

query.Add("limit", fmt.Sprintf("%d", limit))
query.Add("verbose", c.taskVerbosity(filter.All))

if len(filter.Deployment) > 0 {
query.Add("deployment", filter.Deployment)
}

path := fmt.Sprintf("/tasks?%s", query.Encode())

err := c.clientRequest.Get(path, &tasks)
if err != nil {
Expand Down
Loading

0 comments on commit c563bd9

Please sign in to comment.