-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_org.go
136 lines (113 loc) · 4.46 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
package organization
import (
"github.com/cloudfoundry/cli/cf"
"github.com/cloudfoundry/cli/cf/api/feature_flags"
"github.com/cloudfoundry/cli/cf/api/organizations"
"github.com/cloudfoundry/cli/cf/api/quotas"
"github.com/cloudfoundry/cli/cf/command_registry"
"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/i18n"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/flags"
"github.com/cloudfoundry/cli/flags/flag"
)
type CreateOrg struct {
ui terminal.UI
config core_config.Reader
orgRepo organizations.OrganizationRepository
quotaRepo quotas.QuotaRepository
orgRoleSetter user.OrgRoleSetter
flagRepo feature_flags.FeatureFlagRepository
}
func init() {
command_registry.Register(&CreateOrg{})
}
func (cmd *CreateOrg) MetaData() command_registry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["q"] = &cliFlags.StringFlag{ShortName: "q", Usage: T("Quota to assign to the newly created org (excluding this option results in assignment of default quota)")}
return command_registry.CommandMetadata{
Name: "create-org",
ShortName: "co",
Description: T("Create an org"),
Usage: T("CF_NAME create-org ORG"),
Flags: fs,
}
}
func (cmd *CreateOrg) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
if len(fc.Args()) != 1 {
cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + command_registry.Commands.CommandUsage("create-org"))
}
reqs = []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
}
return
}
func (cmd *CreateOrg) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.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 := command_registry.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.ORG_EXISTS {
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("2.37.0") {
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)}))
}