-
Notifications
You must be signed in to change notification settings - Fork 929
/
application_bits.go
183 lines (151 loc) · 5.11 KB
/
application_bits.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
package applicationbits
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/textproto"
"os"
"time"
"code.cloudfoundry.org/cli/cf/api/resources"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/net"
"code.cloudfoundry.org/gofileutils/fileutils"
)
const (
DefaultAppUploadBitsTimeout = 15 * time.Minute
)
//go:generate counterfeiter . Repository
type Repository interface {
GetApplicationFiles(appFilesRequest []resources.AppFileResource) ([]resources.AppFileResource, error)
UploadBits(appGUID string, zipFile *os.File, presentFiles []resources.AppFileResource) (apiErr error)
}
type CloudControllerApplicationBitsRepository struct {
config coreconfig.Reader
gateway net.Gateway
}
func NewCloudControllerApplicationBitsRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerApplicationBitsRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CloudControllerApplicationBitsRepository) UploadBits(appGUID string, zipFile *os.File, presentFiles []resources.AppFileResource) (apiErr error) {
apiURL := fmt.Sprintf("/v2/apps/%s/bits", appGUID)
fileutils.TempFile("requests", func(requestFile *os.File, err error) {
if err != nil {
apiErr = fmt.Errorf("%s: %s", T("Error creating tmp file: {{.Err}}", map[string]interface{}{"Err": err}), err.Error())
return
}
// json.Marshal represents a nil value as "null" instead of an empty slice "[]"
if presentFiles == nil {
presentFiles = []resources.AppFileResource{}
}
presentFilesJSON, err := json.Marshal(presentFiles)
if err != nil {
apiErr = fmt.Errorf("%s: %s", T("Error marshaling JSON"), err.Error())
return
}
boundary, err := repo.writeUploadBody(zipFile, requestFile, presentFilesJSON)
if err != nil {
apiErr = fmt.Errorf("%s: %s", T("Error writing to tmp file: {{.Err}}", map[string]interface{}{"Err": err}), err.Error())
return
}
var request *net.Request
request, apiErr = repo.gateway.NewRequestForFile("PUT", repo.config.APIEndpoint()+apiURL, repo.config.AccessToken(), requestFile)
if apiErr != nil {
return
}
contentType := fmt.Sprintf("multipart/form-data; boundary=%s", boundary)
request.HTTPReq.Header.Set("Content-Type", contentType)
response := &resources.Resource{}
_, apiErr = repo.gateway.PerformPollingRequestForJSONResponse(repo.config.APIEndpoint(), request, response, DefaultAppUploadBitsTimeout)
if apiErr != nil {
return
}
})
return
}
func (repo CloudControllerApplicationBitsRepository) GetApplicationFiles(appFilesToCheck []resources.AppFileResource) ([]resources.AppFileResource, error) {
integrityFieldsJSON, err := json.Marshal(mapAppFilesToIntegrityFields(appFilesToCheck))
if err != nil {
apiErr := fmt.Errorf("%s: %s", T("Failed to create json for resource_match request"), err.Error())
return nil, apiErr
}
responseFieldsColl := []resources.IntegrityFields{}
apiErr := repo.gateway.UpdateResourceSync(
repo.config.APIEndpoint(),
"/v2/resource_match",
bytes.NewReader(integrityFieldsJSON),
&responseFieldsColl)
if apiErr != nil {
return nil, apiErr
}
return intersectAppFilesIntegrityFields(appFilesToCheck, responseFieldsColl), nil
}
func mapAppFilesToIntegrityFields(in []resources.AppFileResource) (out []resources.IntegrityFields) {
for _, appFile := range in {
out = append(out, appFile.ToIntegrityFields())
}
return out
}
func intersectAppFilesIntegrityFields(
appFiles []resources.AppFileResource,
integrityFields []resources.IntegrityFields,
) (out []resources.AppFileResource) {
inputFiles := appFilesBySha(appFiles)
for _, responseFields := range integrityFields {
item, found := inputFiles[responseFields.Sha1]
if found {
out = append(out, item)
}
}
return out
}
func appFilesBySha(in []resources.AppFileResource) (out map[string]resources.AppFileResource) {
out = map[string]resources.AppFileResource{}
for _, inputFileResource := range in {
out[inputFileResource.Sha1] = inputFileResource
}
return out
}
func (repo CloudControllerApplicationBitsRepository) writeUploadBody(zipFile *os.File, body *os.File, presentResourcesJSON []byte) (boundary string, err error) {
writer := multipart.NewWriter(body)
defer writer.Close()
boundary = writer.Boundary()
part, err := writer.CreateFormField("resources")
if err != nil {
return
}
_, err = io.Copy(part, bytes.NewBuffer(presentResourcesJSON))
if err != nil {
return
}
if zipFile != nil {
zipStats, zipErr := zipFile.Stat()
if zipErr != nil {
return
}
if zipStats.Size() == 0 {
return
}
part, zipErr = createZipPartWriter(zipStats, writer)
if zipErr != nil {
return
}
_, zipErr = io.Copy(part, zipFile)
if zipErr != nil {
return
}
}
return
}
func createZipPartWriter(zipStats os.FileInfo, writer *multipart.Writer) (io.Writer, error) {
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", `form-data; name="application"; filename="application.zip"`)
h.Set("Content-Type", "application/zip")
h.Set("Content-Length", fmt.Sprintf("%d", zipStats.Size()))
h.Set("Content-Transfer-Encoding", "binary")
return writer.CreatePart(h)
}