forked from cloudfoundry/cli
/
create_space_command.go
131 lines (112 loc) · 3.91 KB
/
create_space_command.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
package v7
import (
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/resources"
)
type CreateSpaceActor interface {
CreateSpace(spaceName, orgGUID string) (resources.Space, v7action.Warnings, error)
CreateSpaceRole(roleType constant.RoleType, orgGUID string, spaceGUID string, userNameOrGUID string, userOrigin string, isClient bool) (v7action.Warnings, error)
GetOrganizationByName(orgName string) (resources.Organization, v7action.Warnings, error)
ApplySpaceQuotaByName(quotaName string, spaceGUID string, orgGUID string) (v7action.Warnings, error)
}
type CreateSpaceCommand struct {
BaseCommand
RequiredArgs flag.Space `positional-args:"yes"`
Organization string `short:"o" description:"Organization"`
Quota string `long:"quota" short:"q" description:"Quota to assign to the newly created space"`
usage interface{} `usage:"CF_NAME create-space SPACE [-o ORG] [-q QUOTA]"`
relatedCommands interface{} `related_commands:"set-space-isolation-segment, space-quotas, spaces, target"`
}
func (cmd CreateSpaceCommand) Execute(args []string) error {
err := cmd.SharedActor.CheckTarget(false, false)
if err != nil {
return err
}
var (
orgName, orgGUID string
)
if cmd.Organization == "" {
_, err = cmd.SharedActor.RequireTargetedOrg()
if err != nil {
return err
}
orgName = cmd.Config.TargetedOrganization().Name
orgGUID = cmd.Config.TargetedOrganization().GUID
} else {
orgName = cmd.Organization
org, warnings, err := cmd.Actor.GetOrganizationByName(orgName)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
orgGUID = org.GUID
}
user, err := cmd.Config.CurrentUser()
if err != nil {
return err
}
spaceName := cmd.RequiredArgs.Space
cmd.UI.DisplayTextWithFlavor("Creating space {{.Space}} in org {{.Organization}} as {{.User}}...",
map[string]interface{}{
"User": user.Name,
"Space": spaceName,
"Organization": orgName,
})
space, warnings, err := cmd.Actor.CreateSpace(spaceName, orgGUID)
cmd.UI.DisplayWarnings(warnings)
if _, ok := err.(actionerror.SpaceAlreadyExistsError); ok {
cmd.UI.DisplayText(err.Error())
cmd.UI.DisplayOK()
return nil
} else if err != nil {
return err
}
cmd.UI.DisplayOK()
if cmd.Quota != "" {
cmd.UI.DisplayTextWithFlavor("Setting space quota {{.Quota}} to space {{.Space}} as {{.User}}...",
map[string]interface{}{
"Quota": cmd.Quota,
"Space": spaceName,
"User": user.Name,
})
warnings, err = cmd.Actor.ApplySpaceQuotaByName(cmd.Quota, space.GUID, orgGUID)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayOK()
}
cmd.UI.DisplayTextWithFlavor("Assigning role SpaceManager to user {{.User}} in org {{.Organization}} / space {{.Space}} as {{.User}}...",
map[string]interface{}{
"User": user.Name,
"Space": spaceName,
"Organization": orgName,
})
warnings, err = cmd.Actor.CreateSpaceRole(constant.SpaceManagerRole, orgGUID, space.GUID, user.Name, user.Origin, user.IsClient)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayOK()
cmd.UI.DisplayTextWithFlavor("Assigning role SpaceDeveloper to user {{.User}} in org {{.Organization}} / space {{.Space}} as {{.User}}...",
map[string]interface{}{
"User": user.Name,
"Space": spaceName,
"Organization": orgName,
})
warnings, err = cmd.Actor.CreateSpaceRole(constant.SpaceDeveloperRole, orgGUID, space.GUID, user.Name, user.Origin, user.IsClient)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayOK()
cmd.UI.DisplayText(`TIP: Use 'cf target -o "{{.Organization}}" -s "{{.Space}}"' to target new space`,
map[string]interface{}{
"Organization": orgName,
"Space": spaceName,
})
return nil
}