github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/command/v3/v3_set_health_check_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  )
    15  
    16  //go:generate counterfeiter . V3SetHealthCheckActor
    17  
    18  type V3SetHealthCheckActor interface {
    19  	CloudControllerAPIVersion() string
    20  	SetApplicationProcessHealthCheckTypeByNameAndSpace(appName string, spaceGUID string, healthCheckType string, httpEndpoint string, processType string) (v3action.Application, v3action.Warnings, error)
    21  }
    22  
    23  type V3SetHealthCheckCommand struct {
    24  	RequiredArgs flag.SetHealthCheckArgs `positional-args:"yes"`
    25  	HTTPEndpoint string                  `long:"endpoint" default:"/" description:"Path on the app"`
    26  	ProcessType  string                  `long:"process" default:"web" description:"App process to update"`
    27  	usage        interface{}             `usage:"CF_NAME v3-set-health-check APP_NAME (process | port | http [--endpoint PATH]) [--process PROCESS]\n\nEXAMPLES:\n   cf v3-set-health-check worker-app process --process worker\n   cf v3-set-health-check my-web-app http --endpoint /foo"`
    28  
    29  	UI          command.UI
    30  	Config      command.Config
    31  	SharedActor command.SharedActor
    32  	Actor       V3SetHealthCheckActor
    33  }
    34  
    35  func (cmd *V3SetHealthCheckCommand) Setup(config command.Config, ui command.UI) error {
    36  	cmd.UI = ui
    37  	cmd.Config = config
    38  	cmd.SharedActor = sharedaction.NewActor(config)
    39  
    40  	ccClient, _, err := shared.NewClients(config, ui, true)
    41  	if err != nil {
    42  		if v3Err, ok := err.(ccerror.V3UnexpectedResponseError); ok && v3Err.ResponseCode == http.StatusNotFound {
    43  			return translatableerror.MinimumAPIVersionNotMetError{MinimumVersion: ccversion.MinVersionV3}
    44  		}
    45  
    46  		return err
    47  	}
    48  	cmd.Actor = v3action.NewActor(nil, ccClient, config)
    49  
    50  	return nil
    51  }
    52  
    53  func (cmd V3SetHealthCheckCommand) Execute(args []string) error {
    54  	cmd.UI.DisplayText(command.ExperimentalWarning)
    55  	cmd.UI.DisplayNewline()
    56  
    57  	err := command.MinimumAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionV3)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	err = cmd.SharedActor.CheckTarget(cmd.Config, true, true)
    63  	if err != nil {
    64  		return shared.HandleError(err)
    65  	}
    66  
    67  	user, err := cmd.Config.CurrentUser()
    68  	if err != nil {
    69  		return shared.HandleError(err)
    70  	}
    71  
    72  	cmd.UI.DisplayTextWithFlavor("Updating health check type for app {{.AppName}} process {{.ProcessType}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
    73  		"AppName":     cmd.RequiredArgs.AppName,
    74  		"ProcessType": cmd.ProcessType,
    75  		"OrgName":     cmd.Config.TargetedOrganization().Name,
    76  		"SpaceName":   cmd.Config.TargetedSpace().Name,
    77  		"Username":    user.Name,
    78  	})
    79  	cmd.UI.DisplayNewline()
    80  
    81  	app, warnings, err := cmd.Actor.SetApplicationProcessHealthCheckTypeByNameAndSpace(
    82  		cmd.RequiredArgs.AppName,
    83  		cmd.Config.TargetedSpace().GUID,
    84  		cmd.RequiredArgs.HealthCheck.Type,
    85  		cmd.HTTPEndpoint,
    86  		cmd.ProcessType,
    87  	)
    88  
    89  	cmd.UI.DisplayWarnings(warnings)
    90  	if err != nil {
    91  		return shared.HandleError(err)
    92  	}
    93  
    94  	cmd.UI.DisplayOK()
    95  
    96  	if app.Started() {
    97  		cmd.UI.DisplayNewline()
    98  		cmd.UI.DisplayText("TIP: An app restart is required for the change to take effect.")
    99  	}
   100  
   101  	return nil
   102  }