-
Notifications
You must be signed in to change notification settings - Fork 929
/
resource.go
99 lines (82 loc) · 2.61 KB
/
resource.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
package v2action
import (
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
log "github.com/sirupsen/logrus"
)
const (
DefaultFolderPermissions = 0755
DefaultArchiveFilePermissions = 0744
MaxResourceMatchChunkSize = 1000
)
var DefaultIgnoreLines = []string{
".cfignore",
".DS_Store",
".git",
".gitignore",
".hg",
".svn",
"_darcs",
"manifest.yaml",
"manifest.yml",
}
type Resource ccv2.Resource
// ResourceMatch returns a set of matched resources and unmatched resources in
// the order they were given in allResources.
func (actor Actor) ResourceMatch(allResources []Resource) ([]Resource, []Resource, Warnings, error) {
resourcesToSend := [][]ccv2.Resource{{}}
var currentList, sendCount int
for _, resource := range allResources {
// Skip if resource is a directory, symlink, or empty file.
if resource.Size == 0 {
continue
}
resourcesToSend[currentList] = append(
resourcesToSend[currentList],
ccv2.Resource(resource),
)
sendCount++
if len(resourcesToSend[currentList]) == MaxResourceMatchChunkSize {
currentList++
resourcesToSend = append(resourcesToSend, []ccv2.Resource{})
}
}
log.WithFields(log.Fields{
"total_resources": len(allResources),
"resources_to_match": sendCount,
"chunks": len(resourcesToSend),
}).Debug("sending resource match stats")
matchedCCResources := map[string]ccv2.Resource{}
var allWarnings Warnings
for _, chunk := range resourcesToSend {
if len(chunk) == 0 {
log.Debug("chunk size 0, stopping resource match requests")
break
}
returnedResources, warnings, err := actor.CloudControllerClient.UpdateResourceMatch(chunk)
allWarnings = append(allWarnings, warnings...)
if err != nil {
log.Errorln("during resource matching", err)
return nil, nil, allWarnings, err
}
for _, resource := range returnedResources {
matchedCCResources[resource.SHA1] = resource
}
}
log.WithField("matched_resource_count", len(matchedCCResources)).Debug("total number of matched resources")
var matchedResources, unmatchedResources []Resource
for _, resource := range allResources {
if _, ok := matchedCCResources[resource.SHA1]; ok {
matchedResources = append(matchedResources, resource)
} else {
unmatchedResources = append(unmatchedResources, resource)
}
}
return matchedResources, unmatchedResources, allWarnings, nil
}
func (Actor) actorToCCResources(resources []Resource) []ccv2.Resource {
apiResources := make([]ccv2.Resource, 0, len(resources)) // Explicitly done to prevent nils
for _, resource := range resources {
apiResources = append(apiResources, ccv2.Resource(resource))
}
return apiResources
}