github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/api/cloudcontroller/ccv2/application_instance_status.go (about) 1 package ccv2 2 3 import ( 4 "encoding/json" 5 "strconv" 6 7 "github.com/liamawhite/cli-with-i18n/api/cloudcontroller" 8 "github.com/liamawhite/cli-with-i18n/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 // IsolationSegment that the app is currently running on. 26 IsolationSegment string 27 28 // Memory is the instance's memory usage in bytes. 29 Memory int 30 31 // MemoryQuota is the instance's allowed memory usage in bytes. 32 MemoryQuota int 33 34 // State is the instance's state. 35 State ApplicationInstanceState 36 37 // Uptime is the number of seconds the instance has been running. 38 Uptime int 39 } 40 41 // UnmarshalJSON helps unmarshal a Cloud Controller application instance 42 // response. 43 func (instance *ApplicationInstanceStatus) UnmarshalJSON(data []byte) error { 44 var ccInstance struct { 45 State string `json:"state"` 46 IsolationSegment string `json:"isolation_segment"` 47 Stats struct { 48 Usage struct { 49 Disk int `json:"disk"` 50 Memory int `json:"mem"` 51 CPU float64 `json:"cpu"` 52 } `json:"usage"` 53 MemoryQuota int `json:"mem_quota"` 54 DiskQuota int `json:"disk_quota"` 55 Uptime int `json:"uptime"` 56 } `json:"stats"` 57 } 58 if err := json.Unmarshal(data, &ccInstance); err != nil { 59 return err 60 } 61 62 instance.CPU = ccInstance.Stats.Usage.CPU 63 instance.Disk = ccInstance.Stats.Usage.Disk 64 instance.DiskQuota = ccInstance.Stats.DiskQuota 65 instance.IsolationSegment = ccInstance.IsolationSegment 66 instance.Memory = ccInstance.Stats.Usage.Memory 67 instance.MemoryQuota = ccInstance.Stats.MemoryQuota 68 instance.State = ApplicationInstanceState(ccInstance.State) 69 instance.Uptime = ccInstance.Stats.Uptime 70 71 return nil 72 } 73 74 // GetApplicationInstanceStatusesByApplication returns a list of 75 // ApplicationInstance for a given application. Given the state of an 76 // application, it might skip some application instances. 77 func (client *Client) GetApplicationInstanceStatusesByApplication(guid string) (map[int]ApplicationInstanceStatus, Warnings, error) { 78 request, err := client.newHTTPRequest(requestOptions{ 79 RequestName: internal.GetAppStatsRequest, 80 URIParams: Params{"app_guid": guid}, 81 }) 82 if err != nil { 83 return nil, nil, err 84 } 85 86 var instances map[string]ApplicationInstanceStatus 87 response := cloudcontroller.Response{ 88 Result: &instances, 89 } 90 91 err = client.connection.Make(request, &response) 92 if err != nil { 93 return nil, response.Warnings, err 94 } 95 96 returnedInstances := map[int]ApplicationInstanceStatus{} 97 for instanceID, instance := range instances { 98 id, convertErr := strconv.Atoi(instanceID) 99 if convertErr != nil { 100 return nil, response.Warnings, convertErr 101 } 102 instance.ID = id 103 returnedInstances[id] = instance 104 } 105 106 return returnedInstances, response.Warnings, nil 107 }