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