forked from ECSTeam/cf_get_events
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch_spacev3.go
138 lines (114 loc) · 4.33 KB
/
search_spacev3.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
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"strings"
"code.cloudfoundry.org/cli/plugin"
)
// SpaceSearchResults represents top level attributes of JSON response from Cloud Foundry API
type SpaceSearchResultsv3 struct {
Pagination struct {
TotalResults int `json:"total_results"`
TotalPages int `json:"total_pages"`
} `json:"pagination"`
Resources []SpaceSearchResourcesv3 `json:"resources"`
}
// SpaceSearchResources represents resources attribute of JSON response from Cloud Foundry API
type SpaceSearchResourcesv3 struct {
Name string `json:"name"`
GUID string `json:"guid"`
Metadata struct {
Labels map[string]string `json:"labels"`
} `json:"metadata"`
Relationships struct {
Organization struct {
Data struct {
OrgGUID string `json:"guid"`
} `json:"data"`
} `json:"organization"`
} `json:"relationships"`
}
func (ssr SpaceSearchResourcesv3) OrgGUID() string {
return ssr.Relationships.Organization.Data.OrgGUID
}
// GetSearchSpaceData requests all of the Application data from Cloud Foundry
func (c Events) GetSearchSpacesv3(label_selector string, cli plugin.CliConnection) map[string]SpaceSearchResourcesv3 {
if label_selector == "" {
return c.GetSpacesv3(cli)
} else {
return c.SearchSpacesv3(label_selector, cli)
}
}
// GetSpaceData requests all of the Application data from Cloud Foundry
func (c Events) GetSpacesv3(cli plugin.CliConnection) map[string]SpaceSearchResourcesv3 {
var data = make(map[string]SpaceSearchResourcesv3)
spaces := c.GetSpaceDatav3(cli)
for _, val := range spaces.Resources {
data[val.GUID] = val
}
return data
}
// GetSpaceData requests all of the Spaces data from Cloud Foundry
func (c Events) GetSpaceDatav3(cli plugin.CliConnection) SpaceSearchResultsv3 {
var res SpaceSearchResultsv3
res = c.UnmarshallSpaceSearchResultsv3("/v3/spaces?order_by=name&per_page=100", cli)
if res.Pagination.TotalPages > 1 {
for i := 2; i <= res.Pagination.TotalPages; i++ {
apiUrl := fmt.Sprintf("/v3/spaces?order_by=name&page=%v&per_page=100", strconv.Itoa(i))
tRes := c.UnmarshallSpaceSearchResultsv3(apiUrl, cli)
res.Resources = append(res.Resources, tRes.Resources...)
}
}
return res
}
// GetSpaceData requests all of the Application data from Cloud Foundry
func (c Events) SearchSpacesv3(label_selector string, cli plugin.CliConnection) map[string]SpaceSearchResourcesv3 {
var data = make(map[string]SpaceSearchResourcesv3)
spaces := c.SearchSpaceDatav3(label_selector, cli)
for _, val := range spaces.Resources {
data[val.GUID] = val
}
return data
}
// GetSpaceData requests all of the Spaces data from Cloud Foundry
func (c Events) SearchSpaceDatav3(label_selector string, cli plugin.CliConnection) SpaceSearchResultsv3 {
var res SpaceSearchResultsv3
query := fmt.Sprintf("/v3/spaces?order_by=name&per_page=100&label_selector=%s", url.QueryEscape(label_selector))
res = c.UnmarshallSpaceSearchResultsv3(query, cli)
if res.Pagination.TotalPages > 1 {
for i := 2; i <= res.Pagination.TotalPages; i++ {
apiUrl := fmt.Sprintf("%s&page=%v", query, strconv.Itoa(i))
tRes := c.UnmarshallSpaceSearchResultsv3(apiUrl, cli)
res.Resources = append(res.Resources, tRes.Resources...)
}
}
return res
}
func (c Events) UnmarshallSpaceSearchResultsv3(apiUrl string, cli plugin.CliConnection) SpaceSearchResultsv3 {
var tRes SpaceSearchResultsv3
cmd := []string{"curl", apiUrl}
output, _ := cli.CliCommandWithoutTerminalOutput(cmd...)
json.Unmarshal([]byte(strings.Join(output, "")), &tRes)
return tRes
}
func (c Events) WriteSpaceLabel(guid string, labelKey string, labelValue string, cli plugin.CliConnection) string {
apiUrl := fmt.Sprintf("/v3/spaces/%s", guid)
json := fmt.Sprintf("'{ \"metadata\": { \"labels\": { \"%s\": \"%s\" } } }'", labelKey, labelValue)
//special delete
if labelValue == "" {
json = fmt.Sprintf("'{ \"metadata\": { \"labels\": { \"%s\":null } } }'", labelKey)
}
cmd := []string{"curl", apiUrl, "-X", "PATCH", "-d", json}
cli.CliCommandWithoutTerminalOutput(cmd...)
return guid
}
func (c Events) ReadSpaceLabels(guid string, cli plugin.CliConnection) map[string]string {
apiUrl := fmt.Sprintf("/v3/spaces/%s", guid)
var tRes SpaceSearchResourcesv3
cmd := []string{"curl", apiUrl}
output, _ := cli.CliCommandWithoutTerminalOutput(cmd...)
json.Unmarshal([]byte(strings.Join(output, "")), &tRes)
return tRes.Metadata.Labels
}