github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/v3_apply_manifest_command.go (about)

     1  package v3
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/actor/sharedaction"
     7  	"code.cloudfoundry.org/cli/actor/v3action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
    10  	"code.cloudfoundry.org/cli/command"
    11  	"code.cloudfoundry.org/cli/command/flag"
    12  	"code.cloudfoundry.org/cli/command/translatableerror"
    13  	"code.cloudfoundry.org/cli/command/v3/shared"
    14  	"code.cloudfoundry.org/cli/util/manifestparser"
    15  )
    16  
    17  //go:generate counterfeiter . ManifestParser
    18  
    19  type ManifestParser interface {
    20  	v3action.ManifestParser
    21  	Parse(manifestPath string) error
    22  }
    23  
    24  //go:generate counterfeiter . V3ApplyManifestActor
    25  
    26  type V3ApplyManifestActor interface {
    27  	CloudControllerAPIVersion() string
    28  	ApplyApplicationManifest(parser v3action.ManifestParser, spaceGUID string) (v3action.Warnings, error)
    29  }
    30  
    31  type V3ApplyManifestCommand struct {
    32  	PathToManifest flag.PathWithExistenceCheck `short:"f" description:"Path to app manifest" required:"true"`
    33  	usage          interface{}                 `usage:"CF_NAME v3-apply-manifest -f APP_MANIFEST_PATH"`
    34  
    35  	UI          command.UI
    36  	Config      command.Config
    37  	SharedActor command.SharedActor
    38  	Actor       V3ApplyManifestActor
    39  	Parser      ManifestParser
    40  }
    41  
    42  func (cmd *V3ApplyManifestCommand) Setup(config command.Config, ui command.UI) error {
    43  	cmd.UI = ui
    44  	cmd.Config = config
    45  	cmd.SharedActor = sharedaction.NewActor(config)
    46  
    47  	ccClient, _, err := shared.NewClients(config, ui, true)
    48  	if err != nil {
    49  		if v3Err, ok := err.(ccerror.V3UnexpectedResponseError); ok && v3Err.ResponseCode == http.StatusNotFound {
    50  			return translatableerror.MinimumAPIVersionNotMetError{MinimumVersion: ccversion.MinVersionV3}
    51  		}
    52  
    53  		return err
    54  	}
    55  	cmd.Actor = v3action.NewActor(ccClient, config, nil, nil)
    56  	cmd.Parser = manifestparser.NewParser()
    57  
    58  	return nil
    59  }
    60  
    61  func (cmd V3ApplyManifestCommand) Execute(args []string) error {
    62  	pathToManifest := string(cmd.PathToManifest)
    63  
    64  	cmd.UI.DisplayWarning(command.ExperimentalWarning)
    65  
    66  	// TODO: Update minimum API version when apply-manifest is complete in V3 API
    67  	err := command.MinimumAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionV3)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	err = cmd.SharedActor.CheckTarget(true, true)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	user, err := cmd.Config.CurrentUser()
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	cmd.UI.DisplayTextWithFlavor("Applying manifest {{.ManifestPath}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
    83  		"ManifestPath": pathToManifest,
    84  		"OrgName":      cmd.Config.TargetedOrganization().Name,
    85  		"SpaceName":    cmd.Config.TargetedSpace().Name,
    86  		"Username":     user.Name,
    87  	})
    88  
    89  	err = cmd.Parser.Parse(pathToManifest)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	warnings, err := cmd.Actor.ApplyApplicationManifest(cmd.Parser, cmd.Config.TargetedSpace().GUID)
    95  	cmd.UI.DisplayWarnings(warnings)
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	cmd.UI.DisplayOK()
   101  
   102  	return nil
   103  }