github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/api/cloudcontroller/ccv2/application_instance_status.go (about) 1 package ccv2 2 3 import ( 4 "strconv" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller" 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 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 int64 18 19 // DiskQuota is the instance's allowed disk usage in bytes. 20 DiskQuota int64 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 int64 30 31 // MemoryQuota is the instance's allowed memory usage in bytes. 32 MemoryQuota int64 33 34 // State is the instance's state. 35 State constant.ApplicationInstanceState 36 37 // Uptime is the number of seconds the instance has been running. 38 Uptime int64 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 int64 `json:"disk"` 50 Memory int64 `json:"mem"` 51 CPU float64 `json:"cpu"` 52 } `json:"usage"` 53 MemoryQuota int64 `json:"mem_quota"` 54 DiskQuota int64 `json:"disk_quota"` 55 Uptime int64 `json:"uptime"` 56 } `json:"stats"` 57 } 58 err := cloudcontroller.DecodeJSON(data, &ccInstance) 59 if err != nil { 60 return err 61 } 62 63 instance.CPU = ccInstance.Stats.Usage.CPU 64 instance.Disk = ccInstance.Stats.Usage.Disk 65 instance.DiskQuota = ccInstance.Stats.DiskQuota 66 instance.IsolationSegment = ccInstance.IsolationSegment 67 instance.Memory = ccInstance.Stats.Usage.Memory 68 instance.MemoryQuota = ccInstance.Stats.MemoryQuota 69 instance.State = constant.ApplicationInstanceState(ccInstance.State) 70 instance.Uptime = ccInstance.Stats.Uptime 71 72 return nil 73 } 74 75 // GetApplicationApplicationInstanceStatuses returns a list of 76 // ApplicationInstanceStatus for a given application. Depending on the state of 77 // an application, it might skip some application instance statuses. 78 func (client *Client) GetApplicationApplicationInstanceStatuses(guid string) (map[int]ApplicationInstanceStatus, Warnings, error) { 79 request, err := client.newHTTPRequest(requestOptions{ 80 RequestName: internal.GetAppStatsRequest, 81 URIParams: Params{"app_guid": guid}, 82 }) 83 if err != nil { 84 return nil, nil, err 85 } 86 87 var instances map[string]ApplicationInstanceStatus 88 response := cloudcontroller.Response{ 89 DecodeJSONResponseInto: &instances, 90 } 91 92 err = client.connection.Make(request, &response) 93 if err != nil { 94 return nil, response.Warnings, err 95 } 96 97 returnedInstances := map[int]ApplicationInstanceStatus{} 98 for instanceID, instance := range instances { 99 id, convertErr := strconv.Atoi(instanceID) 100 if convertErr != nil { 101 return nil, response.Warnings, convertErr 102 } 103 instance.ID = id 104 returnedInstances[id] = instance 105 } 106 107 return returnedInstances, response.Warnings, nil 108 }