forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
367 lines (309 loc) · 12.9 KB
/
application.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package ccv2
import (
"bytes"
"encoding/json"
"fmt"
"time"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
"code.cloudfoundry.org/cli/types"
)
// Application represents a Cloud Controller Application.
type Application struct {
// Buildpack is the buildpack set by the user.
Buildpack types.FilteredString
// Command is the user specified start command.
Command types.FilteredString
// DetectedBuildpack is the buildpack automatically detected.
DetectedBuildpack types.FilteredString
// DetectedStartCommand is the command used to start the application.
DetectedStartCommand types.FilteredString
// DiskQuota is the disk given to each instance, in megabytes.
DiskQuota types.NullByteSizeInMb
// DockerCredentials is the authentication information for the provided
// DockerImage.
DockerCredentials DockerCredentials
// DockerImage is the docker image location.
DockerImage string
// EnvironmentVariables are the environment variables passed to the app.
EnvironmentVariables map[string]string
// GUID is the unique application identifier.
GUID string
// HealthCheckTimeout is the number of seconds for health checking of an
// staged app when starting up.
HealthCheckTimeout uint64
// HealthCheckType is the type of health check that will be done to the app.
HealthCheckType constant.ApplicationHealthCheckType
// HealthCheckHTTPEndpoint is the url of the http health check endpoint.
HealthCheckHTTPEndpoint string
// Instances is the total number of app instances.
Instances types.NullInt
// Memory is the memory given to each instance, in megabytes.
Memory types.NullByteSizeInMb
// Name is the name given to the application.
Name string
// PackageState represents the staging state of the application bits.
PackageState constant.ApplicationPackageState
// PackageUpdatedAt is the last time the app bits were updated. In RFC3339.
PackageUpdatedAt time.Time
// SpaceGUID is the GUID of the app's space.
SpaceGUID string
// StackGUID is the GUID for the Stack the application is running on.
StackGUID string
// StagingFailedDescription is the verbose description of why the package
// failed to stage.
StagingFailedDescription string
// StagingFailedReason is the reason why the package failed to stage.
StagingFailedReason string
// State is the desired state of the application.
State constant.ApplicationState
}
// MarshalJSON converts an application into a Cloud Controller Application.
func (application Application) MarshalJSON() ([]byte, error) {
ccApp := struct {
Buildpack *string `json:"buildpack,omitempty"`
Command *string `json:"command,omitempty"`
DiskQuota *uint64 `json:"disk_quota,omitempty"`
DockerCredentials *DockerCredentials `json:"docker_credentials,omitempty"`
DockerImage string `json:"docker_image,omitempty"`
EnvironmentVariables map[string]string `json:"environment_json,omitempty"`
HealthCheckHTTPEndpoint *string `json:"health_check_http_endpoint,omitempty"`
HealthCheckTimeout uint64 `json:"health_check_timeout,omitempty"`
HealthCheckType constant.ApplicationHealthCheckType `json:"health_check_type,omitempty"`
Instances *int `json:"instances,omitempty"`
Memory *uint64 `json:"memory,omitempty"`
Name string `json:"name,omitempty"`
SpaceGUID string `json:"space_guid,omitempty"`
StackGUID string `json:"stack_guid,omitempty"`
State constant.ApplicationState `json:"state,omitempty"`
}{
DockerImage: application.DockerImage,
EnvironmentVariables: application.EnvironmentVariables,
HealthCheckTimeout: application.HealthCheckTimeout,
HealthCheckType: application.HealthCheckType,
Name: application.Name,
SpaceGUID: application.SpaceGUID,
StackGUID: application.StackGUID,
State: application.State,
}
if application.Buildpack.IsSet {
ccApp.Buildpack = &application.Buildpack.Value
}
if application.Command.IsSet {
ccApp.Command = &application.Command.Value
}
if application.DiskQuota.IsSet {
ccApp.DiskQuota = &application.DiskQuota.Value
}
if application.DockerCredentials.Username != "" || application.DockerCredentials.Password != "" {
ccApp.DockerCredentials = &DockerCredentials{
Username: application.DockerCredentials.Username,
Password: application.DockerCredentials.Password,
}
}
if application.Instances.IsSet {
ccApp.Instances = &application.Instances.Value
}
if application.HealthCheckType != "" {
ccApp.HealthCheckHTTPEndpoint = &application.HealthCheckHTTPEndpoint
}
if application.Memory.IsSet {
ccApp.Memory = &application.Memory.Value
}
return json.Marshal(ccApp)
}
// UnmarshalJSON helps unmarshal a Cloud Controller Application response.
func (application *Application) UnmarshalJSON(data []byte) error {
var ccApp struct {
Metadata internal.Metadata `json:"metadata"`
Entity struct {
Buildpack string `json:"buildpack"`
Command string `json:"command"`
DetectedBuildpack string `json:"detected_buildpack"`
DetectedStartCommand string `json:"detected_start_command"`
DiskQuota *uint64 `json:"disk_quota"`
DockerImage string `json:"docker_image"`
DockerCredentials DockerCredentials `json:"docker_credentials"`
// EnvironmentVariables' values can be any type, so we must accept
// interface{}, but we convert to string.
EnvironmentVariables map[string]interface{} `json:"environment_json"`
HealthCheckHTTPEndpoint string `json:"health_check_http_endpoint"`
HealthCheckTimeout uint64 `json:"health_check_timeout"`
HealthCheckType string `json:"health_check_type"`
Instances json.Number `json:"instances"`
Memory *uint64 `json:"memory"`
Name string `json:"name"`
PackageState string `json:"package_state"`
PackageUpdatedAt *time.Time `json:"package_updated_at"`
SpaceGUID string `json:"space_guid"`
StackGUID string `json:"stack_guid"`
StagingFailedDescription string `json:"staging_failed_description"`
StagingFailedReason string `json:"staging_failed_reason"`
State string `json:"state"`
} `json:"entity"`
}
err := cloudcontroller.DecodeJSON(data, &ccApp)
if err != nil {
return err
}
application.Buildpack.ParseValue(ccApp.Entity.Buildpack)
application.Command.ParseValue(ccApp.Entity.Command)
application.DetectedBuildpack.ParseValue(ccApp.Entity.DetectedBuildpack)
application.DetectedStartCommand.ParseValue(ccApp.Entity.DetectedStartCommand)
application.DiskQuota.ParseUint64Value(ccApp.Entity.DiskQuota)
application.DockerCredentials = ccApp.Entity.DockerCredentials
application.DockerImage = ccApp.Entity.DockerImage
application.GUID = ccApp.Metadata.GUID
application.HealthCheckHTTPEndpoint = ccApp.Entity.HealthCheckHTTPEndpoint
application.HealthCheckTimeout = ccApp.Entity.HealthCheckTimeout
application.HealthCheckType = constant.ApplicationHealthCheckType(ccApp.Entity.HealthCheckType)
application.Memory.ParseUint64Value(ccApp.Entity.Memory)
application.Name = ccApp.Entity.Name
application.PackageState = constant.ApplicationPackageState(ccApp.Entity.PackageState)
application.SpaceGUID = ccApp.Entity.SpaceGUID
application.StackGUID = ccApp.Entity.StackGUID
application.StagingFailedDescription = ccApp.Entity.StagingFailedDescription
application.StagingFailedReason = ccApp.Entity.StagingFailedReason
application.State = constant.ApplicationState(ccApp.Entity.State)
if len(ccApp.Entity.EnvironmentVariables) > 0 {
envVariableValues := map[string]string{}
for key, value := range ccApp.Entity.EnvironmentVariables {
envVariableValues[key] = fmt.Sprint(value)
}
application.EnvironmentVariables = envVariableValues
}
err = application.Instances.ParseStringValue(ccApp.Entity.Instances.String())
if err != nil {
return err
}
if ccApp.Entity.PackageUpdatedAt != nil {
application.PackageUpdatedAt = *ccApp.Entity.PackageUpdatedAt
}
return nil
}
// CreateApplication creates a cloud controller application in with the given
// settings. SpaceGUID and Name are the only required fields.
func (client *Client) CreateApplication(app Application) (Application, Warnings, error) {
body, err := json.Marshal(app)
if err != nil {
return Application{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostAppRequest,
Body: bytes.NewReader(body),
})
if err != nil {
return Application{}, nil, err
}
var updatedApp Application
response := cloudcontroller.Response{
DecodeJSONResponseInto: &updatedApp,
}
err = client.connection.Make(request, &response)
return updatedApp, response.Warnings, err
}
// GetApplication returns back an Application.
func (client *Client) GetApplication(guid string) (Application, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetAppRequest,
URIParams: Params{"app_guid": guid},
})
if err != nil {
return Application{}, nil, err
}
var app Application
response := cloudcontroller.Response{
DecodeJSONResponseInto: &app,
}
err = client.connection.Make(request, &response)
return app, response.Warnings, err
}
// GetApplications returns back a list of Applications based off of the
// provided filters.
func (client *Client) GetApplications(filters ...Filter) ([]Application, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetAppsRequest,
Query: ConvertFilterParameters(filters),
})
if err != nil {
return nil, nil, err
}
var fullAppsList []Application
warnings, err := client.paginate(request, Application{}, func(item interface{}) error {
if app, ok := item.(Application); ok {
fullAppsList = append(fullAppsList, app)
} else {
return ccerror.UnknownObjectInListError{
Expected: Application{},
Unexpected: item,
}
}
return nil
})
return fullAppsList, warnings, err
}
// GetRouteApplications returns a list of Applications based off a route
// GUID and the provided filters.
func (client *Client) GetRouteApplications(routeGUID string, filters ...Filter) ([]Application, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetRouteAppsRequest,
URIParams: map[string]string{"route_guid": routeGUID},
Query: ConvertFilterParameters(filters),
})
if err != nil {
return nil, nil, err
}
var fullAppsList []Application
warnings, err := client.paginate(request, Application{}, func(item interface{}) error {
if app, ok := item.(Application); ok {
fullAppsList = append(fullAppsList, app)
} else {
return ccerror.UnknownObjectInListError{
Expected: Application{},
Unexpected: item,
}
}
return nil
})
return fullAppsList, warnings, err
}
// RestageApplication restages the application with the given GUID.
func (client *Client) RestageApplication(app Application) (Application, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostAppRestageRequest,
URIParams: Params{"app_guid": app.GUID},
})
if err != nil {
return Application{}, nil, err
}
var restagedApp Application
response := cloudcontroller.Response{
DecodeJSONResponseInto: &restagedApp,
}
err = client.connection.Make(request, &response)
return restagedApp, response.Warnings, err
}
// UpdateApplication updates the application with the given GUID. Note: Sending
// DockerImage and StackGUID at the same time will result in an API error.
func (client *Client) UpdateApplication(app Application) (Application, Warnings, error) {
body, err := json.Marshal(app)
if err != nil {
return Application{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PutAppRequest,
URIParams: Params{"app_guid": app.GUID},
Body: bytes.NewReader(body),
})
if err != nil {
return Application{}, nil, err
}
var updatedApp Application
response := cloudcontroller.Response{
DecodeJSONResponseInto: &updatedApp,
}
err = client.connection.Make(request, &response)
return updatedApp, response.Warnings, err
}