forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind_route_service.go
169 lines (146 loc) · 5.67 KB
/
bind_route_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package service
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/errors"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/cf/util"
"github.com/cloudfoundry/cli/flags"
)
type BindRouteService struct {
ui terminal.UI
config coreconfig.Reader
routeRepo api.RouteRepository
routeServiceBindingRepo api.RouteServiceBindingRepository
domainReq requirements.DomainRequirement
serviceInstanceReq requirements.ServiceInstanceRequirement
}
func init() {
commandregistry.Register(&BindRouteService{})
}
func (cmd *BindRouteService) MetaData() commandregistry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["force"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force binding without confirmation")}
fs["hostname"] = &flags.StringFlag{
Name: "hostname",
ShortName: "n",
Usage: T("Hostname used in combination with DOMAIN to specify the route to bind"),
}
fs["parameters"] = &flags.StringFlag{
ShortName: "c",
Usage: T("Valid JSON object containing service-specific configuration parameters, provided inline or in a file. For a list of supported configuration parameters, see documentation for the particular service offering."),
}
return commandregistry.CommandMetadata{
Name: "bind-route-service",
ShortName: "brs",
Description: T("Bind a service instance to an HTTP route"),
Usage: []string{
T(`CF_NAME bind-route-service DOMAIN SERVICE_INSTANCE [-f] [--hostname HOSTNAME] [-c PARAMETERS_AS_JSON]`),
},
Examples: []string{
`CF_NAME bind-route-service example.com myratelimiter --hostname myapp`,
`CF_NAME bind-route-service example.com myratelimiter -c file.json`,
`CF_NAME bind-route-service example.com myratelimiter -c '{"valid":"json"}'`,
``,
T(`In Windows PowerShell use double-quoted, escaped JSON: "{\"valid\":\"json\"}"`),
T(`In Windows Command Line use single-quoted, escaped JSON: '{\"valid\":\"json\"}'`),
},
Flags: fs,
}
}
func (cmd *BindRouteService) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) []requirements.Requirement {
if len(fc.Args()) != 2 {
cmd.ui.Failed(T("Incorrect Usage. Requires DOMAIN and SERVICE_INSTANCE as arguments\n\n") + commandregistry.Commands.CommandUsage("bind-route-service"))
}
domainName := fc.Args()[0]
cmd.domainReq = requirementsFactory.NewDomainRequirement(domainName)
serviceName := fc.Args()[1]
cmd.serviceInstanceReq = requirementsFactory.NewServiceInstanceRequirement(serviceName)
minAPIVersion, err := semver.Make("2.51.0")
if err != nil {
panic(err.Error())
}
minAPIVersionRequirement := requirementsFactory.NewMinAPIVersionRequirement(
"bind-route-service",
minAPIVersion,
)
reqs := []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
cmd.domainReq,
cmd.serviceInstanceReq,
minAPIVersionRequirement,
}
return reqs
}
func (cmd *BindRouteService) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
cmd.ui = deps.Ui
cmd.config = deps.Config
cmd.routeRepo = deps.RepoLocator.GetRouteRepository()
cmd.routeServiceBindingRepo = deps.RepoLocator.GetRouteServiceBindingRepository()
return cmd
}
func (cmd *BindRouteService) Execute(c flags.FlagContext) {
var path string
var port int
host := c.String("hostname")
domain := cmd.domainReq.GetDomain()
var parameters string
if c.IsSet("parameters") {
jsonBytes, err := util.GetContentsFromFlagValue(c.String("parameters"))
if err != nil {
cmd.ui.Failed(err.Error())
}
parameters = string(jsonBytes)
}
route, err := cmd.routeRepo.Find(host, domain, path, port)
if err != nil {
cmd.ui.Failed(err.Error())
}
serviceInstance := cmd.serviceInstanceReq.GetServiceInstance()
if !serviceInstance.IsUserProvided() {
var requiresRouteForwarding bool
for _, requirement := range serviceInstance.ServiceOffering.Requires {
if requirement == "route_forwarding" {
requiresRouteForwarding = true
break
}
}
confirmed := c.Bool("force")
if requiresRouteForwarding && !confirmed {
confirmed = cmd.ui.Confirm(T("Binding may cause requests for route {{.URL}} to be altered by service instance {{.ServiceInstanceName}}. Do you want to proceed?",
map[string]interface{}{
"URL": route.URL(),
"ServiceInstanceName": serviceInstance.Name,
}))
if !confirmed {
cmd.ui.Warn(T("Bind cancelled"))
return
}
}
}
cmd.ui.Say(T("Binding route {{.URL}} to service instance {{.ServiceInstanceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
map[string]interface{}{
"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name),
"URL": terminal.EntityNameColor(route.URL()),
"OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
}))
err = cmd.routeServiceBindingRepo.Bind(serviceInstance.Guid, route.Guid, serviceInstance.IsUserProvided(), parameters)
if err != nil {
if httpErr, ok := err.(errors.HttpError); ok && httpErr.ErrorCode() == errors.ServiceInstanceAlreadyBoundToSameRoute {
cmd.ui.Warn(T("Route {{.URL}} is already bound to service instance {{.ServiceInstanceName}}.",
map[string]interface{}{
"URL": route.URL(),
"ServiceInstanceName": serviceInstance.Name,
}))
} else {
cmd.ui.Failed(err.Error())
}
}
cmd.ui.Ok()
}