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