forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
233 lines (205 loc) · 6.69 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
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"
)
// Route represents a Cloud Controller Route.
type Route struct {
GUID string `json:"-"`
Host string `json:"host,omitempty"`
Path string `json:"path,omitempty"`
Port int `json:"port,omitempty"`
DomainGUID string `json:"domain_guid"`
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 int `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
}
// BindRouteToApplication binds the given route to the given application.
func (client *Client) BindRouteToApplication(routeGUID string, appGUID string) (Route, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PutBindRouteAppRequest,
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
}
// CreateRoute creates the route with the given properties; SpaceGUID and
// DomainGUID are required. Set generatePort true to generate a random port on
// the cloud controller. generatePort takes precedence over manually specified
// 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 queries.
func (client *Client) GetApplicationRoutes(appGUID string, queryParams []Query) ([]Route, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetAppRoutesRequest,
URIParams: map[string]string{"app_guid": appGUID},
Query: FormatQueryParameters(queryParams),
})
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 queries.
func (client *Client) GetSpaceRoutes(spaceGUID string, queryParams []Query) ([]Route, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetSpaceRoutesRequest,
URIParams: map[string]string{"space_guid": spaceGUID},
Query: FormatQueryParameters(queryParams),
})
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 queries.
func (client *Client) GetRoutes(queryParams []Query) ([]Route, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetRoutesRequest,
Query: FormatQueryParameters(queryParams),
})
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
}
// CheckRoute 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) 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 != 0 {
queryParams.Add("port", fmt.Sprint(route.Port))
}
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
}