forked from cloudfoundry/cli
/
application_instance_status.go
107 lines (88 loc) · 2.99 KB
/
application_instance_status.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
107
package ccv2
import (
"encoding/json"
"strconv"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// ApplicationInstanceStatus represents a Cloud Controller Application Instance.
type ApplicationInstanceStatus struct {
// CPU is the instance's CPU utilization percentage.
CPU float64
// Disk is the instance's disk usage in bytes.
Disk int
// DiskQuota is the instance's allowed disk usage in bytes.
DiskQuota int
// ID is the instance ID.
ID int
// IsolationSegment that the app is currently running on.
IsolationSegment string
// Memory is the instance's memory usage in bytes.
Memory int
// MemoryQuota is the instance's allowed memory usage in bytes.
MemoryQuota int
// State is the instance's state.
State ApplicationInstanceState
// Uptime is the number of seconds the instance has been running.
Uptime int
}
// UnmarshalJSON helps unmarshal a Cloud Controller application instance
// response.
func (instance *ApplicationInstanceStatus) UnmarshalJSON(data []byte) error {
var ccInstance struct {
State string `json:"state"`
IsolationSegment string `json:"isolation_segment"`
Stats struct {
Usage struct {
Disk int `json:"disk"`
Memory int `json:"mem"`
CPU float64 `json:"cpu"`
} `json:"usage"`
MemoryQuota int `json:"mem_quota"`
DiskQuota int `json:"disk_quota"`
Uptime int `json:"uptime"`
} `json:"stats"`
}
if err := json.Unmarshal(data, &ccInstance); err != nil {
return err
}
instance.CPU = ccInstance.Stats.Usage.CPU
instance.Disk = ccInstance.Stats.Usage.Disk
instance.DiskQuota = ccInstance.Stats.DiskQuota
instance.IsolationSegment = ccInstance.IsolationSegment
instance.Memory = ccInstance.Stats.Usage.Memory
instance.MemoryQuota = ccInstance.Stats.MemoryQuota
instance.State = ApplicationInstanceState(ccInstance.State)
instance.Uptime = ccInstance.Stats.Uptime
return nil
}
// GetApplicationInstanceStatusesByApplication returns a list of
// ApplicationInstance for a given application. Given the state of an
// application, it might skip some application instances.
func (client *Client) GetApplicationInstanceStatusesByApplication(guid string) (map[int]ApplicationInstanceStatus, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetAppStatsRequest,
URIParams: Params{"app_guid": guid},
})
if err != nil {
return nil, nil, err
}
var instances map[string]ApplicationInstanceStatus
response := cloudcontroller.Response{
Result: &instances,
}
err = client.connection.Make(request, &response)
if err != nil {
return nil, response.Warnings, err
}
returnedInstances := map[int]ApplicationInstanceStatus{}
for instanceID, instance := range instances {
id, convertErr := strconv.Atoi(instanceID)
if convertErr != nil {
return nil, response.Warnings, convertErr
}
instance.ID = id
returnedInstances[id] = instance
}
return returnedInstances, response.Warnings, nil
}