github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/route/create_route.go (about) 1 package route 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/cf" 7 "code.cloudfoundry.org/cli/cf/api" 8 "code.cloudfoundry.org/cli/cf/commandregistry" 9 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 10 "code.cloudfoundry.org/cli/cf/flags" 11 . "code.cloudfoundry.org/cli/cf/i18n" 12 "code.cloudfoundry.org/cli/cf/models" 13 "code.cloudfoundry.org/cli/cf/requirements" 14 "code.cloudfoundry.org/cli/cf/terminal" 15 ) 16 17 //go:generate counterfeiter . Creator 18 19 type Creator interface { 20 CreateRoute(hostName string, path string, port int, randomPort bool, domain models.DomainFields, space models.SpaceFields) (route models.Route, apiErr error) 21 } 22 23 type CreateRoute struct { 24 ui terminal.UI 25 config coreconfig.Reader 26 routeRepo api.RouteRepository 27 spaceReq requirements.SpaceRequirement 28 domainReq requirements.DomainRequirement 29 } 30 31 func init() { 32 commandregistry.Register(&CreateRoute{}) 33 } 34 35 func (cmd *CreateRoute) MetaData() commandregistry.CommandMetadata { 36 fs := make(map[string]flags.FlagSet) 37 fs["hostname"] = &flags.StringFlag{Name: "hostname", ShortName: "n", Usage: T("Hostname for the HTTP route (required for shared domains)")} 38 fs["path"] = &flags.StringFlag{Name: "path", Usage: T("Path for the HTTP route")} 39 fs["port"] = &flags.IntFlag{Name: "port", Usage: T("Port for the TCP route")} 40 fs["random-port"] = &flags.BoolFlag{Name: "random-port", Usage: T("Create a random port for the TCP route")} 41 42 return commandregistry.CommandMetadata{ 43 Name: "create-route", 44 Description: T("Create a url route in a space for later use"), 45 Usage: []string{ 46 fmt.Sprintf("%s:\n", T("Create an HTTP route")), 47 " CF_NAME create-route ", 48 fmt.Sprintf("%s ", T("SPACE")), 49 fmt.Sprintf("%s ", T("DOMAIN")), 50 fmt.Sprintf("[--hostname %s] ", T("HOSTNAME")), 51 fmt.Sprintf("[--path %s]\n\n", T("PATH")), 52 fmt.Sprintf(" %s:\n", T("Create a TCP route")), 53 " CF_NAME create-route ", 54 fmt.Sprintf("%s ", T("SPACE")), 55 fmt.Sprintf("%s ", T("DOMAIN")), 56 fmt.Sprintf("(--port %s | --random-port)", T("PORT")), 57 }, 58 Examples: []string{ 59 "CF_NAME create-route my-space example.com # example.com", 60 "CF_NAME create-route my-space example.com --hostname myapp # myapp.example.com", 61 "CF_NAME create-route my-space example.com --hostname myapp --path foo # myapp.example.com/foo", 62 "CF_NAME create-route my-space example.com --port 50000 # example.com:50000", 63 }, 64 Flags: fs, 65 } 66 } 67 68 func (cmd *CreateRoute) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 69 if len(fc.Args()) != 2 { 70 cmd.ui.Failed(T("Incorrect Usage. Requires SPACE and DOMAIN as arguments\n\n") + commandregistry.Commands.CommandUsage("create-route")) 71 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2) 72 } 73 74 if fc.IsSet("port") && (fc.IsSet("hostname") || fc.IsSet("path")) { 75 cmd.ui.Failed(T("Cannot specify port together with hostname and/or path.")) 76 return nil, fmt.Errorf("Cannot specify port together with hostname and/or path.") 77 } 78 79 if fc.IsSet("random-port") && (fc.IsSet("port") || fc.IsSet("hostname") || fc.IsSet("path")) { 80 cmd.ui.Failed(T("Cannot specify random-port together with port, hostname and/or path.")) 81 return nil, fmt.Errorf("Cannot specify random-port together with port, hostname and/or path.") 82 } 83 84 domainName := fc.Args()[1] 85 86 cmd.spaceReq = requirementsFactory.NewSpaceRequirement(fc.Args()[0]) 87 cmd.domainReq = requirementsFactory.NewDomainRequirement(domainName) 88 89 reqs := []requirements.Requirement{ 90 requirementsFactory.NewLoginRequirement(), 91 requirementsFactory.NewTargetedOrgRequirement(), 92 cmd.spaceReq, 93 cmd.domainReq, 94 } 95 96 if fc.IsSet("path") { 97 reqs = append(reqs, requirementsFactory.NewMinAPIVersionRequirement("Option '--path'", cf.RoutePathMinimumAPIVersion)) 98 } 99 100 if fc.IsSet("port") { 101 reqs = append(reqs, requirementsFactory.NewMinAPIVersionRequirement("Option '--port'", cf.TCPRoutingMinimumAPIVersion)) 102 } 103 104 if fc.IsSet("random-port") { 105 reqs = append(reqs, requirementsFactory.NewMinAPIVersionRequirement("Option '--random-port'", cf.TCPRoutingMinimumAPIVersion)) 106 } 107 108 return reqs, nil 109 } 110 111 func (cmd *CreateRoute) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 112 cmd.ui = deps.UI 113 cmd.config = deps.Config 114 cmd.routeRepo = deps.RepoLocator.GetRouteRepository() 115 return cmd 116 } 117 118 func (cmd *CreateRoute) Execute(c flags.FlagContext) error { 119 hostName := c.String("n") 120 space := cmd.spaceReq.GetSpace() 121 domain := cmd.domainReq.GetDomain() 122 path := c.String("path") 123 port := c.Int("port") 124 randomPort := c.Bool("random-port") 125 126 _, err := cmd.CreateRoute(hostName, path, port, randomPort, domain, space.SpaceFields) 127 if err != nil { 128 return err 129 } 130 131 return nil 132 } 133 134 func (cmd *CreateRoute) CreateRoute(hostName string, path string, port int, randomPort bool, domain models.DomainFields, space models.SpaceFields) (models.Route, error) { 135 cmd.ui.Say(T("Creating route {{.URL}} for org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", 136 map[string]interface{}{ 137 "URL": terminal.EntityNameColor(domain.URLForHostAndPath(hostName, path, port)), 138 "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), 139 "SpaceName": terminal.EntityNameColor(space.Name), 140 "Username": terminal.EntityNameColor(cmd.config.Username())})) 141 142 route, err := cmd.routeRepo.CreateInSpace(hostName, path, domain.GUID, space.GUID, port, randomPort) 143 if err != nil { 144 var findErr error 145 route, findErr = cmd.routeRepo.Find(hostName, domain, path, port) 146 if findErr != nil { 147 return models.Route{}, err 148 } 149 150 if route.Space.GUID != space.GUID || route.Domain.GUID != domain.GUID { 151 return models.Route{}, err 152 } 153 154 cmd.ui.Ok() 155 cmd.ui.Warn(T("Route {{.URL}} already exists", 156 map[string]interface{}{"URL": route.URL()})) 157 158 return route, nil 159 } 160 161 cmd.ui.Ok() 162 if randomPort { 163 cmd.ui.Say("Route %s:%d has been created", route.Domain.Name, route.Port) 164 } 165 166 return route, nil 167 }