github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/create_route_command.go (about) 1 package v2 2 3 import ( 4 "os" 5 6 "github.com/liamawhite/cli-with-i18n/actor/sharedaction" 7 "github.com/liamawhite/cli-with-i18n/actor/v2action" 8 "github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccversion" 9 oldCmd "github.com/liamawhite/cli-with-i18n/cf/cmd" 10 "github.com/liamawhite/cli-with-i18n/command" 11 "github.com/liamawhite/cli-with-i18n/command/flag" 12 "github.com/liamawhite/cli-with-i18n/command/translatableerror" 13 "github.com/liamawhite/cli-with-i18n/command/v2/shared" 14 ) 15 16 //go:generate counterfeiter . CreateRouteActor 17 18 type CreateRouteActor interface { 19 CloudControllerAPIVersion() string 20 CreateRouteWithExistenceCheck(orgGUID string, spaceName string, route v2action.Route, generatePort bool) (v2action.Route, v2action.Warnings, error) 21 } 22 23 type CreateRouteCommand struct { 24 RequiredArgs flag.SpaceDomain `positional-args:"yes"` 25 Hostname string `long:"hostname" short:"n" description:"Hostname for the HTTP route (required for shared domains)"` 26 Path string `long:"path" description:"Path for the HTTP route"` 27 Port flag.Port `long:"port" description:"Port for the TCP route"` 28 RandomPort bool `long:"random-port" description:"Create a random port for the TCP route"` 29 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"` 30 relatedCommands interface{} `related_commands:"check-route, domains, map-route"` 31 32 UI command.UI 33 Config command.Config 34 SharedActor command.SharedActor 35 Actor CreateRouteActor 36 } 37 38 func (cmd *CreateRouteCommand) Setup(config command.Config, ui command.UI) error { 39 cmd.Config = config 40 cmd.UI = ui 41 cmd.SharedActor = sharedaction.NewActor(config) 42 43 ccClient, uaaClient, err := shared.NewClients(config, ui, true) 44 if err != nil { 45 return err 46 } 47 cmd.Actor = v2action.NewActor(ccClient, uaaClient, config) 48 49 return nil 50 } 51 52 func (cmd CreateRouteCommand) Execute(args []string) error { 53 if !cmd.Config.Experimental() { 54 oldCmd.Main(os.Getenv("CF_TRACE"), os.Args) 55 return nil 56 } 57 58 cmd.UI.DisplayWarning(command.ExperimentalWarning) 59 err := cmd.validateArguments() 60 if err != nil { 61 return shared.HandleError(err) 62 } 63 64 err = cmd.minimumFlagVersions() 65 if err != nil { 66 return shared.HandleError(err) 67 } 68 69 err = cmd.SharedActor.CheckTarget(cmd.Config, true, false) 70 if err != nil { 71 return shared.HandleError(err) 72 } 73 74 user, err := cmd.Config.CurrentUser() 75 if err != nil { 76 return shared.HandleError(err) 77 } 78 79 route := v2action.Route{ 80 Domain: v2action.Domain{Name: cmd.RequiredArgs.Domain}, 81 Host: cmd.Hostname, 82 Path: cmd.Path, 83 Port: cmd.Port.NullInt, 84 } 85 86 cmd.UI.DisplayTextWithFlavor("Creating route {{.Route}} for org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{ 87 "Route": route, 88 "OrgName": cmd.Config.TargetedOrganization().Name, 89 "SpaceName": cmd.RequiredArgs.Space, 90 "Username": user.Name, 91 }) 92 93 createdRoute, warnings, err := cmd.Actor.CreateRouteWithExistenceCheck(cmd.Config.TargetedOrganization().GUID, cmd.RequiredArgs.Space, route, cmd.RandomPort) 94 cmd.UI.DisplayWarnings(warnings) 95 if err != nil { 96 if _, ok := err.(v2action.RouteAlreadyExistsError); ok { 97 cmd.UI.DisplayWarning("Route {{.Route}} already exists.", map[string]interface{}{ 98 "Route": route, 99 }) 100 cmd.UI.DisplayOK() 101 return nil 102 } 103 104 return shared.HandleError(err) 105 } 106 107 cmd.UI.DisplayTextWithFlavor("Route {{.Route}} has been created.", map[string]interface{}{ 108 "Route": createdRoute, 109 }) 110 111 cmd.UI.DisplayOK() 112 113 return nil 114 } 115 116 func (cmd CreateRouteCommand) minimumFlagVersions() error { 117 ccVersion := cmd.Actor.CloudControllerAPIVersion() 118 if err := command.MinimumAPIVersionCheck(ccVersion, ccversion.MinVersionHTTPRoutePath, "Option '--path'"); cmd.Path != "" && err != nil { 119 return err 120 } 121 if err := command.MinimumAPIVersionCheck(ccVersion, ccversion.MinVersionTCPRouting, "Option '--port'"); cmd.Port.IsSet && err != nil { 122 return err 123 } 124 if err := command.MinimumAPIVersionCheck(ccVersion, ccversion.MinVersionTCPRouting, "Option '--random-port'"); cmd.RandomPort && err != nil { 125 return err 126 } 127 return nil 128 } 129 130 func (cmd CreateRouteCommand) validateArguments() error { 131 var failedArgs []string 132 133 if cmd.Hostname != "" { 134 failedArgs = append(failedArgs, "--hostname") 135 } 136 if cmd.Path != "" { 137 failedArgs = append(failedArgs, "--path") 138 } 139 if cmd.Port.IsSet { 140 failedArgs = append(failedArgs, "--port") 141 } 142 if cmd.RandomPort { 143 failedArgs = append(failedArgs, "--random-port") 144 } 145 146 switch { 147 case (cmd.Hostname != "" || cmd.Path != "") && (cmd.Port.IsSet || cmd.RandomPort), 148 cmd.Port.IsSet && cmd.RandomPort: 149 return translatableerror.ArgumentCombinationError{Args: failedArgs} 150 } 151 152 return nil 153 }