forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
122 lines (104 loc) · 4.39 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
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"
)
type RouteRepository interface {
ListRoutes(cb func(models.Route) bool) (apiErr error)
ListAllRoutes(cb func(models.Route) bool) (apiErr error)
FindByHostAndDomain(host string, domain models.DomainFields) (route models.Route, apiErr error)
Create(host string, domain models.DomainFields) (createdRoute models.Route, apiErr error)
CheckIfExists(host string, domain models.DomainFields) (found bool, apiErr error)
CreateInSpace(host, 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) FindByHostAndDomain(host string, domain models.DomainFields) (route models.Route, apiErr error) {
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)),
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) (createdRoute models.Route, apiErr error) {
return repo.CreateInSpace(host, domain.Guid, repo.config.SpaceFields().Guid)
}
func (repo CloudControllerRouteRepository) CheckIfExists(host string, domain models.DomainFields) (found bool, apiErr error) {
var raw_response interface{}
apiErr = repo.gateway.GetResource(fmt.Sprintf("%s/v2/routes/reserved/domain/%s/host/%s", repo.config.ApiEndpoint(), domain.Guid, host), &raw_response)
switch apiErr.(type) {
case nil:
found = true
case *errors.HttpNotFoundError:
found = false
apiErr = nil
default:
return
}
return
}
func (repo CloudControllerRouteRepository) CreateInSpace(host, domainGuid, spaceGuid string) (createdRoute models.Route, apiErr error) {
data := fmt.Sprintf(`{"host":"%s","domain_guid":"%s","space_guid":"%s"}`, host, 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)
}