forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_user_provided_service.go
151 lines (125 loc) · 5.69 KB
/
update_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
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
143
144
145
146
147
148
149
150
151
package service
import (
"encoding/json"
"errors"
"fmt"
"strings"
"code.cloudfoundry.org/cli/cf/flagcontext"
"code.cloudfoundry.org/cli/cf/flags"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/uihelpers"
"code.cloudfoundry.org/cli/cf"
"code.cloudfoundry.org/cli/cf/api"
"code.cloudfoundry.org/cli/cf/commandregistry"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/requirements"
"code.cloudfoundry.org/cli/cf/terminal"
)
type UpdateUserProvidedService struct {
ui terminal.UI
config coreconfig.Reader
userProvidedServiceInstanceRepo api.UserProvidedServiceInstanceRepository
serviceInstanceReq requirements.ServiceInstanceRequirement
}
func init() {
commandregistry.Register(&UpdateUserProvidedService{})
}
func (cmd *UpdateUserProvidedService) MetaData() commandregistry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["p"] = &flags.StringFlag{ShortName: "p", Usage: T("Credentials, provided inline or in a file, to be exposed in the VCAP_SERVICES environment variable for bound applications")}
fs["l"] = &flags.StringFlag{ShortName: "l", Usage: T("URL to which logs for bound applications will be streamed")}
fs["r"] = &flags.StringFlag{ShortName: "r", Usage: T("URL to which requests for bound routes will be forwarded. Scheme for this URL must be https")}
fs["t"] = &flags.StringFlag{ShortName: "t", Usage: T("User provided tags")}
return commandregistry.CommandMetadata{
Name: "update-user-provided-service",
ShortName: "uups",
Description: T("Update user-provided service instance"),
Usage: []string{
T(`CF_NAME update-user-provided-service SERVICE_INSTANCE [-p CREDENTIALS] [-l SYSLOG_DRAIN_URL] [-r ROUTE_SERVICE_URL] [-t TAGS]
Pass comma separated credential parameter names to enable interactive mode:
CF_NAME update-user-provided-service SERVICE_INSTANCE -p "comma, separated, parameter, names"
Pass credential parameters as JSON to create a service non-interactively:
CF_NAME update-user-provided-service SERVICE_INSTANCE -p '{"key1":"value1","key2":"value2"}'
Specify a path to a file containing JSON:
CF_NAME update-user-provided-service SERVICE_INSTANCE -p PATH_TO_FILE`),
},
Examples: []string{
`CF_NAME update-user-provided-service my-db-mine -p '{"username":"admin", "password":"pa55woRD"}'`,
"CF_NAME update-user-provided-service my-db-mine -p /path/to/credentials.json",
`CF_NAME update-user-provided-service my-db-mine -t "list, of, tags"`,
"CF_NAME update-user-provided-service my-drain-service -l syslog://example.com",
"CF_NAME update-user-provided-service my-route-service -r https://example.com",
},
Flags: fs,
}
}
func (cmd *UpdateUserProvidedService) 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("update-user-provided-service"))
return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
}
cmd.serviceInstanceReq = requirementsFactory.NewServiceInstanceRequirement(fc.Args()[0])
reqs := []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
}
if fc.IsSet("t") {
reqs = append(reqs, requirementsFactory.NewMinAPIVersionRequirement("Option '-t'", cf.UserProvidedServiceTagsMinimumAPIVersion))
}
reqs = append(reqs, cmd.serviceInstanceReq)
return reqs, nil
}
func (cmd *UpdateUserProvidedService) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
cmd.ui = deps.UI
cmd.config = deps.Config
cmd.userProvidedServiceInstanceRepo = deps.RepoLocator.GetUserProvidedServiceInstanceRepository()
return cmd
}
func (cmd *UpdateUserProvidedService) Execute(c flags.FlagContext) error {
serviceInstance := cmd.serviceInstanceReq.GetServiceInstance()
if !serviceInstance.IsUserProvided() {
return errors.New(T("Service Instance is not user provided"))
}
drainURL := c.String("l")
credentials := strings.Trim(c.String("p"), `'"`)
routeServiceURL := c.String("r")
tags := c.String("t")
tagsList := uihelpers.ParseTags(tags)
credentialsMap := make(map[string]interface{})
if c.IsSet("p") {
jsonBytes, err := flagcontext.GetContentsFromFlagValue(credentials)
if err != nil {
return err
}
err = json.Unmarshal(jsonBytes, &credentialsMap)
if err != nil {
for _, param := range strings.Split(credentials, ",") {
param = strings.Trim(param, " ")
credentialsMap[param] = cmd.ui.Ask(param)
}
}
}
cmd.ui.Say(T("Updating user provided service {{.ServiceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
map[string]interface{}{
"ServiceName": terminal.EntityNameColor(serviceInstance.Name),
"OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
}))
serviceInstance.Params = credentialsMap
serviceInstance.SysLogDrainURL = drainURL
serviceInstance.RouteServiceURL = routeServiceURL
serviceInstance.Tags = tagsList
err := cmd.userProvidedServiceInstanceRepo.Update(serviceInstance.ServiceInstanceFields)
if err != nil {
return err
}
cmd.ui.Ok()
cmd.ui.Say(T("TIP: Use '{{.CFRestageCommand}}' for any bound apps to ensure your env variable changes take effect",
map[string]interface{}{
"CFRestageCommand": terminal.CommandColor(cf.Name + " restage"),
}))
if routeServiceURL == "" && credentials == "" && drainURL == "" && tags == "" {
cmd.ui.Warn(T("No flags specified. No changes were made."))
}
return nil
}