/
package.go
451 lines (377 loc) · 12.3 KB
/
package.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package ccv3
import (
"bytes"
"encoding/json"
"io"
"mime/multipart"
"os"
"path/filepath"
"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"
)
//go:generate counterfeiter io.Reader
// Package represents a Cloud Controller V3 Package.
type Package struct {
// CreatedAt is the time with zone when the object was created.
CreatedAt string
// DockerImage is the registry address of the docker image.
DockerImage string
// DockerPassword is the password for the docker image's registry.
DockerPassword string
// DockerUsername is the username for the docker image's registry.
DockerUsername string
// GUID is the unique identifier of the package.
GUID string
// Links are links to related resources.
Links APILinks
// Relationships are a list of relationships to other resources.
Relationships Relationships
// State is the state of the package.
State constant.PackageState
// Type is the package type.
Type constant.PackageType
}
// MarshalJSON converts a Package into a Cloud Controller Package.
func (p Package) MarshalJSON() ([]byte, error) {
type ccPackageData struct {
Image string `json:"image,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}
var ccPackage struct {
GUID string `json:"guid,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Links APILinks `json:"links,omitempty"`
Relationships Relationships `json:"relationships,omitempty"`
State constant.PackageState `json:"state,omitempty"`
Type constant.PackageType `json:"type,omitempty"`
Data *ccPackageData `json:"data,omitempty"`
}
ccPackage.GUID = p.GUID
ccPackage.CreatedAt = p.CreatedAt
ccPackage.Links = p.Links
ccPackage.Relationships = p.Relationships
ccPackage.State = p.State
ccPackage.Type = p.Type
if p.DockerImage != "" {
ccPackage.Data = &ccPackageData{
Image: p.DockerImage,
Username: p.DockerUsername,
Password: p.DockerPassword,
}
}
return json.Marshal(ccPackage)
}
// UnmarshalJSON helps unmarshal a Cloud Controller Package response.
func (p *Package) UnmarshalJSON(data []byte) error {
var ccPackage struct {
GUID string `json:"guid,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Links APILinks `json:"links,omitempty"`
Relationships Relationships `json:"relationships,omitempty"`
State constant.PackageState `json:"state,omitempty"`
Type constant.PackageType `json:"type,omitempty"`
Data struct {
Image string `json:"image"`
Username string `json:"username"`
Password string `json:"password"`
} `json:"data"`
}
err := cloudcontroller.DecodeJSON(data, &ccPackage)
if err != nil {
return err
}
p.GUID = ccPackage.GUID
p.CreatedAt = ccPackage.CreatedAt
p.Links = ccPackage.Links
p.Relationships = ccPackage.Relationships
p.State = ccPackage.State
p.Type = ccPackage.Type
p.DockerImage = ccPackage.Data.Image
p.DockerUsername = ccPackage.Data.Username
p.DockerPassword = ccPackage.Data.Password
return nil
}
// CreatePackage creates a package with the given settings, Type and the
// ApplicationRelationship must be set.
func (client *Client) CreatePackage(pkg Package) (Package, Warnings, error) {
bodyBytes, err := json.Marshal(pkg)
if err != nil {
return Package{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostPackageRequest,
Body: bytes.NewReader(bodyBytes),
})
if err != nil {
return Package{}, nil, err
}
var responsePackage Package
response := cloudcontroller.Response{
DecodeJSONResponseInto: &responsePackage,
}
err = client.connection.Make(request, &response)
return responsePackage, response.Warnings, err
}
// GetPackage returns the package with the given GUID.
func (client *Client) GetPackage(packageGUID string) (Package, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetPackageRequest,
URIParams: internal.Params{"package_guid": packageGUID},
})
if err != nil {
return Package{}, nil, err
}
var responsePackage Package
response := cloudcontroller.Response{
DecodeJSONResponseInto: &responsePackage,
}
err = client.connection.Make(request, &response)
return responsePackage, response.Warnings, err
}
// GetPackages returns the list of packages.
func (client *Client) GetPackages(query ...Query) ([]Package, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetPackagesRequest,
Query: query,
})
if err != nil {
return nil, nil, err
}
var fullPackagesList []Package
warnings, err := client.paginate(request, Package{}, func(item interface{}) error {
if pkg, ok := item.(Package); ok {
fullPackagesList = append(fullPackagesList, pkg)
} else {
return ccerror.UnknownObjectInListError{
Expected: Package{},
Unexpected: item,
}
}
return nil
})
return fullPackagesList, warnings, err
}
// UploadBitsPackage uploads the newResources and a list of existing resources
// to the cloud controller. An updated package is returned. The function will
// act differently given the following Readers:
// - io.ReadSeeker: Will function properly on retry.
// - io.Reader: Will return a ccerror.PipeSeekError on retry.
// - nil: Will not add the "application" section to the request. The newResourcesLength is ignored in this case.
//
// Note: In order to determine if package creation is successful, poll the
// Package's state field for more information.
func (client *Client) UploadBitsPackage(pkg Package, matchedResources []Resource, newResources io.Reader, newResourcesLength int64) (Package, Warnings, error) {
link, ok := pkg.Links["upload"]
if !ok {
return Package{}, nil, ccerror.UploadLinkNotFoundError{PackageGUID: pkg.GUID}
}
if matchedResources == nil {
return Package{}, nil, ccerror.NilObjectError{Object: "matchedResources"}
}
if newResources == nil {
return client.uploadExistingResourcesOnly(link, matchedResources)
}
return client.uploadNewAndExistingResources(link, matchedResources, newResources, newResourcesLength)
}
// UploadPackage uploads a file to a given package's Upload resource. Note:
// fileToUpload is read entirely into memory prior to sending data to CC.
func (client *Client) UploadPackage(pkg Package, fileToUpload string) (Package, Warnings, error) {
link, ok := pkg.Links["upload"]
if !ok {
return Package{}, nil, ccerror.UploadLinkNotFoundError{PackageGUID: pkg.GUID}
}
body, contentType, err := client.createUploadStream(fileToUpload, "bits")
if err != nil {
return Package{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
URL: link.HREF,
Method: link.Method,
Body: body,
})
if err != nil {
return Package{}, nil, err
}
request.Header.Set("Content-Type", contentType)
var responsePackage Package
response := cloudcontroller.Response{
DecodeJSONResponseInto: &responsePackage,
}
err = client.connection.Make(request, &response)
return responsePackage, response.Warnings, err
}
func (client *Client) calculateAppBitsRequestSize(matchedResources []Resource, newResourcesLength int64) (int64, error) {
body := &bytes.Buffer{}
form := multipart.NewWriter(body)
jsonResources, err := json.Marshal(matchedResources)
if err != nil {
return 0, err
}
err = form.WriteField("resources", string(jsonResources))
if err != nil {
return 0, err
}
_, err = form.CreateFormFile("bits", "package.zip")
if err != nil {
return 0, err
}
err = form.Close()
if err != nil {
return 0, err
}
return int64(body.Len()) + newResourcesLength, nil
}
func (client *Client) createMultipartBodyAndHeaderForAppBits(matchedResources []Resource, newResources io.Reader, newResourcesLength int64) (string, io.ReadSeeker, <-chan error) {
writerOutput, writerInput := cloudcontroller.NewPipeBomb()
form := multipart.NewWriter(writerInput)
writeErrors := make(chan error)
go func() {
defer close(writeErrors)
defer writerInput.Close()
jsonResources, err := json.Marshal(matchedResources)
if err != nil {
writeErrors <- err
return
}
err = form.WriteField("resources", string(jsonResources))
if err != nil {
writeErrors <- err
return
}
writer, err := form.CreateFormFile("bits", "package.zip")
if err != nil {
writeErrors <- err
return
}
if newResourcesLength != 0 {
_, err = io.Copy(writer, newResources)
if err != nil {
writeErrors <- err
return
}
}
err = form.Close()
if err != nil {
writeErrors <- err
}
}()
return form.FormDataContentType(), writerOutput, writeErrors
}
func (*Client) createUploadStream(path string, paramName string) (io.ReadSeeker, string, error) {
file, err := os.Open(path)
if err != nil {
return nil, "", err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
if err != nil {
return nil, "", err
}
_, err = io.Copy(part, file)
if err != nil {
return nil, "", err
}
err = writer.Close()
return bytes.NewReader(body.Bytes()), writer.FormDataContentType(), err
}
func (client *Client) uploadAsynchronously(request *cloudcontroller.Request, writeErrors <-chan error) (Package, Warnings, error) {
var pkg Package
response := cloudcontroller.Response{
DecodeJSONResponseInto: &pkg,
}
httpErrors := make(chan error)
go func() {
defer close(httpErrors)
err := client.connection.Make(request, &response)
if err != nil {
httpErrors <- err
}
}()
// The following section makes the following assumptions:
// 1) If an error occurs during file reading, an EOF is sent to the request
// object. Thus ending the request transfer.
// 2) If an error occurs during request transfer, an EOF is sent to the pipe.
// Thus ending the writing routine.
var firstError error
var writeClosed, httpClosed bool
for {
select {
case writeErr, ok := <-writeErrors:
if !ok {
writeClosed = true
break // for select
}
if firstError == nil {
firstError = writeErr
}
case httpErr, ok := <-httpErrors:
if !ok {
httpClosed = true
break // for select
}
if firstError == nil {
firstError = httpErr
}
}
if writeClosed && httpClosed {
break // for for
}
}
return pkg, response.Warnings, firstError
}
func (client *Client) uploadExistingResourcesOnly(uploadLink APILink, matchedResources []Resource) (Package, Warnings, error) {
jsonResources, err := json.Marshal(matchedResources)
if err != nil {
return Package{}, nil, err
}
body := bytes.NewBuffer(nil)
form := multipart.NewWriter(body)
err = form.WriteField("resources", string(jsonResources))
if err != nil {
return Package{}, nil, err
}
err = form.Close()
if err != nil {
return Package{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
URL: uploadLink.HREF,
Method: uploadLink.Method,
Body: bytes.NewReader(body.Bytes()),
})
if err != nil {
return Package{}, nil, err
}
request.Header.Set("Content-Type", form.FormDataContentType())
var pkg Package
response := cloudcontroller.Response{
DecodeJSONResponseInto: &pkg,
}
err = client.connection.Make(request, &response)
return pkg, response.Warnings, err
}
func (client *Client) uploadNewAndExistingResources(uploadLink APILink, matchedResources []Resource, newResources io.Reader, newResourcesLength int64) (Package, Warnings, error) {
contentLength, err := client.calculateAppBitsRequestSize(matchedResources, newResourcesLength)
if err != nil {
return Package{}, nil, err
}
contentType, body, writeErrors := client.createMultipartBodyAndHeaderForAppBits(matchedResources, newResources, newResourcesLength)
// This request uses URL/Method instead of an internal RequestName to support
// the possibility of external bit services.
request, err := client.newHTTPRequest(requestOptions{
URL: uploadLink.HREF,
Method: uploadLink.Method,
Body: body,
})
if err != nil {
return Package{}, nil, err
}
request.Header.Set("Content-Type", contentType)
request.ContentLength = contentLength
return client.uploadAsynchronously(request, writeErrors)
}