-
Notifications
You must be signed in to change notification settings - Fork 929
/
routes.go
208 lines (170 loc) · 6.31 KB
/
routes.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
package api
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"strings"
"code.cloudfoundry.org/cli/cf/api/resources"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/errors"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/net"
"github.com/google/go-querystring/query"
)
//go:generate counterfeiter . RouteRepository
type RouteRepository interface {
ListRoutes(cb func(models.Route) bool) (apiErr error)
ListAllRoutes(cb func(models.Route) bool) (apiErr error)
Find(host string, domain models.DomainFields, path string, port int) (route models.Route, apiErr error)
Create(host string, domain models.DomainFields, path string, port int, useRandomPort bool) (createdRoute models.Route, apiErr error)
CheckIfExists(host string, domain models.DomainFields, path string) (found bool, apiErr error)
CreateInSpace(host, path, domainGUID, spaceGUID string, port int, randomPort bool) (createdRoute models.Route, apiErr error)
Bind(routeGUID, appGUID string) (apiErr error)
Unbind(routeGUID, appGUID string) (apiErr error)
Delete(routeGUID string) (apiErr error)
}
type CloudControllerRouteRepository struct {
config coreconfig.Reader
gateway net.Gateway
}
func NewCloudControllerRouteRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerRouteRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CloudControllerRouteRepository) ListRoutes(cb func(models.Route) bool) (apiErr error) {
return repo.gateway.ListPaginatedResources(
repo.config.APIEndpoint(),
fmt.Sprintf("/v2/spaces/%s/routes?inline-relations-depth=1", repo.config.SpaceFields().GUID),
resources.RouteResource{},
func(resource interface{}) bool {
return cb(resource.(resources.RouteResource).ToModel())
})
}
func (repo CloudControllerRouteRepository) ListAllRoutes(cb func(models.Route) bool) (apiErr error) {
return repo.gateway.ListPaginatedResources(
repo.config.APIEndpoint(),
fmt.Sprintf("/v2/routes?q=organization_guid:%s&inline-relations-depth=1", repo.config.OrganizationFields().GUID),
resources.RouteResource{},
func(resource interface{}) bool {
return cb(resource.(resources.RouteResource).ToModel())
})
}
func normalizedPath(path string) string {
if path != "" && !strings.HasPrefix(path, `/`) {
return `/` + path
}
return path
}
func queryStringForRouteSearch(host, guid, path string, port int) string {
args := []string{
fmt.Sprintf("host:%s", host),
fmt.Sprintf("domain_guid:%s", guid),
}
if path != "" {
args = append(args, fmt.Sprintf("path:%s", normalizedPath(path)))
}
if port != 0 {
args = append(args, fmt.Sprintf("port:%d", port))
}
return strings.Join(args, ";")
}
func (repo CloudControllerRouteRepository) Find(host string, domain models.DomainFields, path string, port int) (models.Route, error) {
var route models.Route
queryString := queryStringForRouteSearch(host, domain.GUID, path, port)
q := struct {
Query string `url:"q"`
InlineRelationsDepth int `url:"inline-relations-depth"`
}{queryString, 1}
opt, _ := query.Values(q)
found := false
apiErr := repo.gateway.ListPaginatedResources(
repo.config.APIEndpoint(),
fmt.Sprintf("/v2/routes?%s", opt.Encode()),
resources.RouteResource{},
func(resource interface{}) bool {
keepSearching := true
route = resource.(resources.RouteResource).ToModel()
if doesNotMatchVersionSpecificAttributes(route, path, port) {
return keepSearching
}
found = true
return !keepSearching
})
if apiErr == nil && !found {
apiErr = errors.NewModelNotFoundError("Route", host)
}
return route, apiErr
}
func doesNotMatchVersionSpecificAttributes(route models.Route, path string, port int) bool {
return normalizedPath(route.Path) != normalizedPath(path) || route.Port != port
}
func (repo CloudControllerRouteRepository) Create(host string, domain models.DomainFields, path string, port int, useRandomPort bool) (createdRoute models.Route, apiErr error) {
return repo.CreateInSpace(host, path, domain.GUID, repo.config.SpaceFields().GUID, port, useRandomPort)
}
func (repo CloudControllerRouteRepository) CheckIfExists(host string, domain models.DomainFields, path string) (bool, error) {
path = normalizedPath(path)
u, err := url.Parse(repo.config.APIEndpoint())
if err != nil {
return false, err
}
u.Path = fmt.Sprintf("/v2/routes/reserved/domain/%s/host/%s", domain.GUID, host)
if path != "" {
q := u.Query()
q.Set("path", path)
u.RawQuery = q.Encode()
}
var rawResponse interface{}
err = repo.gateway.GetResource(u.String(), &rawResponse)
if err != nil {
if _, ok := err.(*errors.HTTPNotFoundError); ok {
return false, nil
}
return false, err
}
return true, nil
}
func (repo CloudControllerRouteRepository) CreateInSpace(host, path, domainGUID, spaceGUID string, port int, randomPort bool) (models.Route, error) {
path = normalizedPath(path)
body := struct {
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"`
}{host, path, port, domainGUID, spaceGUID}
data, err := json.Marshal(body)
if err != nil {
return models.Route{}, err
}
q := struct {
GeneratePort bool `url:"generate_port,omitempty"`
InlineRelationsDepth int `url:"inline-relations-depth"`
}{randomPort, 1}
opt, _ := query.Values(q)
uriFragment := "/v2/routes?" + opt.Encode()
resource := new(resources.RouteResource)
err = repo.gateway.CreateResource(
repo.config.APIEndpoint(),
uriFragment,
bytes.NewReader(data),
resource,
)
if err != nil {
return models.Route{}, err
}
return resource.ToModel(), nil
}
func (repo CloudControllerRouteRepository) Bind(routeGUID, appGUID string) (apiErr error) {
path := fmt.Sprintf("/v2/apps/%s/routes/%s", appGUID, routeGUID)
return repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, nil)
}
func (repo CloudControllerRouteRepository) Unbind(routeGUID, appGUID string) (apiErr error) {
path := fmt.Sprintf("/v2/apps/%s/routes/%s", appGUID, routeGUID)
return repo.gateway.DeleteResource(repo.config.APIEndpoint(), path)
}
func (repo CloudControllerRouteRepository) Delete(routeGUID string) (apiErr error) {
path := fmt.Sprintf("/v2/routes/%s", routeGUID)
return repo.gateway.DeleteResource(repo.config.APIEndpoint(), path)
}