-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_space.go
150 lines (129 loc) · 4.73 KB
/
create_space.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package space
import (
"github.com/cloudfoundry/cli/cf"
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/api/organizations"
"github.com/cloudfoundry/cli/cf/api/spacequotas"
"github.com/cloudfoundry/cli/cf/api/spaces"
"github.com/cloudfoundry/cli/cf/commandregistry"
"github.com/cloudfoundry/cli/cf/commands/user"
"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/flags"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
)
type CreateSpace struct {
ui terminal.UI
config coreconfig.Reader
spaceRepo spaces.SpaceRepository
orgRepo organizations.OrganizationRepository
userRepo api.UserRepository
spaceRoleSetter user.SpaceRoleSetter
spaceQuotaRepo spacequotas.SpaceQuotaRepository
}
func init() {
commandregistry.Register(&CreateSpace{})
}
func (cmd *CreateSpace) MetaData() commandregistry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["o"] = &flags.StringFlag{ShortName: "o", Usage: T("Organization")}
fs["q"] = &flags.StringFlag{ShortName: "q", Usage: T("Quota to assign to the newly created space")}
return commandregistry.CommandMetadata{
Name: "create-space",
Description: T("Create a space"),
Usage: []string{
T("CF_NAME create-space SPACE [-o ORG] [-q SPACE-QUOTA]"),
},
Flags: fs,
}
}
func (cmd *CreateSpace) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) []requirements.Requirement {
if len(fc.Args()) != 1 {
cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("create-space"))
}
reqs := []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
}
if fc.String("o") == "" {
reqs = append(reqs, requirementsFactory.NewTargetedOrgRequirement())
}
return reqs
}
func (cmd *CreateSpace) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
cmd.ui = deps.UI
cmd.config = deps.Config
cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository()
cmd.orgRepo = deps.RepoLocator.GetOrganizationRepository()
cmd.userRepo = deps.RepoLocator.GetUserRepository()
cmd.spaceQuotaRepo = deps.RepoLocator.GetSpaceQuotaRepository()
//get command from registry for dependency
commandDep := commandregistry.Commands.FindCommand("set-space-role")
commandDep = commandDep.SetDependency(deps, false)
cmd.spaceRoleSetter = commandDep.(user.SpaceRoleSetter)
return cmd
}
func (cmd *CreateSpace) Execute(c flags.FlagContext) error {
spaceName := c.Args()[0]
orgName := c.String("o")
spaceQuotaName := c.String("q")
orgGUID := ""
if orgName == "" {
orgName = cmd.config.OrganizationFields().Name
orgGUID = cmd.config.OrganizationFields().GUID
}
cmd.ui.Say(T("Creating space {{.SpaceName}} in org {{.OrgName}} as {{.CurrentUser}}...",
map[string]interface{}{
"SpaceName": terminal.EntityNameColor(spaceName),
"OrgName": terminal.EntityNameColor(orgName),
"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
}))
if orgGUID == "" {
org, err := cmd.orgRepo.FindByName(orgName)
switch err.(type) {
case nil:
case *errors.ModelNotFoundError:
return errors.New(T("Org {{.OrgName}} does not exist or is not accessible", map[string]interface{}{"OrgName": orgName}))
default:
return errors.New(T("Error finding org {{.OrgName}}\n{{.ErrorDescription}}",
map[string]interface{}{
"OrgName": orgName,
"ErrorDescription": err.Error(),
}))
}
orgGUID = org.GUID
}
var spaceQuotaGUID string
if spaceQuotaName != "" {
spaceQuota, err := cmd.spaceQuotaRepo.FindByNameAndOrgGUID(spaceQuotaName, orgGUID)
if err != nil {
return err
}
spaceQuotaGUID = spaceQuota.GUID
}
space, err := cmd.spaceRepo.Create(spaceName, orgGUID, spaceQuotaGUID)
if err != nil {
if httpErr, ok := err.(errors.HTTPError); ok && httpErr.ErrorCode() == errors.SpaceNameTaken {
cmd.ui.Ok()
cmd.ui.Warn(T("Space {{.SpaceName}} already exists", map[string]interface{}{"SpaceName": spaceName}))
return nil
}
return err
}
cmd.ui.Ok()
err = cmd.spaceRoleSetter.SetSpaceRole(space, orgGUID, orgName, models.RoleSpaceManager, cmd.config.UserGUID(), cmd.config.Username())
if err != nil {
return err
}
err = cmd.spaceRoleSetter.SetSpaceRole(space, orgGUID, orgName, models.RoleSpaceDeveloper, cmd.config.UserGUID(), cmd.config.Username())
if err != nil {
return err
}
cmd.ui.Say(T("\nTIP: Use '{{.CFTargetCommand}}' to target new space",
map[string]interface{}{
"CFTargetCommand": terminal.CommandColor(cf.Name + " target -o \"" + orgName + "\" -s \"" + space.Name + "\""),
}))
return nil
}