github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+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  }
    32  
    33  // UnmarshalJSON helps unmarshal a V3 Cloud Controller Instance response.
    34  func (instance *ProcessInstance) UnmarshalJSON(data []byte) error {
    35  	var inputInstance struct {
    36  		State string `json:"state"`
    37  		Usage struct {
    38  			CPU  float64 `json:"cpu"`
    39  			Mem  uint64  `json:"mem"`
    40  			Disk uint64  `json:"disk"`
    41  		} `json:"usage"`
    42  		MemQuota  uint64 `json:"mem_quota"`
    43  		DiskQuota uint64 `json:"disk_quota"`
    44  		Index     int    `json:"index"`
    45  		Uptime    int    `json:"uptime"`
    46  	}
    47  	err := cloudcontroller.DecodeJSON(data, &inputInstance)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	instance.State = constant.ProcessInstanceState(inputInstance.State)
    53  	instance.CPU = inputInstance.Usage.CPU
    54  	instance.MemoryUsage = inputInstance.Usage.Mem
    55  	instance.DiskUsage = inputInstance.Usage.Disk
    56  
    57  	instance.MemoryQuota = inputInstance.MemQuota
    58  	instance.DiskQuota = inputInstance.DiskQuota
    59  	instance.Index = inputInstance.Index
    60  	instance.Uptime = inputInstance.Uptime
    61  
    62  	return nil
    63  }
    64  
    65  // DeleteApplicationProcessInstance deletes/stops a particular application's
    66  // process instance.
    67  func (client *Client) DeleteApplicationProcessInstance(appGUID string, processType string, instanceIndex int) (Warnings, error) {
    68  	request, err := client.newHTTPRequest(requestOptions{
    69  		RequestName: internal.DeleteApplicationProcessInstanceRequest,
    70  		URIParams: map[string]string{
    71  			"app_guid": appGUID,
    72  			"type":     processType,
    73  			"index":    strconv.Itoa(instanceIndex),
    74  		},
    75  	})
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	var response cloudcontroller.Response
    81  	err = client.connection.Make(request, &response)
    82  
    83  	return response.Warnings, err
    84  }
    85  
    86  // GetProcessInstances lists instance stats for a given process.
    87  func (client *Client) GetProcessInstances(processGUID string) ([]ProcessInstance, Warnings, error) {
    88  	request, err := client.newHTTPRequest(requestOptions{
    89  		RequestName: internal.GetProcessStatsRequest,
    90  		URIParams:   map[string]string{"process_guid": processGUID},
    91  	})
    92  	if err != nil {
    93  		return nil, nil, err
    94  	}
    95  
    96  	var fullInstancesList []ProcessInstance
    97  	warnings, err := client.paginate(request, ProcessInstance{}, func(item interface{}) error {
    98  		if instance, ok := item.(ProcessInstance); ok {
    99  			fullInstancesList = append(fullInstancesList, instance)
   100  		} else {
   101  			return ccerror.UnknownObjectInListError{
   102  				Expected:   ProcessInstance{},
   103  				Unexpected: item,
   104  			}
   105  		}
   106  		return nil
   107  	})
   108  
   109  	return fullInstancesList, warnings, err
   110  }