forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_org.go
142 lines (117 loc) · 4.53 KB
/
create_org.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
package organization
import (
"fmt"
"code.cloudfoundry.org/cli/cf"
"code.cloudfoundry.org/cli/cf/api/featureflags"
"code.cloudfoundry.org/cli/cf/api/organizations"
"code.cloudfoundry.org/cli/cf/api/quotas"
"code.cloudfoundry.org/cli/cf/commandregistry"
"code.cloudfoundry.org/cli/cf/commands/user"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/errors"
"code.cloudfoundry.org/cli/cf/flags"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/requirements"
"code.cloudfoundry.org/cli/cf/terminal"
)
type CreateOrg struct {
ui terminal.UI
config coreconfig.Reader
orgRepo organizations.OrganizationRepository
quotaRepo quotas.QuotaRepository
orgRoleSetter user.OrgRoleSetter
flagRepo featureflags.FeatureFlagRepository
}
func init() {
commandregistry.Register(&CreateOrg{})
}
func (cmd *CreateOrg) MetaData() commandregistry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["q"] = &flags.StringFlag{ShortName: "q", Usage: T("Quota to assign to the newly created org (excluding this option results in assignment of default quota)")}
return commandregistry.CommandMetadata{
Name: "create-org",
ShortName: "co",
Description: T("Create an org"),
Usage: []string{
T("CF_NAME create-org ORG"),
},
Flags: fs,
}
}
func (cmd *CreateOrg) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
if len(fc.Args()) != 1 {
cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("create-org"))
return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
}
reqs := []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
}
return reqs, nil
}
func (cmd *CreateOrg) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
cmd.ui = deps.UI
cmd.config = deps.Config
cmd.orgRepo = deps.RepoLocator.GetOrganizationRepository()
cmd.quotaRepo = deps.RepoLocator.GetQuotaRepository()
cmd.flagRepo = deps.RepoLocator.GetFeatureFlagRepository()
//get command from registry for dependency
commandDep := commandregistry.Commands.FindCommand("set-org-role")
commandDep = commandDep.SetDependency(deps, false)
cmd.orgRoleSetter = commandDep.(user.OrgRoleSetter)
return cmd
}
func (cmd *CreateOrg) Execute(c flags.FlagContext) error {
name := c.Args()[0]
cmd.ui.Say(T("Creating org {{.OrgName}} as {{.Username}}...",
map[string]interface{}{
"OrgName": terminal.EntityNameColor(name),
"Username": terminal.EntityNameColor(cmd.config.Username())}))
org := models.Organization{OrganizationFields: models.OrganizationFields{Name: name}}
quotaName := c.String("q")
if quotaName != "" {
quota, err := cmd.quotaRepo.FindByName(quotaName)
if err != nil {
return err
}
org.QuotaDefinition.GUID = quota.GUID
}
err := cmd.orgRepo.Create(org)
if err != nil {
if apiErr, ok := err.(errors.HTTPError); ok && apiErr.ErrorCode() == errors.OrganizationNameTaken {
cmd.ui.Ok()
cmd.ui.Warn(T("Org {{.OrgName}} already exists",
map[string]interface{}{"OrgName": name}))
return nil
}
return err
}
cmd.ui.Ok()
if cmd.config.IsMinAPIVersion(cf.SetRolesByUsernameMinimumAPIVersion) {
setRolesByUsernameFlag, err := cmd.flagRepo.FindByName("set_roles_by_username")
if err != nil {
cmd.ui.Warn(T("Warning: accessing feature flag 'set_roles_by_username'") + " - " + err.Error() + "\n" + T("Skip assigning org role to user"))
}
if setRolesByUsernameFlag.Enabled {
org, err := cmd.orgRepo.FindByName(name)
if err != nil {
return errors.New(T("Error accessing org {{.OrgName}} for GUID': ", map[string]interface{}{"Orgname": name}) + err.Error() + "\n" + T("Skip assigning org role to user"))
}
cmd.ui.Say("")
cmd.ui.Say(T("Assigning role {{.Role}} to user {{.CurrentUser}} in org {{.TargetOrg}} ...",
map[string]interface{}{
"Role": terminal.EntityNameColor("OrgManager"),
"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
"TargetOrg": terminal.EntityNameColor(name),
}))
err = cmd.orgRoleSetter.SetOrgRole(org.GUID, models.RoleOrgManager, "", cmd.config.Username())
if err != nil {
return errors.New(T("Failed assigning org role to user: ") + err.Error())
}
cmd.ui.Ok()
}
}
cmd.ui.Say(T("\nTIP: Use '{{.Command}}' to target new org",
map[string]interface{}{"Command": terminal.CommandColor(cf.Name + " target -o " + name)}))
return nil
}