github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/api/cloudcontroller/ccv2/application_instance_status.go (about) 1 package ccv2 2 3 import ( 4 "encoding/json" 5 "strconv" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal" 9 ) 10 11 // ApplicationInstanceStatus represents a Cloud Controller Application Instance. 12 type ApplicationInstanceStatus struct { 13 // CPU is the instance's CPU utilization percentage. 14 CPU float64 15 16 // Disk is the instance's disk usage in bytes. 17 Disk int 18 19 // DiskQuota is the instance's allowed disk usage in bytes. 20 DiskQuota int 21 22 // ID is the instance ID. 23 ID int 24 25 // Memory is the instance's memory usage in bytes. 26 Memory int 27 28 // MemoryQuota is the instance's allowed memory usage in bytes. 29 MemoryQuota int 30 31 // State is the instance's state. 32 State ApplicationInstanceState 33 34 // Uptime is the number of seconds the instance has been running. 35 Uptime int 36 } 37 38 // UnmarshalJSON helps unmarshal a Cloud Controller application instance 39 // response. 40 func (instance *ApplicationInstanceStatus) UnmarshalJSON(data []byte) error { 41 var ccInstance struct { 42 State string `json:"state"` 43 Stats struct { 44 Usage struct { 45 Disk int `json:"disk"` 46 Memory int `json:"mem"` 47 CPU float64 `json:"cpu"` 48 } `json:"usage"` 49 MemoryQuota int `json:"mem_quota"` 50 DiskQuota int `json:"disk_quota"` 51 Uptime int `json:"uptime"` 52 } `json:"stats"` 53 } 54 if err := json.Unmarshal(data, &ccInstance); err != nil { 55 return err 56 } 57 58 instance.CPU = ccInstance.Stats.Usage.CPU 59 instance.Disk = ccInstance.Stats.Usage.Disk 60 instance.DiskQuota = ccInstance.Stats.DiskQuota 61 instance.Memory = ccInstance.Stats.Usage.Memory 62 instance.MemoryQuota = ccInstance.Stats.MemoryQuota 63 instance.State = ApplicationInstanceState(ccInstance.State) 64 instance.Uptime = ccInstance.Stats.Uptime 65 66 return nil 67 } 68 69 // GetApplicationInstanceStatusesByApplication returns a list of 70 // ApplicationInstance for a given application. Given the state of an 71 // application, it might skip some application instances. 72 func (client *Client) GetApplicationInstanceStatusesByApplication(guid string) (map[int]ApplicationInstanceStatus, Warnings, error) { 73 request, err := client.newHTTPRequest(requestOptions{ 74 RequestName: internal.AppInstanceStats, 75 URIParams: Params{"app_guid": guid}, 76 }) 77 if err != nil { 78 return nil, nil, err 79 } 80 81 var instances map[string]ApplicationInstanceStatus 82 response := cloudcontroller.Response{ 83 Result: &instances, 84 } 85 86 err = client.connection.Make(request, &response) 87 if err != nil { 88 return nil, response.Warnings, err 89 } 90 91 returnedInstances := map[int]ApplicationInstanceStatus{} 92 for instanceID, instance := range instances { 93 id, err := strconv.Atoi(instanceID) 94 if err != nil { 95 return nil, response.Warnings, err 96 } 97 instance.ID = id 98 returnedInstances[id] = instance 99 } 100 101 return returnedInstances, response.Warnings, err 102 }