-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
302 lines (261 loc) · 8.89 KB
/
route.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package ccv2
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
"code.cloudfoundry.org/cli/types"
)
// Route represents a Cloud Controller Route.
type Route struct {
// GUID is the unique Route identifier.
GUID string `json:"-"`
// Host is the hostname of the route.
Host string `json:"host,omitempty"`
// Path is the path of the route.
Path string `json:"path,omitempty"`
// Port is the port number of the route.
Port types.NullInt `json:"port,omitempty"`
// DomainGUID is the unique Domain identifier.
DomainGUID string `json:"domain_guid"`
// SpaceGUID is the unique Space identifier.
SpaceGUID string `json:"space_guid"`
}
// UnmarshalJSON helps unmarshal a Cloud Controller Route response.
func (route *Route) UnmarshalJSON(data []byte) error {
var ccRoute struct {
Metadata internal.Metadata `json:"metadata"`
Entity struct {
Host string `json:"host"`
Path string `json:"path"`
Port types.NullInt `json:"port"`
DomainGUID string `json:"domain_guid"`
SpaceGUID string `json:"space_guid"`
} `json:"entity"`
}
if err := json.Unmarshal(data, &ccRoute); err != nil {
return err
}
route.GUID = ccRoute.Metadata.GUID
route.Host = ccRoute.Entity.Host
route.Path = ccRoute.Entity.Path
route.Port = ccRoute.Entity.Port
route.DomainGUID = ccRoute.Entity.DomainGUID
route.SpaceGUID = ccRoute.Entity.SpaceGUID
return nil
}
// UpdateRouteApplication creates a link between the route and application.
func (client *Client) UpdateRouteApplication(routeGUID string, appGUID string) (Route, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PutRouteAppRequest,
URIParams: map[string]string{
"app_guid": appGUID,
"route_guid": routeGUID,
},
})
if err != nil {
return Route{}, nil, err
}
var route Route
response := cloudcontroller.Response{
Result: &route,
}
err = client.connection.Make(request, &response)
return route, response.Warnings, err
}
// DeleteRouteApplication removes the link between the route and application.
func (client *Client) DeleteRouteApplication(routeGUID string, appGUID string) (Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.DeleteRouteAppRequest,
URIParams: map[string]string{
"app_guid": appGUID,
"route_guid": routeGUID,
},
})
if err != nil {
return nil, err
}
var response cloudcontroller.Response
err = client.connection.Make(request, &response)
return response.Warnings, err
}
// CreateRoute creates the route with the given properties; SpaceGUID and
// DomainGUID are required Route properties. Additional configuration rules:
// - generatePort = true to generate a random port on the cloud controller.
// - generatePort takes precedence over the provided port. Setting the port and
// generatePort only works with CC API 2.53.0 or higher and when TCP router
// groups are enabled.
func (client *Client) CreateRoute(route Route, generatePort bool) (Route, Warnings, error) {
body, err := json.Marshal(route)
if err != nil {
return Route{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostRouteRequest,
Body: bytes.NewReader(body),
})
if err != nil {
return Route{}, nil, err
}
if generatePort {
query := url.Values{}
query.Add("generate_port", "true")
request.URL.RawQuery = query.Encode()
}
var updatedRoute Route
response := cloudcontroller.Response{
Result: &updatedRoute,
}
err = client.connection.Make(request, &response)
return updatedRoute, response.Warnings, err
}
// GetApplicationRoutes returns a list of Routes associated with the provided
// Application GUID, and filtered by the provided filters.
func (client *Client) GetApplicationRoutes(appGUID string, filters ...Filter) ([]Route, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetAppRoutesRequest,
URIParams: map[string]string{"app_guid": appGUID},
Query: ConvertFilterParameters(filters),
})
if err != nil {
return nil, nil, err
}
var fullRoutesList []Route
warnings, err := client.paginate(request, Route{}, func(item interface{}) error {
if route, ok := item.(Route); ok {
fullRoutesList = append(fullRoutesList, route)
} else {
return ccerror.UnknownObjectInListError{
Expected: Route{},
Unexpected: item,
}
}
return nil
})
return fullRoutesList, warnings, err
}
// GetSpaceRoutes returns a list of Routes associated with the provided Space
// GUID, and filtered by the provided filters.
func (client *Client) GetSpaceRoutes(spaceGUID string, filters ...Filter) ([]Route, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetSpaceRoutesRequest,
URIParams: map[string]string{"space_guid": spaceGUID},
Query: ConvertFilterParameters(filters),
})
if err != nil {
return nil, nil, err
}
var fullRoutesList []Route
warnings, err := client.paginate(request, Route{}, func(item interface{}) error {
if route, ok := item.(Route); ok {
fullRoutesList = append(fullRoutesList, route)
} else {
return ccerror.UnknownObjectInListError{
Expected: Route{},
Unexpected: item,
}
}
return nil
})
return fullRoutesList, warnings, err
}
// GetRoutes returns a list of Routes based off of the provided filters.
func (client *Client) GetRoutes(filters ...Filter) ([]Route, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetRoutesRequest,
Query: ConvertFilterParameters(filters),
})
if err != nil {
return nil, nil, err
}
var fullRoutesList []Route
warnings, err := client.paginate(request, Route{}, func(item interface{}) error {
if route, ok := item.(Route); ok {
fullRoutesList = append(fullRoutesList, route)
} else {
return ccerror.UnknownObjectInListError{
Expected: Route{},
Unexpected: item,
}
}
return nil
})
return fullRoutesList, warnings, err
}
// DeleteRoute deletes the Route associated with the provided Route GUID.
func (client *Client) DeleteRoute(routeGUID string) (Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.DeleteRouteRequest,
URIParams: map[string]string{"route_guid": routeGUID},
})
if err != nil {
return nil, err
}
var response cloudcontroller.Response
err = client.connection.Make(request, &response)
return response.Warnings, err
}
// DoesRouteExist returns true if the route exists in the CF instance. DomainGUID
// is required for check. This call will only work for CC API 2.55 or higher.
func (client *Client) DoesRouteExist(route Route) (bool, Warnings, error) {
currentVersion := client.APIVersion()
switch {
case cloudcontroller.MinimumAPIVersionCheck(currentVersion, ccversion.MinVersionNoHostInReservedRouteEndpoint) == nil:
return client.checkRoute(route)
case cloudcontroller.MinimumAPIVersionCheck(currentVersion, ccversion.MinVersionHTTPRoutePath) == nil:
return client.checkRouteDeprecated(route.DomainGUID, route.Host, route.Path)
default:
return client.checkRouteDeprecated(route.DomainGUID, route.Host, "")
}
}
func (client *Client) checkRoute(route Route) (bool, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetRouteReservedRequest,
URIParams: map[string]string{"domain_guid": route.DomainGUID},
})
if err != nil {
return false, nil, err
}
queryParams := url.Values{}
if route.Host != "" {
queryParams.Add("host", route.Host)
}
if route.Path != "" {
queryParams.Add("path", route.Path)
}
if route.Port.IsSet {
queryParams.Add("port", fmt.Sprint(route.Port.Value))
}
request.URL.RawQuery = queryParams.Encode()
var response cloudcontroller.Response
err = client.connection.Make(request, &response)
if _, ok := err.(ccerror.ResourceNotFoundError); ok {
return false, response.Warnings, nil
}
return response.HTTPResponse.StatusCode == http.StatusNoContent, response.Warnings, err
}
func (client *Client) checkRouteDeprecated(domainGUID string, host string, path string) (bool, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetRouteReservedDeprecatedRequest,
URIParams: map[string]string{"domain_guid": domainGUID, "host": host},
})
if err != nil {
return false, nil, err
}
queryParams := url.Values{}
if path != "" {
queryParams.Add("path", path)
}
request.URL.RawQuery = queryParams.Encode()
var response cloudcontroller.Response
err = client.connection.Make(request, &response)
if _, ok := err.(ccerror.ResourceNotFoundError); ok {
return false, response.Warnings, nil
}
return response.HTTPResponse.StatusCode == http.StatusNoContent, response.Warnings, err
}