-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
89 lines (71 loc) · 2.47 KB
/
task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package v3action
import (
"fmt"
"net/url"
"strconv"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
)
// Task represents a V3 actor Task.
type Task ccv3.Task
// TaskWorkersUnavailableError is returned when there are no workers to run a
// given task.
type TaskWorkersUnavailableError struct {
Message string
}
func (e TaskWorkersUnavailableError) Error() string {
return e.Message
}
// TaskNotFoundError is returned when no tasks matching the filters are found.
type TaskNotFoundError struct {
SequenceID int
}
func (e TaskNotFoundError) Error() string {
return fmt.Sprintf("Task sequence ID %d not found.", e.SequenceID)
}
// RunTask runs the provided command in the application environment associated
// with the provided application GUID.
func (actor Actor) RunTask(appGUID string, command string, name string) (Task, Warnings, error) {
task, warnings, err := actor.CloudControllerClient.NewTask(appGUID, command, name)
if err != nil {
if e, ok := err.(cloudcontroller.TaskWorkersUnavailableError); ok {
return Task{}, Warnings(warnings), TaskWorkersUnavailableError{Message: e.Error()}
}
}
return Task(task), Warnings(warnings), err
}
// GetApplicationTasks returns a list of tasks associated with the provided
// appplication GUID.
func (actor Actor) GetApplicationTasks(appGUID string, sortOrder SortOrder) ([]Task, Warnings, error) {
query := url.Values{}
if sortOrder == Descending {
query.Add("order_by", "-created_at")
}
tasks, warnings, err := actor.CloudControllerClient.GetApplicationTasks(appGUID, query)
actorWarnings := Warnings(warnings)
if err != nil {
return nil, actorWarnings, err
}
allTasks := []Task{}
for _, task := range tasks {
allTasks = append(allTasks, Task(task))
}
return allTasks, actorWarnings, nil
}
func (actor Actor) GetTaskBySequenceIDAndApplication(sequenceID int, appGUID string) (Task, Warnings, error) {
query := url.Values{
"sequence_ids": []string{strconv.Itoa(sequenceID)},
}
tasks, warnings, err := actor.CloudControllerClient.GetApplicationTasks(appGUID, query)
if err != nil {
return Task{}, Warnings(warnings), err
}
if len(tasks) == 0 {
return Task{}, Warnings(warnings), TaskNotFoundError{SequenceID: sequenceID}
}
return Task(tasks[0]), Warnings(warnings), nil
}
func (actor Actor) TerminateTask(taskGUID string) (Task, Warnings, error) {
task, warnings, err := actor.CloudControllerClient.UpdateTask(taskGUID)
return Task(task), Warnings(warnings), err
}