forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_instance.go
More file actions
129 lines (116 loc) · 4.07 KB
/
Copy pathprocess_instance.go
File metadata and controls
129 lines (116 loc) · 4.07 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package ccv3
import (
"fmt"
"strconv"
"time"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
)
// ProcessInstance represents a single process instance for a particular
// application.
type ProcessInstance struct {
// CPU is the current CPU usage of the instance.
CPU float64
// Details is information about errors placing the instance.
Details string
// DiskQuota is the maximum disk the instance is allowed to use.
DiskQuota uint64
// DiskUsage is the current disk usage of the instance.
DiskUsage uint64
// Index is the index of the instance.
Index int64
// Isolation segment is the current isolation segment that the instance is
// running on. The value is empty when the instance is not placed on a
// particular isolation segment.
IsolationSegment string
// MemoryQuota is the maximum memory the instance is allowed to use.
MemoryQuota uint64
// DiskUsage is the current memory usage of the instance.
MemoryUsage uint64
// State is the state of the instance.
State constant.ProcessInstanceState
// Type is the process type for the instance.
Type string
// Uptime is the duration that the instance has been running.
Uptime time.Duration
}
// UnmarshalJSON helps unmarshal a V3 Cloud Controller Instance response.
func (instance *ProcessInstance) UnmarshalJSON(data []byte) error {
var inputInstance struct {
Details string `json:"details"`
DiskQuota uint64 `json:"disk_quota"`
Index int64 `json:"index"`
IsolationSegment string `json:"isolation_segment"`
MemQuota uint64 `json:"mem_quota"`
State string `json:"state"`
Type string `json:"type"`
Uptime int64 `json:"uptime"`
Usage struct {
CPU float64 `json:"cpu"`
Mem uint64 `json:"mem"`
Disk uint64 `json:"disk"`
} `json:"usage"`
}
err := cloudcontroller.DecodeJSON(data, &inputInstance)
if err != nil {
return err
}
instance.CPU = inputInstance.Usage.CPU
instance.Details = inputInstance.Details
instance.DiskQuota = inputInstance.DiskQuota
instance.DiskUsage = inputInstance.Usage.Disk
instance.Index = inputInstance.Index
instance.IsolationSegment = inputInstance.IsolationSegment
instance.MemoryQuota = inputInstance.MemQuota
instance.MemoryUsage = inputInstance.Usage.Mem
instance.State = constant.ProcessInstanceState(inputInstance.State)
instance.Type = inputInstance.Type
instance.Uptime, err = time.ParseDuration(fmt.Sprintf("%ds", inputInstance.Uptime))
if err != nil {
return err
}
return nil
}
// DeleteApplicationProcessInstance deletes/stops a particular application's
// process instance.
func (client *Client) DeleteApplicationProcessInstance(appGUID string, processType string, instanceIndex int) (Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.DeleteApplicationProcessInstanceRequest,
URIParams: map[string]string{
"app_guid": appGUID,
"type": processType,
"index": strconv.Itoa(instanceIndex),
},
})
if err != nil {
return nil, err
}
var response cloudcontroller.Response
err = client.connection.Make(request, &response)
return response.Warnings, err
}
// GetProcessInstances lists instance stats for a given process.
func (client *Client) GetProcessInstances(processGUID string) ([]ProcessInstance, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetProcessStatsRequest,
URIParams: map[string]string{"process_guid": processGUID},
})
if err != nil {
return nil, nil, err
}
var fullInstancesList []ProcessInstance
warnings, err := client.paginate(request, ProcessInstance{}, func(item interface{}) error {
if instance, ok := item.(ProcessInstance); ok {
fullInstancesList = append(fullInstancesList, instance)
} else {
return ccerror.UnknownObjectInListError{
Expected: ProcessInstance{},
Unexpected: item,
}
}
return nil
})
return fullInstancesList, warnings, err
}