github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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  	HealthCheck ProcessHealthCheck `json:"health_check"`
    18  	Instances   types.NullInt      `json:"instances"`
    19  	MemoryInMB  types.NullUint64   `json:"memory_in_mb"`
    20  	DiskInMB    types.NullUint64   `json:"disk_in_mb"`
    21  }
    22  
    23  type ProcessHealthCheck struct {
    24  	Type string                 `json:"type"`
    25  	Data ProcessHealthCheckData `json:"data"`
    26  }
    27  
    28  type ProcessHealthCheckData struct {
    29  	Endpoint string `json:"endpoint"`
    30  }
    31  
    32  func (p Process) MarshalJSON() ([]byte, error) {
    33  	var ccProcess struct {
    34  		HealthCheck struct {
    35  			Type string `json:"type"`
    36  			Data struct {
    37  				Endpoint interface{} `json:"endpoint"`
    38  			} `json:"data"`
    39  		} `json:"health_check"`
    40  	}
    41  
    42  	ccProcess.HealthCheck.Type = p.HealthCheck.Type
    43  	if p.HealthCheck.Data.Endpoint != "" {
    44  		ccProcess.HealthCheck.Data.Endpoint = p.HealthCheck.Data.Endpoint
    45  	}
    46  	return json.Marshal(ccProcess)
    47  }
    48  
    49  // GetApplicationProcesses lists processes for a given app
    50  func (client *Client) GetApplicationProcesses(appGUID string) ([]Process, Warnings, error) {
    51  	request, err := client.newHTTPRequest(requestOptions{
    52  		RequestName: internal.GetAppProcessesRequest,
    53  		URIParams:   map[string]string{"app_guid": appGUID},
    54  	})
    55  	if err != nil {
    56  		return nil, nil, err
    57  	}
    58  
    59  	var fullProcessesList []Process
    60  	warnings, err := client.paginate(request, Process{}, func(item interface{}) error {
    61  		if process, ok := item.(Process); ok {
    62  			fullProcessesList = append(fullProcessesList, process)
    63  		} else {
    64  			return ccerror.UnknownObjectInListError{
    65  				Expected:   Process{},
    66  				Unexpected: item,
    67  			}
    68  		}
    69  		return nil
    70  	})
    71  
    72  	return fullProcessesList, warnings, err
    73  }
    74  
    75  // GetApplicationProcessByType returns application process of specified type
    76  func (client *Client) GetApplicationProcessByType(appGUID string, processType string) (Process, Warnings, error) {
    77  	request, err := client.newHTTPRequest(requestOptions{
    78  		RequestName: internal.GetApplicationProcessByTypeRequest,
    79  		URIParams: map[string]string{
    80  			"app_guid": appGUID,
    81  			"type":     processType,
    82  		},
    83  	})
    84  	if err != nil {
    85  		return Process{}, nil, err
    86  	}
    87  	var process Process
    88  	response := cloudcontroller.Response{
    89  		Result: &process,
    90  	}
    91  
    92  	err = client.connection.Make(request, &response)
    93  	return process, response.Warnings, err
    94  }
    95  
    96  // PatchApplicationProcessHealthCheck updates application health check type
    97  func (client *Client) PatchApplicationProcessHealthCheck(processGUID string, processHealthCheckType string, processHealthCheckEndpoint string) (Warnings, error) {
    98  	body, err := json.Marshal(Process{
    99  		HealthCheck: ProcessHealthCheck{
   100  			Type: processHealthCheckType,
   101  			Data: ProcessHealthCheckData{
   102  				Endpoint: processHealthCheckEndpoint,
   103  			}}})
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  
   108  	request, err := client.newHTTPRequest(requestOptions{
   109  		RequestName: internal.PatchApplicationProcessHealthCheckRequest,
   110  		Body:        bytes.NewReader(body),
   111  		URIParams:   internal.Params{"process_guid": processGUID},
   112  	})
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	var response cloudcontroller.Response
   118  	err = client.connection.Make(request, &response)
   119  	return response.Warnings, err
   120  }
   121  
   122  // CreateApplicationProcessScale updates process instances count, memory or disk
   123  func (client *Client) CreateApplicationProcessScale(appGUID string, process Process) (Warnings, error) {
   124  	ccProcessScale := struct {
   125  		Instances  json.Number `json:"instances,omitempty"`
   126  		MemoryInMB json.Number `json:"memory_in_mb,omitempty"`
   127  		DiskInMB   json.Number `json:"disk_in_mb,omitempty"`
   128  	}{}
   129  
   130  	if process.Instances.IsSet {
   131  		ccProcessScale.Instances = json.Number(fmt.Sprint(process.Instances.Value))
   132  	}
   133  	if process.MemoryInMB.IsSet {
   134  		ccProcessScale.MemoryInMB = json.Number(fmt.Sprint(process.MemoryInMB.Value))
   135  	}
   136  	if process.DiskInMB.IsSet {
   137  		ccProcessScale.DiskInMB = json.Number(fmt.Sprint(process.DiskInMB.Value))
   138  	}
   139  
   140  	body, err := json.Marshal(ccProcessScale)
   141  	if err != nil {
   142  		return nil, err
   143  	}
   144  
   145  	request, err := client.newHTTPRequest(requestOptions{
   146  		RequestName: internal.PostApplicationProcessScaleRequest,
   147  		Body:        bytes.NewReader(body),
   148  		URIParams:   internal.Params{"app_guid": appGUID, "type": process.Type},
   149  	})
   150  	if err != nil {
   151  		return nil, err
   152  	}
   153  
   154  	var response cloudcontroller.Response
   155  	err = client.connection.Make(request, &response)
   156  	return response.Warnings, err
   157  }