-
Notifications
You must be signed in to change notification settings - Fork 70
/
client.go
246 lines (204 loc) · 5.96 KB
/
client.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package cc_client
import (
"fmt"
"net/http"
"net/url"
"policy-server/api"
"strconv"
"strings"
"code.cloudfoundry.org/cf-networking-helpers/json_client"
"code.cloudfoundry.org/lager"
)
type Client struct {
Logger lager.Logger
JSONClient json_client.JsonClient
}
type AppsV3Response struct {
Pagination struct {
TotalPages int `json:"total_pages"`
First struct {
Href string `json:"href"`
} `json:"first"`
Last struct {
Href string `json:"href"`
} `json:"last"`
Next struct {
Href string `json:"href"`
} `json:"next"`
} `json:"pagination"`
Resources []struct {
GUID string `json:"guid"`
Links struct {
Space struct {
Href string `json:"href"`
} `json:"space"`
} `json:"links"`
} `json:"resources"`
}
type SpaceResponse struct {
Entity struct {
Name string `json:"name"`
OrganizationGUID string `json:"organization_guid"`
} `json:"entity"`
}
type SpacesResponse struct {
Resources []struct {
Metadata struct {
GUID string `json:"guid"`
}
Entity struct {
Name string `json:"name"`
OrganizationGUID string `json:"organization_guid"`
} `json:"entity"`
} `json:"resources"`
}
func (c *Client) GetAllAppGUIDs(token string) (map[string]struct{}, error) {
token = fmt.Sprintf("bearer %s", token)
set := make(map[string]struct{})
nextPage := "?"
for nextPage != "" {
queryParams := strings.Split(nextPage, "?")[1]
response, err := c.makeAppsV3Request(queryParams, token)
if err != nil {
return nil, err
}
for _, resource := range response.Resources {
set[resource.GUID] = struct{}{}
}
nextPage = response.Pagination.Next.Href
}
return set, nil
}
func (c *Client) makeAppsV3Request(queryParams, token string) (AppsV3Response, error) {
route := "/v3/apps"
if queryParams != "" {
route = fmt.Sprintf("%s?%s", route, queryParams)
}
var response AppsV3Response
err := c.JSONClient.Do("GET", route, nil, &response, token)
if err != nil {
return AppsV3Response{}, fmt.Errorf("json client do: %s", err)
}
return response, nil
}
func (c *Client) GetLiveAppGUIDs(token string, appGUIDs []string) (map[string]struct{}, error) {
token = fmt.Sprintf("bearer %s", token)
values := url.Values{}
values.Add("guids", strings.Join(appGUIDs, ","))
values.Add("per_page", strconv.Itoa(len(appGUIDs)))
route := fmt.Sprintf("/v3/apps?%s", values.Encode())
var response AppsV3Response
err := c.JSONClient.Do("GET", route, nil, &response, token)
if err != nil {
return nil, fmt.Errorf("json client do: %s", err)
}
if response.Pagination.TotalPages > 1 {
return nil, fmt.Errorf("pagination support not yet implemented")
}
set := make(map[string]struct{})
for _, r := range response.Resources {
set[r.GUID] = struct{}{}
}
return set, nil
}
func (c *Client) GetSpaceGUIDs(token string, appGUIDs []string) ([]string, error) {
mapping, err := c.GetAppSpaces(token, appGUIDs)
if err != nil {
return nil, err
}
deduplicated := map[string]struct{}{}
for _, spaceID := range mapping {
deduplicated[spaceID] = struct{}{}
}
ret := []string{}
for spaceID, _ := range deduplicated {
ret = append(ret, spaceID)
}
return ret, nil
}
func (c *Client) GetAppSpaces(token string, appGUIDs []string) (map[string]string, error) {
if len(appGUIDs) < 1 {
return map[string]string{}, nil
}
token = fmt.Sprintf("bearer %s", token)
values := url.Values{}
values.Add("guids", strings.Join(appGUIDs, ","))
values.Add("per_page", strconv.Itoa(len(appGUIDs)))
route := fmt.Sprintf("/v3/apps?%s", values.Encode())
var response AppsV3Response
err := c.JSONClient.Do("GET", route, nil, &response, token)
if err != nil {
return nil, fmt.Errorf("json client do: %s", err)
}
if response.Pagination.TotalPages > 1 {
return nil, fmt.Errorf("pagination support not yet implemented")
}
set := make(map[string]string)
for _, r := range response.Resources {
href := r.Links.Space.Href
parts := strings.Split(href, "/")
appID := r.GUID
spaceID := parts[len(parts)-1]
set[appID] = spaceID
}
return set, nil
}
func (c *Client) GetSpace(token, spaceGUID string) (*api.Space, error) {
token = fmt.Sprintf("bearer %s", token)
route := fmt.Sprintf("/v2/spaces/%s", spaceGUID)
var response SpaceResponse
err := c.JSONClient.Do("GET", route, nil, &response, token)
if err != nil {
typedErr, ok := err.(*json_client.HttpResponseCodeError)
if !ok {
return nil, fmt.Errorf("json client do: %s", err)
}
if typedErr.StatusCode == http.StatusNotFound {
return nil, nil
}
return nil, fmt.Errorf("json client do: %s", err)
}
return &api.Space{
Name: response.Entity.Name,
OrgGUID: response.Entity.OrganizationGUID,
}, nil
}
func (c *Client) GetUserSpace(token, userGUID string, space api.Space) (*api.Space, error) {
token = fmt.Sprintf("bearer %s", token)
values := url.Values{}
values.Add("q", fmt.Sprintf("developer_guid:%s", userGUID))
values.Add("q", fmt.Sprintf("name:%s", space.Name))
values.Add("q", fmt.Sprintf("organization_guid:%s", space.OrgGUID))
route := fmt.Sprintf("/v2/spaces?%s", values.Encode())
var response SpacesResponse
err := c.JSONClient.Do("GET", route, nil, &response, token)
if err != nil {
return nil, fmt.Errorf("json client do: %s", err)
}
numSpaces := len(response.Resources)
if numSpaces == 0 {
return nil, nil
}
if numSpaces > 1 {
return nil, fmt.Errorf("found more than one matching space")
}
return &api.Space{
Name: response.Resources[0].Entity.Name,
OrgGUID: response.Resources[0].Entity.OrganizationGUID,
}, nil
}
func (c *Client) GetUserSpaces(token, userGUID string) (map[string]struct{}, error) {
token = fmt.Sprintf("bearer %s", token)
route := fmt.Sprintf("/v2/users/%s/spaces", userGUID)
var response SpacesResponse
err := c.JSONClient.Do("GET", route, nil, &response, token)
if err != nil {
return nil, fmt.Errorf("json client do: %s", err)
}
userSpaces := map[string]struct{}{}
for _, space := range response.Resources {
spaceID := space.Metadata.GUID
userSpaces[spaceID] = struct{}{}
}
return userSpaces, nil
}