github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/ccv3/instance.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
    11  )
    12  
    13  type ProcessInstance struct {
    14  	Index       int
    15  	State       constant.ProcessInstanceState
    16  	Uptime      int
    17  	CPU         float64
    18  	MemoryUsage uint64
    19  	MemoryQuota uint64
    20  	DiskUsage   uint64
    21  	DiskQuota   uint64
    22  }
    23  
    24  // UnmarshalJSON helps unmarshal a V3 Cloud Controller Instance response.
    25  func (instance *ProcessInstance) UnmarshalJSON(data []byte) error {
    26  	var inputInstance struct {
    27  		State string `json:"state"`
    28  		Usage struct {
    29  			CPU  float64 `json:"cpu"`
    30  			Mem  uint64  `json:"mem"`
    31  			Disk uint64  `json:"disk"`
    32  		} `json:"usage"`
    33  		MemQuota  uint64 `json:"mem_quota"`
    34  		DiskQuota uint64 `json:"disk_quota"`
    35  		Index     int    `json:"index"`
    36  		Uptime    int    `json:"uptime"`
    37  	}
    38  	if err := json.Unmarshal(data, &inputInstance); err != nil {
    39  		return err
    40  	}
    41  
    42  	instance.State = constant.ProcessInstanceState(inputInstance.State)
    43  	instance.CPU = inputInstance.Usage.CPU
    44  	instance.MemoryUsage = inputInstance.Usage.Mem
    45  	instance.DiskUsage = inputInstance.Usage.Disk
    46  
    47  	instance.MemoryQuota = inputInstance.MemQuota
    48  	instance.DiskQuota = inputInstance.DiskQuota
    49  	instance.Index = inputInstance.Index
    50  	instance.Uptime = inputInstance.Uptime
    51  
    52  	return nil
    53  }
    54  
    55  // DeleteApplicationProcessInstance deletes/stops a particular application's
    56  // process instance.
    57  func (client *Client) DeleteApplicationProcessInstance(appGUID string, processType string, instanceIndex int) (Warnings, error) {
    58  	request, err := client.newHTTPRequest(requestOptions{
    59  		RequestName: internal.DeleteApplicationProcessInstanceRequest,
    60  		URIParams: map[string]string{
    61  			"app_guid": appGUID,
    62  			"type":     processType,
    63  			"index":    strconv.Itoa(instanceIndex),
    64  		},
    65  	})
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	var response cloudcontroller.Response
    71  	err = client.connection.Make(request, &response)
    72  
    73  	return response.Warnings, err
    74  }
    75  
    76  // GetProcessInstances lists instance stats for a given process.
    77  func (client *Client) GetProcessInstances(processGUID string) ([]ProcessInstance, Warnings, error) {
    78  	request, err := client.newHTTPRequest(requestOptions{
    79  		RequestName: internal.GetProcessInstancesRequest,
    80  		URIParams:   map[string]string{"process_guid": processGUID},
    81  	})
    82  	if err != nil {
    83  		return nil, nil, err
    84  	}
    85  
    86  	var fullInstancesList []ProcessInstance
    87  	warnings, err := client.paginate(request, ProcessInstance{}, func(item interface{}) error {
    88  		if instance, ok := item.(ProcessInstance); ok {
    89  			fullInstancesList = append(fullInstancesList, instance)
    90  		} else {
    91  			return ccerror.UnknownObjectInListError{
    92  				Expected:   ProcessInstance{},
    93  				Unexpected: item,
    94  			}
    95  		}
    96  		return nil
    97  	})
    98  
    99  	return fullInstancesList, warnings, err
   100  }