forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage.go
266 lines (220 loc) · 7.76 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
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"
"code.cloudfoundry.org/cli/resources"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 io.Reader
// CreatePackage creates a package with the given settings, Type and the
// ApplicationRelationship must be set.
func (client *Client) CreatePackage(pkg resources.Package) (resources.Package, Warnings, error) {
var responseBody resources.Package
_, warnings, err := client.MakeRequest(RequestParams{
RequestName: internal.PostPackageRequest,
RequestBody: pkg,
ResponseBody: &responseBody,
})
return responseBody, warnings, err
}
// GetPackage returns the package with the given GUID.
func (client *Client) GetPackage(packageGUID string) (resources.Package, Warnings, error) {
var responseBody resources.Package
_, warnings, err := client.MakeRequest(RequestParams{
RequestName: internal.GetPackageRequest,
URIParams: internal.Params{"package_guid": packageGUID},
ResponseBody: &responseBody,
})
return responseBody, warnings, err
}
// GetPackages returns the list of packages.
func (client *Client) GetPackages(query ...Query) ([]resources.Package, Warnings, error) {
var packages []resources.Package
_, warnings, err := client.MakeListRequest(RequestParams{
RequestName: internal.GetPackagesRequest,
Query: query,
ResponseBody: resources.Package{},
AppendToList: func(item interface{}) error {
packages = append(packages, item.(resources.Package))
return nil
},
})
return packages, 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 resources.Package, matchedResources []Resource, newResources io.Reader, newResourcesLength int64) (resources.Package, Warnings, error) {
if matchedResources == nil {
return resources.Package{}, nil, ccerror.NilObjectError{Object: "matchedResources"}
}
if newResources == nil {
return client.uploadExistingResourcesOnly(pkg.GUID, matchedResources)
}
return client.uploadNewAndExistingResources(pkg.GUID, 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 resources.Package, fileToUpload string) (resources.Package, Warnings, error) {
body, contentType, err := client.createUploadBuffer(fileToUpload, "bits")
if err != nil {
return resources.Package{}, nil, err
}
responsePackage := resources.Package{}
_, warnings, err := client.MakeRequestSendRaw(
internal.PostPackageBitsRequest,
internal.Params{"package_guid": pkg.GUID},
body.Bytes(),
contentType,
&responsePackage,
)
return responsePackage, warnings, err
}
// CopyPackage copies a package from a source package to a destination package
// Note: source app guid is in URL; dest app guid is in body
func (client *Client) CopyPackage(sourcePkgGUID string, targetAppGUID string) (resources.Package, Warnings, error) {
var targetPackage resources.Package
_, warnings, err := client.MakeRequest(RequestParams{
RequestName: internal.PostPackageRequest,
Query: []Query{{Key: SourceGUID, Values: []string{sourcePkgGUID}}},
RequestBody: map[string]resources.Relationships{
"relationships": {
constant.RelationshipTypeApplication: resources.Relationship{GUID: targetAppGUID},
},
},
ResponseBody: &targetPackage,
})
return targetPackage, 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) createUploadBuffer(path string, paramName string) (bytes.Buffer, string, error) {
file, err := os.Open(path)
if err != nil {
return bytes.Buffer{}, "", err
}
defer file.Close()
body := bytes.Buffer{}
writer := multipart.NewWriter(&body)
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
if err != nil {
return bytes.Buffer{}, "", err
}
_, err = io.Copy(part, file)
if err != nil {
return bytes.Buffer{}, "", err
}
err = writer.Close()
return body, writer.FormDataContentType(), err
}
func (client *Client) uploadExistingResourcesOnly(packageGUID string, matchedResources []Resource) (resources.Package, Warnings, error) {
jsonResources, err := json.Marshal(matchedResources)
if err != nil {
return resources.Package{}, nil, err
}
body := bytes.NewBuffer(nil)
form := multipart.NewWriter(body)
err = form.WriteField("resources", string(jsonResources))
if err != nil {
return resources.Package{}, nil, err
}
err = form.Close()
if err != nil {
return resources.Package{}, nil, err
}
responsePackage := resources.Package{}
_, warnings, err := client.MakeRequestSendRaw(
internal.PostPackageBitsRequest,
internal.Params{"package_guid": packageGUID},
body.Bytes(),
form.FormDataContentType(),
&responsePackage,
)
return responsePackage, warnings, err
}
func (client *Client) uploadNewAndExistingResources(packageGUID string, matchedResources []Resource, newResources io.Reader, newResourcesLength int64) (resources.Package, Warnings, error) {
contentLength, err := client.calculateAppBitsRequestSize(matchedResources, newResourcesLength)
if err != nil {
return resources.Package{}, nil, err
}
contentType, body, writeErrors := client.createMultipartBodyAndHeaderForAppBits(matchedResources, newResources, newResourcesLength)
responseBody := resources.Package{}
_, warnings, err := client.MakeRequestUploadAsync(
internal.PostPackageBitsRequest,
internal.Params{"package_guid": packageGUID},
contentType,
body,
contentLength,
&responseBody,
writeErrors,
)
return responseBody, warnings, err
}