-
Notifications
You must be signed in to change notification settings - Fork 927
/
create_route_command.go
125 lines (103 loc) · 4.12 KB
/
create_route_command.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
package v6
import (
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v2action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/command/v6/shared"
)
//go:generate counterfeiter . CreateRouteActor
type CreateRouteActor interface {
CloudControllerAPIVersion() string
CreateRouteWithExistenceCheck(orgGUID string, spaceName string, route v2action.Route, generatePort bool) (v2action.Route, v2action.Warnings, error)
}
type CreateRouteCommand struct {
RequiredArgs flag.SpaceDomain `positional-args:"yes"`
Hostname string `long:"hostname" short:"n" description:"Hostname for the HTTP route (required for shared domains)"`
Path string `long:"path" description:"Path for the HTTP route"`
Port flag.Port `long:"port" description:"Port for the TCP route"`
RandomPort bool `long:"random-port" description:"Create a random port for the TCP route"`
usage interface{} `usage:"Create an HTTP route:\n CF_NAME create-route SPACE DOMAIN [--hostname HOSTNAME] [--path PATH]\n\n Create a TCP route:\n CF_NAME create-route SPACE DOMAIN (--port PORT | --random-port)\n\nEXAMPLES:\n CF_NAME create-route my-space example.com # example.com\n CF_NAME create-route my-space example.com --hostname myapp # myapp.example.com\n CF_NAME create-route my-space example.com --hostname myapp --path foo # myapp.example.com/foo\n CF_NAME create-route my-space example.com --port 5000 # example.com:5000"`
relatedCommands interface{} `related_commands:"check-route, domains, map-route"`
UI command.UI
Config command.Config
SharedActor command.SharedActor
Actor CreateRouteActor
}
func (cmd *CreateRouteCommand) Setup(config command.Config, ui command.UI) error {
cmd.Config = config
cmd.UI = ui
cmd.SharedActor = sharedaction.NewActor(config)
ccClient, uaaClient, err := shared.NewClients(config, ui, true)
if err != nil {
return err
}
cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
return nil
}
func (cmd CreateRouteCommand) Execute(args []string) error {
err := cmd.validateArguments()
if err != nil {
return err
}
err = cmd.SharedActor.CheckTarget(true, false)
if err != nil {
return err
}
user, err := cmd.Config.CurrentUser()
if err != nil {
return err
}
route := v2action.Route{
Domain: v2action.Domain{Name: cmd.RequiredArgs.Domain},
Host: cmd.Hostname,
Path: cmd.Path,
Port: cmd.Port.NullInt,
}
cmd.UI.DisplayTextWithFlavor("Creating route {{.Route}} for org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
"Route": route,
"OrgName": cmd.Config.TargetedOrganization().Name,
"SpaceName": cmd.RequiredArgs.Space,
"Username": user.Name,
})
createdRoute, warnings, err := cmd.Actor.CreateRouteWithExistenceCheck(cmd.Config.TargetedOrganization().GUID, cmd.RequiredArgs.Space, route, cmd.RandomPort)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
if _, ok := err.(actionerror.RouteAlreadyExistsError); ok {
cmd.UI.DisplayWarning("Route {{.Route}} already exists.", map[string]interface{}{
"Route": route,
})
cmd.UI.DisplayOK()
return nil
}
return err
}
cmd.UI.DisplayTextWithFlavor("Route {{.Route}} has been created.", map[string]interface{}{
"Route": createdRoute,
})
cmd.UI.DisplayOK()
return nil
}
func (cmd CreateRouteCommand) validateArguments() error {
var failedArgs []string
if cmd.Hostname != "" {
failedArgs = append(failedArgs, "--hostname")
}
if cmd.Path != "" {
failedArgs = append(failedArgs, "--path")
}
if cmd.Port.IsSet {
failedArgs = append(failedArgs, "--port")
}
if cmd.RandomPort {
failedArgs = append(failedArgs, "--random-port")
}
switch {
case (cmd.Hostname != "" || cmd.Path != "") && (cmd.Port.IsSet || cmd.RandomPort),
cmd.Port.IsSet && cmd.RandomPort:
return translatableerror.ArgumentCombinationError{Args: failedArgs}
}
return nil
}