github.com/LukasHeimann/cloudfoundrycli@v7.1.0+incompatible/api/cloudcontroller/ccv3/process.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
    10  	"code.cloudfoundry.org/cli/resources"
    11  	"code.cloudfoundry.org/cli/types"
    12  )
    13  
    14  type Process struct {
    15  	GUID string
    16  	Type string
    17  	// Command is the process start command. Note: This value will be obfuscated when obtained from listing.
    18  	Command                      types.FilteredString
    19  	HealthCheckType              constant.HealthCheckType
    20  	HealthCheckEndpoint          string
    21  	HealthCheckInvocationTimeout int64
    22  	HealthCheckTimeout           int64
    23  	Instances                    types.NullInt
    24  	MemoryInMB                   types.NullUint64
    25  	DiskInMB                     types.NullUint64
    26  	AppGUID                      string
    27  }
    28  
    29  func (p Process) MarshalJSON() ([]byte, error) {
    30  	var ccProcess marshalProcess
    31  
    32  	marshalCommand(p, &ccProcess)
    33  	marshalInstances(p, &ccProcess)
    34  	marshalMemory(p, &ccProcess)
    35  	marshalDisk(p, &ccProcess)
    36  	marshalHealthCheck(p, &ccProcess)
    37  
    38  	return json.Marshal(ccProcess)
    39  }
    40  
    41  func (p *Process) UnmarshalJSON(data []byte) error {
    42  	var ccProcess struct {
    43  		Command       types.FilteredString    `json:"command"`
    44  		DiskInMB      types.NullUint64        `json:"disk_in_mb"`
    45  		GUID          string                  `json:"guid"`
    46  		Instances     types.NullInt           `json:"instances"`
    47  		MemoryInMB    types.NullUint64        `json:"memory_in_mb"`
    48  		Type          string                  `json:"type"`
    49  		Relationships resources.Relationships `json:"relationships"`
    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  	p.AppGUID = ccProcess.Relationships[constant.RelationshipTypeApplication].GUID
    77  
    78  	return nil
    79  }
    80  
    81  // CreateApplicationProcessScale updates process instances count, memory or disk
    82  func (client *Client) CreateApplicationProcessScale(appGUID string, process Process) (Process, Warnings, error) {
    83  	var responseBody Process
    84  
    85  	_, warnings, err := client.MakeRequest(RequestParams{
    86  		RequestName:  internal.PostApplicationProcessActionScaleRequest,
    87  		URIParams:    internal.Params{"app_guid": appGUID, "type": process.Type},
    88  		RequestBody:  process,
    89  		ResponseBody: &responseBody,
    90  	})
    91  
    92  	return responseBody, warnings, err
    93  }
    94  
    95  // GetApplicationProcessByType returns application process of specified type
    96  func (client *Client) GetApplicationProcessByType(appGUID string, processType string) (Process, Warnings, error) {
    97  	var responseBody Process
    98  
    99  	_, warnings, err := client.MakeRequest(RequestParams{
   100  		RequestName:  internal.GetApplicationProcessRequest,
   101  		URIParams:    internal.Params{"app_guid": appGUID, "type": processType},
   102  		ResponseBody: &responseBody,
   103  	})
   104  
   105  	return responseBody, warnings, err
   106  }
   107  
   108  // GetApplicationProcesses lists processes for a given application. **Note**:
   109  // Due to security, the API obfuscates certain values such as `command`.
   110  func (client *Client) GetApplicationProcesses(appGUID string) ([]Process, Warnings, error) {
   111  	var resources []Process
   112  
   113  	_, warnings, err := client.MakeListRequest(RequestParams{
   114  		RequestName:  internal.GetApplicationProcessesRequest,
   115  		URIParams:    internal.Params{"app_guid": appGUID},
   116  		ResponseBody: Process{},
   117  		AppendToList: func(item interface{}) error {
   118  			resources = append(resources, item.(Process))
   119  			return nil
   120  		},
   121  	})
   122  
   123  	return resources, warnings, err
   124  }
   125  
   126  // GetNewApplicationProcesses gets processes for an application in the middle of a deployment.
   127  // The app's processes will include a web process that will be removed when the deployment completes,
   128  // so exclude that soon-to-be-removed process from the result.
   129  func (client *Client) GetNewApplicationProcesses(appGUID string, deploymentGUID string) ([]Process, Warnings, error) {
   130  	var allWarnings Warnings
   131  
   132  	deployment, warnings, err := client.GetDeployment(deploymentGUID)
   133  	allWarnings = append(allWarnings, warnings...)
   134  	if err != nil {
   135  		return nil, allWarnings, err
   136  	}
   137  
   138  	allProcesses, warnings, err := client.GetApplicationProcesses(appGUID)
   139  	allWarnings = append(allWarnings, warnings...)
   140  	if err != nil {
   141  		return nil, allWarnings, err
   142  	}
   143  
   144  	var newWebProcessGUID string
   145  	for _, process := range deployment.NewProcesses {
   146  		if process.Type == constant.ProcessTypeWeb {
   147  			newWebProcessGUID = process.GUID
   148  		}
   149  	}
   150  
   151  	var processesList []Process
   152  	for _, process := range allProcesses {
   153  		if process.Type == constant.ProcessTypeWeb {
   154  			if process.GUID == newWebProcessGUID {
   155  				processesList = append(processesList, process)
   156  			}
   157  		} else {
   158  			processesList = append(processesList, process)
   159  		}
   160  	}
   161  
   162  	return processesList, allWarnings, nil
   163  }
   164  
   165  // GetProcess returns a process with the given guid
   166  func (client *Client) GetProcess(processGUID string) (Process, Warnings, error) {
   167  	var responseBody Process
   168  
   169  	_, warnings, err := client.MakeRequest(RequestParams{
   170  		RequestName:  internal.GetProcessRequest,
   171  		URIParams:    internal.Params{"process_guid": processGUID},
   172  		ResponseBody: &responseBody,
   173  	})
   174  
   175  	return responseBody, warnings, err
   176  }
   177  
   178  func (client Client) GetProcesses(query ...Query) ([]Process, Warnings, error) {
   179  	var resources []Process
   180  
   181  	_, warnings, err := client.MakeListRequest(RequestParams{
   182  		RequestName:  internal.GetProcessesRequest,
   183  		Query:        query,
   184  		ResponseBody: Process{},
   185  		AppendToList: func(item interface{}) error {
   186  			resources = append(resources, item.(Process))
   187  			return nil
   188  		},
   189  	})
   190  
   191  	return resources, warnings, err
   192  }
   193  
   194  // UpdateProcess updates the process's command or health check settings. GUID
   195  // is always required; HealthCheckType is only required when updating health
   196  // check settings.
   197  func (client *Client) UpdateProcess(process Process) (Process, Warnings, error) {
   198  	var responseBody Process
   199  
   200  	_, warnings, err := client.MakeRequest(RequestParams{
   201  		RequestName: internal.PatchProcessRequest,
   202  		URIParams:   internal.Params{"process_guid": process.GUID},
   203  		RequestBody: Process{
   204  			Command:                      process.Command,
   205  			HealthCheckType:              process.HealthCheckType,
   206  			HealthCheckEndpoint:          process.HealthCheckEndpoint,
   207  			HealthCheckTimeout:           process.HealthCheckTimeout,
   208  			HealthCheckInvocationTimeout: process.HealthCheckInvocationTimeout,
   209  		},
   210  		ResponseBody: &responseBody,
   211  	})
   212  
   213  	return responseBody, warnings, err
   214  }
   215  
   216  type healthCheck struct {
   217  	Type constant.HealthCheckType `json:"type,omitempty"`
   218  	Data struct {
   219  		Endpoint          interface{} `json:"endpoint,omitempty"`
   220  		InvocationTimeout int64       `json:"invocation_timeout,omitempty"`
   221  		Timeout           int64       `json:"timeout,omitempty"`
   222  	} `json:"data"`
   223  }
   224  
   225  type marshalProcess struct {
   226  	Command    interface{} `json:"command,omitempty"`
   227  	Instances  json.Number `json:"instances,omitempty"`
   228  	MemoryInMB json.Number `json:"memory_in_mb,omitempty"`
   229  	DiskInMB   json.Number `json:"disk_in_mb,omitempty"`
   230  
   231  	HealthCheck *healthCheck `json:"health_check,omitempty"`
   232  }
   233  
   234  func marshalCommand(p Process, ccProcess *marshalProcess) {
   235  	if p.Command.IsSet {
   236  		ccProcess.Command = &p.Command
   237  	}
   238  }
   239  
   240  func marshalDisk(p Process, ccProcess *marshalProcess) {
   241  	if p.DiskInMB.IsSet {
   242  		ccProcess.DiskInMB = json.Number(fmt.Sprint(p.DiskInMB.Value))
   243  	}
   244  }
   245  
   246  func marshalHealthCheck(p Process, ccProcess *marshalProcess) {
   247  	if p.HealthCheckType != "" || p.HealthCheckEndpoint != "" || p.HealthCheckInvocationTimeout != 0 || p.HealthCheckTimeout != 0 {
   248  		ccProcess.HealthCheck = new(healthCheck)
   249  		ccProcess.HealthCheck.Type = p.HealthCheckType
   250  		ccProcess.HealthCheck.Data.InvocationTimeout = p.HealthCheckInvocationTimeout
   251  		ccProcess.HealthCheck.Data.Timeout = p.HealthCheckTimeout
   252  		if p.HealthCheckEndpoint != "" {
   253  			ccProcess.HealthCheck.Data.Endpoint = p.HealthCheckEndpoint
   254  		}
   255  	}
   256  }
   257  
   258  func marshalInstances(p Process, ccProcess *marshalProcess) {
   259  	if p.Instances.IsSet {
   260  		ccProcess.Instances = json.Number(fmt.Sprint(p.Instances.Value))
   261  	}
   262  }
   263  
   264  func marshalMemory(p Process, ccProcess *marshalProcess) {
   265  	if p.MemoryInMB.IsSet {
   266  		ccProcess.MemoryInMB = json.Number(fmt.Sprint(p.MemoryInMB.Value))
   267  	}
   268  }