code.cloudfoundry.org/cli@v7.1.0+incompatible/actor/v3action/process_health_check.go (about)

     1  package v3action
     2  
     3  import (
     4  	"sort"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    10  )
    11  
    12  type ProcessHealthCheck struct {
    13  	ProcessType       string
    14  	HealthCheckType   constant.HealthCheckType
    15  	Endpoint          string
    16  	InvocationTimeout int64
    17  }
    18  
    19  type ProcessHealthChecks []ProcessHealthCheck
    20  
    21  func (phs ProcessHealthChecks) Sort() {
    22  	sort.Slice(phs, func(i int, j int) bool {
    23  		var iScore int
    24  		var jScore int
    25  
    26  		switch phs[i].ProcessType {
    27  		case constant.ProcessTypeWeb:
    28  			iScore = 0
    29  		default:
    30  			iScore = 1
    31  		}
    32  
    33  		switch phs[j].ProcessType {
    34  		case constant.ProcessTypeWeb:
    35  			jScore = 0
    36  		default:
    37  			jScore = 1
    38  		}
    39  
    40  		if iScore == 1 && jScore == 1 {
    41  			return phs[i].ProcessType < phs[j].ProcessType
    42  		}
    43  		return iScore < jScore
    44  	})
    45  }
    46  
    47  func (actor Actor) GetApplicationProcessHealthChecksByNameAndSpace(appName string, spaceGUID string) ([]ProcessHealthCheck, Warnings, error) {
    48  	app, allWarnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    49  	if err != nil {
    50  		return nil, allWarnings, err
    51  	}
    52  
    53  	ccv3Processes, warnings, err := actor.CloudControllerClient.GetApplicationProcesses(app.GUID)
    54  	allWarnings = append(allWarnings, Warnings(warnings)...)
    55  	if err != nil {
    56  		return nil, allWarnings, err
    57  	}
    58  
    59  	var processHealthChecks ProcessHealthChecks
    60  	for _, ccv3Process := range ccv3Processes {
    61  		processHealthCheck := ProcessHealthCheck{
    62  			ProcessType:       ccv3Process.Type,
    63  			HealthCheckType:   ccv3Process.HealthCheckType,
    64  			Endpoint:          ccv3Process.HealthCheckEndpoint,
    65  			InvocationTimeout: ccv3Process.HealthCheckInvocationTimeout,
    66  		}
    67  		processHealthChecks = append(processHealthChecks, processHealthCheck)
    68  	}
    69  
    70  	processHealthChecks.Sort()
    71  
    72  	return processHealthChecks, allWarnings, nil
    73  }
    74  
    75  func (actor Actor) SetApplicationProcessHealthCheckTypeByNameAndSpace(
    76  	appName string,
    77  	spaceGUID string,
    78  	healthCheckType constant.HealthCheckType,
    79  	httpEndpoint string,
    80  	processType string,
    81  	invocationTimeout int64,
    82  ) (Application, Warnings, error) {
    83  
    84  	if healthCheckType != constant.HTTP {
    85  		if httpEndpoint == "/" {
    86  			httpEndpoint = ""
    87  		} else {
    88  			return Application{}, nil, actionerror.HTTPHealthCheckInvalidError{}
    89  		}
    90  	}
    91  
    92  	app, allWarnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    93  	if err != nil {
    94  		return Application{}, allWarnings, err
    95  	}
    96  
    97  	process, warnings, err := actor.CloudControllerClient.GetApplicationProcessByType(app.GUID, processType)
    98  	allWarnings = append(allWarnings, Warnings(warnings)...)
    99  	if err != nil {
   100  		if _, ok := err.(ccerror.ProcessNotFoundError); ok {
   101  			return Application{}, allWarnings, actionerror.ProcessNotFoundError{ProcessType: processType}
   102  		}
   103  		return Application{}, allWarnings, err
   104  	}
   105  
   106  	process.HealthCheckType = healthCheckType
   107  	process.HealthCheckEndpoint = httpEndpoint
   108  	process.HealthCheckInvocationTimeout = invocationTimeout
   109  
   110  	_, updateWarnings, err := actor.CloudControllerClient.UpdateProcess(ccv3.Process(process))
   111  	allWarnings = append(allWarnings, Warnings(updateWarnings)...)
   112  	if err != nil {
   113  		return Application{}, allWarnings, err
   114  	}
   115  
   116  	return app, allWarnings, nil
   117  }