github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+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  	// Details is information about errors placing the instance.
    18  	Details string
    19  	// DiskQuota is the maximum disk the instance is allowed to use.
    20  	DiskQuota uint64
    21  	// DiskUsage is the current disk usage of the instance.
    22  	DiskUsage uint64
    23  	// Index is the index of the instance.
    24  	Index int
    25  	// Isolation segment is the current isolation segment that the instance is
    26  	// running on. The value is empty when the instance is not placed on a
    27  	// particular isolation segment.
    28  	IsolationSegment string
    29  	// MemoryQuota is the maximum memory the instance is allowed to use.
    30  	MemoryQuota uint64
    31  	// DiskUsage is the current memory usage of the instance.
    32  	MemoryUsage uint64
    33  	// State is the state of the instance.
    34  	State constant.ProcessInstanceState
    35  	// Type is the process type for the instance.
    36  	Type string
    37  	// Uptime is the uptime in seconds for the instance.
    38  	Uptime int
    39  }
    40  
    41  // UnmarshalJSON helps unmarshal a V3 Cloud Controller Instance response.
    42  func (instance *ProcessInstance) UnmarshalJSON(data []byte) error {
    43  	var inputInstance struct {
    44  		Details          string `json:"details"`
    45  		DiskQuota        uint64 `json:"disk_quota"`
    46  		Index            int    `json:"index"`
    47  		IsolationSegment string `json:"isolation_segment"`
    48  		MemQuota         uint64 `json:"mem_quota"`
    49  		State            string `json:"state"`
    50  		Type             string `json:"type"`
    51  		Uptime           int    `json:"uptime"`
    52  		Usage            struct {
    53  			CPU  float64 `json:"cpu"`
    54  			Mem  uint64  `json:"mem"`
    55  			Disk uint64  `json:"disk"`
    56  		} `json:"usage"`
    57  	}
    58  
    59  	err := cloudcontroller.DecodeJSON(data, &inputInstance)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	instance.CPU = inputInstance.Usage.CPU
    65  	instance.Details = inputInstance.Details
    66  	instance.DiskQuota = inputInstance.DiskQuota
    67  	instance.DiskUsage = inputInstance.Usage.Disk
    68  	instance.Index = inputInstance.Index
    69  	instance.IsolationSegment = inputInstance.IsolationSegment
    70  	instance.MemoryQuota = inputInstance.MemQuota
    71  	instance.MemoryUsage = inputInstance.Usage.Mem
    72  	instance.State = constant.ProcessInstanceState(inputInstance.State)
    73  	instance.Type = inputInstance.Type
    74  	instance.Uptime = inputInstance.Uptime
    75  
    76  	return nil
    77  }
    78  
    79  // DeleteApplicationProcessInstance deletes/stops a particular application's
    80  // process instance.
    81  func (client *Client) DeleteApplicationProcessInstance(appGUID string, processType string, instanceIndex int) (Warnings, error) {
    82  	request, err := client.newHTTPRequest(requestOptions{
    83  		RequestName: internal.DeleteApplicationProcessInstanceRequest,
    84  		URIParams: map[string]string{
    85  			"app_guid": appGUID,
    86  			"type":     processType,
    87  			"index":    strconv.Itoa(instanceIndex),
    88  		},
    89  	})
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  
    94  	var response cloudcontroller.Response
    95  	err = client.connection.Make(request, &response)
    96  
    97  	return response.Warnings, err
    98  }
    99  
   100  // GetProcessInstances lists instance stats for a given process.
   101  func (client *Client) GetProcessInstances(processGUID string) ([]ProcessInstance, Warnings, error) {
   102  	request, err := client.newHTTPRequest(requestOptions{
   103  		RequestName: internal.GetProcessStatsRequest,
   104  		URIParams:   map[string]string{"process_guid": processGUID},
   105  	})
   106  	if err != nil {
   107  		return nil, nil, err
   108  	}
   109  
   110  	var fullInstancesList []ProcessInstance
   111  	warnings, err := client.paginate(request, ProcessInstance{}, func(item interface{}) error {
   112  		if instance, ok := item.(ProcessInstance); ok {
   113  			fullInstancesList = append(fullInstancesList, instance)
   114  		} else {
   115  			return ccerror.UnknownObjectInListError{
   116  				Expected:   ProcessInstance{},
   117  				Unexpected: item,
   118  			}
   119  		}
   120  		return nil
   121  	})
   122  
   123  	return fullInstancesList, warnings, err
   124  }