github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/api/cloudcontroller/ccv3/process.go (about)

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