/
api.go
127 lines (110 loc) · 3.28 KB
/
api.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"github.com/cloudfoundry/cli/plugin"
"strings"
"encoding/json"
)
type StartedApp struct {
Name string
Guid string
SpaceUrl string
State string
}
type AppStats struct {
AppInstanceStats []AppInstanceStats
}
type AppInstanceStats struct {
Instance string
Host string
}
type Space struct {
Guid string
Name string
OrgName string
}
type CcApi interface{
StartedApps() ([]StartedApp, error)
AppStats(guid string) (AppStats, error)
Space(space_url string) (Space, error)
}
type CliCcApi struct {
cliConnection plugin.CliConnection
}
func NewCliCcApi(cliConnection plugin.CliConnection) (api CliCcApi){
api.cliConnection = cliConnection
return
}
func (api CliCcApi) StartedApps() ([]StartedApp, error) {
return startedApps(api.cliConnection, "/v2/apps")
}
func startedApps(cliConnection plugin.CliConnection, url string)([]StartedApp, error){
appsJson, err := cliConnection.CliCommandWithoutTerminalOutput("curl", url)
if nil != err {
return nil, err
}
data := strings.Join(appsJson, "\n")
var appsData map[string]interface{}
json.Unmarshal([]byte(data), &appsData)
apps := []StartedApp{}
for _, app := range appsData["resources"].([]interface{}) {
entity := app.(map[string]interface{})["entity"].(map[string]interface{})
state := entity["state"].(string)
if state == "STARTED" {
metadata := app.(map[string]interface{})["metadata"].(map[string]interface{})
result := StartedApp{
Name: entity["name"].(string),
Guid: metadata["guid"].(string),
SpaceUrl: entity["space_url"].(string),
State: state,
}
apps = append(apps, result)
}
}
if nil != appsData["next_url"] {
next, _ := startedApps(cliConnection, appsData["next_url"].(string))
apps = append(apps, next...)
}
return apps, err
}
func (api CliCcApi) AppStats(guid string) (AppStats, error) {
output, _ := api.cliConnection.CliCommandWithoutTerminalOutput("curl", "/v2/apps/" + guid + "/stats")
data := strings.Join(output, "\n")
var instances map[string]map[string]interface{}
json.Unmarshal([]byte(data), &instances)
appStats := AppStats{}
for key, value := range instances {
if value["state"] == "RUNNING" {
appStats.AppInstanceStats = append(appStats.AppInstanceStats, AppInstanceStats{
Instance: key,
Host: value["stats"].(map[string]interface{})["host"].(string),
})
}
}
return appStats, nil
}
func (api CliCcApi) Space(space_url string) (Space, error) {
output, error := api.cliConnection.CliCommandWithoutTerminalOutput("curl", space_url)
if error != nil {
return Space{}, error
}
data := strings.Join(output, "\n")
var space map[string]interface{}
json.Unmarshal([]byte(data), &space)
entity := space["entity"].(map[string]interface{})
orgName, error := organizationName(api.cliConnection, entity["organization_url"].(string))
return Space{
Name: entity["name"].(string),
OrgName: orgName,
}, error
}
func organizationName(cliConnection plugin.CliConnection, org_url string) (string, error) {
output, error := cliConnection.CliCommandWithoutTerminalOutput("curl", org_url)
if error != nil {
return "", error
}
data := strings.Join(output, "\n")
var org map[string]interface{}
json.Unmarshal([]byte(data), &org)
entity := org["entity"].(map[string]interface{})
return entity["name"].(string), error
}