github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/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  	// MemoryUsage is the current memory usage of the instance.
    33  	MemoryUsage uint64
    34  	// LogRateLimit is the maximum rate that the instance is allowed to log.
    35  	LogRateLimit int64
    36  	// LogRate is the current rate that the instance is logging.
    37  	LogRate uint64
    38  	// State is the state of the instance.
    39  	State constant.ProcessInstanceState
    40  	// Type is the process type for the instance.
    41  	Type string
    42  	// Uptime is the duration that the instance has been running.
    43  	Uptime time.Duration
    44  }
    45  
    46  // UnmarshalJSON helps unmarshal a V3 Cloud Controller Instance response.
    47  func (instance *ProcessInstance) UnmarshalJSON(data []byte) error {
    48  	var inputInstance struct {
    49  		Details          string `json:"details"`
    50  		DiskQuota        uint64 `json:"disk_quota"`
    51  		Index            int64  `json:"index"`
    52  		IsolationSegment string `json:"isolation_segment"`
    53  		MemQuota         uint64 `json:"mem_quota"`
    54  		LogRateLimit     int64  `json:"log_rate_limit"`
    55  		State            string `json:"state"`
    56  		Type             string `json:"type"`
    57  		Uptime           int64  `json:"uptime"`
    58  		Usage            struct {
    59  			CPU     float64 `json:"cpu"`
    60  			Mem     uint64  `json:"mem"`
    61  			Disk    uint64  `json:"disk"`
    62  			LogRate uint64  `json:"log_rate"`
    63  		} `json:"usage"`
    64  	}
    65  
    66  	err := cloudcontroller.DecodeJSON(data, &inputInstance)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	instance.CPU = inputInstance.Usage.CPU
    72  	instance.Details = inputInstance.Details
    73  	instance.DiskQuota = inputInstance.DiskQuota
    74  	instance.DiskUsage = inputInstance.Usage.Disk
    75  	instance.Index = inputInstance.Index
    76  	instance.IsolationSegment = inputInstance.IsolationSegment
    77  	instance.MemoryQuota = inputInstance.MemQuota
    78  	instance.MemoryUsage = inputInstance.Usage.Mem
    79  	instance.LogRateLimit = inputInstance.LogRateLimit
    80  	instance.LogRate = inputInstance.Usage.LogRate
    81  	instance.State = constant.ProcessInstanceState(inputInstance.State)
    82  	instance.Type = inputInstance.Type
    83  	instance.Uptime, err = time.ParseDuration(fmt.Sprintf("%ds", inputInstance.Uptime))
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  // DeleteApplicationProcessInstance deletes/stops a particular application's
    92  // process instance.
    93  func (client *Client) DeleteApplicationProcessInstance(appGUID string, processType string, instanceIndex int) (Warnings, error) {
    94  	_, warnings, err := client.MakeRequest(RequestParams{
    95  		RequestName: internal.DeleteApplicationProcessInstanceRequest,
    96  		URIParams: internal.Params{
    97  			"app_guid": appGUID,
    98  			"type":     processType,
    99  			"index":    strconv.Itoa(instanceIndex),
   100  		},
   101  	})
   102  
   103  	return warnings, err
   104  }
   105  
   106  // GetProcessInstances lists instance stats for a given process.
   107  func (client *Client) GetProcessInstances(processGUID string) ([]ProcessInstance, Warnings, error) {
   108  	var resources []ProcessInstance
   109  
   110  	_, warnings, err := client.MakeListRequest(RequestParams{
   111  		RequestName:  internal.GetProcessStatsRequest,
   112  		URIParams:    internal.Params{"process_guid": processGUID},
   113  		ResponseBody: ProcessInstance{},
   114  		AppendToList: func(item interface{}) error {
   115  			resources = append(resources, item.(ProcessInstance))
   116  			return nil
   117  		},
   118  	})
   119  
   120  	return resources, warnings, err
   121  }