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