forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
142 lines (118 loc) · 4.85 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
package api
import (
"fmt"
"net/url"
"strings"
"github.com/cloudfoundry/cli/cf/api/resources"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
)
//go:generate counterfeiter -o fakes/fake_route_repository.go . 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) (route models.Route, apiErr error)
Create(host string, domain models.DomainFields, path string) (createdRoute models.Route, apiErr error)
CheckIfExists(host string, domain models.DomainFields, path string) (found bool, apiErr error)
CreateInSpace(host, path, domainGuid, spaceGuid string) (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 core_config.Reader
gateway net.Gateway
}
func NewCloudControllerRouteRepository(config core_config.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 (repo CloudControllerRouteRepository) Find(host string, domain models.DomainFields, path string) (route models.Route, apiErr error) {
if path != "" && !strings.HasPrefix(path, `/`) {
path = `/` + path
}
found := false
apiErr = repo.gateway.ListPaginatedResources(
repo.config.ApiEndpoint(),
fmt.Sprintf("/v2/routes?inline-relations-depth=1&q=%s", url.QueryEscape("host:"+host+";domain_guid:"+domain.Guid+";path:"+path)),
resources.RouteResource{},
func(resource interface{}) bool {
route = resource.(resources.RouteResource).ToModel()
found = true
return false
})
if apiErr == nil && !found {
apiErr = errors.NewModelNotFoundError("Route", host)
}
return
}
func (repo CloudControllerRouteRepository) Create(host string, domain models.DomainFields, path string) (createdRoute models.Route, apiErr error) {
return repo.CreateInSpace(host, path, domain.Guid, repo.config.SpaceFields().Guid)
}
func (repo CloudControllerRouteRepository) CheckIfExists(host string, domain models.DomainFields, path string) (bool, error) {
var raw_response interface{}
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()
}
err = repo.gateway.GetResource(u.String(), &raw_response)
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) (createdRoute models.Route, apiErr error) {
if path != "" && !strings.HasPrefix(path, `/`) {
path = `/` + path
}
data := fmt.Sprintf(`{"host":"%s","path":"%s","domain_guid":"%s","space_guid":"%s"}`, host, path, domainGuid, spaceGuid)
resource := new(resources.RouteResource)
apiErr = repo.gateway.CreateResource(repo.config.ApiEndpoint(), "/v2/routes?inline-relations-depth=1", strings.NewReader(data), resource)
if apiErr != nil {
return
}
createdRoute = resource.ToModel()
return
}
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)
}