-
Notifications
You must be signed in to change notification settings - Fork 928
/
create_org_command.go
99 lines (81 loc) · 2.73 KB
/
create_org_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
package v6
import (
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v2action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/v6/shared"
)
//go:generate counterfeiter . CreateOrgActor
type CreateOrgActor interface {
CreateOrganization(orgName string, quotaName string) (v2action.Organization, v2action.Warnings, error)
GrantOrgManagerByUsername(guid string, username string) (v2action.Warnings, error)
}
type CreateOrgCommand struct {
RequiredArgs flag.Organization `positional-args:"yes"`
Quota string `short:"q" description:"Quota to assign to the newly created org (excluding this option results in assignment of default quota)"`
usage interface{} `usage:"CF_NAME create-org ORG"`
relatedCommands interface{} `related_commands:"create-space, orgs, quotas, set-org-role"`
UI command.UI
Config command.Config
Actor CreateOrgActor
SharedActor command.SharedActor
}
func (cmd *CreateOrgCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
cmd.SharedActor = sharedaction.NewActor(config)
ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui)
if err != nil {
return err
}
cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
return nil
}
func (cmd CreateOrgCommand) Execute(args []string) error {
err := cmd.SharedActor.CheckTarget(false, false)
if err != nil {
return err
}
orgName := cmd.RequiredArgs.Organization
user, err := cmd.Config.CurrentUser()
if err != nil {
return err
}
cmd.UI.DisplayTextWithFlavor("Creating org {{.OrgName}} as {{.Username}}...",
map[string]interface{}{
"OrgName": orgName,
"Username": user.Name,
})
org, warnings, err := cmd.Actor.CreateOrganization(orgName, cmd.Quota)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
if _, ok := err.(actionerror.OrganizationNameTakenError); ok {
cmd.UI.DisplayOK()
cmd.UI.DisplayWarning("Org {{.OrgName}} already exists.", map[string]interface{}{
"OrgName": cmd.RequiredArgs.Organization,
})
return nil
}
return err
}
cmd.UI.DisplayOK()
cmd.UI.DisplayTextWithFlavor("Assigning role {{.Role}} to user {{.Username}} in org {{.OrgName}}...",
map[string]interface{}{
"Role": "OrgManager",
"OrgName": orgName,
"Username": user.Name,
})
warnings, err = cmd.Actor.GrantOrgManagerByUsername(org.GUID, user.Name)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayOK()
cmd.UI.DisplayText(`TIP: Use 'cf target -o "{{.OrgName}}"' to target new org`,
map[string]interface{}{
"OrgName": orgName,
})
return nil
}