forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_space.go
150 lines (130 loc) · 4.88 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/space_quotas"
"github.com/cloudfoundry/cli/cf/api/spaces"
"github.com/cloudfoundry/cli/cf/command_registry"
"github.com/cloudfoundry/cli/cf/commands/user"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/errors"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/flags"
"github.com/cloudfoundry/cli/flags/flag"
)
type CreateSpace struct {
ui terminal.UI
config core_config.Reader
spaceRepo spaces.SpaceRepository
orgRepo organizations.OrganizationRepository
userRepo api.UserRepository
spaceRoleSetter user.SpaceRoleSetter
spaceQuotaRepo space_quotas.SpaceQuotaRepository
}
func init() {
command_registry.Register(&CreateSpace{})
}
func (cmd *CreateSpace) MetaData() command_registry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["o"] = &cliFlags.StringFlag{ShortName: "o", Usage: T("Organization")}
fs["q"] = &cliFlags.StringFlag{ShortName: "q", Usage: T("Quota to assign to the newly created space (excluding this option results in assignment of default quota)")}
return command_registry.CommandMetadata{
Name: "create-space",
Description: T("Create a space"),
Usage: T("CF_NAME create-space SPACE [-o ORG] [-q SPACE-QUOTA]"),
Flags: fs,
}
}
func (cmd *CreateSpace) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
if len(fc.Args()) != 1 {
cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + command_registry.Commands.CommandUsage("create-space"))
}
reqs = []requirements.Requirement{requirementsFactory.NewLoginRequirement()}
if fc.String("o") == "" {
reqs = append(reqs, requirementsFactory.NewTargetedOrgRequirement())
}
return
}
func (cmd *CreateSpace) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.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 := command_registry.Commands.FindCommand("set-space-role")
commandDep = commandDep.SetDependency(deps, false)
cmd.spaceRoleSetter = commandDep.(user.SpaceRoleSetter)
return cmd
}
func (cmd *CreateSpace) Execute(c flags.FlagContext) {
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()),
}))
var spaceQuotaGuid string
if spaceQuotaName != "" {
spaceQuota, err := cmd.spaceQuotaRepo.FindByName(spaceQuotaName)
if err != nil {
cmd.ui.Failed(err.Error())
}
spaceQuotaGuid = spaceQuota.Guid
}
if orgGuid == "" {
org, apiErr := cmd.orgRepo.FindByName(orgName)
switch apiErr.(type) {
case nil:
case *errors.ModelNotFoundError:
cmd.ui.Failed(T("Org {{.OrgName}} does not exist or is not accessible", map[string]interface{}{"OrgName": orgName}))
return
default:
cmd.ui.Failed(T("Error finding org {{.OrgName}}\n{{.ErrorDescription}}",
map[string]interface{}{
"OrgName": orgName,
"ErrorDescription": apiErr.Error(),
}))
return
}
orgGuid = org.Guid
}
space, err := cmd.spaceRepo.Create(spaceName, orgGuid, spaceQuotaGuid)
if err != nil {
if httpErr, ok := err.(errors.HttpError); ok && httpErr.ErrorCode() == errors.SPACE_EXISTS {
cmd.ui.Ok()
cmd.ui.Warn(T("Space {{.SpaceName}} already exists", map[string]interface{}{"SpaceName": spaceName}))
return
}
cmd.ui.Failed(err.Error())
return
}
cmd.ui.Ok()
err = cmd.spaceRoleSetter.SetSpaceRole(space, models.SPACE_MANAGER, cmd.config.UserGuid(), cmd.config.Username())
if err != nil {
cmd.ui.Failed(err.Error())
return
}
err = cmd.spaceRoleSetter.SetSpaceRole(space, models.SPACE_DEVELOPER, cmd.config.UserGuid(), cmd.config.Username())
if err != nil {
cmd.ui.Failed(err.Error())
return
}
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 + "\""),
}))
}