forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_service_broker.go
100 lines (85 loc) · 3.12 KB
/
create_service_broker.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
package servicebroker
import (
"github.com/blang/semver"
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/commandregistry"
"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/flags"
)
type CreateServiceBroker struct {
ui terminal.UI
config coreconfig.Reader
serviceBrokerRepo api.ServiceBrokerRepository
}
func init() {
commandregistry.Register(&CreateServiceBroker{})
}
func (cmd *CreateServiceBroker) MetaData() commandregistry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["space-scoped"] = &flags.BoolFlag{Name: "space-scoped", Usage: T("Make the broker's service plans only visible within the targeted space")}
return commandregistry.CommandMetadata{
Name: "create-service-broker",
ShortName: "csb",
Description: T("Create a service broker"),
Usage: []string{
T("CF_NAME create-service-broker SERVICE_BROKER USERNAME PASSWORD URL [--space-scoped]"),
},
Flags: fs,
}
}
func (cmd *CreateServiceBroker) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) []requirements.Requirement {
if len(fc.Args()) != 4 {
cmd.ui.Failed(T("Incorrect Usage. Requires SERVICE_BROKER, USERNAME, PASSWORD, URL as arguments\n\n") + commandregistry.Commands.CommandUsage("create-service-broker"))
}
reqs := []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
}
if fc.IsSet("space-scoped") {
requiredVersion, err := semver.Make("2.47.0")
if err != nil {
panic(err.Error())
}
reqs = append(
reqs,
requirementsFactory.NewTargetedSpaceRequirement(),
requirementsFactory.NewMinAPIVersionRequirement("--space-scoped", requiredVersion),
)
}
return reqs
}
func (cmd *CreateServiceBroker) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
cmd.ui = deps.UI
cmd.config = deps.Config
cmd.serviceBrokerRepo = deps.RepoLocator.GetServiceBrokerRepository()
return cmd
}
func (cmd *CreateServiceBroker) Execute(c flags.FlagContext) error {
name := c.Args()[0]
username := c.Args()[1]
password := c.Args()[2]
url := c.Args()[3]
var err error
if c.Bool("space-scoped") {
cmd.ui.Say(T("Creating service broker {{.Name}} in org {{.Org}} / space {{.Space}} as {{.Username}}...",
map[string]interface{}{
"Name": terminal.EntityNameColor(name),
"Org": terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
"Space": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
"Username": terminal.EntityNameColor(cmd.config.Username())}))
err = cmd.serviceBrokerRepo.Create(name, url, username, password, cmd.config.SpaceFields().GUID)
} else {
cmd.ui.Say(T("Creating service broker {{.Name}} as {{.Username}}...",
map[string]interface{}{
"Name": terminal.EntityNameColor(name),
"Username": terminal.EntityNameColor(cmd.config.Username())}))
err = cmd.serviceBrokerRepo.Create(name, url, username, password, "")
}
if err != nil {
return err
}
cmd.ui.Ok()
return err
}