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