github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/set_health_check_command.go (about)

     1  package v2
     2  
     3  import (
     4  	"github.com/liamawhite/cli-with-i18n/actor/sharedaction"
     5  	"github.com/liamawhite/cli-with-i18n/actor/v2action"
     6  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccversion"
     7  
     8  	"github.com/liamawhite/cli-with-i18n/command"
     9  	"github.com/liamawhite/cli-with-i18n/command/flag"
    10  	"github.com/liamawhite/cli-with-i18n/command/translatableerror"
    11  	"github.com/liamawhite/cli-with-i18n/command/v2/shared"
    12  )
    13  
    14  //go:generate counterfeiter . SetHealthCheckActor
    15  type SetHealthCheckActor interface {
    16  	SetApplicationHealthCheckTypeByNameAndSpace(name string, spaceGUID string, healthCheckType v2action.ApplicationHealthCheckType, httpEndpoint string) (v2action.Application, v2action.Warnings, error)
    17  	CloudControllerAPIVersion() string
    18  }
    19  
    20  type SetHealthCheckCommand struct {
    21  	RequiredArgs flag.SetHealthCheckArgs `positional-args:"yes"`
    22  	HTTPEndpoint string                  `long:"endpoint" default:"/" description:"Path on the app"`
    23  	usage        interface{}             `usage:"CF_NAME set-health-check APP_NAME (process | port | http [--endpoint PATH])\n\nTIP: 'none' has been deprecated but is accepted for 'process'.\n\nEXAMPLES:\n   cf set-health-check worker-app process\n   cf set-health-check my-web-app http --endpoint /foo"`
    24  	UI           command.UI
    25  	Config       command.Config
    26  	SharedActor  command.SharedActor
    27  	Actor        SetHealthCheckActor
    28  }
    29  
    30  func (cmd *SetHealthCheckCommand) Setup(config command.Config, ui command.UI) error {
    31  	cmd.Config = config
    32  	cmd.UI = ui
    33  	cmd.SharedActor = sharedaction.NewActor(config)
    34  
    35  	ccClient, uaaClient, err := shared.NewClients(config, ui, true)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
    40  
    41  	return nil
    42  }
    43  
    44  func (cmd *SetHealthCheckCommand) Execute(args []string) error {
    45  	var err error
    46  
    47  	switch cmd.RequiredArgs.HealthCheck.Type {
    48  	case "http":
    49  		err = command.MinimumAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionProcessHealthCheckV2)
    50  		if err != nil {
    51  			return translatableerror.HealthCheckTypeUnsupportedError{SupportedTypes: []string{"port", "none"}}
    52  		}
    53  		err = command.MinimumAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionHTTPEndpointHealthCheckV2)
    54  		if err != nil {
    55  			return translatableerror.HealthCheckTypeUnsupportedError{SupportedTypes: []string{"port", "none", "process"}}
    56  		}
    57  	case "process":
    58  		err = command.MinimumAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionProcessHealthCheckV2)
    59  		if err != nil {
    60  			return translatableerror.HealthCheckTypeUnsupportedError{SupportedTypes: []string{"port", "none"}}
    61  		}
    62  	}
    63  
    64  	err = cmd.SharedActor.CheckTarget(cmd.Config, true, true)
    65  	if err != nil {
    66  		return shared.HandleError(err)
    67  	}
    68  
    69  	user, err := cmd.Config.CurrentUser()
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	cmd.UI.DisplayTextWithFlavor("Updating health check type for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...",
    75  		map[string]interface{}{
    76  			"AppName":   cmd.RequiredArgs.AppName,
    77  			"OrgName":   cmd.Config.TargetedOrganization().Name,
    78  			"SpaceName": cmd.Config.TargetedSpace().Name,
    79  			"Username":  user.Name,
    80  		})
    81  
    82  	app, warnings, err := cmd.Actor.SetApplicationHealthCheckTypeByNameAndSpace(
    83  		cmd.RequiredArgs.AppName,
    84  		cmd.Config.TargetedSpace().GUID,
    85  		v2action.ApplicationHealthCheckType(cmd.RequiredArgs.HealthCheck.Type),
    86  		cmd.HTTPEndpoint,
    87  	)
    88  	cmd.UI.DisplayWarnings(warnings)
    89  	if err != nil {
    90  		return shared.HandleError(err)
    91  	}
    92  
    93  	cmd.UI.DisplayOK()
    94  
    95  	if app.Started() {
    96  		cmd.UI.DisplayNewline()
    97  		cmd.UI.DisplayText("TIP: An app restart is required for the change to take affect.")
    98  	}
    99  
   100  	return nil
   101  }