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