/
process.go
297 lines (256 loc) · 9.01 KB
/
process.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package ccv3
import (
"bytes"
"encoding/json"
"fmt"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/types"
)
type Process struct {
GUID string
Type string
// Command is the process start command. Note: This value will be obfuscated when obtained from listing.
Command types.FilteredString
HealthCheckType constant.HealthCheckType
HealthCheckEndpoint string
HealthCheckInvocationTimeout int64
HealthCheckTimeout int64
Instances types.NullInt
MemoryInMB types.NullUint64
DiskInMB types.NullUint64
}
func (p Process) MarshalJSON() ([]byte, error) {
var ccProcess marshalProcess
marshalCommand(p, &ccProcess)
marshalInstances(p, &ccProcess)
marshalMemory(p, &ccProcess)
marshalDisk(p, &ccProcess)
marshalHealthCheck(p, &ccProcess)
return json.Marshal(ccProcess)
}
func (p *Process) UnmarshalJSON(data []byte) error {
var ccProcess struct {
Command types.FilteredString `json:"command"`
DiskInMB types.NullUint64 `json:"disk_in_mb"`
GUID string `json:"guid"`
Instances types.NullInt `json:"instances"`
MemoryInMB types.NullUint64 `json:"memory_in_mb"`
Type string `json:"type"`
HealthCheck struct {
Type constant.HealthCheckType `json:"type"`
Data struct {
Endpoint string `json:"endpoint"`
InvocationTimeout int64 `json:"invocation_timeout"`
Timeout int64 `json:"timeout"`
} `json:"data"`
} `json:"health_check"`
}
err := cloudcontroller.DecodeJSON(data, &ccProcess)
if err != nil {
return err
}
p.Command = ccProcess.Command
p.DiskInMB = ccProcess.DiskInMB
p.GUID = ccProcess.GUID
p.HealthCheckEndpoint = ccProcess.HealthCheck.Data.Endpoint
p.HealthCheckInvocationTimeout = ccProcess.HealthCheck.Data.InvocationTimeout
p.HealthCheckTimeout = ccProcess.HealthCheck.Data.Timeout
p.HealthCheckType = ccProcess.HealthCheck.Type
p.Instances = ccProcess.Instances
p.MemoryInMB = ccProcess.MemoryInMB
p.Type = ccProcess.Type
return nil
}
// CreateApplicationProcessScale updates process instances count, memory or disk
func (client *Client) CreateApplicationProcessScale(appGUID string, process Process) (Process, Warnings, error) {
body, err := json.Marshal(process)
if err != nil {
return Process{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostApplicationProcessActionScaleRequest,
Body: bytes.NewReader(body),
URIParams: internal.Params{"app_guid": appGUID, "type": process.Type},
})
if err != nil {
return Process{}, nil, err
}
var responseProcess Process
response := cloudcontroller.Response{
DecodeJSONResponseInto: &responseProcess,
}
err = client.connection.Make(request, &response)
return responseProcess, response.Warnings, err
}
// GetApplicationProcessByType returns application process of specified type
func (client *Client) GetApplicationProcessByType(appGUID string, processType string) (Process, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetApplicationProcessRequest,
URIParams: map[string]string{
"app_guid": appGUID,
"type": processType,
},
})
if err != nil {
return Process{}, nil, err
}
var process Process
response := cloudcontroller.Response{
DecodeJSONResponseInto: &process,
}
err = client.connection.Make(request, &response)
return process, response.Warnings, err
}
// GetApplicationProcesses lists processes for a given application. **Note**:
// Due to security, the API obfuscates certain values such as `command`.
func (client *Client) GetApplicationProcesses(appGUID string) ([]Process, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetApplicationProcessesRequest,
URIParams: map[string]string{"app_guid": appGUID},
})
if err != nil {
return nil, nil, err
}
var fullProcessesList []Process
warnings, err := client.paginate(request, Process{}, func(item interface{}) error {
if process, ok := item.(Process); ok {
fullProcessesList = append(fullProcessesList, process)
} else {
return ccerror.UnknownObjectInListError{
Expected: Process{},
Unexpected: item,
}
}
return nil
})
return fullProcessesList, warnings, err
}
// GetNewApplicationProcesses gets processes for an application in the middle of a deployment.
// The app's processes will include a web process that will be removed when the deployment completes,
// so exclude that soon-to-be-removed process from the result.
func (client *Client) GetNewApplicationProcesses(appGUID string, deploymentGUID string) ([]Process, Warnings, error) {
var allWarnings Warnings
deployment, warnings, err := client.GetDeployment(deploymentGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return nil, allWarnings, err
}
allProcesses, warnings, err := client.GetApplicationProcesses(appGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return nil, allWarnings, err
}
var newWebProcessGUID string
for _, process := range deployment.NewProcesses {
if process.Type == constant.ProcessTypeWeb {
newWebProcessGUID = process.GUID
}
}
var processesList []Process
for _, process := range allProcesses {
if process.Type == constant.ProcessTypeWeb {
if process.GUID == newWebProcessGUID {
processesList = append(processesList, process)
}
} else {
processesList = append(processesList, process)
}
}
return processesList, allWarnings, nil
}
// GetProcess returns a process with the given guid
func (client *Client) GetProcess(processGUID string) (Process, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetProcessRequest,
URIParams: map[string]string{
"process_guid": processGUID,
},
})
if err != nil {
return Process{}, nil, err
}
var process Process
response := cloudcontroller.Response{
DecodeJSONResponseInto: &process,
}
err = client.connection.Make(request, &response)
return process, response.Warnings, err
}
// UpdateProcess updates the process's command or health check settings. GUID
// is always required; HealthCheckType is only required when updating health
// check settings.
func (client *Client) UpdateProcess(process Process) (Process, Warnings, error) {
body, err := json.Marshal(Process{
Command: process.Command,
HealthCheckType: process.HealthCheckType,
HealthCheckEndpoint: process.HealthCheckEndpoint,
HealthCheckTimeout: process.HealthCheckTimeout,
HealthCheckInvocationTimeout: process.HealthCheckInvocationTimeout,
})
if err != nil {
return Process{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PatchProcessRequest,
Body: bytes.NewReader(body),
URIParams: internal.Params{"process_guid": process.GUID},
})
if err != nil {
return Process{}, nil, err
}
var responseProcess Process
response := cloudcontroller.Response{
DecodeJSONResponseInto: &responseProcess,
}
err = client.connection.Make(request, &response)
return responseProcess, response.Warnings, err
}
type healthCheck struct {
Type constant.HealthCheckType `json:"type,omitempty"`
Data struct {
Endpoint interface{} `json:"endpoint,omitempty"`
InvocationTimeout int64 `json:"invocation_timeout,omitempty"`
Timeout int64 `json:"timeout,omitempty"`
} `json:"data"`
}
type marshalProcess struct {
Command interface{} `json:"command,omitempty"`
Instances json.Number `json:"instances,omitempty"`
MemoryInMB json.Number `json:"memory_in_mb,omitempty"`
DiskInMB json.Number `json:"disk_in_mb,omitempty"`
HealthCheck *healthCheck `json:"health_check,omitempty"`
}
func marshalCommand(p Process, ccProcess *marshalProcess) {
if p.Command.IsSet {
ccProcess.Command = &p.Command
}
}
func marshalDisk(p Process, ccProcess *marshalProcess) {
if p.DiskInMB.IsSet {
ccProcess.DiskInMB = json.Number(fmt.Sprint(p.DiskInMB.Value))
}
}
func marshalHealthCheck(p Process, ccProcess *marshalProcess) {
if p.HealthCheckType != "" || p.HealthCheckEndpoint != "" || p.HealthCheckInvocationTimeout != 0 || p.HealthCheckTimeout != 0 {
ccProcess.HealthCheck = new(healthCheck)
ccProcess.HealthCheck.Type = p.HealthCheckType
ccProcess.HealthCheck.Data.InvocationTimeout = p.HealthCheckInvocationTimeout
ccProcess.HealthCheck.Data.Timeout = p.HealthCheckTimeout
if p.HealthCheckEndpoint != "" {
ccProcess.HealthCheck.Data.Endpoint = p.HealthCheckEndpoint
}
}
}
func marshalInstances(p Process, ccProcess *marshalProcess) {
if p.Instances.IsSet {
ccProcess.Instances = json.Number(fmt.Sprint(p.Instances.Value))
}
}
func marshalMemory(p Process, ccProcess *marshalProcess) {
if p.MemoryInMB.IsSet {
ccProcess.MemoryInMB = json.Number(fmt.Sprint(p.MemoryInMB.Value))
}
}