github.com/sleungcy/cli@v7.1.0+incompatible/actor/v7action/application_manifest.go (about)

     1  package v7action
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     6  )
     7  
     8  //go:generate counterfeiter . ManifestParser
     9  
    10  type ManifestParser interface {
    11  	AppNames() []string
    12  	RawAppManifest(name string) ([]byte, error)
    13  }
    14  
    15  // ApplyApplicationManifest reads in the manifest from the path and provides it
    16  // to the cloud controller.
    17  func (actor Actor) ApplyApplicationManifest(parser ManifestParser, spaceGUID string) (Warnings, error) {
    18  	var allWarnings Warnings
    19  
    20  	for _, appName := range parser.AppNames() {
    21  		rawManifest, err := parser.RawAppManifest(appName)
    22  		if err != nil {
    23  			return allWarnings, err
    24  		}
    25  
    26  		app, getAppWarnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    27  
    28  		allWarnings = append(allWarnings, getAppWarnings...)
    29  		if err != nil {
    30  			return allWarnings, err
    31  		}
    32  
    33  		applyManifestWarnings, err := actor.SetApplicationManifest(app.GUID, rawManifest)
    34  		allWarnings = append(allWarnings, applyManifestWarnings...)
    35  		if err != nil {
    36  			return allWarnings, err
    37  		}
    38  	}
    39  
    40  	return allWarnings, nil
    41  }
    42  
    43  func (actor Actor) SetApplicationManifest(appGUID string, rawManifest []byte) (Warnings, error) {
    44  	var allWarnings Warnings
    45  	jobURL, applyManifestWarnings, err := actor.CloudControllerClient.UpdateApplicationApplyManifest(appGUID, rawManifest)
    46  	allWarnings = append(allWarnings, applyManifestWarnings...)
    47  	if err != nil {
    48  		return allWarnings, err
    49  	}
    50  
    51  	pollWarnings, err := actor.CloudControllerClient.PollJob(jobURL)
    52  	allWarnings = append(allWarnings, pollWarnings...)
    53  	if err != nil {
    54  		if newErr, ok := err.(ccerror.V3JobFailedError); ok {
    55  			return allWarnings, actionerror.ApplicationManifestError{Message: newErr.Detail}
    56  		}
    57  		return allWarnings, err
    58  	}
    59  	return allWarnings, nil
    60  }
    61  
    62  func (actor Actor) GetRawApplicationManifestByNameAndSpace(appName string, spaceGUID string) ([]byte, Warnings, error) {
    63  	app, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    64  	if err != nil {
    65  		return nil, warnings, err
    66  	}
    67  
    68  	rawManifest, manifestWarnings, err := actor.CloudControllerClient.GetApplicationManifest(app.GUID)
    69  	return rawManifest, append(warnings, manifestWarnings...), err
    70  }