github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/cf/commands/space/create_space.go (about) 1 package space 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/api/organizations" 9 "code.cloudfoundry.org/cli/cf/api/spacequotas" 10 "code.cloudfoundry.org/cli/cf/api/spaces" 11 "code.cloudfoundry.org/cli/cf/commandregistry" 12 "code.cloudfoundry.org/cli/cf/commands/user" 13 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 14 "code.cloudfoundry.org/cli/cf/errors" 15 "code.cloudfoundry.org/cli/cf/flags" 16 . "code.cloudfoundry.org/cli/cf/i18n" 17 "code.cloudfoundry.org/cli/cf/models" 18 "code.cloudfoundry.org/cli/cf/requirements" 19 "code.cloudfoundry.org/cli/cf/terminal" 20 ) 21 22 type CreateSpace struct { 23 ui terminal.UI 24 config coreconfig.Reader 25 spaceRepo spaces.SpaceRepository 26 orgRepo organizations.OrganizationRepository 27 userRepo api.UserRepository 28 spaceRoleSetter user.SpaceRoleSetter 29 spaceQuotaRepo spacequotas.SpaceQuotaRepository 30 } 31 32 func init() { 33 commandregistry.Register(&CreateSpace{}) 34 } 35 36 func (cmd *CreateSpace) MetaData() commandregistry.CommandMetadata { 37 fs := make(map[string]flags.FlagSet) 38 fs["o"] = &flags.StringFlag{ShortName: "o", Usage: T("Organization")} 39 fs["q"] = &flags.StringFlag{ShortName: "q", Usage: T("Quota to assign to the newly created space")} 40 41 return commandregistry.CommandMetadata{ 42 Name: "create-space", 43 Description: T("Create a space"), 44 Usage: []string{ 45 T("CF_NAME create-space SPACE [-o ORG] [-q SPACE-QUOTA]"), 46 }, 47 Flags: fs, 48 } 49 } 50 51 func (cmd *CreateSpace) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 52 if len(fc.Args()) != 1 { 53 cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("create-space")) 54 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1) 55 } 56 57 reqs := []requirements.Requirement{ 58 requirementsFactory.NewLoginRequirement(), 59 } 60 61 if fc.String("o") == "" { 62 reqs = append(reqs, requirementsFactory.NewTargetedOrgRequirement()) 63 } 64 65 return reqs, nil 66 } 67 68 func (cmd *CreateSpace) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 69 cmd.ui = deps.UI 70 cmd.config = deps.Config 71 cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository() 72 cmd.orgRepo = deps.RepoLocator.GetOrganizationRepository() 73 cmd.userRepo = deps.RepoLocator.GetUserRepository() 74 cmd.spaceQuotaRepo = deps.RepoLocator.GetSpaceQuotaRepository() 75 76 //get command from registry for dependency 77 commandDep := commandregistry.Commands.FindCommand("set-space-role") 78 commandDep = commandDep.SetDependency(deps, false) 79 cmd.spaceRoleSetter = commandDep.(user.SpaceRoleSetter) 80 81 return cmd 82 } 83 84 func (cmd *CreateSpace) Execute(c flags.FlagContext) error { 85 spaceName := c.Args()[0] 86 orgName := c.String("o") 87 spaceQuotaName := c.String("q") 88 orgGUID := "" 89 if orgName == "" { 90 orgName = cmd.config.OrganizationFields().Name 91 orgGUID = cmd.config.OrganizationFields().GUID 92 } 93 94 cmd.ui.Say(T("Creating space {{.SpaceName}} in org {{.OrgName}} as {{.CurrentUser}}...", 95 map[string]interface{}{ 96 "SpaceName": terminal.EntityNameColor(spaceName), 97 "OrgName": terminal.EntityNameColor(orgName), 98 "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), 99 })) 100 101 if orgGUID == "" { 102 org, err := cmd.orgRepo.FindByName(orgName) 103 switch err.(type) { 104 case nil: 105 case *errors.ModelNotFoundError: 106 return errors.New(T("Org {{.OrgName}} does not exist or is not accessible", map[string]interface{}{"OrgName": orgName})) 107 default: 108 return errors.New(T("Error finding org {{.OrgName}}\n{{.ErrorDescription}}", 109 map[string]interface{}{ 110 "OrgName": orgName, 111 "ErrorDescription": err.Error(), 112 })) 113 } 114 115 orgGUID = org.GUID 116 } 117 118 var spaceQuotaGUID string 119 if spaceQuotaName != "" { 120 spaceQuota, err := cmd.spaceQuotaRepo.FindByNameAndOrgGUID(spaceQuotaName, orgGUID) 121 if err != nil { 122 return err 123 } 124 spaceQuotaGUID = spaceQuota.GUID 125 } 126 127 space, err := cmd.spaceRepo.Create(spaceName, orgGUID, spaceQuotaGUID) 128 if err != nil { 129 if httpErr, ok := err.(errors.HTTPError); ok && httpErr.ErrorCode() == errors.SpaceNameTaken { 130 cmd.ui.Ok() 131 cmd.ui.Warn(T("Space {{.SpaceName}} already exists", map[string]interface{}{"SpaceName": spaceName})) 132 return nil 133 } 134 return err 135 } 136 cmd.ui.Ok() 137 138 err = cmd.spaceRoleSetter.SetSpaceRole(space, orgGUID, orgName, models.RoleSpaceManager, cmd.config.UserGUID(), cmd.config.Username()) 139 if err != nil { 140 return err 141 } 142 143 err = cmd.spaceRoleSetter.SetSpaceRole(space, orgGUID, orgName, models.RoleSpaceDeveloper, cmd.config.UserGUID(), cmd.config.Username()) 144 if err != nil { 145 return err 146 } 147 148 cmd.ui.Say(T("\nTIP: Use '{{.CFTargetCommand}}' to target new space", 149 map[string]interface{}{ 150 "CFTargetCommand": terminal.CommandColor(cf.Name + " target -o \"" + orgName + "\" -s \"" + space.Name + "\""), 151 })) 152 return nil 153 }