-
Notifications
You must be signed in to change notification settings - Fork 928
/
create_space.go
140 lines (124 loc) · 4.47 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
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_metadata"
"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/flag_helpers"
. "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/codegangsta/cli"
)
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 NewCreateSpace(ui terminal.UI, config core_config.Reader, spaceRoleSetter user.SpaceRoleSetter, spaceRepo spaces.SpaceRepository, orgRepo organizations.OrganizationRepository, userRepo api.UserRepository, spaceQuotaRepo space_quotas.SpaceQuotaRepository) (cmd CreateSpace) {
cmd.ui = ui
cmd.config = config
cmd.spaceRoleSetter = spaceRoleSetter
cmd.spaceRepo = spaceRepo
cmd.orgRepo = orgRepo
cmd.userRepo = userRepo
cmd.spaceQuotaRepo = spaceQuotaRepo
return
}
func (cmd CreateSpace) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "create-space",
Description: T("Create a space"),
Usage: T("CF_NAME create-space SPACE [-o ORG] [-q SPACE-QUOTA]"),
Flags: []cli.Flag{
flag_helpers.NewStringFlag("o", T("Organization")),
flag_helpers.NewStringFlag("q", T("Quota to assign to the newly created space (excluding this option results in assignment of default quota)")),
},
}
}
func (cmd CreateSpace) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) != 1 {
cmd.ui.FailWithUsage(c)
}
reqs = []requirements.Requirement{requirementsFactory.NewLoginRequirement()}
if c.String("o") == "" {
reqs = append(reqs, requirementsFactory.NewTargetedOrgRequirement())
}
return
}
func (cmd CreateSpace) Run(c *cli.Context) {
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),
}))
}