forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
67 lines (56 loc) · 2.28 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
package actors
import (
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/errors"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/terminal"
)
type RouteActor struct {
ui terminal.UI
routeRepo api.RouteRepository
}
func NewRouteActor(ui terminal.UI, routeRepo api.RouteRepository) RouteActor {
return RouteActor{ui: ui, routeRepo: routeRepo}
}
func (routeActor RouteActor) FindOrCreateRoute(hostname string, domain models.DomainFields, path string) (route models.Route) {
route, apiErr := routeActor.routeRepo.Find(hostname, domain, path)
switch apiErr.(type) {
case nil:
routeActor.ui.Say(T("Using route {{.RouteURL}}", map[string]interface{}{"RouteURL": terminal.EntityNameColor(route.URL())}))
case *errors.ModelNotFoundError:
routeActor.ui.Say(T("Creating route {{.Hostname}}...", map[string]interface{}{"Hostname": terminal.EntityNameColor(domain.UrlForHostAndPath(hostname, path))}))
route, apiErr = routeActor.routeRepo.Create(hostname, domain, path)
if apiErr != nil {
routeActor.ui.Failed(apiErr.Error())
}
routeActor.ui.Ok()
routeActor.ui.Say("")
default:
routeActor.ui.Failed(apiErr.Error())
}
return
}
func (routeActor RouteActor) BindRoute(app models.Application, route models.Route) {
if !app.HasRoute(route) {
routeActor.ui.Say(T("Binding {{.URL}} to {{.AppName}}...", map[string]interface{}{"URL": terminal.EntityNameColor(route.URL()), "AppName": terminal.EntityNameColor(app.Name)}))
apiErr := routeActor.routeRepo.Bind(route.Guid, app.Guid)
switch apiErr := apiErr.(type) {
case nil:
routeActor.ui.Ok()
routeActor.ui.Say("")
return
case errors.HttpError:
if apiErr.ErrorCode() == errors.INVALID_RELATION {
routeActor.ui.Failed(T("The route {{.URL}} is already in use.\nTIP: Change the hostname with -n HOSTNAME or use --random-route to generate a new route and then push again.", map[string]interface{}{"URL": route.URL()}))
}
}
routeActor.ui.Failed(apiErr.Error())
}
}
func (routeActor RouteActor) UnbindAll(app models.Application) {
for _, route := range app.Routes {
routeActor.ui.Say(T("Removing route {{.URL}}...", map[string]interface{}{"URL": terminal.EntityNameColor(route.URL())}))
routeActor.routeRepo.Unbind(route.Guid, app.Guid)
}
}