-
Notifications
You must be signed in to change notification settings - Fork 929
/
create_user_provided_service.go
117 lines (94 loc) · 4.12 KB
/
create_user_provided_service.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
package service
import (
"encoding/json"
"strings"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/flags"
"github.com/cloudfoundry/cli/flags/flag"
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/command_registry"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
)
type CreateUserProvidedService struct {
ui terminal.UI
config core_config.Reader
userProvidedServiceInstanceRepo api.UserProvidedServiceInstanceRepository
}
func init() {
command_registry.Register(&CreateUserProvidedService{})
}
func (cmd *CreateUserProvidedService) MetaData() command_registry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["p"] = &cliFlags.StringFlag{Name: "p", Usage: T("Credentials")}
fs["l"] = &cliFlags.StringFlag{Name: "l", Usage: T("Syslog Drain Url")}
return command_registry.CommandMetadata{
Name: "create-user-provided-service",
ShortName: "cups",
Description: T("Make a user-provided service instance available to cf apps"),
Usage: T(`CF_NAME create-user-provided-service SERVICE_INSTANCE [-p CREDENTIALS] [-l SYSLOG-DRAIN-URL]
Pass comma separated credential parameter names to enable interactive mode:
CF_NAME create-user-provided-service SERVICE_INSTANCE -p "comma, separated, parameter, names"
Pass credential parameters as JSON to create a service non-interactively:
CF_NAME create-user-provided-service SERVICE_INSTANCE -p '{"name":"value","name":"value"}'
EXAMPLE
CF_NAME create-user-provided-service my-db-mine -p "username, password"
CF_NAME create-user-provided-service my-drain-service -l syslog://example.com
Linux/Mac:
CF_NAME create-user-provided-service my-db-mine -p '{"username":"admin","password":"pa55woRD"}'
Windows Command Line
CF_NAME create-user-provided-service my-db-mine -p "{\"username\":\"admin\",\"password\":\"pa55woRD\"}"
Windows PowerShell
CF_NAME create-user-provided-service my-db-mine -p '{\"username\":\"admin\",\"password\":\"pa55woRD\"}'
`),
Flags: fs,
}
}
func (cmd *CreateUserProvidedService) 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-user-provided-service"))
}
reqs = []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
requirementsFactory.NewTargetedSpaceRequirement(),
}
return
}
func (cmd *CreateUserProvidedService) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
cmd.ui = deps.Ui
cmd.config = deps.Config
cmd.userProvidedServiceInstanceRepo = deps.RepoLocator.GetUserProvidedServiceInstanceRepository()
return cmd
}
func (cmd *CreateUserProvidedService) Execute(c flags.FlagContext) {
name := c.Args()[0]
drainUrl := c.String("l")
params := c.String("p")
params = strings.Trim(params, `"`)
paramsMap := make(map[string]interface{})
err := json.Unmarshal([]byte(params), ¶msMap)
if err != nil && params != "" {
paramsMap = cmd.mapValuesFromPrompt(params, paramsMap)
}
cmd.ui.Say(T("Creating user provided service {{.ServiceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
map[string]interface{}{
"ServiceName": terminal.EntityNameColor(name),
"OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
}))
apiErr := cmd.userProvidedServiceInstanceRepo.Create(name, drainUrl, paramsMap)
if apiErr != nil {
cmd.ui.Failed(apiErr.Error())
return
}
cmd.ui.Ok()
}
func (cmd CreateUserProvidedService) mapValuesFromPrompt(params string, paramsMap map[string]interface{}) map[string]interface{} {
for _, param := range strings.Split(params, ",") {
param = strings.Trim(param, " ")
paramsMap[param] = cmd.ui.Ask("%s", param)
}
return paramsMap
}