github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/domain/create_shared_domain.go (about) 1 package domain 2 3 import ( 4 "errors" 5 "fmt" 6 7 "code.cloudfoundry.org/cli/cf" 8 "code.cloudfoundry.org/cli/cf/api" 9 "code.cloudfoundry.org/cli/cf/commandregistry" 10 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 11 "code.cloudfoundry.org/cli/cf/flags" 12 . "code.cloudfoundry.org/cli/cf/i18n" 13 "code.cloudfoundry.org/cli/cf/models" 14 "code.cloudfoundry.org/cli/cf/requirements" 15 "code.cloudfoundry.org/cli/cf/terminal" 16 ) 17 18 type CreateSharedDomain struct { 19 ui terminal.UI 20 config coreconfig.Reader 21 domainRepo api.DomainRepository 22 routingAPIRepo api.RoutingAPIRepository 23 } 24 25 func init() { 26 commandregistry.Register(&CreateSharedDomain{}) 27 } 28 29 func (cmd *CreateSharedDomain) MetaData() commandregistry.CommandMetadata { 30 fs := make(map[string]flags.FlagSet) 31 fs["router-group"] = &flags.StringFlag{Name: "router-group", Usage: T("Routes for this domain will be configured only on the specified router group")} 32 return commandregistry.CommandMetadata{ 33 Name: "create-shared-domain", 34 Description: T("Create a domain that can be used by all orgs (admin-only)"), 35 Usage: []string{ 36 T("CF_NAME create-shared-domain DOMAIN [--router-group ROUTER_GROUP]"), 37 }, 38 Flags: fs, 39 } 40 } 41 42 func (cmd *CreateSharedDomain) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 43 if len(fc.Args()) != 1 { 44 cmd.ui.Failed(T("Incorrect Usage. Requires DOMAIN as an argument\n\n") + commandregistry.Commands.CommandUsage("create-shared-domain")) 45 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1) 46 } 47 48 reqs := []requirements.Requirement{ 49 requirementsFactory.NewLoginRequirement(), 50 } 51 52 if fc.String("router-group") != "" { 53 reqs = append(reqs, []requirements.Requirement{ 54 requirementsFactory.NewMinAPIVersionRequirement("Option '--router-group'", cf.RoutePathMinimumAPIVersion), 55 requirementsFactory.NewRoutingAPIRequirement(), 56 }...) 57 } 58 59 return reqs, nil 60 } 61 62 func (cmd *CreateSharedDomain) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 63 cmd.ui = deps.UI 64 cmd.config = deps.Config 65 cmd.domainRepo = deps.RepoLocator.GetDomainRepository() 66 cmd.routingAPIRepo = deps.RepoLocator.GetRoutingAPIRepository() 67 return cmd 68 } 69 70 func (cmd *CreateSharedDomain) Execute(c flags.FlagContext) error { 71 var routerGroup models.RouterGroup 72 domainName := c.Args()[0] 73 routerGroupName := c.String("router-group") 74 75 if routerGroupName != "" { 76 var routerGroupFound bool 77 err := cmd.routingAPIRepo.ListRouterGroups(func(group models.RouterGroup) bool { 78 if group.Name == routerGroupName { 79 routerGroup = group 80 routerGroupFound = true 81 return false 82 } 83 84 return true 85 }) 86 87 if err != nil { 88 return err 89 } 90 if !routerGroupFound { 91 return errors.New(T("Router group {{.RouterGroup}} not found", map[string]interface{}{ 92 "RouterGroup": routerGroupName, 93 })) 94 } 95 } 96 97 cmd.ui.Say(T("Creating shared domain {{.DomainName}} as {{.Username}}...", 98 map[string]interface{}{ 99 "DomainName": terminal.EntityNameColor(domainName), 100 "Username": terminal.EntityNameColor(cmd.config.Username())})) 101 102 err := cmd.domainRepo.CreateSharedDomain(domainName, routerGroup.GUID) 103 if err != nil { 104 return err 105 } 106 107 cmd.ui.Ok() 108 return nil 109 }