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