forked from ECSTeam/cf_get_events
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch_app.go
81 lines (69 loc) · 2.62 KB
/
search_app.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
package main
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"code.cloudfoundry.org/cli/plugin"
)
// AppSearchResults represents top level attributes of JSON response from Cloud Foundry API
type AppSearchResults struct {
TotalResults int `json:"total_results"`
TotalPages int `json:"total_pages"`
PrevUrl string `json:"prev_url"`
NextUrl string `json:"next_url"`
Resources []AppSearchResources `json:"resources"`
}
// AppSearchResources represents resources attribute of JSON response from Cloud Foundry API
type AppSearchResources struct {
Entity AppSearchEntity `json:"entity"`
Metadata Metadata `json:"metadata"`
}
// AppSearchEntity represents entity attribute of resources attribute within JSON response from Cloud Foundry API
type AppSearchEntity struct {
Name string `json:"name"`
Buildpack string `json:"buildpack"`
DetectedBuildpack string `json:"detected_buildpack"`
SpaceGUID string `json:"space_guid"`
Instances int `json:"instances"`
State string `json:"state"` // STARTED, STOPPED, ??TODO
Memory int `json:"memory"`
DiskQuota int `json:"disk_quota"`
}
// util to remove i in the array
func (c Events) remove(slice []AppSearchResources, i int) []AppSearchResources {
copy(slice[i:], slice[i+1:])
return slice[:len(slice)-1]
}
//// GetAppData requests all of the Application data from Cloud Foundry
//func (c Events) GetApps(cli plugin.CliConnection) map[string]AppSearchResults {
// var data map[string]AppSearchResults
// data = make(map[string]AppSearchResults)
// spaces := c.GetAppData(cli)
//
// for _, val := range spaces.Resources {
// data[val.Metadata.GUID] = val.Metadata
// }
//
// return data
//}
// GetAppData requests all of the Application data from Cloud Foundry
func (c Events) GetAppData(cli plugin.CliConnection) AppSearchResults {
var res AppSearchResults
res = c.UnmarshallAppSearchResults("/v2/apps?order-direction=asc&results-per-page=100", cli)
if res.TotalPages > 1 {
for i := 2; i <= res.TotalPages; i++ {
apiUrl := fmt.Sprintf("/v2/apps?order-direction=asc&page=%v&results-per-page=100", strconv.Itoa(i))
tRes := c.UnmarshallAppSearchResults(apiUrl, cli)
res.Resources = append(res.Resources, tRes.Resources...)
}
}
return res
}
func (c Events) UnmarshallAppSearchResults(apiUrl string, cli plugin.CliConnection) AppSearchResults {
var tRes AppSearchResults
cmd := []string{"curl", apiUrl}
output, _ := cli.CliCommandWithoutTerminalOutput(cmd...)
json.Unmarshal([]byte(strings.Join(output, "")), &tRes)
return tRes
}