github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/actor/v3action/process_health_check.go (about)

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