-
Notifications
You must be signed in to change notification settings - Fork 928
/
app_instances.go
113 lines (94 loc) · 2.96 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
112
113
package appinstances
import (
"fmt"
"strconv"
"strings"
"time"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/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
}
}
}
//go:generate counterfeiter . Repository
type Repository interface {
GetInstances(appGUID string) (instances []models.AppInstanceFields, apiErr error)
DeleteInstance(appGUID string, instance int) error
}
type CloudControllerAppInstancesRepository struct {
config coreconfig.Reader
gateway net.Gateway
}
func NewCloudControllerAppInstancesRepository(config coreconfig.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
}