github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/actor/v2action/resource.go (about)

     1  package v2action
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     5  	log "github.com/sirupsen/logrus"
     6  )
     7  
     8  const (
     9  	DefaultFolderPermissions      = 0755
    10  	DefaultArchiveFilePermissions = 0744
    11  	MaxResourceMatchChunkSize     = 1000
    12  )
    13  
    14  var DefaultIgnoreLines = []string{
    15  	".cfignore",
    16  	".DS_Store",
    17  	".git",
    18  	".gitignore",
    19  	".hg",
    20  	".svn",
    21  	"_darcs",
    22  	"manifest.yaml",
    23  	"manifest.yml",
    24  }
    25  
    26  type Resource ccv2.Resource
    27  
    28  // ResourceMatch returns a set of matched resources and unmatched resources in
    29  // the order they were given in allResources.
    30  func (actor Actor) ResourceMatch(allResources []Resource) ([]Resource, []Resource, Warnings, error) {
    31  	resourcesToSend := [][]ccv2.Resource{{}}
    32  	var currentList, sendCount int
    33  	for _, resource := range allResources {
    34  		// Skip if resource is a directory, symlink, or empty file.
    35  		if resource.Size == 0 {
    36  			continue
    37  		}
    38  
    39  		resourcesToSend[currentList] = append(
    40  			resourcesToSend[currentList],
    41  			ccv2.Resource(resource),
    42  		)
    43  		sendCount++
    44  
    45  		if len(resourcesToSend[currentList]) == MaxResourceMatchChunkSize {
    46  			currentList++
    47  			resourcesToSend = append(resourcesToSend, []ccv2.Resource{})
    48  		}
    49  	}
    50  
    51  	log.WithFields(log.Fields{
    52  		"total_resources":    len(allResources),
    53  		"resources_to_match": sendCount,
    54  		"chunks":             len(resourcesToSend),
    55  	}).Debug("sending resource match stats")
    56  
    57  	matchedCCResources := map[string]ccv2.Resource{}
    58  	var allWarnings Warnings
    59  	for _, chunk := range resourcesToSend {
    60  		if len(chunk) == 0 {
    61  			log.Debug("chunk size 0, stopping resource match requests")
    62  			break
    63  		}
    64  
    65  		returnedResources, warnings, err := actor.CloudControllerClient.UpdateResourceMatch(chunk)
    66  		allWarnings = append(allWarnings, warnings...)
    67  
    68  		if err != nil {
    69  			log.Errorln("during resource matching", err)
    70  			return nil, nil, allWarnings, err
    71  		}
    72  
    73  		for _, resource := range returnedResources {
    74  			matchedCCResources[resource.SHA1] = resource
    75  		}
    76  	}
    77  	log.WithField("matched_resource_count", len(matchedCCResources)).Debug("total number of matched resources")
    78  
    79  	var matchedResources, unmatchedResources []Resource
    80  	for _, resource := range allResources {
    81  		if _, ok := matchedCCResources[resource.SHA1]; ok {
    82  			matchedResources = append(matchedResources, resource)
    83  		} else {
    84  			unmatchedResources = append(unmatchedResources, resource)
    85  		}
    86  	}
    87  
    88  	return matchedResources, unmatchedResources, allWarnings, nil
    89  }
    90  
    91  func (Actor) actorToCCResources(resources []Resource) []ccv2.Resource {
    92  	apiResources := make([]ccv2.Resource, 0, len(resources)) // Explicitly done to prevent nils
    93  
    94  	for _, resource := range resources {
    95  		apiResources = append(apiResources, ccv2.Resource(resource))
    96  	}
    97  
    98  	return apiResources
    99  }