-
Notifications
You must be signed in to change notification settings - Fork 928
/
route.go
297 lines (247 loc) · 6.96 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
package ccv3
import (
"bytes"
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
)
type Route struct {
GUID string
SpaceGUID string
DomainGUID string
Host string
Path string
URL string
}
func (r Route) MarshalJSON() ([]byte, error) {
type Data struct {
GUID string `json:"guid,omitempty"`
}
type RelationshipData struct {
Data Data `json:"data,omitempty"`
}
type Relationships struct {
Space RelationshipData `json:"space,omitempty"`
Domain RelationshipData `json:"domain,omitempty"`
}
// Building up the request body in ccRoute
type ccRoute struct {
GUID string `json:"guid,omitempty"`
Host string `json:"host,omitempty"`
Path string `json:"path,omitempty"`
Relationships *Relationships `json:"relationships,omitempty"`
}
ccR := ccRoute{
GUID: r.GUID,
Host: r.Host,
Path: r.Path,
}
if r.SpaceGUID != "" {
ccR.Relationships = &Relationships{RelationshipData{Data{GUID: r.SpaceGUID}},
RelationshipData{Data{GUID: r.DomainGUID}}}
}
return json.Marshal(ccR)
}
func (r *Route) UnmarshalJSON(data []byte) error {
var alias struct {
GUID string `json:"guid,omitempty"`
Host string `json:"host,omitempty"`
Path string `json:"path,omitempty"`
URL string `json:"url,omitempty"`
Relationships struct {
Space struct {
Data struct {
GUID string `json:"guid,omitempty"`
} `json:"data,omitempty"`
} `json:"space,omitempty"`
Domain struct {
Data struct {
GUID string `json:"guid,omitempty"`
} `json:"data,omitempty"`
} `json:"domain,omitempty"`
} `json:"relationships,omitempty"`
}
err := cloudcontroller.DecodeJSON(data, &alias)
if err != nil {
return err
}
r.GUID = alias.GUID
r.Host = alias.Host
r.SpaceGUID = alias.Relationships.Space.Data.GUID
r.DomainGUID = alias.Relationships.Domain.Data.GUID
r.Path = alias.Path
r.URL = alias.URL
return nil
}
type RouteDestinationApp struct {
GUID string
Process struct {
Type string
}
}
type RouteDestination struct {
GUID string
App RouteDestinationApp
}
func (client Client) CreateRoute(route Route) (Route, Warnings, error) {
bodyBytes, err := json.Marshal(route)
if err != nil {
return Route{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostRouteRequest,
Body: bytes.NewReader(bodyBytes),
})
if err != nil {
return Route{}, nil, err
}
var ccRoute Route
response := cloudcontroller.Response{
DecodeJSONResponseInto: &ccRoute,
}
err = client.connection.Make(request, &response)
return ccRoute, response.Warnings, err
}
func (client Client) DeleteOrphanedRoutes(spaceGUID string) (JobURL, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.DeleteOrphanedRoutesRequest,
URIParams: map[string]string{
"space_guid": spaceGUID,
},
Query: []Query{{Key: UnmappedFilter, Values: []string{"true"}}},
})
if err != nil {
return "", nil, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return JobURL(response.ResourceLocationURL), response.Warnings, err
}
func (client Client) DeleteRoute(routeGUID string) (JobURL, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
URIParams: map[string]string{
"route_guid": routeGUID,
},
RequestName: internal.DeleteRouteRequest,
})
if err != nil {
return "", nil, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return JobURL(response.ResourceLocationURL), response.Warnings, err
}
func (client Client) GetApplicationRoutes(appGUID string) ([]Route, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetApplicationRoutesRequest,
URIParams: internal.Params{"app_guid": appGUID},
})
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
}
func (client Client) GetRouteDestinations(routeGUID string) ([]RouteDestination, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetRouteDestinationsRequest,
URIParams: internal.Params{"route_guid": routeGUID},
})
if err != nil {
return nil, nil, err
}
var destinationResponse struct {
Destinations []RouteDestination `json:"destinations"`
}
response := cloudcontroller.Response{
DecodeJSONResponseInto: &destinationResponse,
}
err = client.connection.Make(request, &response)
return destinationResponse.Destinations, response.Warnings, err
}
func (client Client) GetRoutes(query ...Query) ([]Route, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetRoutesRequest,
Query: query,
})
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
}
func (client Client) MapRoute(routeGUID string, appGUID string) (Warnings, error) {
type destinationProcess struct {
ProcessType string `json:"process_type"`
}
type destinationApp struct {
GUID string `json:"guid"`
Process *destinationProcess `json:"process,omitempty"`
}
type destination struct {
App destinationApp `json:"app"`
}
type body struct {
Destinations []destination `json:"destinations"`
}
requestBody := body{
Destinations: []destination{
{App: destinationApp{GUID: appGUID}},
},
}
bodyBytes, err := json.Marshal(requestBody)
if err != nil {
return nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.MapRouteRequest,
URIParams: map[string]string{
"route_guid": routeGUID,
},
Body: bytes.NewReader(bodyBytes),
})
if err != nil {
return nil, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return response.Warnings, err
}
func (client Client) UnmapRoute(routeGUID string, destinationGUID string) (Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.UnmapRouteRequest,
URIParams: map[string]string{
"route_guid": routeGUID,
"destination_guid": destinationGUID,
},
})
if err != nil {
return nil, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return response.Warnings, err
}