/
processes.go
102 lines (87 loc) · 2.39 KB
/
processes.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
package cfclient
import (
"encoding/json"
"net/url"
)
// ProcessListResponse is the json body returned from the API
type ProcessListResponse struct {
Pagination Pagination `json:"pagination"`
Processes []Process `json:"resources"`
}
// Process represents a running process in a container.
type Process struct {
GUID string `json:"guid"`
Type string `json:"type"`
Instances int `json:"instances"`
MemoryInMB int `json:"memory_in_mb"`
DiskInMB int `json:"disk_in_mb"`
Ports []int `json:"ports,omitempty"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
HealthCheck struct {
Type string `json:"type"`
Data struct {
Timeout int `json:"timeout"`
InvocationTimeout int `json:"invocation_timeout"`
Endpoint string `json:"endpoint"`
} `json:"data"`
} `json:"health_check"`
Links struct {
Self Link `json:"self"`
Scale Link `json:"scale"`
App Link `json:"app"`
Space Link `json:"space"`
Stats Link `json:"stats"`
} `json:"links"`
}
// ListAllProcesses will call the v3 processes api
func (c *Client) ListAllProcesses() ([]Process, error) {
return c.ListAllProcessesByQuery(url.Values{})
}
// ListAllProcessesByQuery will call the v3 processes api
func (c *Client) ListAllProcessesByQuery(query url.Values) ([]Process, error) {
var allProcesses []Process
requestURL := "/v3/processes"
if e := query.Encode(); len(e) > 0 {
requestURL += "?" + e
}
for {
resp, err := c.getProcessPage(requestURL)
if err != nil {
return nil, err
}
if resp.Pagination.TotalResults == 0 {
return nil, nil
}
if allProcesses == nil {
allProcesses = make([]Process, 0, resp.Pagination.TotalResults)
}
allProcesses = append(allProcesses, resp.Processes...)
if resp.Pagination.Next.Href == "" {
break
}
requestURL = resp.Pagination.Next.Href
if requestURL == "" {
return allProcesses, nil
}
requestURL, err = extractPathFromURL(requestURL)
if err != nil {
return nil, err
}
}
return allProcesses, nil
}
func (c *Client) getProcessPage(requestURL string) (*ProcessListResponse, error) {
req := c.NewRequest("GET", requestURL)
resp, err := c.DoRequest(req)
if err != nil {
return nil, err
}
procResp := new(ProcessListResponse)
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(procResp)
if err != nil {
return nil, err
}
return procResp, nil
}