-
Notifications
You must be signed in to change notification settings - Fork 928
/
resource_match.go
59 lines (48 loc) · 1.77 KB
/
resource_match.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
package v7action
import (
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
log "github.com/sirupsen/logrus"
)
func (actor Actor) ResourceMatch(resources []sharedaction.V3Resource) ([]sharedaction.V3Resource, Warnings, error) {
resourceChunks := actor.chunkResources(resources)
log.WithFields(log.Fields{
"total_resources": len(resources),
"chunks": len(resourceChunks),
}).Debug("sending resource match stats")
var (
allWarnings Warnings
matchedAPIResources []ccv3.Resource
)
for _, chunk := range resourceChunks {
newMatchedAPIResources, warnings, err := actor.CloudControllerClient.ResourceMatch(chunk)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return nil, allWarnings, err
}
matchedAPIResources = append(matchedAPIResources, newMatchedAPIResources...)
}
var matchedResources []sharedaction.V3Resource
for _, resource := range matchedAPIResources {
matchedResources = append(matchedResources, sharedaction.V3Resource(resource))
}
log.WithFields(log.Fields{
"matchedResources": len(matchedResources),
}).Debug("number of resources matched by CC")
return matchedResources, allWarnings, nil
}
func (Actor) chunkResources(resources []sharedaction.V3Resource) [][]ccv3.Resource {
var chunkedResources [][]ccv3.Resource
var currentSet []ccv3.Resource
for index, resource := range resources {
if resource.SizeInBytes != 0 {
currentSet = append(currentSet, ccv3.Resource(resource))
}
if len(currentSet) == constant.MaxNumberOfResourcesForMatching || index+1 == len(resources) {
chunkedResources = append(chunkedResources, currentSet)
currentSet = []ccv3.Resource{}
}
}
return chunkedResources
}