github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/v7action/resource_match.go (about)

     1  package v7action
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/sharedaction"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     7  	log "github.com/sirupsen/logrus"
     8  )
     9  
    10  func (actor Actor) ResourceMatch(resources []sharedaction.V3Resource) ([]sharedaction.V3Resource, Warnings, error) {
    11  	resourceChunks := actor.chunkResources(resources)
    12  
    13  	log.WithFields(log.Fields{
    14  		"total_resources": len(resources),
    15  		"chunks":          len(resourceChunks),
    16  	}).Debug("sending resource match stats")
    17  
    18  	var (
    19  		allWarnings         Warnings
    20  		matchedAPIResources []ccv3.Resource
    21  	)
    22  
    23  	for _, chunk := range resourceChunks {
    24  		newMatchedAPIResources, warnings, err := actor.CloudControllerClient.ResourceMatch(chunk)
    25  		allWarnings = append(allWarnings, warnings...)
    26  		if err != nil {
    27  			return nil, allWarnings, err
    28  		}
    29  		matchedAPIResources = append(matchedAPIResources, newMatchedAPIResources...)
    30  	}
    31  
    32  	var matchedResources []sharedaction.V3Resource
    33  	for _, resource := range matchedAPIResources {
    34  		matchedResources = append(matchedResources, sharedaction.V3Resource(resource))
    35  	}
    36  
    37  	log.WithFields(log.Fields{
    38  		"matchedResources": len(matchedResources),
    39  	}).Debug("number of resources matched by CC")
    40  
    41  	return matchedResources, allWarnings, nil
    42  }
    43  
    44  func (Actor) chunkResources(resources []sharedaction.V3Resource) [][]ccv3.Resource {
    45  	var chunkedResources [][]ccv3.Resource
    46  	var currentSet []ccv3.Resource
    47  
    48  	for index, resource := range resources {
    49  		if resource.SizeInBytes != 0 {
    50  			currentSet = append(currentSet, ccv3.Resource(resource))
    51  		}
    52  
    53  		if len(currentSet) == constant.MaxNumberOfResourcesForMatching || index+1 == len(resources) {
    54  			chunkedResources = append(chunkedResources, currentSet)
    55  			currentSet = []ccv3.Resource{}
    56  		}
    57  	}
    58  	return chunkedResources
    59  }