forked from alexvasseur/cf_get_events
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_orgsummary.go
80 lines (65 loc) · 2.16 KB
/
search_orgsummary.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
package main
import (
"encoding/json"
"strings"
"github.com/cloudfoundry/cli/plugin"
)
type OrgSummaryFromQuota struct {
Mem int
MemOrgQuota int
}
type OrgSummary struct {
Name string `json:"name"`
Resources []OrgSummarySpaceEntity `json:"spaces"`
Memory int
MemoryLimitOrgQuota int
MemoryUsage int
}
type OrgSummarySpaceEntity struct {
Name string `json:"name"`
ServiceCount int `json:"service_count"`
Memory int `json:"mem_dev_total"`
}
type QuotaDefinition struct {
Entity QuotaEntity `json:"entity"`
}
type QuotaEntity struct {
MemoryLimit int `json:"memory_limit"`
}
func (c Events) GetOrgsSummary(cli plugin.CliConnection) map[string]OrgSummary {
var data = make(map[string]OrgSummary)
orgs := c.GetOrgData(cli)
var quotaCache = make(map[string]QuotaDefinition)
for _, val := range orgs.Resources {
// lookup usage in /v2/organizations/<org guid>/summary
var orgSummary = c.GetOrgSummaryData(cli, val.Metadata.GUID)
// compute Mem use total from all org' spaces
for _, space := range orgSummary.Resources {
orgSummary.Memory += space.Memory
}
// lookup quota def
orgQuota, exist := quotaCache[val.Entity.QuotaGuid]
if !exist {
orgQuota = c.GetQuotaData(cli, val.Entity.QuotaGuid)
quotaCache[val.Entity.QuotaGuid] = orgQuota
}
orgSummary.MemoryLimitOrgQuota = orgQuota.Entity.MemoryLimit
orgSummary.MemoryUsage = (int)(orgSummary.Memory * 100 / orgSummary.MemoryLimitOrgQuota)
data[val.Metadata.GUID] = orgSummary
}
return data
}
func (c Events) GetOrgSummaryData(cli plugin.CliConnection, orgGuid string) OrgSummary {
var res OrgSummary
cmd := []string{"curl", "/v2/organizations/" + orgGuid + "/summary"}
output, _ := cli.CliCommandWithoutTerminalOutput(cmd...)
json.Unmarshal([]byte(strings.Join(output, "")), &res)
return res
}
func (c Events) GetQuotaData(cli plugin.CliConnection, quotaGuid string) QuotaDefinition {
var res QuotaDefinition
cmd := []string{"curl", "/v2/quota_definitions/" + quotaGuid}
output, _ := cli.CliCommandWithoutTerminalOutput(cmd...)
json.Unmarshal([]byte(strings.Join(output, "")), &res)
return res
}