-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_org.go
138 lines (114 loc) · 4.43 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
package organization
import (
"github.com/cloudfoundry/cli/cf"
"github.com/cloudfoundry/cli/cf/api/featureflags"
"github.com/cloudfoundry/cli/cf/api/organizations"
"github.com/cloudfoundry/cli/cf/api/quotas"
"github.com/cloudfoundry/cli/cf/commandregistry"
"github.com/cloudfoundry/cli/cf/commands/user"
"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
"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"
)
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 {
if len(fc.Args()) != 1 {
cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("create-org"))
}
reqs := []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
}
return reqs
}
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) {
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 {
cmd.ui.Failed(err.Error())
}
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
}
cmd.ui.Failed(err.Error())
}
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 {
cmd.ui.Failed(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, "OrgManager", "", cmd.config.Username())
if err != nil {
cmd.ui.Failed(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)}))
}