github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/api/cloudcontroller/ccv3/instance.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
     8  )
     9  
    10  type Instance struct {
    11  	State       string
    12  	CPU         float64
    13  	MemoryUsage uint64
    14  	DiskUsage   uint64
    15  	MemoryQuota uint64
    16  	DiskQuota   uint64
    17  	Index       int
    18  	Uptime      int
    19  }
    20  
    21  // UnmarshalJSON helps unmarshal a V3 Cloud Controller Instance response.
    22  func (instance *Instance) UnmarshalJSON(data []byte) error {
    23  	var inputInstance struct {
    24  		State string `json:"state"`
    25  		Usage struct {
    26  			CPU  float64 `json:"cpu"`
    27  			Mem  uint64  `json:"mem"`
    28  			Disk uint64  `json:"disk"`
    29  		} `json:"usage"`
    30  		MemQuota  uint64 `json:"mem_quota"`
    31  		DiskQuota uint64 `json:"disk_quota"`
    32  		Index     int    `json:"index"`
    33  		Uptime    int    `json:"uptime"`
    34  	}
    35  	if err := json.Unmarshal(data, &inputInstance); err != nil {
    36  		return err
    37  	}
    38  
    39  	instance.State = inputInstance.State
    40  	instance.CPU = inputInstance.Usage.CPU
    41  	instance.MemoryUsage = inputInstance.Usage.Mem
    42  	instance.DiskUsage = inputInstance.Usage.Disk
    43  
    44  	instance.MemoryQuota = inputInstance.MemQuota
    45  	instance.DiskQuota = inputInstance.DiskQuota
    46  	instance.Index = inputInstance.Index
    47  	instance.Uptime = inputInstance.Uptime
    48  
    49  	return nil
    50  }
    51  
    52  // GetProcessInstances lists instance stats for a given process.
    53  func (client *Client) GetProcessInstances(processGUID string) ([]Instance, Warnings, error) {
    54  	request, err := client.newHTTPRequest(requestOptions{
    55  		RequestName: internal.GetProcessInstancesRequest,
    56  		URIParams:   map[string]string{"guid": processGUID},
    57  	})
    58  	if err != nil {
    59  		return nil, nil, err
    60  	}
    61  
    62  	var fullInstancesList []Instance
    63  	warnings, err := client.paginate(request, Instance{}, func(item interface{}) error {
    64  		if instance, ok := item.(Instance); ok {
    65  			fullInstancesList = append(fullInstancesList, instance)
    66  		} else {
    67  			return ccerror.UnknownObjectInListError{
    68  				Expected:   Instance{},
    69  				Unexpected: item,
    70  			}
    71  		}
    72  		return nil
    73  	})
    74  
    75  	return fullInstancesList, warnings, err
    76  }