forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
106 lines (92 loc) · 2.7 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package ccv3
import (
"bytes"
"encoding/json"
"net/url"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
)
// Task represents a Cloud Controller V3 Task.
type Task struct {
GUID string `json:"guid,omitempty"`
SequenceID int `json:"sequence_id,omitempty"`
Name string `json:"name,omitempty"`
Command string `json:"command"`
State string `json:"state,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
MemoryInMB uint64 `json:"memory_in_mb,omitempty"`
DiskInMB uint64 `json:"disk_in_mb,omitempty"`
}
// CreateApplicationTask runs a command in the Application environment
// associated with the provided Application GUID.
func (client *Client) CreateApplicationTask(appGUID string, task Task) (Task, Warnings, error) {
bodyBytes, err := json.Marshal(task)
if err != nil {
return Task{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostAppTasksRequest,
URIParams: internal.Params{
"guid": appGUID,
},
Body: bytes.NewReader(bodyBytes),
})
if err != nil {
return Task{}, nil, err
}
var responseTask Task
response := cloudcontroller.Response{
Result: &responseTask,
}
err = client.connection.Make(request, &response)
return responseTask, response.Warnings, err
}
// GetApplicationTasks returns a list of tasks associated with the provided
// application GUID. Results can be filtered by providing URL queries.
func (client *Client) GetApplicationTasks(appGUID string, query url.Values) ([]Task, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetAppTasksRequest,
URIParams: internal.Params{
"guid": appGUID,
},
Query: query,
})
if err != nil {
return nil, nil, err
}
var fullTasksList []Task
warnings, err := client.paginate(request, Task{}, func(item interface{}) error {
if task, ok := item.(Task); ok {
fullTasksList = append(fullTasksList, task)
} else {
return ccerror.UnknownObjectInListError{
Expected: Task{},
Unexpected: item,
}
}
return nil
})
return fullTasksList, warnings, err
}
// UpdateTask cancels a task.
func (client *Client) UpdateTask(taskGUID string) (Task, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PutTaskCancelRequest,
URIParams: internal.Params{
"guid": taskGUID,
},
})
if err != nil {
return Task{}, nil, err
}
var task Task
response := cloudcontroller.Response{
Result: &task,
}
err = client.connection.Make(request, &response)
if err != nil {
return Task{}, response.Warnings, err
}
return task, response.Warnings, nil
}