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

     1  package ccv3
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
    10  )
    11  
    12  // ProcessInstance represents a single process instance for a particular
    13  // application.
    14  type ProcessInstance struct {
    15  	//CPU is the current CPU usage of the instance.
    16  	CPU float64
    17  	//DiskQuota is the maximum disk the instance is allowed to use.
    18  	DiskQuota uint64
    19  	//DiskUsage is the current disk usage of the instance.
    20  	DiskUsage uint64
    21  	//Index is the index of the instance.
    22  	Index int
    23  	//MemoryQuota is the maximum memory the instance is allowed to use.
    24  	MemoryQuota uint64
    25  	//DiskUsage is the current memory usage of the instance.
    26  	MemoryUsage uint64
    27  	//State is the state of the instance.
    28  	State constant.ProcessInstanceState
    29  	//Uptime is the uptime in seconds for the instance.
    30  	Uptime int
    31  	//Details is information about errors placing the instance.
    32  	Details string
    33  }
    34  
    35  // UnmarshalJSON helps unmarshal a V3 Cloud Controller Instance response.
    36  func (instance *ProcessInstance) UnmarshalJSON(data []byte) error {
    37  	var inputInstance struct {
    38  		State string `json:"state"`
    39  		Usage struct {
    40  			CPU  float64 `json:"cpu"`
    41  			Mem  uint64  `json:"mem"`
    42  			Disk uint64  `json:"disk"`
    43  		} `json:"usage"`
    44  		MemQuota  uint64 `json:"mem_quota"`
    45  		DiskQuota uint64 `json:"disk_quota"`
    46  		Index     int    `json:"index"`
    47  		Uptime    int    `json:"uptime"`
    48  		Details   string `json:"details"`
    49  	}
    50  	err := cloudcontroller.DecodeJSON(data, &inputInstance)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	instance.State = constant.ProcessInstanceState(inputInstance.State)
    56  	instance.CPU = inputInstance.Usage.CPU
    57  	instance.MemoryUsage = inputInstance.Usage.Mem
    58  	instance.DiskUsage = inputInstance.Usage.Disk
    59  
    60  	instance.MemoryQuota = inputInstance.MemQuota
    61  	instance.DiskQuota = inputInstance.DiskQuota
    62  	instance.Index = inputInstance.Index
    63  	instance.Uptime = inputInstance.Uptime
    64  	instance.Details = inputInstance.Details
    65  
    66  	return nil
    67  }
    68  
    69  // DeleteApplicationProcessInstance deletes/stops a particular application's
    70  // process instance.
    71  func (client *Client) DeleteApplicationProcessInstance(appGUID string, processType string, instanceIndex int) (Warnings, error) {
    72  	request, err := client.newHTTPRequest(requestOptions{
    73  		RequestName: internal.DeleteApplicationProcessInstanceRequest,
    74  		URIParams: map[string]string{
    75  			"app_guid": appGUID,
    76  			"type":     processType,
    77  			"index":    strconv.Itoa(instanceIndex),
    78  		},
    79  	})
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	var response cloudcontroller.Response
    85  	err = client.connection.Make(request, &response)
    86  
    87  	return response.Warnings, err
    88  }
    89  
    90  // GetProcessInstances lists instance stats for a given process.
    91  func (client *Client) GetProcessInstances(processGUID string) ([]ProcessInstance, Warnings, error) {
    92  	request, err := client.newHTTPRequest(requestOptions{
    93  		RequestName: internal.GetProcessStatsRequest,
    94  		URIParams:   map[string]string{"process_guid": processGUID},
    95  	})
    96  	if err != nil {
    97  		return nil, nil, err
    98  	}
    99  
   100  	var fullInstancesList []ProcessInstance
   101  	warnings, err := client.paginate(request, ProcessInstance{}, func(item interface{}) error {
   102  		if instance, ok := item.(ProcessInstance); ok {
   103  			fullInstancesList = append(fullInstancesList, instance)
   104  		} else {
   105  			return ccerror.UnknownObjectInListError{
   106  				Expected:   ProcessInstance{},
   107  				Unexpected: item,
   108  			}
   109  		}
   110  		return nil
   111  	})
   112  
   113  	return fullInstancesList, warnings, err
   114  }