github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/api/cloudcontroller/ccv3/process_instance.go (about)

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