forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_user_provided_service.go
112 lines (91 loc) · 3.71 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
package service
import (
"encoding/json"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf"
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/flag_helpers"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
type UpdateUserProvidedService struct {
ui terminal.UI
config core_config.Reader
userProvidedServiceInstanceRepo api.UserProvidedServiceInstanceRepository
serviceInstanceReq requirements.ServiceInstanceRequirement
}
func NewUpdateUserProvidedService(ui terminal.UI, config core_config.Reader, userProvidedServiceInstanceRepo api.UserProvidedServiceInstanceRepository) (cmd *UpdateUserProvidedService) {
cmd = new(UpdateUserProvidedService)
cmd.ui = ui
cmd.config = config
cmd.userProvidedServiceInstanceRepo = userProvidedServiceInstanceRepo
return
}
func (cmd *UpdateUserProvidedService) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "update-user-provided-service",
ShortName: "uups",
Description: T("Update user-provided service instance name value pairs"),
Usage: T(`CF_NAME update-user-provided-service SERVICE_INSTANCE [-p CREDENTIALS] [-l SYSLOG-DRAIN-URL]'
EXAMPLE:
CF_NAME update-user-provided-service my-db-mine -p '{"username":"admin","password":"pa55woRD"}'
CF_NAME update-user-provided-service my-drain-service -l syslog://example.com`),
Flags: []cli.Flag{
flag_helpers.NewStringFlag("p", T("Credentials")),
flag_helpers.NewStringFlag("l", T("Syslog Drain Url")),
},
}
}
func (cmd *UpdateUserProvidedService) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) != 1 {
cmd.ui.FailWithUsage(c)
}
cmd.serviceInstanceReq = requirementsFactory.NewServiceInstanceRequirement(c.Args()[0])
reqs = []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
cmd.serviceInstanceReq,
}
return
}
func (cmd *UpdateUserProvidedService) Run(c *cli.Context) {
serviceInstance := cmd.serviceInstanceReq.GetServiceInstance()
if !serviceInstance.IsUserProvided() {
cmd.ui.Failed(T("Service Instance is not user provided"))
return
}
drainUrl := c.String("l")
params := c.String("p")
paramsMap := make(map[string]interface{})
if params != "" {
err := json.Unmarshal([]byte(params), ¶msMap)
if err != nil {
cmd.ui.Failed(T("JSON is invalid: {{.ErrorDescription}}", map[string]interface{}{"ErrorDescription": err.Error()}))
return
}
}
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 = paramsMap
serviceInstance.SysLogDrainUrl = drainUrl
apiErr := cmd.userProvidedServiceInstanceRepo.Update(serviceInstance.ServiceInstanceFields)
if apiErr != nil {
cmd.ui.Failed(apiErr.Error())
return
}
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 params == "" && drainUrl == "" {
cmd.ui.Warn(T("No flags specified. No changes were made."))
}
}