github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/api/cloudcontroller/ccv3/process.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
    11  	"code.cloudfoundry.org/cli/types"
    12  )
    13  
    14  type Process struct {
    15  	GUID string `json:"guid"`
    16  	Type string `json:"type"`
    17  	// Command is the process start command. Note: This value will be obfuscated when obtained from listing.
    18  	Command                      string           `json:"command,omitempty"`
    19  	HealthCheckType              string           `json:"-"`
    20  	HealthCheckEndpoint          string           `json:"-"`
    21  	HealthCheckInvocationTimeout int              `json:"-"`
    22  	Instances                    types.NullInt    `json:"instances,omitempty"`
    23  	MemoryInMB                   types.NullUint64 `json:"memory_in_mb,omitempty"`
    24  	DiskInMB                     types.NullUint64 `json:"disk_in_mb,omitempty"`
    25  }
    26  
    27  func (p Process) MarshalJSON() ([]byte, error) {
    28  	type healthCheck struct {
    29  		Type string `json:"type"`
    30  		Data struct {
    31  			Endpoint          interface{} `json:"endpoint"`
    32  			InvocationTimeout int         `json:"invocation_timeout,omitempty"`
    33  		} `json:"data"`
    34  	}
    35  
    36  	var ccProcess struct {
    37  		Instances  json.Number `json:"instances,omitempty"`
    38  		MemoryInMB json.Number `json:"memory_in_mb,omitempty"`
    39  		DiskInMB   json.Number `json:"disk_in_mb,omitempty"`
    40  
    41  		HealthCheck *healthCheck `json:"health_check,omitempty"`
    42  	}
    43  
    44  	if p.Instances.IsSet {
    45  		ccProcess.Instances = json.Number(fmt.Sprint(p.Instances.Value))
    46  	}
    47  	if p.MemoryInMB.IsSet {
    48  		ccProcess.MemoryInMB = json.Number(fmt.Sprint(p.MemoryInMB.Value))
    49  	}
    50  	if p.DiskInMB.IsSet {
    51  		ccProcess.DiskInMB = json.Number(fmt.Sprint(p.DiskInMB.Value))
    52  	}
    53  
    54  	if p.HealthCheckType != "" || p.HealthCheckEndpoint != "" || p.HealthCheckInvocationTimeout != 0 {
    55  		ccProcess.HealthCheck = new(healthCheck)
    56  		ccProcess.HealthCheck.Type = p.HealthCheckType
    57  		ccProcess.HealthCheck.Data.InvocationTimeout = p.HealthCheckInvocationTimeout
    58  		if p.HealthCheckEndpoint != "" {
    59  			ccProcess.HealthCheck.Data.Endpoint = p.HealthCheckEndpoint
    60  		}
    61  	}
    62  
    63  	return json.Marshal(ccProcess)
    64  }
    65  
    66  func (p *Process) UnmarshalJSON(data []byte) error {
    67  	type rawProcess Process
    68  	var ccProcess struct {
    69  		*rawProcess
    70  
    71  		HealthCheck struct {
    72  			Type string `json:"type"`
    73  			Data struct {
    74  				Endpoint          string `json:"endpoint"`
    75  				InvocationTimeout int    `json:"invocation_timeout"`
    76  			} `json:"data"`
    77  		} `json:"health_check"`
    78  	}
    79  
    80  	ccProcess.rawProcess = (*rawProcess)(p)
    81  	err := cloudcontroller.DecodeJSON(data, &ccProcess)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	p.HealthCheckEndpoint = ccProcess.HealthCheck.Data.Endpoint
    87  	p.HealthCheckType = ccProcess.HealthCheck.Type
    88  	p.HealthCheckInvocationTimeout = ccProcess.HealthCheck.Data.InvocationTimeout
    89  
    90  	return nil
    91  }
    92  
    93  // CreateApplicationProcessScale updates process instances count, memory or disk
    94  func (client *Client) CreateApplicationProcessScale(appGUID string, process Process) (Process, Warnings, error) {
    95  	body, err := json.Marshal(process)
    96  	if err != nil {
    97  		return Process{}, nil, err
    98  	}
    99  
   100  	request, err := client.newHTTPRequest(requestOptions{
   101  		RequestName: internal.PostApplicationProcessActionScaleRequest,
   102  		Body:        bytes.NewReader(body),
   103  		URIParams:   internal.Params{"app_guid": appGUID, "type": process.Type},
   104  	})
   105  	if err != nil {
   106  		return Process{}, nil, err
   107  	}
   108  
   109  	var responseProcess Process
   110  	response := cloudcontroller.Response{
   111  		Result: &responseProcess,
   112  	}
   113  	err = client.connection.Make(request, &response)
   114  	return responseProcess, response.Warnings, err
   115  }
   116  
   117  // GetApplicationProcessByType returns application process of specified type
   118  func (client *Client) GetApplicationProcessByType(appGUID string, processType string) (Process, Warnings, error) {
   119  	request, err := client.newHTTPRequest(requestOptions{
   120  		RequestName: internal.GetApplicationProcessRequest,
   121  		URIParams: map[string]string{
   122  			"app_guid": appGUID,
   123  			"type":     processType,
   124  		},
   125  	})
   126  	if err != nil {
   127  		return Process{}, nil, err
   128  	}
   129  	var process Process
   130  	response := cloudcontroller.Response{
   131  		Result: &process,
   132  	}
   133  
   134  	err = client.connection.Make(request, &response)
   135  	return process, response.Warnings, err
   136  }
   137  
   138  // GetApplicationProcesses lists processes for a given application. **Note**:
   139  // Due to security, the API obfuscates certain values such as `command`.
   140  func (client *Client) GetApplicationProcesses(appGUID string) ([]Process, Warnings, error) {
   141  	request, err := client.newHTTPRequest(requestOptions{
   142  		RequestName: internal.GetApplicationProcessesRequest,
   143  		URIParams:   map[string]string{"app_guid": appGUID},
   144  	})
   145  	if err != nil {
   146  		return nil, nil, err
   147  	}
   148  
   149  	var fullProcessesList []Process
   150  	warnings, err := client.paginate(request, Process{}, func(item interface{}) error {
   151  		if process, ok := item.(Process); ok {
   152  			fullProcessesList = append(fullProcessesList, process)
   153  		} else {
   154  			return ccerror.UnknownObjectInListError{
   155  				Expected:   Process{},
   156  				Unexpected: item,
   157  			}
   158  		}
   159  		return nil
   160  	})
   161  
   162  	return fullProcessesList, warnings, err
   163  }
   164  
   165  // PatchApplicationProcessHealthCheck updates application health check type
   166  func (client *Client) PatchApplicationProcessHealthCheck(processGUID string, processHealthCheckType string, processHealthCheckEndpoint string, processHealthCheckInvocationTimeout int) (Process, Warnings, error) {
   167  	body, err := json.Marshal(Process{
   168  		HealthCheckType:              processHealthCheckType,
   169  		HealthCheckEndpoint:          processHealthCheckEndpoint,
   170  		HealthCheckInvocationTimeout: processHealthCheckInvocationTimeout,
   171  	})
   172  	if err != nil {
   173  		return Process{}, nil, err
   174  	}
   175  
   176  	request, err := client.newHTTPRequest(requestOptions{
   177  		RequestName: internal.PatchProcessRequest,
   178  		Body:        bytes.NewReader(body),
   179  		URIParams:   internal.Params{"process_guid": processGUID},
   180  	})
   181  	if err != nil {
   182  		return Process{}, nil, err
   183  	}
   184  
   185  	var responseProcess Process
   186  	response := cloudcontroller.Response{
   187  		Result: &responseProcess,
   188  	}
   189  	err = client.connection.Make(request, &response)
   190  	return responseProcess, response.Warnings, err
   191  }