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