github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/appinstances/app_instances.go (about) 1 package appinstances 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 "time" 8 9 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 10 "code.cloudfoundry.org/cli/cf/models" 11 "code.cloudfoundry.org/cli/cf/net" 12 ) 13 14 type InstancesAPIResponse map[string]InstanceAPIResponse 15 16 type InstanceAPIResponse struct { 17 State string 18 Since float64 19 Details string 20 } 21 22 type StatsAPIResponse map[string]InstanceStatsAPIResponse 23 24 type InstanceStatsAPIResponse struct { 25 Stats struct { 26 DiskQuota int64 `json:"disk_quota"` 27 MemQuota int64 `json:"mem_quota"` 28 Usage struct { 29 CPU float64 30 Disk int64 31 Mem int64 32 } 33 } 34 } 35 36 //go:generate counterfeiter . Repository 37 38 type Repository interface { 39 GetInstances(appGUID string) (instances []models.AppInstanceFields, apiErr error) 40 DeleteInstance(appGUID string, instance int) error 41 } 42 43 type CloudControllerAppInstancesRepository struct { 44 config coreconfig.Reader 45 gateway net.Gateway 46 } 47 48 func NewCloudControllerAppInstancesRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerAppInstancesRepository) { 49 repo.config = config 50 repo.gateway = gateway 51 return 52 } 53 54 func (repo CloudControllerAppInstancesRepository) GetInstances(appGUID string) (instances []models.AppInstanceFields, err error) { 55 instancesResponse := InstancesAPIResponse{} 56 err = repo.gateway.GetResource( 57 fmt.Sprintf("%s/v2/apps/%s/instances", repo.config.APIEndpoint(), appGUID), 58 &instancesResponse) 59 if err != nil { 60 return 61 } 62 63 instances = make([]models.AppInstanceFields, len(instancesResponse), len(instancesResponse)) 64 for k, v := range instancesResponse { 65 index, err := strconv.Atoi(k) 66 if err != nil { 67 continue 68 } 69 70 instances[index] = models.AppInstanceFields{ 71 State: models.InstanceState(strings.ToLower(v.State)), 72 Details: v.Details, 73 Since: time.Unix(int64(v.Since), 0), 74 } 75 } 76 77 return repo.updateInstancesWithStats(appGUID, instances) 78 } 79 80 func (repo CloudControllerAppInstancesRepository) DeleteInstance(appGUID string, instance int) error { 81 err := repo.gateway.DeleteResource(repo.config.APIEndpoint(), fmt.Sprintf("/v2/apps/%s/instances/%d", appGUID, instance)) 82 if err != nil { 83 return err 84 } 85 return nil 86 } 87 88 func (repo CloudControllerAppInstancesRepository) updateInstancesWithStats(guid string, instances []models.AppInstanceFields) (updatedInst []models.AppInstanceFields, apiErr error) { 89 path := fmt.Sprintf("%s/v2/apps/%s/stats", repo.config.APIEndpoint(), guid) 90 statsResponse := StatsAPIResponse{} 91 apiErr = repo.gateway.GetResource(path, &statsResponse) 92 if apiErr != nil { 93 return 94 } 95 96 updatedInst = make([]models.AppInstanceFields, len(statsResponse), len(statsResponse)) 97 for k, v := range statsResponse { 98 index, err := strconv.Atoi(k) 99 if err != nil { 100 continue 101 } 102 103 instance := instances[index] 104 instance.CPUUsage = v.Stats.Usage.CPU 105 instance.DiskQuota = v.Stats.DiskQuota 106 instance.DiskUsage = v.Stats.Usage.Disk 107 instance.MemQuota = v.Stats.MemQuota 108 instance.MemUsage = v.Stats.Usage.Mem 109 110 updatedInst[index] = instance 111 } 112 return 113 }